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.config.serverconfig;
18  
19  import com.atlassian.theplugin.ConnectionWrapper;
20  import com.atlassian.theplugin.commons.cfg.ServerCfg;
21  import com.atlassian.theplugin.commons.cfg.UserCfg;
22  import com.atlassian.theplugin.commons.remoteapi.ServerData;
23  import com.atlassian.theplugin.commons.util.UrlUtil;
24  import com.atlassian.theplugin.idea.TestConnectionListener;
25  import com.atlassian.theplugin.idea.TestConnectionProcessor;
26  import com.atlassian.theplugin.util.Connector;
27  import com.intellij.openapi.application.ApplicationManager;
28  import com.intellij.openapi.application.ModalityState;
29  import com.intellij.openapi.project.Project;
30  import com.intellij.openapi.ui.Messages;
31  import com.intellij.ui.DocumentAdapter;
32  import com.intellij.uiDesigner.core.GridConstraints;
33  import com.intellij.uiDesigner.core.GridLayoutManager;
34  
35  import javax.swing.*;
36  import javax.swing.event.ChangeEvent;
37  import javax.swing.event.ChangeListener;
38  import javax.swing.event.DocumentEvent;
39  import javax.swing.event.DocumentListener;
40  import java.awt.*;
41  import java.awt.event.ActionEvent;
42  import java.awt.event.ActionListener;
43  import java.awt.event.FocusAdapter;
44  import java.awt.event.FocusEvent;
45  
46  /**
47   * Plugin configuration form.
48   */
49  public class GenericServerConfigForm implements TestConnectionProcessor {
50      private JPanel rootComponent;
51      private JTextField serverName;
52      private JTextField serverUrl;
53      private JTextField username;
54      private JPasswordField password;
55      private JButton testConnection;
56      private JCheckBox chkPasswordRemember;
57      private JCheckBox cbEnabled;
58      private JCheckBox useDefault;
59      private DocumentListener listener;
60  
61      private transient ServerCfg serverCfg;
62      private Project project;
63      private DocumentAdapter urlDocumentListener;
64  
65      synchronized ServerCfg getServerCfg() {
66          return serverCfg;
67      }
68  
69      public GenericServerConfigForm(final Project project, final UserCfg defaultUser, final Connector tester) {
70          this.project = project;
71          $$$setupUI$$$();
72          testConnection
73                  .addActionListener(new TestConnectionListener(project, tester, new TestConnectionListener.ServerDataProvider() {
74                      public ServerData getServer() {
75                          synchronized (GenericServerConfigForm.this) {
76                              saveData();
77                              final String username = serverCfg.isUseDefaultCredentials() ? defaultUser.getUserName()
78                                      : serverCfg.getUsername();
79                              final String password = serverCfg.isUseDefaultCredentials() ? defaultUser.getPassword()
80                                      : serverCfg.getPassword();
81                              return new ServerData(serverCfg.getName(), serverCfg.getServerId().toString(), username, password,
82                                      serverCfg.getUrl());
83                          }
84                      }
85                  }, this));
86          serverUrl.addFocusListener(new FocusAdapter() {
87              @Override
88              public void focusLost(FocusEvent e) {
89                  adjustUrl();
90              }
91          });
92  
93          listener = new DocumentListener() {
94  
95              public void insertUpdate(DocumentEvent e) {
96                  setServerState();
97              }
98  
99              public void removeUpdate(DocumentEvent e) {
100                 setServerState();
101             }
102 
103             public void changedUpdate(DocumentEvent e) {
104                 setServerState();
105             }
106         };
107 
108         useDefault.addActionListener(new ActionListener() {
109             public void actionPerformed(final ActionEvent e) {
110                 enableDisableUserPassword();
111             }
112         });
113 
114         cbEnabled.addChangeListener(new ChangeListener() {
115 
116             public void stateChanged(ChangeEvent changeEvent) {
117 
118             }
119         });
120 
121         urlDocumentListener = new DocumentAdapter() {
122 
123             protected void textChanged(DocumentEvent documentEvent) {
124                 enableDisableSever(false);
125             }
126         };
127 
128         enableDisableSever(true);
129         enableDisableUserPassword();
130     }
131 
132     private synchronized void enableDisableSever(boolean quiet) {
133         if (serverUrl.getText().length() > 0) {
134             cbEnabled.setEnabled(true);
135         } else {
136             if (cbEnabled.isEnabled() && cbEnabled.isSelected() && !quiet) {
137                 emptyURLMessage();
138                 serverCfg.setEnabled(false);
139             }
140             cbEnabled.setSelected(false);
141             cbEnabled.setEnabled(false);
142 
143         }
144     }
145 
146     private void enableDisableUserPassword() {
147         if (useDefault.isSelected()) {
148             username.setEnabled(false);
149             password.setEnabled(false);
150             chkPasswordRemember.setEnabled(false);
151         } else {
152             username.setEnabled(true);
153             password.setEnabled(true);
154             chkPasswordRemember.setEnabled(true);
155         }
156     }
157 
158     public void finalizeData() {
159         adjustUrl();
160     }
161 
162     private void setServerState() {
163         // user name and password can be empty (for anonymous connections), do not check for them
164         boolean enabled = serverName.getText().length() > 0 && serverUrl.getText().length() > 0;
165         cbEnabled.setSelected(enabled);
166     }
167 
168     private void adjustUrl() {
169         serverUrl.getDocument().removeDocumentListener(urlDocumentListener);
170         String url = serverUrl.getText();
171         url = adjustUrl(url);
172         serverUrl.setText(url);
173         serverUrl.getDocument().addDocumentListener(urlDocumentListener);
174     }
175 
176     public static String adjustUrl(String url) {
177         url = UrlUtil.addHttpPrefix(url);
178         url = UrlUtil.removeUrlTrailingSlashes(url);
179         return url;
180     }
181 
182     public synchronized void setData(ServerCfg server) {
183         serverUrl.getDocument().removeDocumentListener(urlDocumentListener);
184 
185         username.getDocument().removeDocumentListener(listener);
186         password.getDocument().removeDocumentListener(listener);
187 
188         serverCfg = server;
189 
190 
191         serverName.setText(server.getName());
192         serverUrl.setText(server.getUrl());
193         username.setText(server.getUsername());
194         chkPasswordRemember.setSelected(server.isPasswordStored());
195         password.setText(server.getPassword());
196         cbEnabled.setSelected(server.isEnabled());
197         useDefault.setSelected(server.isUseDefaultCredentials());
198 
199         username.getDocument().addDocumentListener(listener);
200         password.getDocument().addDocumentListener(listener);
201         enableDisableUserPassword();
202         enableDisableSever(true);
203         serverUrl.getDocument().addDocumentListener(urlDocumentListener);
204     }
205 
206     public synchronized void saveData() {
207         if (serverCfg == null) {
208             return;
209         }
210 
211         serverCfg.setName(serverName.getText());
212         serverCfg.setUrl(serverUrl.getText());
213         serverCfg.setUsername(username.getText());
214         serverCfg.setPassword(String.valueOf(password.getPassword()));
215         serverCfg.setPasswordStored(chkPasswordRemember.isSelected());
216         serverCfg.setEnabled(cbEnabled.isSelected());
217         serverCfg.setUseDefaultCredentials(useDefault.isSelected());
218         if (serverCfg.getUrl().length() > 0) {
219             cbEnabled.setEnabled(true);
220         } else {
221             cbEnabled.setEnabled(false);
222         }
223     }
224 
225     public JComponent getRootComponent() {
226         return rootComponent;
227     }
228 
229 
230     public String getServerUrl() {
231         return serverUrl.getText();
232     }
233 
234     public String getUserName() {
235         return username.getText();
236     }
237 
238     public String getPassword() {
239         return String.valueOf(password.getPassword());
240     }
241 
242     public void onSuccess() {
243     }
244 
245     public void onError(final String errorMessage) {
246     }
247 
248     public void setConnectionResult(ConnectionWrapper.ConnectionState result) {
249         if (result == ConnectionWrapper.ConnectionState.SUCCEEDED) {
250             SwingUtilities.invokeLater(new Runnable() {
251                 public void run() {
252                     setServerState();
253                 }
254             });
255         }
256     }
257 
258 
259     public void emptyURLMessage() {
260 
261         ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
262             public void run() {
263 
264                 final ModalityState modalityState = ModalityState.stateForComponent(rootComponent);
265                 ApplicationManager.getApplication().invokeAndWait(new Runnable() {
266                     public void run() {
267                         Messages.showInfoMessage(project,
268                                 "<html>Server <b>" + serverCfg.getName() + "</b> will be disabled</b>",
269                                 "Empty server URL");
270                     }
271 
272                 }, modalityState);
273             }
274         });
275     }
276 
277     /**
278      * Method generated by IntelliJ IDEA GUI Designer
279      * >>> IMPORTANT!! <<<
280      * DO NOT edit this method OR call it in your code!
281      *
282      * @noinspection ALL
283      */
284     private void $$$setupUI$$$() {
285         rootComponent = new JPanel();
286         rootComponent.setLayout(new GridBagLayout());
287         final JPanel panel1 = new JPanel();
288         panel1.setLayout(new GridLayoutManager(7, 3, new Insets(0, 0, 0, 0), -1, -1));
289         GridBagConstraints gbc;
290         gbc = new GridBagConstraints();
291         gbc.gridx = 0;
292         gbc.gridy = 0;
293         gbc.weightx = 1.0;
294         gbc.fill = GridBagConstraints.BOTH;
295         rootComponent.add(panel1, gbc);
296         serverName = new JTextField();
297         serverName.setText("");
298         panel1.add(serverName, new GridConstraints(1, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
299         final JLabel label1 = new JLabel();
300         label1.setHorizontalAlignment(4);
301         label1.setHorizontalTextPosition(4);
302         label1.setText("Server Name:");
303         label1.setDisplayedMnemonic('S');
304         label1.setDisplayedMnemonicIndex(0);
305         panel1.add(label1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, new Dimension(80, -1), new Dimension(92, 16), null, 0, false));
306         serverUrl = new JTextField();
307         panel1.add(serverUrl, new GridConstraints(2, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
308         username = new JTextField();
309         panel1.add(username, new GridConstraints(3, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
310         password = new JPasswordField();
311         panel1.add(password, new GridConstraints(4, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
312         final JLabel label2 = new JLabel();
313         label2.setHorizontalAlignment(4);
314         label2.setText("Server URL:");
315         label2.setDisplayedMnemonic('U');
316         label2.setDisplayedMnemonicIndex(7);
317         panel1.add(label2, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, new Dimension(80, -1), new Dimension(92, 16), null, 0, false));
318         final JLabel label3 = new JLabel();
319         label3.setHorizontalAlignment(4);
320         label3.setText("Username:");
321         label3.setDisplayedMnemonic('N');
322         label3.setDisplayedMnemonicIndex(4);
323         panel1.add(label3, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(92, 16), null, 0, false));
324         final JLabel label4 = new JLabel();
325         label4.setHorizontalAlignment(4);
326         label4.setText("Password:");
327         label4.setDisplayedMnemonic('P');
328         label4.setDisplayedMnemonicIndex(0);
329         panel1.add(label4, new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(92, 16), null, 0, false));
330         testConnection = new JButton();
331         testConnection.setText("Test Connection");
332         testConnection.setMnemonic('T');
333         testConnection.setDisplayedMnemonicIndex(0);
334         panel1.add(testConnection, new GridConstraints(5, 2, 1, 1, GridConstraints.ANCHOR_NORTHEAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
335         chkPasswordRemember = new JCheckBox();
336         chkPasswordRemember.setSelected(true);
337         chkPasswordRemember.setText("Remember Password");
338         chkPasswordRemember.setMnemonic('R');
339         chkPasswordRemember.setDisplayedMnemonicIndex(0);
340         panel1.add(chkPasswordRemember, new GridConstraints(5, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
341         cbEnabled = new JCheckBox();
342         cbEnabled.setEnabled(false);
343         cbEnabled.setHorizontalTextPosition(11);
344         cbEnabled.setText("Server Enabled");
345         cbEnabled.setMnemonic('E');
346         cbEnabled.setDisplayedMnemonicIndex(7);
347         panel1.add(cbEnabled, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(115, 25), null, 0, false));
348         useDefault = new JCheckBox();
349         useDefault.setText("Use Default Credentials");
350         panel1.add(useDefault, new GridConstraints(6, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
351         final JPanel spacer1 = new JPanel();
352         gbc = new GridBagConstraints();
353         gbc.gridx = 0;
354         gbc.gridy = 1;
355         gbc.weighty = 1.0;
356         gbc.fill = GridBagConstraints.VERTICAL;
357         rootComponent.add(spacer1, gbc);
358         label1.setLabelFor(serverName);
359         label2.setLabelFor(serverUrl);
360         label3.setLabelFor(username);
361         label4.setLabelFor(password);
362     }
363 
364     /**
365      * @noinspection ALL
366      */
367     public JComponent $$$getRootComponent$$$() {
368         return rootComponent;
369     }
370 }