View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
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  
17  package com.atlassian.theplugin.idea.crucible;
18  
19  import com.atlassian.theplugin.cfg.CfgUtil;
20  import com.atlassian.theplugin.commons.cfg.CfgManager;
21  import com.atlassian.theplugin.commons.cfg.CrucibleServerCfg;
22  import com.atlassian.theplugin.commons.cfg.ServerCfg;
23  import com.atlassian.theplugin.commons.cfg.ServerId;
24  import com.atlassian.theplugin.commons.crucible.CrucibleServerFacade;
25  import com.atlassian.theplugin.commons.crucible.CrucibleServerFacadeImpl;
26  import com.atlassian.theplugin.commons.crucible.api.model.CustomFilterBean;
27  import com.atlassian.theplugin.commons.crucible.api.model.Project;
28  import com.atlassian.theplugin.commons.crucible.api.model.ProjectBean;
29  import com.atlassian.theplugin.commons.crucible.api.model.State;
30  import com.atlassian.theplugin.commons.crucible.api.model.User;
31  import com.atlassian.theplugin.commons.crucible.api.model.UserBean;
32  import com.atlassian.theplugin.commons.exception.ServerPasswordNotProvidedException;
33  import com.atlassian.theplugin.commons.remoteapi.RemoteApiException;
34  import com.intellij.openapi.actionSystem.ActionManager;
35  import com.jgoodies.forms.layout.CellConstraints;
36  import com.jgoodies.forms.layout.FormLayout;
37  
38  import javax.swing.*;
39  import java.awt.*;
40  import java.awt.event.ActionEvent;
41  import java.awt.event.ActionListener;
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.List;
45  
46  public class CrucibleCustomFilterPanel extends JPanel {
47      private JPanel rootPanel;
48      private JTextField filterTitle;
49      private JCheckBox draftCheckBox;
50      private JCheckBox pendingApprovalCheckBox;
51      private JCheckBox underReviewCheckBox;
52      private JCheckBox summarizeCheckBox;
53      private JCheckBox closedCheckBox;
54      private JCheckBox abandonedCheckBox;
55      private JCheckBox rejectedCheckBox;
56      private JCheckBox reviewNeedsFixingCheckBox;
57      private JComboBox projectComboBox;
58      private JComboBox authorComboBox;
59      private JComboBox moderatorComboBox;
60      private JComboBox creatorComboBox;
61      private JComboBox reviewerStatusComboBox;
62      private JComboBox reviewerComboBox;
63      private JComboBox serverComboBox;
64      private JComboBox matchRoleComboBox;
65      private CrucibleServerFacade crucibleServerFacade;
66      private transient CustomFilterBean filter;
67  
68      private ProjectBean anyProject;
69      private UserBean anyUser;
70  	private final com.intellij.openapi.project.Project project;
71  	private final CfgManager cfgManager;
72  
73  	CrucibleCustomFilterPanel(final com.intellij.openapi.project.Project project, final CfgManager cfgManager) {
74  		this.project = project;
75  		this.cfgManager = cfgManager;
76  		$$$setupUI$$$();
77  
78          anyProject = new ProjectBean();
79          anyProject.setName("Any");
80          anyUser = new UserBean();
81          anyUser.setDisplayName("Any");
82          anyUser.setUserName("_any_");
83  
84          reviewerStatusComboBox.addItem("Any");
85          reviewerStatusComboBox.addItem("Incomplete");
86          reviewerStatusComboBox.addItem("Complete");
87  
88          matchRoleComboBox.addItem("Any");
89          matchRoleComboBox.addItem("All");
90  
91          crucibleServerFacade = CrucibleServerFacadeImpl.getInstance();
92          serverComboBox.addActionListener(new ActionListener() {
93              public void actionPerformed(ActionEvent e) {
94                  fillServerRelatedCombos(getSelectedServer());
95  
96              }
97          });
98      }
99  
100     public void setFilter(CustomFilterBean filter) {
101         ServerCfg server = null;
102 
103         if (filter == null) {
104             this.filter = new CustomFilterBean();
105         } else {
106             this.filter = filter;
107 			final ServerId serverId = new ServerId(filter.getServerUid());
108 			server = cfgManager.getServer(CfgUtil.getProjectId(project), serverId);
109             filterTitle.setText((filter.getTitle()));
110         }
111 
112         fillInCrucibleServers();
113         if (server == null && serverComboBox.getItemCount() > 0) {
114             serverComboBox.setSelectedIndex(0);
115         }
116 
117         fillServerRelatedCombos(getSelectedServer());
118     }
119 
120     private CrucibleServerCfg getSelectedServer() {
121         CrucibleServerCfg server = null;
122 
123         if (serverComboBox.getItemCount() > 0 &&
124                 serverComboBox.getSelectedItem() != null &&
125                 serverComboBox.getSelectedItem() instanceof ServerComboBoxItem) {
126             server = ((ServerComboBoxItem) serverComboBox.getSelectedItem()).getServer();
127         }
128 
129         return server;
130     }
131 
132     public CustomFilterBean getFilter() {
133         CrucibleServerCfg s = ((ServerComboBoxItem) this.serverComboBox.getSelectedItem()).getServer();
134         filter.setServerUid(s.getServerId().getUuid().toString());
135 
136         filter.setTitle(filterTitle.getText());
137         if (!((ProjectComboBoxItem) projectComboBox.getSelectedItem()).getProject().getName().equals(anyProject.getName())) {
138             filter.setProjectKey(((ProjectComboBoxItem) projectComboBox.getSelectedItem()).getProject().getKey());
139         }
140         if (!((UserComboBoxItem) authorComboBox.getSelectedItem()).getUserData().getUserName().equals(anyUser.getUserName())) {
141             filter.setAuthor(((UserComboBoxItem) authorComboBox.getSelectedItem()).getUserData().getUserName());
142         }
143         if (!((UserComboBoxItem) creatorComboBox.getSelectedItem()).getUserData().getUserName().equals(anyUser.getUserName())) {
144             filter.setCreator(((UserComboBoxItem) creatorComboBox.getSelectedItem()).getUserData().getUserName());
145         }
146         if (!((UserComboBoxItem) moderatorComboBox.getSelectedItem()).getUserData().getUserName()
147                 .equals(anyUser.getUserName())) {
148             filter.setModerator(((UserComboBoxItem) moderatorComboBox.getSelectedItem()).getUserData().getUserName());
149         }
150         if (!((UserComboBoxItem) reviewerComboBox.getSelectedItem()).getUserData().getUserName()
151                 .equals(anyUser.getUserName())) {
152             filter.setReviewer(((UserComboBoxItem) reviewerComboBox.getSelectedItem()).getUserData().getUserName());
153         }
154 
155         List<String> states = new ArrayList<String>();
156         if (draftCheckBox.isSelected()) {
157             states.add(State.DRAFT.value());
158         }
159         if (pendingApprovalCheckBox.isSelected()) {
160             states.add(State.APPROVAL.value());
161         }
162         if (summarizeCheckBox.isSelected()) {
163             states.add(State.SUMMARIZE.value());
164         }
165         if (closedCheckBox.isSelected()) {
166             states.add(State.CLOSED.value());
167         }
168         if (rejectedCheckBox.isSelected()) {
169             states.add(State.REJECTED.value());
170         }
171         if (underReviewCheckBox.isSelected()) {
172             states.add(State.REVIEW.value());
173         }
174         filter.setState(states.toArray(new String[states.size()]));
175 
176         String role = (String) matchRoleComboBox.getSelectedItem();
177         filter.setOrRoles("Any".equals(role));
178 
179         String complete = (String) reviewerStatusComboBox.getSelectedItem();
180         filter.setComplete("Complete".equals(complete));
181 
182         return filter;
183     }
184 
185     private void fillServerRelatedCombos(final CrucibleServerCfg server) {
186         final CrucibleServerCfg crucibleServerCfg = (server != null) ? server : getSelectedServer();
187 
188         if (crucibleServerCfg != null) {
189             projectComboBox.setEnabled(false);
190 
191             new Thread(new Runnable() {
192                 public void run() {
193                     List<Project> projects = new ArrayList<Project>();
194                     List<User> users = new ArrayList<User>();
195                     try {
196                         projects = crucibleServerFacade.getProjects(crucibleServerCfg);
197                         users = crucibleServerFacade.getUsers(crucibleServerCfg);
198                     } catch (RemoteApiException e) {
199                         // nothing can be done here
200                     } catch (ServerPasswordNotProvidedException e) {
201                         // nothing can be done here
202                     }
203                     final List<Project> finalProjects = projects;
204                     final List<User> finalUsers = users;
205 
206                     EventQueue.invokeLater(new Runnable() {
207                         public void run() {
208                             updateServerRelatedCombos(finalProjects, finalUsers);
209                         }
210                     });
211                 }
212             }, "atlassian-idea-plugin crucible custom filter upload combos refresh").start();
213         }
214     }
215 
216     private void updateServerRelatedCombos(List<Project> projects, List<User> users) {
217         projectComboBox.removeAllItems();
218         projectComboBox.addItem(new ProjectComboBoxItem(anyProject));
219         authorComboBox.removeAllItems();
220         authorComboBox.addItem(new UserComboBoxItem(anyUser));
221         moderatorComboBox.removeAllItems();
222         moderatorComboBox.addItem(new UserComboBoxItem(anyUser));
223         creatorComboBox.removeAllItems();
224         creatorComboBox.addItem(new UserComboBoxItem(anyUser));
225         reviewerComboBox.removeAllItems();
226         reviewerComboBox.addItem(new UserComboBoxItem(anyUser));
227 
228         if (projects.isEmpty()) {
229             projectComboBox.setEnabled(false);
230             projectComboBox.addItem("No projects");
231             setEnabledApplyButton(false);
232         } else {
233             for (Project project : projects) {
234                 projectComboBox.addItem(new ProjectComboBoxItem(project));
235             }
236             projectComboBox.setEnabled(true);
237             setEnabledApplyButton(true);
238         }
239 
240         if (!users.isEmpty()) {
241             for (User user : users) {
242                 authorComboBox.addItem(new UserComboBoxItem(user));
243                 moderatorComboBox.addItem(new UserComboBoxItem(user));
244                 creatorComboBox.addItem(new UserComboBoxItem(user));
245                 reviewerComboBox.addItem(new UserComboBoxItem(user));
246             }
247         }
248         setProject(filter.getProjectKey(), projectComboBox);
249         setActiveUser(filter.getAuthor(), authorComboBox);
250         setActiveUser(filter.getCreator(), creatorComboBox);
251         setActiveUser(filter.getModerator(), moderatorComboBox);
252         setActiveUser(filter.getReviewer(), reviewerComboBox);
253 
254         draftCheckBox.setSelected(false);
255         pendingApprovalCheckBox.setSelected(false);
256         underReviewCheckBox.setSelected(false);
257         summarizeCheckBox.setSelected(false);
258         closedCheckBox.setSelected(false);
259         abandonedCheckBox.setSelected(false);
260         rejectedCheckBox.setSelected(false);
261         reviewNeedsFixingCheckBox.setSelected(false);
262 
263         if (filter.getState() != null) {
264             for (String state : filter.getState()) {
265                 State value = State.fromValue(state);
266                 switch (value) {
267                     case APPROVAL:
268                         pendingApprovalCheckBox.setSelected(true);
269                         break;
270                     case DRAFT:
271                         draftCheckBox.setSelected(true);
272                         break;
273                     case REVIEW:
274                         underReviewCheckBox.setSelected(true);
275                         break;
276                     case SUMMARIZE:
277                         summarizeCheckBox.setSelected(true);
278                         break;
279                     case CLOSED:
280                         closedCheckBox.setSelected(true);
281                         break;
282                     case REJECTED:
283                         rejectedCheckBox.setSelected(true);
284                         break;
285                 }
286             }
287         }
288     }
289 
290     private void setProject(String projectName, JComboBox combo) {
291         if (projectName != null) {
292             for (int i = 0; i < combo.getModel().getSize(); ++i) {
293                 ProjectComboBoxItem item = (ProjectComboBoxItem) combo.getModel().getElementAt(i);
294                 if (projectName.equals(item.getProject().getKey())) {
295                     combo.setSelectedItem(item);
296                     break;
297                 }
298             }
299         }
300     }
301 
302     private void setActiveUser(String userName, JComboBox combo) {
303         if (userName != null) {
304             for (int i = 0; i < combo.getModel().getSize(); ++i) {
305                 UserComboBoxItem item = (UserComboBoxItem) combo.getModel().getElementAt(i);
306                 if (userName.equals(item.getUserData().getUserName())) {
307                     combo.setSelectedItem(item);
308                     break;
309                 }
310             }
311         }
312     }
313 
314     private void setEnabledApplyButton(Boolean enabled) {
315         ActionManager.getInstance().getAction("ThePlugin.Crucible.ShowFilter").getTemplatePresentation().setEnabled(enabled);
316     }
317 
318     private void fillInCrucibleServers() {
319 		final Collection<CrucibleServerCfg> enabledServers = cfgManager.getAllEnabledCrucibleServers(
320 				CfgUtil.getProjectId(project));
321 
322         serverComboBox.removeAllItems();
323         if (enabledServers.isEmpty()) {
324             serverComboBox.setEnabled(false);
325             serverComboBox.addItem("Enable a Crucible server first!");
326             //@todo disable apply filter button in toolbar
327         } else {
328             for (CrucibleServerCfg server : enabledServers) {
329                 serverComboBox.addItem(new ServerComboBoxItem(server));
330             }
331         }
332 
333 
334     }
335 
336     /**
337      * Method generated by IntelliJ IDEA GUI Designer
338      * >>> IMPORTANT!! <<<
339      * DO NOT edit this method OR call it in your code!
340      *
341      * @noinspection ALL
342      */
343     private void $$$setupUI$$$() {
344         rootPanel = new JPanel();
345         rootPanel.setLayout(new FormLayout("fill:p:grow,fill:max(d;4px):noGrow",
346                 "top:3dlu:noGrow,center:max(d;4px):noGrow,top:3dlu:noGrow,center:max(d;4px):noGrow,center:max(d;4px):noGrow,top:3dlu:noGrow,center:max(d;4px):noGrow,top:3dlu:noGrow,center:19px:noGrow,center:max(d;4px):noGrow,center:max(d;4px):noGrow,center:33px:noGrow,center:18px:noGrow,center:31px:noGrow,center:19px:noGrow,center:30px:noGrow,center:19px:noGrow,center:30px:noGrow,center:17px:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:3dlu:noGrow,top:4dlu:noGrow,center:24px:noGrow,center:23px:noGrow,center:25px:noGrow,center:max(d;4px):noGrow,center:25px:noGrow,center:24px:noGrow,center:24px:noGrow,center:max(d;4px):noGrow"));
347         filterTitle = new JTextField();
348         CellConstraints cc = new CellConstraints();
349         rootPanel.add(filterTitle, cc.xyw(1, 7, 2, CellConstraints.FILL, CellConstraints.DEFAULT));
350         final JLabel label1 = new JLabel();
351         label1.setText("Title:");
352         rootPanel.add(label1, cc.xy(1, 5));
353         final JLabel label2 = new JLabel();
354         label2.setText("Project:");
355         rootPanel.add(label2, cc.xy(1, 9));
356         projectComboBox = new JComboBox();
357         projectComboBox.setMinimumSize(new Dimension(120, 27));
358         rootPanel.add(projectComboBox, cc.xy(1, 10));
359         authorComboBox = new JComboBox();
360         rootPanel.add(authorComboBox, cc.xy(1, 12));
361         final JLabel label3 = new JLabel();
362         label3.setInheritsPopupMenu(true);
363         label3.setText("Author:");
364         rootPanel.add(label3, cc.xy(1, 11));
365         moderatorComboBox = new JComboBox();
366         rootPanel.add(moderatorComboBox, cc.xy(1, 14));
367         final JLabel label4 = new JLabel();
368         label4.setText("Moderator:");
369         rootPanel.add(label4, cc.xy(1, 13));
370         creatorComboBox = new JComboBox();
371         rootPanel.add(creatorComboBox, cc.xy(1, 16));
372         final JLabel label5 = new JLabel();
373         label5.setText("Creator:");
374         rootPanel.add(label5, cc.xy(1, 15));
375         reviewerComboBox = new JComboBox();
376         rootPanel.add(reviewerComboBox, cc.xy(1, 18));
377         final JLabel label6 = new JLabel();
378         label6.setText("Reviewer:");
379         rootPanel.add(label6, cc.xy(1, 17));
380         reviewerStatusComboBox = new JComboBox();
381         rootPanel.add(reviewerStatusComboBox, cc.xy(1, 20));
382         final JLabel label7 = new JLabel();
383         label7.setMinimumSize(new Dimension(10, 16));
384         label7.setText("Reviewer Status:");
385         rootPanel.add(label7, cc.xy(1, 19));
386         draftCheckBox = new JCheckBox();
387         draftCheckBox.setText("Draft");
388         rootPanel.add(draftCheckBox, cc.xy(1, 27));
389         pendingApprovalCheckBox = new JCheckBox();
390         pendingApprovalCheckBox.setEnabled(false);
391         pendingApprovalCheckBox.setText("Pending Approval");
392         rootPanel.add(pendingApprovalCheckBox, cc.xy(1, 28));
393         underReviewCheckBox = new JCheckBox();
394         underReviewCheckBox.setText("Under Review");
395         rootPanel.add(underReviewCheckBox, cc.xy(1, 29));
396         summarizeCheckBox = new JCheckBox();
397         summarizeCheckBox.setText("Summarize");
398         rootPanel.add(summarizeCheckBox, cc.xy(1, 30));
399         closedCheckBox = new JCheckBox();
400         closedCheckBox.setText("Closed");
401         rootPanel.add(closedCheckBox, cc.xy(1, 31));
402         abandonedCheckBox = new JCheckBox();
403         abandonedCheckBox.setText("Abandoned");
404         rootPanel.add(abandonedCheckBox, cc.xy(1, 32));
405         rejectedCheckBox = new JCheckBox();
406         rejectedCheckBox.setText("Rejected");
407         rootPanel.add(rejectedCheckBox, cc.xy(1, 33));
408         reviewNeedsFixingCheckBox = new JCheckBox();
409         reviewNeedsFixingCheckBox.setEnabled(false);
410         reviewNeedsFixingCheckBox.setText("Review needs fixing");
411         rootPanel.add(reviewNeedsFixingCheckBox, cc.xy(1, 34));
412         serverComboBox = new JComboBox();
413         rootPanel.add(serverComboBox, cc.xy(1, 4));
414         final JLabel label8 = new JLabel();
415         label8.setText("Crucible server:");
416         rootPanel.add(label8, cc.xy(1, 2));
417         final JLabel label9 = new JLabel();
418         label9.setText("Match role");
419         rootPanel.add(label9, cc.xy(1, 22));
420         matchRoleComboBox = new JComboBox();
421         rootPanel.add(matchRoleComboBox, cc.xy(1, 24));
422         label1.setLabelFor(filterTitle);
423         label2.setLabelFor(projectComboBox);
424         label3.setLabelFor(authorComboBox);
425         label4.setLabelFor(moderatorComboBox);
426         label5.setLabelFor(creatorComboBox);
427         label6.setLabelFor(reviewerComboBox);
428         label7.setLabelFor(reviewerStatusComboBox);
429         label8.setLabelFor(serverComboBox);
430     }
431 
432     /**
433      * @noinspection ALL
434      */
435     public JComponent $$$getRootComponent$$$() {
436         return rootPanel;
437     }
438 
439 
440     private static final class ServerComboBoxItem {
441         private final CrucibleServerCfg server;
442 
443         private ServerComboBoxItem(CrucibleServerCfg server) {
444             this.server = server;
445         }
446 
447         public String toString() {
448             return server.getName();
449         }
450 
451         public CrucibleServerCfg getServer() {
452             return server;
453         }
454     }
455 
456     private static final class ProjectComboBoxItem {
457         private final Project project;
458 
459         private ProjectComboBoxItem(Project project) {
460             this.project = project;
461         }
462 
463         public String toString() {
464             return project.getName();
465         }
466 
467         public Project getProject() {
468             return project;
469         }
470     }
471 
472 
473     private class UserComboBoxItem {
474         private final User user;
475 
476         public UserComboBoxItem(User user) {
477             this.user = user;
478         }
479 
480         public String toString() {
481             return user.getDisplayName();
482         }
483 
484         public User getUserData() {
485             return user;
486         }
487     }
488 }
489