1 14 15 package org.quickserver.swing; 16 17 import javax.swing.UIManager ; 18 import javax.swing.ImageIcon ; 19 import java.util.logging.*; 20 21 25 public class SensitiveInput extends javax.swing.JFrame { 26 private static Logger logger = Logger.getLogger(SensitiveInput.class.getName()); 27 28 private javax.swing.JLabel inputLabel; 29 private javax.swing.JPanel jPanel1; 30 private javax.swing.JPasswordField passwordField; 31 private javax.swing.JButton submitButton; 32 private boolean gotInput = false; 33 private char input[] = null; 34 35 private ImageIcon logo = new ImageIcon (getClass().getResource("/icons/logo.gif")); 36 37 public SensitiveInput() { 38 this("Input sensitive property value.."); 39 } 40 41 public SensitiveInput(String title) { 42 logger.finest("Loading swing gui.."); 43 try { 44 UIManager.setLookAndFeel("net.sourceforge.mlf.metouia.MetouiaLookAndFeel"); 45 } catch(Exception e) { 46 try { 47 UIManager.setLookAndFeel( 48 UIManager.getSystemLookAndFeelClassName()); 49 } catch(Exception ee) { 50 } 52 } 53 initComponents(title); 54 } 55 56 private void initComponents(String title) { 57 setIconImage(logo.getImage()); 58 59 inputLabel = new javax.swing.JLabel (); 60 jPanel1 = new javax.swing.JPanel (); 61 submitButton = new javax.swing.JButton (); 62 passwordField = new javax.swing.JPasswordField (); 63 64 getContentPane().setLayout(new java.awt.BorderLayout (1, 1)); 65 66 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 67 setTitle(title); 68 setName("InputFrm"); 70 setResizable(false); 71 addWindowListener(new java.awt.event.WindowAdapter () { 72 public void windowClosed(java.awt.event.WindowEvent evt) { 73 formWindowClosed(evt); 74 } 75 }); 76 77 inputLabel.setText(" Param Name"); 78 inputLabel.setName("inputLabel"); 79 inputLabel.setPreferredSize(new java.awt.Dimension (250, 11)); 80 javax.swing.JPanel lp = new javax.swing.JPanel (); 81 lp.add(inputLabel); 82 getContentPane().add(lp, java.awt.BorderLayout.NORTH); 83 84 jPanel1.setLayout(new java.awt.BorderLayout (5, 2)); 85 86 jPanel1.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (1, 1, 1, 1))); 87 submitButton.setText("Submit"); 88 submitButton.addActionListener(new java.awt.event.ActionListener () { 89 public void actionPerformed(java.awt.event.ActionEvent evt) { 90 submitButtonActionPerformed(evt); 91 } 92 }); 93 94 jPanel1.add(submitButton, java.awt.BorderLayout.EAST); 95 96 passwordField.addActionListener(new java.awt.event.ActionListener () { 97 public void actionPerformed(java.awt.event.ActionEvent 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 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 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 evt) { 120 loadPassword(); 121 } 122 123 private void submitButtonActionPerformed(java.awt.event.ActionEvent 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 inputName) throws java.io.IOException { 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 e) { 155 logger.warning("Error: "+e); 156 throw e; 157 } 158 return input; 159 } catch(Exception 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 args[]) throws Exception { 167 SensitiveInput si = new SensitiveInput(); 168 char pass[] = si.getInput("Some Password"); 169 if(pass!=null) 170 logger.info("Some Password : "+new String (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 (pass)); 177 else 178 logger.info("Other Password : "+pass); 179 } 180 } 181 | Popular Tags |