KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > swing > SensitiveInput


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.swing;
16
17 import javax.swing.UIManager JavaDoc;
18 import javax.swing.ImageIcon JavaDoc;
19 import java.util.logging.*;
20
21 /**
22  * Simple GUI frame that prompts for masked input.
23  * @author Akshathkumar Shetty
24  */

25 public class SensitiveInput extends javax.swing.JFrame JavaDoc {
26     private static Logger logger = Logger.getLogger(SensitiveInput.class.getName());
27
28     private javax.swing.JLabel JavaDoc inputLabel;
29     private javax.swing.JPanel JavaDoc jPanel1;
30     private javax.swing.JPasswordField JavaDoc passwordField;
31     private javax.swing.JButton JavaDoc submitButton;
32     private boolean gotInput = false;
33     private char input[] = null;
34
35     private ImageIcon JavaDoc logo = new ImageIcon JavaDoc(getClass().getResource("/icons/logo.gif"));
36
37     public SensitiveInput() {
38         this("Input sensitive property value..");
39     }
40
41     public SensitiveInput(String JavaDoc title) {
42         logger.finest("Loading swing gui..");
43         try {
44             UIManager.setLookAndFeel("net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
45         } catch(Exception JavaDoc e) {
46             try {
47                 UIManager.setLookAndFeel(
48                     UIManager.getSystemLookAndFeelClassName());
49             } catch(Exception JavaDoc ee) {
50                 //ignore
51
}
52         }
53         initComponents(title);
54     }
55     
56     private void initComponents(String JavaDoc title) {
57         setIconImage(logo.getImage());
58
59         inputLabel = new javax.swing.JLabel JavaDoc();
60         jPanel1 = new javax.swing.JPanel JavaDoc();
61         submitButton = new javax.swing.JButton JavaDoc();
62         passwordField = new javax.swing.JPasswordField JavaDoc();
63
64         getContentPane().setLayout(new java.awt.BorderLayout JavaDoc(1, 1));
65
66         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
67         setTitle(title);
68         //setAlwaysOnTop(true);
69
setName("InputFrm");
70         setResizable(false);
71         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
72             public void windowClosed(java.awt.event.WindowEvent JavaDoc evt) {
73                 formWindowClosed(evt);
74             }
75         });
76
77         inputLabel.setText(" Param Name");
78         inputLabel.setName("inputLabel");
79         inputLabel.setPreferredSize(new java.awt.Dimension JavaDoc(250, 11));
80         javax.swing.JPanel JavaDoc lp = new javax.swing.JPanel JavaDoc();
81         lp.add(inputLabel);
82         getContentPane().add(lp, java.awt.BorderLayout.NORTH);
83
84         jPanel1.setLayout(new java.awt.BorderLayout JavaDoc(5, 2));
85
86         jPanel1.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(1, 1, 1, 1)));
87         submitButton.setText("Submit");
88         submitButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
89             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
90                 submitButtonActionPerformed(evt);
91             }
92         });
93
94         jPanel1.add(submitButton, java.awt.BorderLayout.EAST);
95
96         passwordField.addActionListener(new java.awt.event.ActionListener JavaDoc() {
97             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
98                 passwordFieldActionPerformed(evt);
99             }
100         });
101
102         jPanel1.add(passwordField, java.awt.BorderLayout.CENTER);
103
104         getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
105
106         java.awt.Dimension JavaDoc screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
107         setBounds((screenSize.width-260)/2, (screenSize.height-70)/2, 260, 70);
108     }
109
110     private void formWindowClosed(java.awt.event.WindowEvent JavaDoc evt) {
111         input = null;
112         gotInput = true;
113         passwordField.setText("");
114         synchronized(this) {
115             notify();
116         }
117     }
118
119     private void passwordFieldActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
120        loadPassword();
121     }
122
123     private void submitButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
124        loadPassword();
125     }
126     
127     private void loadPassword() {
128         input = passwordField.getPassword();
129         gotInput = true;
130         passwordField.setText("");
131         synchronized(this) {
132             notify();
133         }
134     }
135     
136     public char[] getInput(String JavaDoc inputName) throws java.io.IOException JavaDoc {
137         try {
138             gotInput = false;
139             input = null;
140             inputLabel.setText("<html><font style=\"font-size:10pt;color:#535353\"><b>"+inputName+"</b></font>");
141             inputLabel.setToolTipText("Value for "+inputName);
142             if(inputName.length()>=30) {
143                 passwordField.setToolTipText("Value for "+inputName);
144             }
145             System.out.println("Opening gui to input sensitive property value: "+inputName);
146             setVisible(true);
147             try {
148                 if(gotInput==false) {
149                     synchronized(this) {
150                         wait();
151                     }
152                 }
153                 setVisible(false);
154             } catch(Exception JavaDoc e) {
155                 logger.warning("Error: "+e);
156                 throw e;
157             }
158             return input;
159         } catch(Exception JavaDoc e) {
160             logger.warning("Error opening GUI to input sensitive property value : "+e);
161             return org.quickserver.util.io.PasswordField.getPassword("Input property value for "+inputName+" : ");
162         }
163     }
164     
165     
166     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
167        SensitiveInput si = new SensitiveInput();
168        char pass[] = si.getInput("Some Password");
169        if(pass!=null)
170            logger.info("Some Password : "+new String JavaDoc(pass));
171        else
172            logger.info("Some Password : "+pass);
173        
174        pass = si.getInput("Other Password");
175         if(pass!=null)
176            logger.info("Other Password : "+new String JavaDoc(pass));
177         else
178            logger.info("Other Password : "+pass);
179     }
180 }
181
Popular Tags