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;
18  
19  import com.atlassian.theplugin.LoginDataProvided;
20  import com.atlassian.theplugin.commons.Server;
21  import com.atlassian.theplugin.commons.exception.ThePluginException;
22  import com.atlassian.theplugin.commons.remoteapi.ProductServerFacade;
23  import com.atlassian.theplugin.commons.remoteapi.RemoteApiException;
24  import com.atlassian.theplugin.util.Connector;
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.*;
31  
32  public class PasswordDialog extends JDialog implements LoginDataProvided {
33  
34  	private JPanel passwordPanel;
35  	private JCheckBox chkRememberPassword;
36  	private JPasswordField passwordField;
37  	private JButton testConnectionButton;
38  	private JLabel lblCommand;
39  	private JTextField userName;
40  	private transient Server server;
41  
42  	public PasswordDialog(final Server server, final ProductServerFacade serverFacade) {
43  		this.server = server;
44  		setContentPane(passwordPanel);
45  		setModal(true);
46  // call onCancel() when cross is clicked
47  		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
48  		addWindowListener(new WindowAdapter() {
49  			public void windowClosing(WindowEvent e) {
50  				onCancel();
51  			}
52  		});
53  		lblCommand.setText("<html><p>Please provide password to connect \"" + this.server.getName() + "\" server:</p> <p><i>" + this.server.getUrlString() + "</i></p></html>");
54  // call onCancel() on ESCAPE
55  		passwordPanel.registerKeyboardAction(new ActionListener() {
56  			public void actionPerformed(ActionEvent e) {
57  				onCancel();
58  			}
59  		}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
60  
61  		testConnectionButton.addActionListener(new TestConnectionListener(new Connector() {
62  
63  			public void connect() throws ThePluginException {
64  				this.validate();
65  				try {
66  					serverFacade.testServerConnection(
67  							super.getUrl(), super.getUserName(), super.getPassword());
68  				} catch (RemoteApiException e) {
69  					throw new ThePluginException("Error connecting server.", e);
70  				}
71  			}
72  
73  		}, this));
74  	}
75  
76  	private void onCancel() {
77  // add your code here if necessary
78  		dispose();
79  	}
80  
81  
82  	public JPanel getPasswordPanel() {
83  		return passwordPanel;
84  	}
85  
86  	public String getPasswordString() {
87  		return String.valueOf(passwordField.getPassword());
88  	}
89  
90  	public Boolean getShouldPasswordBeStored() {
91  		return chkRememberPassword.isSelected();
92  	}
93  
94  	public void setUserName(String anUsername) {
95  		this.userName.setText(anUsername);
96  	}
97  
98  	public String getUserName() {
99  		return this.userName.getText();
100 	}
101 
102 	private void createUIComponents() {
103 		// TODO: place custom component creation code here
104 	}
105 
106 	public String getServerUrl() {
107 		return server.getUrlString();
108 	}
109 
110 	public String getPassword() {
111 		return getPasswordString();
112 	}
113 
114 	{
115 // GUI initializer generated by IntelliJ IDEA GUI Designer
116 // >>> IMPORTANT!! <<<
117 // DO NOT EDIT OR ADD ANY CODE HERE!
118 		$$$setupUI$$$();
119 	}
120 
121 	/**
122 	 * Method generated by IntelliJ IDEA GUI Designer
123 	 * >>> IMPORTANT!! <<<
124 	 * DO NOT edit this method OR call it in your code!
125 	 *
126 	 * @noinspection ALL
127 	 */
128 	private void $$$setupUI$$$() {
129 		passwordPanel = new JPanel();
130 		passwordPanel.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
131 		final JPanel panel1 = new JPanel();
132 		panel1.setLayout(new GridLayoutManager(5, 2, new Insets(0, 0, 0, 0), -1, -1));
133 		passwordPanel.add(panel1, new GridConstraints(1, 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));
134 		passwordField = new JPasswordField();
135 		panel1.add(passwordField, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
136 		testConnectionButton = new JButton();
137 		testConnectionButton.setText("Test connection");
138 		testConnectionButton.setMnemonic('T');
139 		testConnectionButton.setDisplayedMnemonicIndex(0);
140 		panel1.add(testConnectionButton, new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
141 		chkRememberPassword = new JCheckBox();
142 		chkRememberPassword.setEnabled(true);
143 		chkRememberPassword.setSelected(false);
144 		chkRememberPassword.setText("Store password in configuration");
145 		panel1.add(chkRememberPassword, new GridConstraints(4, 1, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
146 		lblCommand = new JLabel();
147 		lblCommand.setText("");
148 		panel1.add(lblCommand, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
149 		final JLabel label1 = new JLabel();
150 		label1.setText("Password");
151 		panel1.add(label1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
152 		final JLabel label2 = new JLabel();
153 		label2.setText("User Name");
154 		panel1.add(label2, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
155 		userName = new JTextField();
156 		panel1.add(userName, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
157 		label1.setLabelFor(passwordField);
158 		label2.setLabelFor(userName);
159 	}
160 
161 	/**
162 	 * @noinspection ALL
163 	 */
164 	public JComponent $$$getRootComponent$$$() {
165 		return passwordPanel;
166 	}
167 }