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.LoginDataProvided;
20  import com.atlassian.theplugin.commons.Server;
21  import com.atlassian.theplugin.commons.configuration.ServerBean;
22  import com.atlassian.theplugin.idea.TestConnectionListener;
23  import com.atlassian.theplugin.util.Connector;
24  import com.atlassian.theplugin.commons.util.UrlUtil;
25  import com.intellij.uiDesigner.core.GridConstraints;
26  import com.intellij.uiDesigner.core.GridLayoutManager;
27  
28  import javax.swing.*;
29  import java.awt.*;
30  import java.awt.event.FocusAdapter;
31  import java.awt.event.FocusEvent;
32  
33  /**
34   * Plugin configuration form.
35   */
36  public class GenericServerConfigForm extends JComponent implements ServerPanel, LoginDataProvided {
37  	private JPanel rootComponent;
38  	private JTextField serverName;
39  	private JTextField serverUrl;
40  	private JTextField username;
41  	private JPasswordField password;
42  	private JButton testConnection;
43  	private JCheckBox chkPasswordRemember;
44  	private JCheckBox cbEnabled;
45  
46  	private transient Server originalServer;
47  
48  	public GenericServerConfigForm(final Connector tester) {
49  
50  		$$$setupUI$$$();
51  		testConnection.addActionListener(new TestConnectionListener(tester, this));
52  		serverUrl.addFocusListener(new FocusAdapter() {
53  			public void focusLost(FocusEvent e) {
54  				adjustUrl();
55  			}
56  		});
57  	}
58  
59  	private void adjustUrl() {
60  		String url = serverUrl.getText();
61  		url = UrlUtil.addHttpPrefix(url);
62  		url = UrlUtil.removeUrlTrailingSlashes(url);
63  		serverUrl.setText(url);
64  	}
65  
66  	public void setData(Server server) {
67  		this.originalServer = new ServerBean(server);
68  
69  		serverName.setText(server.getName());
70  		serverUrl.setText(server.getUrlString());
71  		username.setText(server.getUserName());
72  		chkPasswordRemember.setSelected(server.getShouldPasswordBeStored());
73  		password.setText(server.transientGetPasswordString());
74  		cbEnabled.setSelected(server.getEnabled());
75  	}
76  
77  	public Server getData() {
78  		adjustUrl();
79  
80  		Server server = new ServerBean(originalServer);
81  		server.setName(serverName.getText());
82  		server.setUrlString(serverUrl.getText());
83  		server.setUserName(username.getText());
84  		server.transientSetPasswordString(String.valueOf(password.getPassword()), chkPasswordRemember.isSelected());
85  		server.setEnabled(cbEnabled.isSelected());
86  		return server;
87  	}
88  
89  	public boolean isModified() {
90  		boolean isModified = false;
91  
92  		if (originalServer != null) {
93  			if (chkPasswordRemember.isSelected() != originalServer.getShouldPasswordBeStored()) {
94  				return true;
95  			}
96  			if (serverName.getText() != null
97  					? !serverName.getText().equals(originalServer.getName()) : originalServer.getName() != null) {
98  				return true;
99  			}
100 			if (cbEnabled.isSelected() != originalServer.getEnabled()) {
101 				return true;
102 			}
103 			if (serverUrl.getText() != null
104 					? !serverUrl.getText().equals(originalServer.getUrlString()) : originalServer.getUrlString() != null) {
105 				return true;
106 			}
107 			if (username.getText() != null
108 					? !username.getText().equals(originalServer.getUserName()) : originalServer.getUserName() != null) {
109 				return true;
110 			}
111 			String pass = String.valueOf(password.getPassword());
112 			if (!pass.equals(originalServer.transientGetPasswordString())) {
113 				return true;
114 			}
115 
116 		}
117 		return isModified;
118 	}
119 
120 
121 	public JComponent getRootComponent() {
122 		return rootComponent;
123 	}
124 
125 	public void setVisible(boolean visible) {
126 		rootComponent.setVisible(visible);
127 	}
128 
129 	private void createUIComponents() {
130 	}
131 
132 	public String getServerUrl() {
133 		return serverUrl.getText();
134 	}
135 
136 	public String getUserName() {
137 		return username.getText();
138 	}
139 
140 	public String getPassword() {
141 		return String.valueOf(password.getPassword());
142 	}
143 
144 	/**
145 	 * Method generated by IntelliJ IDEA GUI Designer
146 	 * >>> IMPORTANT!! <<<
147 	 * DO NOT edit this method OR call it in your code!
148 	 *
149 	 * @noinspection ALL
150 	 */
151 	private void $$$setupUI$$$() {
152 		rootComponent = new JPanel();
153 		rootComponent.setLayout(new GridBagLayout());
154 		final JPanel panel1 = new JPanel();
155 		panel1.setLayout(new GridLayoutManager(7, 3, new Insets(0, 0, 0, 0), -1, -1));
156 		GridBagConstraints gbc;
157 		gbc = new GridBagConstraints();
158 		gbc.gridx = 0;
159 		gbc.gridy = 0;
160 		gbc.weightx = 1.0;
161 		gbc.fill = GridBagConstraints.BOTH;
162 		rootComponent.add(panel1, gbc);
163 		serverName = new JTextField();
164 		serverName.setText("");
165 		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));
166 		final JLabel label1 = new JLabel();
167 		label1.setText("Server Name");
168 		label1.setDisplayedMnemonic('S');
169 		label1.setDisplayedMnemonicIndex(0);
170 		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));
171 		serverUrl = new JTextField();
172 		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));
173 		username = new JTextField();
174 		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));
175 		password = new JPasswordField();
176 		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));
177 		final JLabel label2 = new JLabel();
178 		label2.setText("Server URL");
179 		label2.setDisplayedMnemonic('U');
180 		label2.setDisplayedMnemonicIndex(7);
181 		panel1.add(label2, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
182 		final JLabel label3 = new JLabel();
183 		label3.setText("Username");
184 		label3.setDisplayedMnemonic('N');
185 		label3.setDisplayedMnemonicIndex(4);
186 		panel1.add(label3, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
187 		final JLabel label4 = new JLabel();
188 		label4.setText("Password");
189 		label4.setDisplayedMnemonic('P');
190 		label4.setDisplayedMnemonicIndex(0);
191 		panel1.add(label4, new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
192 		testConnection = new JButton();
193 		testConnection.setText("Test Connection");
194 		testConnection.setMnemonic('T');
195 		testConnection.setDisplayedMnemonicIndex(0);
196 		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));
197 		chkPasswordRemember = new JCheckBox();
198 		chkPasswordRemember.setSelected(true);
199 		chkPasswordRemember.setText("Remember Password");
200 		chkPasswordRemember.setMnemonic('R');
201 		chkPasswordRemember.setDisplayedMnemonicIndex(0);
202 		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));
203 		cbEnabled = new JCheckBox();
204 		cbEnabled.setHorizontalTextPosition(11);
205 		cbEnabled.setText("Server Enabled");
206 		cbEnabled.setMnemonic('E');
207 		cbEnabled.setDisplayedMnemonicIndex(7);
208 		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));
209 		final JPanel spacer1 = new JPanel();
210 		gbc = new GridBagConstraints();
211 		gbc.gridx = 0;
212 		gbc.gridy = 1;
213 		gbc.weighty = 1.0;
214 		gbc.fill = GridBagConstraints.VERTICAL;
215 		rootComponent.add(spacer1, gbc);
216 		label1.setLabelFor(serverName);
217 		label2.setLabelFor(serverUrl);
218 		label3.setLabelFor(username);
219 		label4.setLabelFor(password);
220 	}
221 
222 	/**
223 	 * @noinspection ALL
224 	 */
225 	public JComponent $$$getRootComponent$$$() {
226 		return rootComponent;
227 	}
228 }