001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018:
019: package org.columba.mail.gui.util;
020:
021: import java.awt.BorderLayout;
022: import java.awt.GridBagConstraints;
023: import java.awt.GridBagLayout;
024: import java.awt.GridLayout;
025: import java.awt.Insets;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.KeyEvent;
029: import java.io.File;
030:
031: import javax.swing.BorderFactory;
032: import javax.swing.JButton;
033: import javax.swing.JCheckBox;
034: import javax.swing.JComponent;
035: import javax.swing.JDialog;
036: import javax.swing.JLabel;
037: import javax.swing.JOptionPane;
038: import javax.swing.JPanel;
039: import javax.swing.JPasswordField;
040: import javax.swing.KeyStroke;
041:
042: import org.columba.core.config.DefaultConfigDirectory;
043: import org.columba.core.gui.base.ButtonWithMnemonic;
044: import org.columba.core.gui.frame.FrameManager;
045: import org.columba.core.resourceloader.ImageLoader;
046: import org.columba.mail.util.MailResourceLoader;
047:
048: /**
049: * Password dialog prompts user for password.
050: *
051: * @author fdietz
052: */
053: public class PasswordDialog extends JDialog implements ActionListener {
054: private char[] password;
055:
056: private boolean bool = false;
057:
058: private JPasswordField passwordField;
059:
060: private JCheckBox checkbox;
061:
062: private JButton okButton;
063:
064: private JButton cancelButton;
065:
066: private JButton helpButton;
067:
068: public PasswordDialog() {
069: super (FrameManager.getInstance().getActiveFrame(), true);
070:
071: }
072:
073: protected JPanel createButtonPanel() {
074: JPanel bottom = new JPanel();
075: bottom.setLayout(new BorderLayout());
076:
077: //bottom.setLayout( new BoxLayout( bottom, BoxLayout.X_AXIS ) );
078: bottom.setBorder(BorderFactory
079: .createEmptyBorder(17, 12, 11, 11));
080:
081: //bottom.add( Box.createHorizontalStrut());
082: cancelButton = new ButtonWithMnemonic(MailResourceLoader
083: .getString("global", "cancel"));
084:
085: //$NON-NLS-1$ //$NON-NLS-2$
086: cancelButton.addActionListener(this );
087: cancelButton.setActionCommand("CANCEL"); //$NON-NLS-1$
088:
089: okButton = new ButtonWithMnemonic(MailResourceLoader.getString(
090: "global", "ok"));
091:
092: //$NON-NLS-1$ //$NON-NLS-2$
093: okButton.addActionListener(this );
094: okButton.setActionCommand("OK"); //$NON-NLS-1$
095: okButton.setDefaultCapable(true);
096: getRootPane().setDefaultButton(okButton);
097:
098: helpButton = new ButtonWithMnemonic(MailResourceLoader
099: .getString("global", "help"));
100:
101: //$NON-NLS-1$ //$NON-NLS-2$
102: JPanel buttonPanel = new JPanel();
103: buttonPanel.setLayout(new GridLayout(1, 3, 5, 0));
104: buttonPanel.add(okButton);
105: buttonPanel.add(cancelButton);
106: buttonPanel.add(helpButton);
107:
108: //bottom.add( Box.createHorizontalGlue() );
109: bottom.add(buttonPanel, BorderLayout.EAST);
110:
111: return bottom;
112: }
113:
114: /**
115: * Make dialog visible.
116: * <p>
117: * Note that the emailAddress parameter, needs to be really unique. I
118: * therefore suggest a combination between host and login name, instead.
119: * This way user can see his login name. -> changed method signature!
120: *
121: * @param login
122: * login name
123: * @param host
124: * host name
125: * @param password
126: * password
127: * @param save
128: * should the password be saved?
129: */
130: public void showDialog(String message, String password, boolean save) {
131:
132: /*
133: * JLabel hostLabel = new JLabel(MessageFormat.format(MailResourceLoader
134: * .getString("dialog", "password", "enter_password"), new Object[] {
135: * user, host }));
136: */
137: JLabel hostLabel = new JLabel(message);
138:
139: passwordField = new JPasswordField(password, 40);
140:
141: checkbox = new JCheckBox(MailResourceLoader.getString("dialog",
142: "password", "save_password"));
143: checkbox.setSelected(save);
144: checkbox.setActionCommand("SAVE");
145: checkbox.addActionListener(this );
146:
147: setTitle(MailResourceLoader.getString("dialog", "password",
148: "dialog_title"));
149:
150: JPanel centerPanel = new JPanel();
151: centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5,
152: 5));
153:
154: getContentPane().add(centerPanel, BorderLayout.CENTER);
155:
156: GridBagLayout mainLayout = new GridBagLayout();
157: centerPanel.setLayout(mainLayout);
158:
159: GridBagConstraints mainConstraints = new GridBagConstraints();
160:
161: JLabel iconLabel = new JLabel(ImageLoader
162: .getMiscIcon("signature-nokey.png"));
163: mainConstraints.anchor = GridBagConstraints.NORTHWEST;
164: mainConstraints.weightx = 1.0;
165: mainConstraints.gridwidth = GridBagConstraints.RELATIVE;
166: mainConstraints.fill = GridBagConstraints.HORIZONTAL;
167: mainLayout.setConstraints(iconLabel, mainConstraints);
168: centerPanel.add(iconLabel);
169:
170: mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
171: mainConstraints.anchor = GridBagConstraints.WEST;
172: mainConstraints.insets = new Insets(0, 5, 0, 0);
173: mainLayout.setConstraints(hostLabel, mainConstraints);
174: centerPanel.add(hostLabel);
175:
176: mainConstraints.insets = new Insets(5, 5, 0, 0);
177: mainLayout.setConstraints(passwordField, mainConstraints);
178: centerPanel.add(passwordField);
179:
180: mainConstraints.insets = new Insets(5, 5, 0, 0);
181: mainLayout.setConstraints(checkbox, mainConstraints);
182: centerPanel.add(checkbox);
183:
184: JPanel bottomPanel = new JPanel();
185: bottomPanel.setLayout(new BorderLayout());
186:
187: JPanel buttonPanel = createButtonPanel();
188: bottomPanel.add(buttonPanel, BorderLayout.CENTER);
189:
190: getContentPane().add(bottomPanel, BorderLayout.SOUTH);
191: getRootPane().setDefaultButton(okButton);
192: getRootPane().registerKeyboardAction(this , "CANCEL",
193: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
194: JComponent.WHEN_IN_FOCUSED_WINDOW);
195: pack();
196: setLocationRelativeTo(null);
197: setVisible(true);
198: requestFocus();
199: passwordField.requestFocus();
200: }
201:
202: public char[] getPassword() {
203: return password;
204: }
205:
206: public boolean success() {
207: return bool;
208: }
209:
210: public boolean getSave() {
211: return checkbox.isSelected();
212: }
213:
214: public void actionPerformed(ActionEvent e) {
215: String action = e.getActionCommand();
216:
217: if (action.equals("OK")) {
218: password = passwordField.getPassword();
219:
220: bool = true;
221: dispose();
222: } else if (action.equals("CANCEL")) {
223: bool = false;
224: dispose();
225: } else if (action.equals("SAVE")) {
226: if (!checkbox.isSelected()) {
227: return;
228: } else {
229: File configPath = DefaultConfigDirectory.getInstance()
230: .getCurrentPath();
231: File defaultConfigPath = DefaultConfigDirectory
232: .getDefaultPath();
233: while (!configPath.equals(defaultConfigPath)) {
234: configPath = configPath.getParentFile();
235: if (configPath == null) {
236: JOptionPane.showMessageDialog(this ,
237: MailResourceLoader.getString("dialog",
238: "password", "warn_save_msg"),
239: MailResourceLoader.getString("dialog",
240: "password", "warn_save_title"),
241: JOptionPane.WARNING_MESSAGE);
242: return;
243: }
244: }
245: }
246: }
247: }
248: }
|