001: /*
002: * NumberPropertyEditor.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.event.DocumentEvent;
019: import javax.swing.event.DocumentListener;
020: import javax.swing.text.Document;
021:
022: import workbench.interfaces.SimplePropertyEditor;
023: import workbench.log.LogMgr;
024:
025: /**
026: *
027: * @author support@sql-workbench.net
028: */
029: public class NumberPropertyEditor extends NumberField implements
030: DocumentListener, SimplePropertyEditor, FocusListener {
031: private Object source;
032: private Method setter;
033: private Method getter;
034: private String propName;
035: private boolean changed;
036: private boolean immediateUpdate = false;
037:
038: public NumberPropertyEditor() {
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
048: .toUpperCase(aProperty.charAt(0))
049: + aProperty.substring(1);
050: Document doc = this .getDocument();
051: if (doc != null)
052: doc.removeDocumentListener(this );
053: try {
054: String name = "get" + propertyName;
055: Class cls = aSource.getClass();
056: this .getter = cls.getMethod(name, (Class[]) null);
057:
058: name = "set" + propertyName;
059: Class[] parms = { int.class };
060: this .setter = cls.getMethod(name, parms);
061:
062: String value = this .getter.invoke(this .source,
063: (Object[]) null).toString();
064: super .setText(value);
065: } catch (Exception e) {
066: LogMgr.logError("NumberPropertyEditor.setSourceObject()",
067: "Error during init", e);
068: }
069: doc = this .getDocument();
070: if (doc != null)
071: doc.addDocumentListener(this );
072: }
073:
074: public void applyChanges() {
075: if (this .setter == null)
076: return;
077: if (!this .changed)
078: return;
079:
080: Object args[] = new Object[1];
081: args[0] = this .getText();
082: try {
083: this .setter.invoke(this .source, args);
084: this .changed = true;
085: } catch (Exception e) {
086: e.printStackTrace();
087: }
088: }
089:
090: public void changedUpdate(DocumentEvent e) {
091: this .changed = true;
092: if (this .immediateUpdate) {
093: this .applyChanges();
094: }
095: firePropertyChange(this .propName, null, null);
096: }
097:
098: public void insertUpdate(DocumentEvent e) {
099: this .changed = true;
100: if (this .immediateUpdate) {
101: this .applyChanges();
102: }
103: firePropertyChange(this .propName, null, null);
104: }
105:
106: public void removeUpdate(DocumentEvent e) {
107: this .changed = true;
108: if (this .immediateUpdate) {
109: this .applyChanges();
110: }
111: this .changed = true;
112: if (this .immediateUpdate) {
113: this .applyChanges();
114: }
115: firePropertyChange(this .propName, null, null);
116: }
117:
118: public boolean isChanged() {
119: return this .changed;
120: }
121:
122: public void setImmediateUpdate(boolean aFlag) {
123: this .immediateUpdate = aFlag;
124: if (aFlag)
125: this .applyChanges();
126: }
127:
128: public boolean getImmediateUpdate() {
129: return this .immediateUpdate;
130: }
131:
132: /** Invoked when a component gains the keyboard focus.
133: *
134: */
135: public void focusGained(FocusEvent e) {
136: this .selectAll();
137: }
138:
139: /** Invoked when a component loses the keyboard focus.
140: *
141: */
142: public void focusLost(FocusEvent e) {
143: if (!this.immediateUpdate)
144: this.applyChanges();
145: }
146: }
|