001: package net.suberic.util.gui.propedit;
002:
003: import javax.swing.*;
004: import java.awt.event.*;
005: import net.suberic.util.*;
006: import java.awt.FlowLayout;
007:
008: public class PasswordEditorPane extends StringEditorPane {
009: String originalScrambledValue;
010: JLabel label;
011:
012: /**
013: * @param propertyName The property to be edited.
014: * @param template The property that will define the layout of the
015: * editor.
016: * @param manager The PropertyEditorManager that will manage the
017: * changes.
018: * @param isEnabled Whether or not this editor is enabled by default.
019: */
020: public void configureEditor(String propertyName, String template,
021: PropertyEditorManager newManager, boolean isEnabled) {
022: property = propertyName;
023: manager = newManager;
024: editorTemplate = template;
025: originalScrambledValue = manager.getProperty(property, "");
026: if (!originalScrambledValue.equals(""))
027: originalValue = descrambleString(originalScrambledValue);
028: else
029: originalValue = "";
030: currentValue = originalValue;
031:
032: if (debug) {
033: System.out.println("property is " + property
034: + "; editorTemplate is " + editorTemplate);
035: }
036:
037: label = createLabel();
038:
039: inputField = new JPasswordField(originalValue);
040: inputField.setPreferredSize(new java.awt.Dimension(150,
041: inputField.getMinimumSize().height));
042: inputField.addFocusListener(new FocusAdapter() {
043: public void focusLost(FocusEvent e) {
044: String newPassword = new String(
045: ((JPasswordField) inputField).getPassword());
046: if (!newPassword.equals(currentValue)) {
047: try {
048: firePropertyChangingEvent(newPassword);
049: firePropertyChangedEvent(newPassword);
050: currentValue = newPassword;
051: } catch (PropertyValueVetoException pvve) {
052: manager.getFactory().showError(
053: inputField,
054: "Error changing value "
055: + label.getText() + ": "
056: + pvve.getReason());
057: inputField.setText(currentValue);
058: }
059: }
060: }
061: });
062: this .add(label);
063: this .add(inputField);
064: this .setEnabled(isEnabled);
065:
066: labelComponent = label;
067: valueComponent = inputField;
068:
069: manager.registerPropertyEditor(property, this );
070: }
071:
072: /**
073: * This writes the currently configured value in the PropertyEditorUI
074: * to the source VariableBundle.
075: */
076: public void setValue() throws PropertyValueVetoException {
077: String value = new String(((JPasswordField) inputField)
078: .getPassword());
079:
080: if (isEnabled() && !(value.equals(currentValue))) {
081: firePropertyChangingEvent(value);
082: firePropertyChangedEvent(value);
083: }
084:
085: if (isEnabled() && !(value.equals(originalValue))) {
086: manager.setProperty(property, scrambleString(value));
087: }
088: }
089:
090: /**
091: * Returns the current values of the edited properties as a
092: * java.util.Properties object.
093: */
094: public java.util.Properties getValue() {
095: String value = new String(((JPasswordField) inputField)
096: .getPassword());
097: java.util.Properties retProps = new java.util.Properties();
098: if (value.equals(originalValue))
099: retProps.setProperty(property, originalScrambledValue);
100: else
101: retProps.setProperty(property, scrambleString(value));
102: return retProps;
103: }
104:
105: /**
106: * This resets the editor to the original (or latest set, if setValue()
107: * has been called) value of the edited property.
108: */
109: public void resetDefaultValue() {
110: String fieldValue = new String(((JPasswordField) inputField)
111: .getPassword());
112: if (!(fieldValue.equals(currentValue) && fieldValue
113: .equals(originalValue))) {
114: // something has changed, so we'll have to deal with it.
115: try {
116: if (!currentValue.equals(originalValue)) {
117: firePropertyChangingEvent(originalValue);
118: firePropertyChangedEvent(originalValue);
119: currentValue = originalValue;
120: }
121: inputField.setText(originalValue);
122: } catch (PropertyValueVetoException pvve) {
123: manager.getFactory().showError(
124: inputField,
125: "Error changing value " + label.getText()
126: + " to " + originalValue + ": "
127: + pvve.getReason());
128: }
129: }
130: }
131:
132: // the list of characters to use for scrambling.
133: private static char[] scrambleChars = new char[] { 'A', 'a', 'B',
134: 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H',
135: 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N',
136: 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T',
137: 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z',
138: 'z' };
139:
140: /**
141: * This is a simple scrambler.
142: */
143: public static String scrambleString(String key) {
144: int[] salt = new int[4];
145: int keySize = key.length();
146: long seed = System.currentTimeMillis();
147:
148: salt[0] = (int) ((seed / 107) % 2704);
149: salt[1] = (int) ((seed / 19) % 2704);
150: salt[2] = (int) ((seed / 17) % 2704);
151: salt[3] = (int) ((seed / 91) % 2704);
152:
153: char[] scrambledString = new char[(keySize * 2) + 8];
154:
155: for (int i = 0; i < keySize; i++) {
156: int numValue = (int) (key.charAt(i));
157: numValue = (numValue + salt[i % 4]) % 2704;
158: scrambledString[i * 2] = scrambleChars[numValue / 52];
159: scrambledString[(i * 2) + 1] = scrambleChars[numValue % 52];
160: }
161:
162: for (int i = 0; i < 3; i++) {
163: int numValue = (salt[i] + salt[i + 1]) % 2704;
164: scrambledString[(keySize + i) * 2] = scrambleChars[numValue / 52];
165: scrambledString[((keySize + i) * 2) + 1] = scrambleChars[numValue % 52];
166: }
167:
168: scrambledString[(keySize + 3) * 2] = scrambleChars[salt[3] / 52];
169: scrambledString[((keySize + 3) * 2) + 1] = scrambleChars[salt[3] % 52];
170:
171: return new String(scrambledString);
172: }
173:
174: /**
175: * And this is a simple descrambler.
176: */
177: public static String descrambleString(String value) {
178: int[] salt = new int[4];
179: int scrambleSize = value.length();
180: char[] key = new char[(scrambleSize - 8) / 2];
181: salt[3] = (findCharValue(value.charAt(scrambleSize - 2)) * 52)
182: + findCharValue(value.charAt(scrambleSize - 1));
183:
184: for (int i = 2; i >= 0; i--) {
185: salt[i] = (2704
186: - salt[i + 1]
187: + (findCharValue(value.charAt(scrambleSize
188: - ((4 - i) * 2))) * 52) + findCharValue(value
189: .charAt(scrambleSize - ((4 - i) * 2) + 1))) % 2704;
190: }
191:
192: for (int i = 0; i < (scrambleSize - 8) / 2; i++) {
193: key[i] = (char) ((2704 - salt[i % 4]
194: + (findCharValue(value.charAt(i * 2)) * 52) + findCharValue(value
195: .charAt((i * 2) + 1))) % 2704);
196: }
197:
198: return new String(key);
199: }
200:
201: /**
202: * This very inefficiently finds a character value in the scrambleChars
203: * array.
204: */
205: private static int findCharValue(char a) {
206: for (int i = 0; i < scrambleChars.length; i++)
207: if (a == scrambleChars[i])
208: return i;
209:
210: return 0;
211: }
212:
213: }
|