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.LoginDataProvided;
21  import com.atlassian.theplugin.commons.cfg.ServerCfg;
22  import com.atlassian.theplugin.commons.util.UrlUtil;
23  import com.atlassian.theplugin.idea.TestConnectionListener;
24  import com.atlassian.theplugin.util.Connector;
25  import com.intellij.openapi.project.Project;
26  import com.intellij.uiDesigner.core.GridConstraints;
27  import com.intellij.uiDesigner.core.GridLayoutManager;
28  
29  import javax.swing.*;
30  import javax.swing.event.DocumentEvent;
31  import javax.swing.event.DocumentListener;
32  import java.awt.*;
33  import java.awt.event.FocusAdapter;
34  import java.awt.event.FocusEvent;
35  
36  /**
37   * Plugin configuration form.
38   */
39  public class GenericServerConfigForm implements LoginDataProvided {
40  	private JPanel rootComponent;
41  	private JTextField serverName;
42  	private JTextField serverUrl;
43  	private JTextField username;
44  	private JPasswordField password;
45  	private JButton testConnection;
46  	private JCheckBox chkPasswordRemember;
47  	private JCheckBox cbEnabled;
48      private DocumentListener listener;
49  
50  	private transient ServerCfg serverCfg;
51  
52  	public ServerCfg getServerCfg() {
53  		return serverCfg;
54  	}
55  
56  	public GenericServerConfigForm(final Project project, final Connector tester) {
57  
58  		$$$setupUI$$$();
59  		testConnection.addActionListener(new TestConnectionListener(project, tester, this));
60  		serverUrl.addFocusListener(new FocusAdapter() {
61  			@Override
62  			public void focusLost(FocusEvent e) {
63  				adjustUrl();
64  			}
65  		});
66  
67  		listener = new DocumentListener() {
68  
69  			public void insertUpdate(DocumentEvent e) {
70  				setServerState();
71  			}
72  
73  			public void removeUpdate(DocumentEvent e) {
74  				setServerState();
75  			}
76  
77  			public void changedUpdate(DocumentEvent e) {
78  				setServerState();
79  			}
80  		};
81  	}
82  
83  	public void finalizeData() {
84  		adjustUrl();
85  	}
86  
87  	private void setServerState() {
88  		// password can be empty, do not check for it
89  		boolean enabled =
90  				username.getText().length() > 0
91  				&& serverName.getText().length() > 0
92  				&& serverUrl.getText().length() > 0;
93  		cbEnabled.setSelected(enabled);
94  	}
95  
96  	private void adjustUrl() {
97  		String url = serverUrl.getText();
98  		url = adjustUrl(url);
99  		serverUrl.setText(url);
100 	}
101 
102 	public static String adjustUrl(String url) {
103 		url = UrlUtil.addHttpPrefix(url);
104 		url = UrlUtil.removeUrlTrailingSlashes(url);
105 		return url;
106 	}
107 
108 	public void setData(ServerCfg server) {
109 
110 		username.getDocument().removeDocumentListener(listener);
111 		password.getDocument().removeDocumentListener(listener);
112 
113 		serverCfg = server;
114 
115 		serverName.setText(server.getName());
116 		serverUrl.setText(server.getUrl());
117 		username.setText(server.getUsername());
118 		chkPasswordRemember.setSelected(server.isPasswordStored());
119 		password.setText(server.getPassword());
120 		cbEnabled.setSelected(server.isEnabled());
121 
122 
123 		username.getDocument().addDocumentListener(listener);
124 		password.getDocument().addDocumentListener(listener);
125 	}
126 
127 	public void saveData() {
128 		if (serverCfg == null) {
129 			return;
130 		}
131 
132 		serverCfg.setName(serverName.getText());
133 		serverCfg.setUrl(serverUrl.getText());
134 		serverCfg.setUsername(username.getText());
135 		serverCfg.setPassword(String.valueOf(password.getPassword()));
136 		serverCfg.setPasswordStored(chkPasswordRemember.isSelected());
137 		serverCfg.setEnabled(cbEnabled.isSelected());
138 	}
139 
140 	public JComponent getRootComponent() {
141 		return rootComponent;
142 	}
143 
144 
145 	public String getServerUrl() {
146 		return serverUrl.getText();
147 	}
148 
149 	public String getUserName() {
150 		return username.getText();
151 	}
152 
153 	public String getPassword() {
154 		return String.valueOf(password.getPassword());
155 	}
156 
157 	public void setConnectionResult(ConnectionWrapper.ConnectionState result) {
158 		if (result == ConnectionWrapper.ConnectionState.SUCCEEDED) {
159 			SwingUtilities.invokeLater(new Runnable() {
160 				public void run() {
161 					setServerState();
162 				}
163 			});
164 		}
165 	}
166 
167 	/**
168 	 * Method generated by IntelliJ IDEA GUI Designer
169 	 * >>> IMPORTANT!! <<<
170 	 * DO NOT edit this method OR call it in your code!
171 	 *
172 	 * @noinspection ALL
173 	 */
174 	private void $$$setupUI$$$() {
175 		rootComponent = new JPanel();
176 		rootComponent.setLayout(new GridBagLayout());
177 		final JPanel panel1 = new JPanel();
178 		panel1.setLayout(new GridLayoutManager(7, 3, new Insets(0, 0, 0, 0), -1, -1));
179 		GridBagConstraints gbc;
180 		gbc = new GridBagConstraints();
181 		gbc.gridx = 0;
182 		gbc.gridy = 0;
183 		gbc.weightx = 1.0;
184 		gbc.fill = GridBagConstraints.BOTH;
185 		rootComponent.add(panel1, gbc);
186 		serverName = new JTextField();
187 		serverName.setText("");
188 		panel1.add(serverName, new GridConstraints(1, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL,
189 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
190 				null, new Dimension(150, -1), null, 0, false));
191 		final JLabel label1 = new JLabel();
192 		label1.setHorizontalAlignment(4);
193 		label1.setHorizontalTextPosition(4);
194 		label1.setText("Server Name");
195 		label1.setDisplayedMnemonic('S');
196 		label1.setDisplayedMnemonicIndex(0);
197 		panel1.add(label1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
198 				GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, new Dimension(80, -1),
199 				new Dimension(80, -1), null, 0, false));
200 		serverUrl = new JTextField();
201 		panel1.add(serverUrl, new GridConstraints(2, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL,
202 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
203 				null, new Dimension(150, -1), null, 0, false));
204 		username = new JTextField();
205 		panel1.add(username, new GridConstraints(3, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL,
206 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
207 				null, new Dimension(150, -1), null, 0, false));
208 		password = new JPasswordField();
209 		panel1.add(password, new GridConstraints(4, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL,
210 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
211 				null, new Dimension(150, -1), null, 0, false));
212 		final JLabel label2 = new JLabel();
213 		label2.setText("Server URL");
214 		label2.setDisplayedMnemonic('U');
215 		label2.setDisplayedMnemonicIndex(7);
216 		panel1.add(label2, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
217 				GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
218 		final JLabel label3 = new JLabel();
219 		label3.setText("Username");
220 		label3.setDisplayedMnemonic('N');
221 		label3.setDisplayedMnemonicIndex(4);
222 		panel1.add(label3, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
223 				GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
224 		final JLabel label4 = new JLabel();
225 		label4.setText("Password");
226 		label4.setDisplayedMnemonic('P');
227 		label4.setDisplayedMnemonicIndex(0);
228 		panel1.add(label4, new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
229 				GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
230 		testConnection = new JButton();
231 		testConnection.setText("Test Connection");
232 		testConnection.setMnemonic('T');
233 		testConnection.setDisplayedMnemonicIndex(0);
234 		panel1.add(testConnection, new GridConstraints(5, 2, 1, 1, GridConstraints.ANCHOR_NORTHEAST, GridConstraints.FILL_NONE,
235 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
236 				null, null, null, 0, false));
237 		chkPasswordRemember = new JCheckBox();
238 		chkPasswordRemember.setSelected(true);
239 		chkPasswordRemember.setText("Remember Password");
240 		chkPasswordRemember.setMnemonic('R');
241 		chkPasswordRemember.setDisplayedMnemonicIndex(0);
242 		panel1.add(chkPasswordRemember, new GridConstraints(5, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
243 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
244 				null, null, null, 0, false));
245 		cbEnabled = new JCheckBox();
246 		cbEnabled.setHorizontalTextPosition(11);
247 		cbEnabled.setText("Server Enabled");
248 		cbEnabled.setMnemonic('E');
249 		cbEnabled.setDisplayedMnemonicIndex(7);
250 		panel1.add(cbEnabled, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
251 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
252 				null, new Dimension(115, 25), null, 0, false));
253 		final JPanel spacer1 = new JPanel();
254 		gbc = new GridBagConstraints();
255 		gbc.gridx = 0;
256 		gbc.gridy = 1;
257 		gbc.weighty = 1.0;
258 		gbc.fill = GridBagConstraints.VERTICAL;
259 		rootComponent.add(spacer1, gbc);
260 		label1.setLabelFor(serverName);
261 		label2.setLabelFor(serverUrl);
262 		label3.setLabelFor(username);
263 		label4.setLabelFor(password);
264 	}
265 
266 	/**
267 	 * @noinspection ALL
268 	 */
269 	public JComponent $$$getRootComponent$$$() {
270 		return rootComponent;
271 	}
272 }