View Javadoc

1   package com.atlassian.theplugin.idea.crucible;
2   
3   import com.atlassian.theplugin.commons.cfg.CrucibleServerCfg;
4   import com.atlassian.theplugin.configuration.CrucibleViewConfigurationBean;
5   import com.atlassian.theplugin.idea.ui.KeyPressGobbler;
6   import com.intellij.openapi.project.Project;
7   import com.intellij.openapi.ui.DialogWrapper;
8   import com.intellij.uiDesigner.core.GridConstraints;
9   import com.intellij.uiDesigner.core.GridLayoutManager;
10  import com.intellij.uiDesigner.core.Spacer;
11  import org.jetbrains.annotations.Nullable;
12  
13  import javax.swing.*;
14  import java.awt.*;
15  import java.awt.event.ActionEvent;
16  import java.awt.event.ActionListener;
17  import java.awt.event.KeyAdapter;
18  import java.awt.event.KeyEvent;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashSet;
22  
23  public class SearchReviewDialog extends DialogWrapper {
24      private JPanel rootPane;
25      private JTextField ctrlReviewSearch;
26      private JPanel ctrlServersPanel;
27      private JLabel ctrlLocalInfoLabel;
28      private Collection<CrucibleServerCfg> selectedServers = new HashSet<CrucibleServerCfg>();
29      private CrucibleViewConfigurationBean crucibleViewConfiguration;
30  
31      public Collection<CrucibleServerCfg> getSelectedServers() {
32          return selectedServers;
33      }
34  
35      public SearchReviewDialog(Project project, final Collection<CrucibleServerCfg> servers,
36                                final CrucibleViewConfigurationBean crucibleViewConfiguration) {
37          super(project, true);
38          this.crucibleViewConfiguration = crucibleViewConfiguration;
39  
40          $$$setupUI$$$();
41          init();
42          pack();
43  
44          setTitle("Search Review");
45          getOKAction().putValue(Action.NAME, "Search");
46          getOKAction().setEnabled(false);
47  
48          addServersCheckboxes(servers);
49          if (selectedServers.size() != 0) {
50              ctrlLocalInfoLabel.setVisible(false);
51          }
52  
53          ctrlReviewSearch.addKeyListener(new KeyAdapter() {
54              public void keyReleased(final KeyEvent e) {
55                  if (ctrlReviewSearch.getText().length() == 0) {
56                      getOKAction().setEnabled(false);
57                  } else {
58                      getOKAction().setEnabled(true);
59                  }
60              }
61          });
62          KeyPressGobbler.gobbleKeyPress(ctrlReviewSearch);
63          setOKActionEnabled(false);
64      }
65  
66      protected void doOKAction() {
67  
68          if (crucibleViewConfiguration != null) {
69  
70              Collection<String> searchServers = new ArrayList<String>();
71  
72              for (CrucibleServerCfg server : selectedServers) {
73                  searchServers.add(server.getServerId().toString());
74              }
75              crucibleViewConfiguration.setSearchServers(searchServers);
76          }
77  
78          super.doOKAction();
79  //		close(OK_EXIT_CODE);
80      }
81  
82      public JComponent getPreferredFocusedComponent() {
83          return ctrlReviewSearch;
84      }
85  
86      private void addServersCheckboxes(final Collection<CrucibleServerCfg> servers) {
87  
88          ctrlServersPanel.setLayout(new BoxLayout(ctrlServersPanel, BoxLayout.Y_AXIS));
89  
90          if (servers != null) {
91              for (final CrucibleServerCfg server : servers) {
92                  final CrucibleServerCheckbox checkbox = new CrucibleServerCheckbox(server);
93                  ctrlServersPanel.add(checkbox);
94  
95                  if (crucibleViewConfiguration != null && crucibleViewConfiguration.getSearchServers() != null
96                          && crucibleViewConfiguration.getSearchServers().contains(server.getServerId().toString())) {
97                      checkbox.setSelected(true);
98                      selectedServers.add(server);
99                  }
100 
101                 checkbox.addActionListener(new ActionListener() {
102                     public void actionPerformed(final ActionEvent e) {
103                         if (checkbox.isSelected()) {
104                             selectedServers.add(server);
105                             ctrlLocalInfoLabel.setVisible(false);
106                         } else {
107                             selectedServers.remove(server);
108                             if (selectedServers.size() == 0) {
109                                 ctrlLocalInfoLabel.setVisible(true);
110                                 pack();
111                             }
112                         }
113                     }
114                 });
115             }
116         }
117     }
118 
119     @Nullable
120     protected JComponent createCenterPanel() {
121         return getRootComponent();
122     }
123 
124     private JComponent getRootComponent() {
125         return rootPane;
126     }
127 
128     public String getSearchKey() {
129         return ctrlReviewSearch.getText().trim();
130     }
131 
132     /**
133      * Method generated by IntelliJ IDEA GUI Designer
134      * >>> IMPORTANT!! <<<
135      * DO NOT edit this method OR call it in your code!
136      *
137      * @noinspection ALL
138      */
139     private void $$$setupUI$$$() {
140         rootPane = new JPanel();
141         rootPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 0, 10), -1, -1));
142         final JPanel panel1 = new JPanel();
143         panel1.setLayout(new GridLayoutManager(3, 2, new Insets(0, 0, 10, 0), -1, -1));
144         rootPane.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));
145         final JLabel label1 = new JLabel();
146         label1.setHorizontalAlignment(4);
147         label1.setHorizontalTextPosition(0);
148         label1.setText("Server(s): ");
149         label1.setVerticalAlignment(0);
150         label1.setVerticalTextPosition(0);
151         panel1.add(label1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
152         final Spacer spacer1 = new Spacer();
153         panel1.add(spacer1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
154         ctrlReviewSearch = new JTextField();
155         panel1.add(ctrlReviewSearch, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
156         final JLabel label2 = new JLabel();
157         label2.setHorizontalAlignment(4);
158         label2.setText("Review Key: ");
159         panel1.add(label2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
160         ctrlServersPanel = new JPanel();
161         ctrlServersPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
162         panel1.add(ctrlServersPanel, new GridConstraints(1, 1, 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));
163         ctrlLocalInfoLabel = new JLabel();
164         ctrlLocalInfoLabel.setText("No selection. Only local list of reviews will be searched");
165         ctrlLocalInfoLabel.setVerticalAlignment(0);
166         rootPane.add(ctrlLocalInfoLabel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
167     }
168 
169     /**
170      * @noinspection ALL
171      */
172     public JComponent $$$getRootComponent$$$() {
173         return rootPane;
174     }
175 
176 
177     private static class CrucibleServerCheckbox extends JCheckBox {
178         private CrucibleServerCfg server;
179 
180         public CrucibleServerCheckbox(CrucibleServerCfg server) {
181             super(server.getName());
182             this.server = server;
183         }
184 
185         public CrucibleServerCfg getServer() {
186             return server;
187         }
188     }
189 }