James Moger
2011-11-01 c25a1d65ed2c94b65741d81862a7612ae12bdf76
commit | author | age
15a51d 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.client;
17
18 import java.awt.Component;
19 import java.io.IOException;
20 import java.text.MessageFormat;
21
22 import javax.swing.JOptionPane;
23 import javax.swing.SwingWorker;
24
25 import com.gitblit.Constants.RpcRequest;
26 import com.gitblit.GitBlitException.ForbiddenException;
8b7636 27 import com.gitblit.GitBlitException.NotAllowedException;
15a51d 28 import com.gitblit.GitBlitException.UnauthorizedException;
8b7636 29 import com.gitblit.GitBlitException.UnknownRequestException;
15a51d 30
JM 31 public abstract class GitblitWorker extends SwingWorker<Boolean, Void> {
32
33     private final Component parent;
34
35     private final RpcRequest request;
36
37     public GitblitWorker(Component parent, RpcRequest request) {
38         this.parent = parent;
39         this.request = request;
40     }
41
42     protected RpcRequest getRequestType() {
43         return request;
44     }
45
46     @Override
47     protected Boolean doInBackground() throws IOException {
48         return doRequest();
49     }
50
51     protected void done() {
52         try {
53             Boolean success = get();
54             if (success) {
55                 onSuccess();
56             } else {
57                 onFailure();
58             }
59         } catch (Throwable t) {
60             if (t instanceof ForbiddenException) {
61                 Utils.explainForbidden(parent, request);
62             } else if (t instanceof UnauthorizedException) {
63                 Utils.explainUnauthorized(parent, request);
8b7636 64             } else if (t instanceof NotAllowedException) {
JM 65                 Utils.explainNotAllowed(parent, request);
66             } else if (t instanceof UnknownRequestException) {
67                 Utils.explainNotAllowed(parent, request);
15a51d 68             } else {
JM 69                 Utils.showException(parent, t);
70             }
71         }
72     }
73
74     protected abstract Boolean doRequest() throws IOException;
75
76     protected abstract void onSuccess();
77
78     protected void onFailure() {
79     }
80
81     protected void showFailure(String message, Object... args) {
82         String msg = MessageFormat.format(message, args);
83         JOptionPane.showMessageDialog(parent, msg, Translation.get("gb.error"),
84                 JOptionPane.ERROR_MESSAGE);
85     }
86 }