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