KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > propedit > PasswordEditorPane


1 package net.suberic.util.gui.propedit;
2 import javax.swing.*;
3 import java.awt.event.*;
4 import net.suberic.util.*;
5 import java.awt.FlowLayout JavaDoc;
6
7 public class PasswordEditorPane extends StringEditorPane {
8   String JavaDoc originalScrambledValue;
9
10   /**
11    * @param propertyName The property to be edited.
12    * @param template The property that will define the layout of the
13    * editor.
14    * @param manager The PropertyEditorManager that will manage the
15    * changes.
16    */

17   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
18     configureBasic(propertyName, template, propertyBaseName, newManager);
19
20     originalScrambledValue = manager.getProperty(property, "");
21     if (!originalScrambledValue.equals(""))
22       originalValue = descrambleString(originalScrambledValue);
23     else
24       originalValue = "";
25     currentValue = originalValue;
26
27     getLogger().fine("property is " + property + "; editorTemplate is " + editorTemplate);
28
29     label = createLabel();
30
31     inputField = new JPasswordField(originalValue);
32     inputField.setPreferredSize(new java.awt.Dimension JavaDoc(150, inputField.getMinimumSize().height));
33     inputField.setMaximumSize(new java.awt.Dimension JavaDoc(Integer.MAX_VALUE, inputField.getMinimumSize().height));
34     inputField.addFocusListener(new FocusAdapter() {
35         public void focusLost(FocusEvent e) {
36           String JavaDoc newPassword = new String JavaDoc(((JPasswordField)inputField).getPassword());
37           if (! newPassword.equals(currentValue)) {
38             try {
39               firePropertyChangingEvent(newPassword);
40               firePropertyChangedEvent(newPassword);
41               currentValue = newPassword;
42             } catch (PropertyValueVetoException pvve) {
43               manager.getFactory().showError(inputField, "Error changing value " + label.getText() + ": " + pvve.getReason());
44               inputField.setText(currentValue);
45             }
46           }
47         }
48
49         public void focusGained(FocusEvent e) {
50           if (inputField.getText() != null && inputField.getText().length() > 0) {
51             inputField.setSelectionStart(0);
52             inputField.setSelectionEnd(inputField.getText().length());
53           }
54         }
55
56       });
57     this.add(label);
58     this.add(inputField);
59     updateEditorEnabled();
60
61     labelComponent = label;
62     valueComponent = inputField;
63
64     //manager.registerPropertyEditor(property, this);
65
}
66
67   /**
68    * This writes the currently configured value in the PropertyEditorUI
69    * to the source VariableBundle.
70    */

71   public void setValue() throws PropertyValueVetoException {
72     String JavaDoc value = new String JavaDoc(((JPasswordField)inputField).getPassword());
73
74     if (isEditorEnabled() && ! (value.equals(currentValue))) {
75       firePropertyChangingEvent(value);
76       firePropertyChangedEvent(value);
77     }
78
79     if (isEditorEnabled() && !(value.equals(originalValue))) {
80       manager.setProperty(property, scrambleString(value));
81       originalValue = value;
82     }
83   }
84
85   /**
86    * Returns the current values of the edited properties as a
87    * java.util.Properties object.
88    */

89   public java.util.Properties JavaDoc getValue() {
90     String JavaDoc value = new String JavaDoc(((JPasswordField)inputField).getPassword());
91     java.util.Properties JavaDoc retProps = new java.util.Properties JavaDoc();
92     if (value.equals(originalValue))
93       retProps.setProperty(property, originalScrambledValue);
94     else
95       retProps.setProperty(property, scrambleString(value));
96     return retProps;
97   }
98
99   /**
100    * This resets the editor to the original (or latest set, if setValue()
101    * has been called) value of the edited property.
102    */

103   public void resetDefaultValue() {
104     String JavaDoc fieldValue = new String JavaDoc(((JPasswordField)inputField).getPassword());
105     if (! (fieldValue.equals(currentValue) && fieldValue.equals(originalValue))) {
106       // something has changed, so we'll have to deal with it.
107
try {
108         if (! currentValue.equals(originalValue)) {
109           firePropertyChangingEvent(originalValue);
110           firePropertyChangedEvent(originalValue);
111           currentValue = originalValue;
112         }
113         inputField.setText(originalValue);
114       } catch (PropertyValueVetoException pvve) {
115         manager.getFactory().showError(inputField, "Error changing value " + label.getText() + " to " + originalValue + ": " + pvve.getReason());
116       }
117     }
118   }
119
120   // the list of characters to use for scrambling.
121
private static char[] scrambleChars = new char[] {'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', 'z'};
122
123   /**
124    * This is a simple scrambler.
125    */

126   public static String JavaDoc scrambleString(String JavaDoc key) {
127     int[] salt = new int[4];
128     int keySize = key.length();
129     long seed = System.currentTimeMillis();
130
131     salt[0] = (int)((seed / 107) % 2704);
132     salt[1] = (int)((seed / 19) % 2704);
133     salt[2] = (int)((seed / 17) % 2704);
134     salt[3] = (int)((seed / 91) % 2704);
135
136     char [] scrambledString = new char[(keySize * 2) + 8];
137
138     for (int i = 0; i < keySize; i++) {
139       int numValue = (int)(key.charAt(i));
140       numValue = (numValue + salt[i % 4]) % 2704;
141       scrambledString[i * 2] = scrambleChars[numValue / 52];
142       scrambledString[(i * 2) + 1] = scrambleChars[numValue % 52];
143     }
144
145     for (int i = 0; i < 3; i++) {
146       int numValue = (salt[i] + salt[i + 1]) % 2704;
147       scrambledString[(keySize + i) * 2] = scrambleChars[numValue / 52];
148       scrambledString[((keySize + i) * 2) + 1] = scrambleChars[numValue % 52];
149     }
150
151     scrambledString[(keySize + 3) * 2] = scrambleChars[salt[3] / 52];
152     scrambledString[((keySize + 3) * 2) + 1] = scrambleChars[salt[3] % 52];
153
154     return new String JavaDoc(scrambledString);
155   }
156
157   /**
158    * And this is a simple descrambler.
159    */

160   public static String JavaDoc descrambleString(String JavaDoc value) {
161     int[] salt = new int[4];
162     int scrambleSize = value.length();
163     char[] key = new char[(scrambleSize - 8) / 2];
164     salt[3] = (findCharValue(value.charAt(scrambleSize - 2)) * 52) + findCharValue(value.charAt(scrambleSize - 1));
165
166     for (int i = 2; i >= 0; i--) {
167       salt[i] = (2704 - salt[i + 1] + (findCharValue(value.charAt(scrambleSize - ((4 - i) * 2) )) * 52) + findCharValue(value.charAt(scrambleSize - ((4 - i) * 2) + 1))) % 2704;
168     }
169
170     for (int i = 0; i < (scrambleSize - 8) / 2; i++) {
171       key[i] = (char)((2704 - salt[i % 4] + (findCharValue(value.charAt(i * 2)) * 52) + findCharValue(value.charAt((i * 2) + 1))) % 2704);
172     }
173
174     return new String JavaDoc(key);
175   }
176
177   /**
178    * This very inefficiently finds a character value in the scrambleChars
179    * array.
180    */

181   private static int findCharValue(char a) {
182     for (int i = 0; i < scrambleChars.length; i++)
183       if (a == scrambleChars[i])
184         return i;
185
186     return 0;
187   }
188
189 }
190
Popular Tags