001: /*
002: * PasswordPropertyEditor.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.event.FocusEvent;
015: import java.awt.event.FocusListener;
016: import java.lang.reflect.Method;
017:
018: import javax.swing.JPasswordField;
019: import javax.swing.event.DocumentEvent;
020: import javax.swing.event.DocumentListener;
021:
022: import workbench.interfaces.SimplePropertyEditor;
023: import workbench.log.LogMgr;
024:
025: /**
026: *
027: * @author support@sql-workbench.net
028: */
029: public class PasswordPropertyEditor extends JPasswordField implements
030: DocumentListener, SimplePropertyEditor, FocusListener {
031: private Object source;
032: private Method setter;
033: private Method getter;
034: private boolean changed;
035: private String propName;
036: private boolean immediateUpdate = false;
037:
038: public PasswordPropertyEditor() {
039: super ();
040: this .addFocusListener(this );
041: }
042:
043: public void setSourceObject(Object aSource, String aProperty) {
044: this .source = aSource;
045: this .changed = false;
046: this .propName = aProperty;
047: //String propertyName = Character.toUpperCase(aProperty.charAt(0)) + aProperty.substring(1);
048:
049: this .getDocument().removeDocumentListener(this );
050:
051: try {
052: String name = "getInputPassword";
053: Class cls = aSource.getClass();
054: this .getter = cls.getMethod(name, (Class[]) null);
055:
056: name = "setPassword";
057: Class[] parms = { String.class };
058: this .setter = cls.getMethod(name, parms);
059:
060: String value = (String) this .getter.invoke(this .source,
061: (Object[]) null);
062: this .setText(value);
063: } catch (Exception e) {
064: LogMgr.logError("PasswordPropertyEditor.setSourceObject()",
065: "Error during init", e);
066: }
067: this .getDocument().addDocumentListener(this );
068: }
069:
070: public void applyChanges() {
071: if (!this .changed)
072: return;
073: if (this .source == null)
074: return;
075: if (this .setter == null)
076: return;
077: Object args[] = new Object[1];
078: args[0] = new String(this .getPassword());
079: try {
080: this .setter.invoke(this .source, args);
081: this .changed = false;
082: } catch (Exception e) {
083: e.printStackTrace();
084: }
085: }
086:
087: public boolean isChanged() {
088: return this .changed;
089: }
090:
091: public void changedUpdate(DocumentEvent e) {
092: this .changed = true;
093: if (this .immediateUpdate) {
094: this .applyChanges();
095: }
096: firePropertyChange(this .propName, null, null);
097: }
098:
099: public void insertUpdate(DocumentEvent e) {
100: this .changed = true;
101: if (this .immediateUpdate) {
102: this .applyChanges();
103: }
104: firePropertyChange(this .propName, null, null);
105: }
106:
107: public void removeUpdate(DocumentEvent e) {
108: this .changed = true;
109: if (this .immediateUpdate) {
110: this .applyChanges();
111: }
112: firePropertyChange(this .propName, null, null);
113: }
114:
115: public void setImmediateUpdate(boolean aFlag) {
116: this .immediateUpdate = aFlag;
117: if (aFlag)
118: this .applyChanges();
119: }
120:
121: public boolean getImmediateUpdate() {
122: return this .immediateUpdate;
123: }
124:
125: /** Invoked when a component gains the keyboard focus.
126: *
127: */
128: public void focusGained(FocusEvent e) {
129: }
130:
131: /** Invoked when a component loses the keyboard focus.
132: *
133: */
134: public void focusLost(FocusEvent e) {
135: if (!this.immediateUpdate)
136: this.applyChanges();
137: }
138:
139: }
|