001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.swing;
016:
017: import javax.swing.UIManager;
018: import javax.swing.ImageIcon;
019: import java.util.logging.*;
020:
021: /**
022: * Simple GUI frame that prompts for masked input.
023: * @author Akshathkumar Shetty
024: */
025: public class SensitiveInput extends javax.swing.JFrame {
026: private static Logger logger = Logger
027: .getLogger(SensitiveInput.class.getName());
028:
029: private javax.swing.JLabel inputLabel;
030: private javax.swing.JPanel jPanel1;
031: private javax.swing.JPasswordField passwordField;
032: private javax.swing.JButton submitButton;
033: private boolean gotInput = false;
034: private char input[] = null;
035:
036: private ImageIcon logo = new ImageIcon(getClass().getResource(
037: "/icons/logo.gif"));
038:
039: public SensitiveInput() {
040: this ("Input sensitive property value..");
041: }
042:
043: public SensitiveInput(String title) {
044: logger.finest("Loading swing gui..");
045: try {
046: UIManager
047: .setLookAndFeel("net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
048: } catch (Exception e) {
049: try {
050: UIManager.setLookAndFeel(UIManager
051: .getSystemLookAndFeelClassName());
052: } catch (Exception ee) {
053: //ignore
054: }
055: }
056: initComponents(title);
057: }
058:
059: private void initComponents(String title) {
060: setIconImage(logo.getImage());
061:
062: inputLabel = new javax.swing.JLabel();
063: jPanel1 = new javax.swing.JPanel();
064: submitButton = new javax.swing.JButton();
065: passwordField = new javax.swing.JPasswordField();
066:
067: getContentPane().setLayout(new java.awt.BorderLayout(1, 1));
068:
069: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
070: setTitle(title);
071: //setAlwaysOnTop(true);
072: setName("InputFrm");
073: setResizable(false);
074: addWindowListener(new java.awt.event.WindowAdapter() {
075: public void windowClosed(java.awt.event.WindowEvent evt) {
076: formWindowClosed(evt);
077: }
078: });
079:
080: inputLabel.setText(" Param Name");
081: inputLabel.setName("inputLabel");
082: inputLabel.setPreferredSize(new java.awt.Dimension(250, 11));
083: javax.swing.JPanel lp = new javax.swing.JPanel();
084: lp.add(inputLabel);
085: getContentPane().add(lp, java.awt.BorderLayout.NORTH);
086:
087: jPanel1.setLayout(new java.awt.BorderLayout(5, 2));
088:
089: jPanel1.setBorder(new javax.swing.border.EmptyBorder(
090: new java.awt.Insets(1, 1, 1, 1)));
091: submitButton.setText("Submit");
092: submitButton
093: .addActionListener(new java.awt.event.ActionListener() {
094: public void actionPerformed(
095: java.awt.event.ActionEvent evt) {
096: submitButtonActionPerformed(evt);
097: }
098: });
099:
100: jPanel1.add(submitButton, java.awt.BorderLayout.EAST);
101:
102: passwordField
103: .addActionListener(new java.awt.event.ActionListener() {
104: public void actionPerformed(
105: java.awt.event.ActionEvent evt) {
106: passwordFieldActionPerformed(evt);
107: }
108: });
109:
110: jPanel1.add(passwordField, java.awt.BorderLayout.CENTER);
111:
112: getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
113:
114: java.awt.Dimension screenSize = java.awt.Toolkit
115: .getDefaultToolkit().getScreenSize();
116: setBounds((screenSize.width - 260) / 2,
117: (screenSize.height - 70) / 2, 260, 70);
118: }
119:
120: private void formWindowClosed(java.awt.event.WindowEvent evt) {
121: input = null;
122: gotInput = true;
123: passwordField.setText("");
124: synchronized (this ) {
125: notify();
126: }
127: }
128:
129: private void passwordFieldActionPerformed(
130: java.awt.event.ActionEvent evt) {
131: loadPassword();
132: }
133:
134: private void submitButtonActionPerformed(
135: java.awt.event.ActionEvent evt) {
136: loadPassword();
137: }
138:
139: private void loadPassword() {
140: input = passwordField.getPassword();
141: gotInput = true;
142: passwordField.setText("");
143: synchronized (this ) {
144: notify();
145: }
146: }
147:
148: public char[] getInput(String inputName) throws java.io.IOException {
149: try {
150: gotInput = false;
151: input = null;
152: inputLabel
153: .setText("<html><font style=\"font-size:10pt;color:#535353\"><b>"
154: + inputName + "</b></font>");
155: inputLabel.setToolTipText("Value for " + inputName);
156: if (inputName.length() >= 30) {
157: passwordField.setToolTipText("Value for " + inputName);
158: }
159: System.out
160: .println("Opening gui to input sensitive property value: "
161: + inputName);
162: setVisible(true);
163: try {
164: if (gotInput == false) {
165: synchronized (this ) {
166: wait();
167: }
168: }
169: setVisible(false);
170: } catch (Exception e) {
171: logger.warning("Error: " + e);
172: throw e;
173: }
174: return input;
175: } catch (Exception e) {
176: logger
177: .warning("Error opening GUI to input sensitive property value : "
178: + e);
179: return org.quickserver.util.io.PasswordField
180: .getPassword("Input property value for "
181: + inputName + " : ");
182: }
183: }
184:
185: public static void main(String args[]) throws Exception {
186: SensitiveInput si = new SensitiveInput();
187: char pass[] = si.getInput("Some Password");
188: if (pass != null)
189: logger.info("Some Password : " + new String(pass));
190: else
191: logger.info("Some Password : " + pass);
192:
193: pass = si.getInput("Other Password");
194: if (pass != null)
195: logger.info("Other Password : " + new String(pass));
196: else
197: logger.info("Other Password : " + pass);
198: }
199: }
|