001: /*
002: * PasswordDialog.java
003: *
004: * Copyright (C) 1998-2004 Peter Graves
005: * $Id: PasswordDialog.java,v 1.4 2004/09/13 00:45:45 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.BorderLayout;
025: import java.awt.Dimension;
026: import java.awt.Font;
027: import java.awt.Graphics;
028: import java.awt.event.FocusEvent;
029: import java.awt.event.FocusListener;
030: import java.awt.event.KeyEvent;
031: import java.awt.event.KeyListener;
032: import javax.swing.BoxLayout;
033: import javax.swing.JDialog;
034: import javax.swing.JPanel;
035: import javax.swing.JPasswordField;
036: import javax.swing.border.EmptyBorder;
037:
038: public final class PasswordDialog extends JDialog implements
039: FocusListener, KeyListener {
040: private final Editor editor;
041: private final PasswordField textField;
042: private String input;
043:
044: private PasswordDialog(Editor editor, String prompt, String title) {
045: super (editor.getFrame(), title, true);
046: this .editor = editor;
047: input = null;
048: JPanel panel = new JPanel();
049: panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
050: panel.setBorder(new EmptyBorder(5, 5, 5, 5));
051: Label label = new Label(prompt);
052: panel.add(label);
053: textField = new PasswordField(20);
054: textField.setAlignmentX(LEFT_ALIGNMENT);
055: textField.addKeyListener(this );
056: panel.add(textField);
057: getContentPane().add(panel, BorderLayout.CENTER);
058: pack();
059: addFocusListener(this );
060: }
061:
062: public void keyPressed(KeyEvent e) {
063: switch (e.getKeyCode()) {
064: case KeyEvent.VK_ENTER:
065: input = textField.getText();
066: dispose();
067: return;
068: case KeyEvent.VK_ESCAPE:
069: dispose();
070: return;
071: }
072: }
073:
074: public void keyReleased(KeyEvent e) {
075: }
076:
077: public void keyTyped(KeyEvent e) {
078: }
079:
080: public static String showPasswordDialog(Editor editor,
081: String prompt, String title) {
082: PasswordDialog d = new PasswordDialog(editor, prompt, title);
083: editor.centerDialog(d);
084: d.show();
085: return d.input;
086: }
087:
088: public void dispose() {
089: super .dispose();
090: editor.restoreFocus();
091: }
092:
093: public void focusGained(FocusEvent e) {
094: textField.requestFocus();
095: }
096:
097: public void focusLost(FocusEvent e) {
098: }
099:
100: private static class PasswordField extends JPasswordField {
101: public PasswordField(int columns) {
102: super (columns);
103: final Preferences preferences = Editor.preferences();
104: final String fontName = preferences
105: .getStringProperty(Property.TEXT_FIELD_FONT_NAME);
106: if (fontName != null) {
107: int fontSize = preferences
108: .getIntegerProperty(Property.TEXT_FIELD_FONT_SIZE);
109: if (fontSize == 0)
110: fontSize = preferences
111: .getIntegerProperty(Property.DIALOG_FONT_SIZE);
112: setFont(new Font(fontName, Font.PLAIN, fontSize));
113: }
114: }
115:
116: public Dimension getPreferredSize() {
117: Dimension size = super .getPreferredSize();
118: size.width = getColumns() * 11;
119: return size;
120: }
121:
122: public void paintComponent(Graphics g) {
123: Display.setRenderingHints(g);
124: super.paintComponent(g);
125: }
126: }
127: }
|