KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > util > PasswordDialog


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18

19 package org.columba.mail.gui.util;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.GridBagConstraints JavaDoc;
23 import java.awt.GridBagLayout JavaDoc;
24 import java.awt.GridLayout JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.io.File JavaDoc;
30
31 import javax.swing.BorderFactory JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JCheckBox JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JDialog JavaDoc;
36 import javax.swing.JLabel JavaDoc;
37 import javax.swing.JOptionPane JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.JPasswordField JavaDoc;
40 import javax.swing.KeyStroke JavaDoc;
41
42 import org.columba.core.config.DefaultConfigDirectory;
43 import org.columba.core.gui.base.ButtonWithMnemonic;
44 import org.columba.core.gui.frame.FrameManager;
45 import org.columba.core.resourceloader.ImageLoader;
46 import org.columba.mail.util.MailResourceLoader;
47
48 /**
49  * Password dialog prompts user for password.
50  *
51  * @author fdietz
52  */

53 public class PasswordDialog extends JDialog JavaDoc implements ActionListener JavaDoc {
54     private char[] password;
55
56     private boolean bool = false;
57
58     private JPasswordField JavaDoc passwordField;
59
60     private JCheckBox JavaDoc checkbox;
61
62     private JButton JavaDoc okButton;
63
64     private JButton JavaDoc cancelButton;
65
66     private JButton JavaDoc helpButton;
67
68     public PasswordDialog() {
69         super(FrameManager.getInstance().getActiveFrame(), true);
70
71     }
72
73     protected JPanel JavaDoc createButtonPanel() {
74         JPanel JavaDoc bottom = new JPanel JavaDoc();
75         bottom.setLayout(new BorderLayout JavaDoc());
76
77         //bottom.setLayout( new BoxLayout( bottom, BoxLayout.X_AXIS ) );
78
bottom.setBorder(BorderFactory.createEmptyBorder(17, 12, 11, 11));
79
80         //bottom.add( Box.createHorizontalStrut());
81
cancelButton = new ButtonWithMnemonic(MailResourceLoader.getString(
82                 "global", "cancel"));
83
84         //$NON-NLS-1$ //$NON-NLS-2$
85
cancelButton.addActionListener(this);
86         cancelButton.setActionCommand("CANCEL"); //$NON-NLS-1$
87

88         okButton = new ButtonWithMnemonic(MailResourceLoader.getString(
89                 "global", "ok"));
90
91         //$NON-NLS-1$ //$NON-NLS-2$
92
okButton.addActionListener(this);
93         okButton.setActionCommand("OK"); //$NON-NLS-1$
94
okButton.setDefaultCapable(true);
95         getRootPane().setDefaultButton(okButton);
96
97         helpButton = new ButtonWithMnemonic(MailResourceLoader.getString(
98                 "global", "help"));
99
100         //$NON-NLS-1$ //$NON-NLS-2$
101
JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
102         buttonPanel.setLayout(new GridLayout JavaDoc(1, 3, 5, 0));
103         buttonPanel.add(okButton);
104         buttonPanel.add(cancelButton);
105         buttonPanel.add(helpButton);
106
107         //bottom.add( Box.createHorizontalGlue() );
108
bottom.add(buttonPanel, BorderLayout.EAST);
109
110         return bottom;
111     }
112
113     /**
114      * Make dialog visible.
115      * <p>
116      * Note that the emailAddress parameter, needs to be really unique. I
117      * therefore suggest a combination between host and login name, instead.
118      * This way user can see his login name. -> changed method signature!
119      *
120      * @param login
121      * login name
122      * @param host
123      * host name
124      * @param password
125      * password
126      * @param save
127      * should the password be saved?
128      */

129     public void showDialog(String JavaDoc message, String JavaDoc password, boolean save) {
130
131         /*
132          * JLabel hostLabel = new JLabel(MessageFormat.format(MailResourceLoader
133          * .getString("dialog", "password", "enter_password"), new Object[] {
134          * user, host }));
135          */

136         JLabel JavaDoc hostLabel = new JLabel JavaDoc(message);
137
138         passwordField = new JPasswordField JavaDoc(password, 40);
139
140         checkbox = new JCheckBox JavaDoc(MailResourceLoader.getString("dialog",
141                 "password", "save_password"));
142         checkbox.setSelected(save);
143         checkbox.setActionCommand("SAVE");
144         checkbox.addActionListener(this);
145
146         setTitle(MailResourceLoader.getString("dialog", "password",
147                 "dialog_title"));
148
149         JPanel JavaDoc centerPanel = new JPanel JavaDoc();
150         centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
151
152         getContentPane().add(centerPanel, BorderLayout.CENTER);
153
154         GridBagLayout JavaDoc mainLayout = new GridBagLayout JavaDoc();
155         centerPanel.setLayout(mainLayout);
156
157         GridBagConstraints JavaDoc mainConstraints = new GridBagConstraints JavaDoc();
158
159         JLabel JavaDoc iconLabel = new JLabel JavaDoc(ImageLoader
160                 .getMiscIcon("signature-nokey.png"));
161         mainConstraints.anchor = GridBagConstraints.NORTHWEST;
162         mainConstraints.weightx = 1.0;
163         mainConstraints.gridwidth = GridBagConstraints.RELATIVE;
164         mainConstraints.fill = GridBagConstraints.HORIZONTAL;
165         mainLayout.setConstraints(iconLabel, mainConstraints);
166         centerPanel.add(iconLabel);
167
168         mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
169         mainConstraints.anchor = GridBagConstraints.WEST;
170         mainConstraints.insets = new Insets JavaDoc(0, 5, 0, 0);
171         mainLayout.setConstraints(hostLabel, mainConstraints);
172         centerPanel.add(hostLabel);
173
174         mainConstraints.insets = new Insets JavaDoc(5, 5, 0, 0);
175         mainLayout.setConstraints(passwordField, mainConstraints);
176         centerPanel.add(passwordField);
177
178         mainConstraints.insets = new Insets JavaDoc(5, 5, 0, 0);
179         mainLayout.setConstraints(checkbox, mainConstraints);
180         centerPanel.add(checkbox);
181
182         JPanel JavaDoc bottomPanel = new JPanel JavaDoc();
183         bottomPanel.setLayout(new BorderLayout JavaDoc());
184
185         JPanel JavaDoc buttonPanel = createButtonPanel();
186         bottomPanel.add(buttonPanel, BorderLayout.CENTER);
187
188         getContentPane().add(bottomPanel, BorderLayout.SOUTH);
189         getRootPane().setDefaultButton(okButton);
190         getRootPane().registerKeyboardAction(this, "CANCEL",
191                 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
192                 JComponent.WHEN_IN_FOCUSED_WINDOW);
193         pack();
194         setLocationRelativeTo(null);
195         setVisible(true);
196         requestFocus();
197         passwordField.requestFocus();
198     }
199
200     public char[] getPassword() {
201         return password;
202     }
203
204     public boolean success() {
205         return bool;
206     }
207
208     public boolean getSave() {
209         return checkbox.isSelected();
210     }
211
212     public void actionPerformed(ActionEvent JavaDoc e) {
213         String JavaDoc action = e.getActionCommand();
214
215         if (action.equals("OK")) {
216             password = passwordField.getPassword();
217
218             bool = true;
219             dispose();
220         } else if (action.equals("CANCEL")) {
221             bool = false;
222             dispose();
223         } else if (action.equals("SAVE")) {
224             if (!checkbox.isSelected()) {
225                 return;
226             } else {
227                 File JavaDoc configPath = DefaultConfigDirectory.getInstance().getCurrentPath();
228                 File JavaDoc defaultConfigPath = DefaultConfigDirectory.getDefaultPath();
229                 while (!configPath.equals(defaultConfigPath)) {
230                     configPath = configPath.getParentFile();
231                     if (configPath == null) {
232                         JOptionPane.showMessageDialog(this, MailResourceLoader
233                                 .getString("dialog", "password",
234                                         "warn_save_msg"), MailResourceLoader
235                                 .getString("dialog", "password",
236                                         "warn_save_title"),
237                                 JOptionPane.WARNING_MESSAGE);
238                         return;
239                     }
240                 }
241             }
242         }
243     }
244 }
Popular Tags