KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > PasswordDialog


1 /*
2  * PasswordDialog.java
3  *
4  * Copyright (C) 1998-2004 Peter Graves
5  * $Id: PasswordDialog.java,v 1.4 2004/09/13 00:45:45 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.BorderLayout JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.Graphics JavaDoc;
28 import java.awt.event.FocusEvent JavaDoc;
29 import java.awt.event.FocusListener JavaDoc;
30 import java.awt.event.KeyEvent JavaDoc;
31 import java.awt.event.KeyListener JavaDoc;
32 import javax.swing.BoxLayout JavaDoc;
33 import javax.swing.JDialog JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.JPasswordField JavaDoc;
36 import javax.swing.border.EmptyBorder JavaDoc;
37
38 public final class PasswordDialog extends JDialog JavaDoc implements FocusListener JavaDoc,
39     KeyListener JavaDoc
40 {
41     private final Editor editor;
42     private final PasswordField JavaDoc textField;
43     private String JavaDoc input;
44
45     private PasswordDialog(Editor editor, String JavaDoc prompt, String JavaDoc title)
46     {
47         super(editor.getFrame(), title, true);
48         this.editor = editor;
49         input = null;
50         JPanel JavaDoc panel = new JPanel JavaDoc();
51         panel.setLayout(new BoxLayout JavaDoc(panel, BoxLayout.Y_AXIS));
52         panel.setBorder(new EmptyBorder JavaDoc(5, 5, 5, 5));
53         Label label = new Label(prompt);
54         panel.add(label);
55         textField = new PasswordField JavaDoc(20);
56         textField.setAlignmentX(LEFT_ALIGNMENT);
57         textField.addKeyListener(this);
58         panel.add(textField);
59         getContentPane().add(panel, BorderLayout.CENTER);
60         pack();
61         addFocusListener(this);
62     }
63
64     public void keyPressed(KeyEvent JavaDoc e)
65     {
66         switch (e.getKeyCode()) {
67             case KeyEvent.VK_ENTER:
68                 input = textField.getText();
69                 dispose();
70                 return;
71             case KeyEvent.VK_ESCAPE:
72                 dispose();
73                 return;
74         }
75     }
76
77     public void keyReleased(KeyEvent JavaDoc e) {}
78
79     public void keyTyped(KeyEvent JavaDoc e) {}
80
81     public static String JavaDoc showPasswordDialog(Editor editor, String JavaDoc prompt,
82                                             String JavaDoc title)
83     {
84         PasswordDialog d = new PasswordDialog(editor, prompt, title);
85         editor.centerDialog(d);
86         d.show();
87         return d.input;
88     }
89
90     public void dispose()
91     {
92         super.dispose();
93         editor.restoreFocus();
94     }
95
96     public void focusGained(FocusEvent JavaDoc e)
97     {
98         textField.requestFocus();
99     }
100
101     public void focusLost(FocusEvent JavaDoc e) {}
102
103     private static class PasswordField extends JPasswordField JavaDoc
104     {
105         public PasswordField(int columns)
106         {
107             super(columns);
108             final Preferences preferences = Editor.preferences();
109             final String JavaDoc fontName =
110                 preferences.getStringProperty(Property.TEXT_FIELD_FONT_NAME);
111             if (fontName != null) {
112                 int fontSize =
113                     preferences.getIntegerProperty(Property.TEXT_FIELD_FONT_SIZE);
114                 if (fontSize == 0)
115                     fontSize =
116                     preferences.getIntegerProperty(Property.DIALOG_FONT_SIZE);
117                 setFont(new Font JavaDoc(fontName, Font.PLAIN, fontSize));
118             }
119         }
120
121         public Dimension JavaDoc getPreferredSize() {
122             Dimension JavaDoc size = super.getPreferredSize();
123             size.width = getColumns() * 11;
124             return size;
125         }
126
127         public void paintComponent(Graphics JavaDoc g)
128         {
129             Display.setRenderingHints(g);
130             super.paintComponent(g);
131         }
132     }
133 }
134
Popular Tags