001 /*
002 * Copyright (C) 2003 Adam Olsen
003 * This program is free software; you can redistribute it and/or modify
004 * it under the terms of the GNU General Public License as published by
005 * the Free Software Foundation; either version 1, or (at your option)
006 * any later version.
007 * This program is distributed in the hope that it will be useful,
008 * but WITHOUT ANY WARRANTY; without even the implied warranty of
009 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010 * GNU General Public License for more details.
011 * You should have received a copy of the GNU General Public License
012 * along with this program; if not, write to the Free Software
013 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
014 */
015 package com.valhalla.jbother;
016
017 import java.awt.GridBagConstraints;
018 import java.awt.GridBagLayout;
019 import java.awt.event.ActionEvent;
020 import java.awt.event.ActionListener;
021 import java.awt.event.WindowAdapter;
022 import java.awt.event.WindowEvent;
023 import java.util.Locale;
024 import java.util.ResourceBundle;
025
026 import javax.swing.BorderFactory;
027 import javax.swing.Box;
028 import javax.swing.BoxLayout;
029 import javax.swing.JButton;
030 import javax.swing.JDialog;
031 import javax.swing.JFrame;
032 import javax.swing.JPanel;
033 import javax.swing.JPasswordField;
034
035 /**
036 * Creates dialog for entering masked password
037 *
038 * @author synic
039 * @created March 9, 2005
040 */
041
042 public class PasswordDialog extends JDialog {
043 private ResourceBundle resources = ResourceBundle.getBundle(
044 "JBotherBundle", Locale.getDefault());
045
046 private JPanel main;
047
048 private JButton okButton = new JButton(resources.getString("okButton"));
049
050 private String text = null;
051
052 private JPasswordField field = new JPasswordField();
053 private String title;
054
055 /**
056 * Constructor for the PasswordDialog object
057 */
058 public PasswordDialog(String title) {
059 setModal(true);
060 this.title = title;
061
062 initComponents();
063 }
064
065 private void initComponents()
066 {
067 main = (JPanel) getContentPane();
068 main.setBorder(BorderFactory.createTitledBorder(this.title));
069 field.setColumns(25);
070
071 GridBagLayout grid = new GridBagLayout();
072 GridBagConstraints c = new GridBagConstraints();
073 main.setLayout(grid);
074
075 c.fill = GridBagConstraints.HORIZONTAL;
076 c.gridx = 0;
077 c.gridy = 0;
078 c.weighty = 0;
079
080 JPanel fieldPanel = new JPanel();
081 fieldPanel.add(field);
082 grid.setConstraints(fieldPanel, c);
083
084 main.add(fieldPanel);
085 fieldPanel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
086 field.setFont(okButton.getFont());
087
088 JPanel buttons = new JPanel();
089 buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
090 buttons.add(Box.createHorizontalGlue());
091 buttons.add(okButton);
092 buttons.add(Box.createHorizontalGlue());
093 buttons.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
094
095 c.gridy++;
096 grid.setConstraints(buttons, c);
097 main.add(buttons);
098
099 addListeners();
100 pack();
101 setLocationRelativeTo(null);
102
103 addWindowListener(new WindowAdapter() {
104 public void windowClosing(WindowEvent e) {
105 okHandler();
106 }
107 });
108
109 //Dimension dim = getSize();
110 //setSize( 300, (int)dim.getHeight() );
111 setResizable(false);
112
113 this.setVisible(true);
114 }
115
116 public PasswordDialog(JFrame parent,String title)
117 {
118 super(parent,title,true);
119 this.title = title;
120 initComponents();
121 }
122
123 public PasswordDialog(JDialog parent,String title)
124 {
125 super(parent,title,true);
126 this.title = title;
127 initComponents();
128 }
129
130 /**
131 * Adds the event listeners to the buttons
132 */
133 private void addListeners() {
134 PEDialogListener listener = new PEDialogListener();
135 okButton.addActionListener(listener);
136 field.addActionListener(listener);
137 }
138
139 /**
140 * Handles events
141 *
142 * @author synic
143 * @created November 30, 2004
144 */
145 class PEDialogListener implements ActionListener {
146 /**
147 * Description of the Method
148 *
149 * @param e
150 * Description of the Parameter
151 */
152 public void actionPerformed(ActionEvent e) {
153 okHandler();
154 }
155 }
156
157 /**
158 * Cancels the dialog, and quits if exitOnClose is set to true
159 */
160 private void okHandler() {
161 this.text = new String(field.getPassword());
162 dispose();
163 }
164
165 /**
166 * Gets string entered by user
167 *
168 * @return The text value
169 */
170 public String getText() {
171 return this.text;
172 }
173 }
174