1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.atlassian.theplugin.idea.crucible;
18
19 import com.atlassian.theplugin.commons.cfg.CrucibleServerCfg;
20 import com.atlassian.theplugin.commons.crucible.CrucibleServerFacade;
21 import com.atlassian.theplugin.commons.crucible.api.model.*;
22 import com.atlassian.theplugin.commons.crucible.api.model.Project;
23 import com.atlassian.theplugin.commons.exception.ServerPasswordNotProvidedException;
24 import com.atlassian.theplugin.commons.remoteapi.RemoteApiException;
25 import com.atlassian.theplugin.cfg.CfgUtil;
26 import com.atlassian.theplugin.idea.IdeaHelper;
27 import com.intellij.openapi.ui.DialogWrapper;
28 import com.intellij.openapi.ui.Messages;
29 import static com.intellij.openapi.ui.Messages.showMessageDialog;
30 import com.intellij.openapi.vcs.changes.Change;
31 import com.intellij.openapi.vcs.changes.ChangeList;
32 import com.intellij.uiDesigner.core.GridConstraints;
33 import com.intellij.uiDesigner.core.GridLayoutManager;
34 import com.intellij.uiDesigner.core.Spacer;
35 import com.intellij.ide.BrowserUtil;
36 import org.jetbrains.annotations.Nullable;
37
38 import javax.swing.*;
39 import javax.swing.Action;
40 import java.awt.*;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.List;
46
47 enum AddMode {
48 ADDREVISION,
49 ADDPATCH,
50 VIEWREVISION
51 }
52
53 public class CrucibleHelperForm extends DialogWrapper {
54 private JPanel rootComponent;
55 private JComboBox crucibleServersComboBox;
56 private JComboBox repoComboBox;
57
58 private CrucibleServerFacade crucibleServerFacade;
59 private ChangeList[] changes;
60 private final com.intellij.openapi.project.Project project;
61 private PermId permId;
62 private String rev;
63 private String patch;
64 private AddMode mode;
65
66 protected CrucibleHelperForm(com.intellij.openapi.project.Project project, CrucibleServerFacade crucibleServerFacade,
67 PermId permId, ChangeList[] changes) {
68 this(project, crucibleServerFacade);
69 this.permId = permId;
70 this.changes = changes;
71 this.mode = AddMode.ADDREVISION;
72 setTitle("Add revision");
73 getOKAction().putValue(Action.NAME, "Add revision...");
74 }
75
76 protected CrucibleHelperForm(com.intellij.openapi.project.Project project, CrucibleServerFacade crucibleServerFacade,
77 String rev) {
78 this(project, crucibleServerFacade);
79 this.rev = rev;
80 this.mode = AddMode.VIEWREVISION;
81 setTitle("View revision");
82 getOKAction().putValue(Action.NAME, "View revision...");
83 }
84
85 protected CrucibleHelperForm(com.intellij.openapi.project.Project project, CrucibleServerFacade crucibleServerFacade,
86 PermId permId, String patch) {
87 this(project, crucibleServerFacade);
88 this.permId = permId;
89 this.patch = patch;
90 this.mode = AddMode.ADDPATCH;
91 setTitle("Add patch");
92 getOKAction().putValue(Action.NAME, "Add patch...");
93 }
94
95 private CrucibleHelperForm(com.intellij.openapi.project.Project project, CrucibleServerFacade crucibleServerFacade) {
96 super(false);
97
98 this.crucibleServerFacade = crucibleServerFacade;
99 this.project = project;
100 $$$setupUI$$$();
101 init();
102
103 crucibleServersComboBox.addActionListener(new ActionListener() {
104 public void actionPerformed(ActionEvent e) {
105 if (crucibleServersComboBox.getItemCount() > 0 && crucibleServersComboBox.getSelectedItem() != null && crucibleServersComboBox.getSelectedItem() instanceof ServerComboBoxItem) {
106 final ServerComboBoxItem boxItem = (ServerComboBoxItem) crucibleServersComboBox.getSelectedItem();
107 fillServerRelatedCombos(boxItem.getServer());
108 }
109 }
110 });
111
112 fillInCrucibleServers();
113 }
114
115 @Override
116 public JComponent getPreferredFocusedComponent() {
117 return this.crucibleServersComboBox;
118 }
119
120
121
122
123
124
125
126
127 private void $$$setupUI$$$() {
128 rootComponent = new JPanel();
129 rootComponent.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
130 rootComponent.setMinimumSize(new Dimension(450, 200));
131 final JPanel panel1 = new JPanel();
132 panel1.setLayout(new GridLayoutManager(2, 2, new Insets(1, 1, 1, 1), -1, -1));
133 rootComponent.add(panel1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
134 crucibleServersComboBox = new JComboBox();
135 panel1.add(crucibleServersComboBox, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
136 final JLabel label1 = new JLabel();
137 label1.setText("Server:");
138 panel1.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
139 repoComboBox = new JComboBox();
140 panel1.add(repoComboBox, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
141 final JLabel label2 = new JLabel();
142 label2.setText("Repository:");
143 panel1.add(label2, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
144 final Spacer spacer1 = new Spacer();
145 rootComponent.add(spacer1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
146 label1.setLabelFor(crucibleServersComboBox);
147 }
148
149
150
151
152 public JComponent $$$getRootComponent$$$() {
153 return rootComponent;
154 }
155
156 private static final class ServerComboBoxItem {
157 private final CrucibleServerCfg server;
158
159 private ServerComboBoxItem(CrucibleServerCfg server) {
160 this.server = server;
161 }
162
163 @Override
164 public String toString() {
165 return server.getName();
166 }
167
168 public CrucibleServerCfg getServer() {
169 return server;
170 }
171 }
172
173 private void fillInCrucibleServers() {
174 final Collection<CrucibleServerCfg> enabledServers = IdeaHelper.getCfgManager()
175 .getAllEnabledCrucibleServers(CfgUtil.getProjectId(project));
176
177 if (enabledServers.isEmpty()) {
178 crucibleServersComboBox.setEnabled(false);
179 crucibleServersComboBox.addItem("Enable a Crucible server first!");
180 getOKAction().setEnabled(false);
181 } else {
182 for (CrucibleServerCfg server : enabledServers) {
183 crucibleServersComboBox.addItem(new ServerComboBoxItem(server));
184 }
185 }
186 }
187
188 private void fillServerRelatedCombos(final CrucibleServerCfg server) {
189 repoComboBox.removeAllItems();
190 getOKAction().setEnabled(false);
191
192 new Thread(new Runnable() {
193 public void run() {
194 List<Project> projects = new ArrayList<Project>();
195 List<Repository> repositories = new ArrayList<Repository>();
196 List<User> users = new ArrayList<User>();
197
198 try {
199 projects = crucibleServerFacade.getProjects(server);
200 repositories = crucibleServerFacade.getRepositories(server);
201 users = crucibleServerFacade.getUsers(server);
202 } catch (RemoteApiException e) {
203
204 } catch (ServerPasswordNotProvidedException e) {
205
206 }
207 final List<Repository> finalRepositories = repositories;
208 EventQueue.invokeLater(new Runnable() {
209 public void run() {
210 updateServerRelatedCombos(server, finalRepositories);
211 }
212 });
213 }
214 }, "atlassian-idea-plugin crucible patch upload combos refresh").start();
215 }
216
217 private void updateServerRelatedCombos(
218 CrucibleServerCfg server,
219 List<Repository> repositories) {
220 repoComboBox.addItem("");
221 if (!repositories.isEmpty()) {
222 for (Repository repo : repositories) {
223 repoComboBox.addItem(new RepositoryComboBoxItem(repo));
224 }
225 getOKAction().setEnabled(true);
226 }
227 }
228
229 private static final class RepositoryComboBoxItem {
230 private final Repository repo;
231
232 private RepositoryComboBoxItem(Repository repo) {
233 this.repo = repo;
234 }
235
236 @Override
237 public String toString() {
238 return repo.getName();
239 }
240
241 public Repository getRepository() {
242 return repo;
243 }
244 }
245
246
247 public JComponent getRootComponent() {
248 return rootComponent;
249 }
250
251 @Override
252 @Nullable
253 protected JComponent createCenterPanel() {
254 return getRootComponent();
255 }
256
257
258 protected void doOKAction() {
259 CrucibleServerCfg server = ((ServerComboBoxItem) crucibleServersComboBox.getSelectedItem()).getServer();
260 Repository repo = ((RepositoryComboBoxItem) this.repoComboBox.getSelectedItem()).getRepository();
261 switch (mode) {
262 case ADDREVISION:
263 try {
264 List<String> revisions = new ArrayList<String>();
265 for (ChangeList change : changes) {
266 for (Change change1 : change.getChanges()) {
267 revisions.add(change1.getAfterRevision().getRevisionNumber().asString());
268 break;
269 }
270 }
271 Review draftReview = crucibleServerFacade.addRevisionsToReview(server, permId, repo.getName(), revisions);
272 super.doOKAction();
273
274 } catch (RemoteApiException e) {
275 showMessageDialog(e.getMessage(),
276 "Error creating review: " + server.getUrl(), Messages.getErrorIcon());
277 } catch (ServerPasswordNotProvidedException e) {
278 e.printStackTrace();
279 }
280 break;
281
282 case ADDPATCH:
283 try {
284 Review draftReview = crucibleServerFacade.addPatchToReview(server, permId, repo.getName(), patch);
285 super.doOKAction();
286
287 } catch (RemoteApiException e) {
288 showMessageDialog(e.getMessage(),
289 "Error creating review: " + server.getUrl(), Messages.getErrorIcon());
290 } catch (ServerPasswordNotProvidedException e) {
291 e.printStackTrace();
292 }
293 break;
294
295 case VIEWREVISION:
296 String url = server.getUrl() + "/changelog/" + repo.getName() + "/?cs=" + rev;
297 BrowserUtil.launchBrowser(url);
298 super.doOKAction();
299 break;
300 }
301 }
302
303 private void createUIComponents() {
304 }
305 }