001: /*
002: * PasswordDialog.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Dimension;
025: import java.awt.Frame;
026: import java.awt.GridBagConstraints;
027: import java.awt.GridBagLayout;
028: import java.awt.Insets;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031:
032: import javax.swing.JButton;
033: import javax.swing.JDialog;
034: import javax.swing.JLabel;
035: import javax.swing.JPanel;
036: import javax.swing.JPasswordField;
037:
038: /* ----------------------------------------------------------
039: * CVS NOTE: Changes to the CVS repository prior to the
040: * release of version 3.0.0beta1 has meant a
041: * resetting of CVS revision numbers.
042: * ----------------------------------------------------------
043: */
044:
045: /**
046: *
047: * @author Takis Diakoumis
048: * @version $Revision: 1.4 $
049: * @date $Date: 2006/05/14 06:56:07 $
050: */
051: public class PasswordDialog extends JDialog {
052:
053: public static final int OK = 1;
054: public static final int CANCEL = 0;
055:
056: private JButton okButton;
057: private JButton cancelButton;
058:
059: private JPasswordField field;
060:
061: private String message;
062:
063: private int result;
064:
065: /** <p>Constructs a new instance with the specified owner,
066: * title and message.
067: *
068: * @param the dialog owner
069: * @param the dialog title
070: * @param the dialog message displayed
071: */
072: public PasswordDialog(Frame owner, String title, String message) {
073: super (owner, title, true);
074: this .message = message;
075: jbInit();
076: display();
077: }
078:
079: private void jbInit() {
080: try {
081: JPanel base = new JPanel(new GridBagLayout());
082:
083: okButton = new JButton("OK");
084: cancelButton = new JButton("Cancel");
085:
086: Insets btnIns = new Insets(2, 2, 2, 2);
087: okButton.setMargin(btnIns);
088: cancelButton.setMargin(btnIns);
089:
090: Dimension btnDim = new Dimension(65, 25);
091: okButton.setPreferredSize(btnDim);
092: cancelButton.setPreferredSize(btnDim);
093:
094: ActionListener buttonListener = new ActionListener() {
095: public void actionPerformed(ActionEvent e) {
096: buttons_actionPerformed(e);
097: }
098: };
099:
100: okButton.addActionListener(buttonListener);
101: cancelButton.addActionListener(buttonListener);
102:
103: field = new JPasswordField();
104: field.setPreferredSize(new Dimension(250, 20));
105: field.addActionListener(buttonListener);
106:
107: GridBagConstraints gbc = new GridBagConstraints();
108: Insets ins = new Insets(10, 15, 0, 15);
109: gbc.gridwidth = 2;
110: gbc.insets = ins;
111: gbc.anchor = GridBagConstraints.NORTHWEST;
112: base.add(new JLabel(message), gbc);
113: gbc.gridy = 1;
114: base.add(field, gbc);
115: gbc.gridy = 2;
116: gbc.gridwidth = 1;
117: gbc.anchor = GridBagConstraints.EAST;
118: gbc.insets.right = 0;
119: gbc.insets.left = 70;
120: gbc.insets.bottom = 15;
121: gbc.weighty = 1.0;
122: base.add(okButton, gbc);
123: gbc.anchor = GridBagConstraints.WEST;
124: gbc.insets.left = 5;
125: gbc.gridx = 1;
126: base.add(cancelButton, gbc);
127:
128: setResizable(false);
129: getContentPane().add(base);
130:
131: } catch (Exception e) {
132: e.printStackTrace();
133: }
134: }
135:
136: public String getValue() {
137:
138: if (result == CANCEL) {
139: return null;
140: }
141:
142: char[] pwd = field.getPassword();
143:
144: StringBuffer pwdBuffer = new StringBuffer(10);
145:
146: for (int i = 0; i < pwd.length; i++) {
147: pwdBuffer.append(pwd[i]);
148: pwd[i] = 0;
149: }
150:
151: return pwdBuffer.toString();
152: }
153:
154: public int getResult() {
155: return result;
156: }
157:
158: private void buttons_actionPerformed(ActionEvent e) {
159: result = -1;
160:
161: if (e.getSource() == cancelButton) {
162: result = CANCEL;
163: } else {
164: result = OK;
165: }
166: setVisible(false);
167: }
168:
169: private void display() {
170: pack();
171: setLocation(GUIUtils
172: .getLocationForDialog(getOwner(), getSize()));
173: setVisible(true);
174: }
175:
176: }
|