001: /*
002: * IntegerPropertyEditor.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.JTextField;
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 IntegerPropertyEditor extends JTextField implements
030: DocumentListener, SimplePropertyEditor, FocusListener {
031: private Object source;
032: private Method setter;
033: private Method getter;
034: private boolean changed;
035: private boolean immediateUpdate = false;
036:
037: private String propName;
038:
039: public IntegerPropertyEditor() {
040: super ();
041: this .addFocusListener(this );
042: this .addMouseListener(new TextComponentMouseListener());
043: }
044:
045: public void setSourceObject(Object aSource, String aProperty) {
046: this .setSourceObject(aSource, aProperty, null);
047: }
048:
049: /**
050: * Return the value that the user entered
051: * as an int. If the user did not enter anything
052: * or not a number Integer.MIN_VALUE will be returned
053: */
054: public int getValue() {
055: try {
056: return Integer.parseInt(this .getText());
057: } catch (Exception e) {
058: return Integer.MIN_VALUE;
059: }
060: }
061:
062: public void setSourceObject(Object aSource, String aProperty,
063: String initialText) {
064: this .source = aSource;
065: this .changed = false;
066: this .propName = aProperty;
067:
068: this .getDocument().removeDocumentListener(this );
069:
070: if (this .source == null) {
071: this .setter = null;
072: this .getter = null;
073: this .setText("");
074: return;
075: }
076:
077: String propertyName = Character
078: .toUpperCase(aProperty.charAt(0))
079: + aProperty.substring(1);
080:
081: if (initialText != null) {
082: this .setText(initialText);
083: }
084:
085: try {
086: String name = "get" + propertyName;
087: Class cls = aSource.getClass();
088: this .getter = cls.getMethod(name, (Class[]) null);
089:
090: name = "set" + propertyName;
091: Class[] parms = { Integer.class };
092: this .setter = cls.getMethod(name, parms);
093:
094: Integer value = (Integer) this .getter.invoke(this .source,
095: (Object[]) null);
096: if (value == null)
097: this .setText("");
098: else
099: this .setText(value.toString());
100: } catch (Exception e) {
101: LogMgr.logError("IntegerPropertyEditor.setSourceObject()",
102: "Error during init", e);
103: }
104: this .getDocument().addDocumentListener(this );
105: }
106:
107: public void applyChanges() {
108: if (!this .changed)
109: return;
110: if (this .source == null)
111: return;
112: if (this .setter == null)
113: return;
114: Object args[] = new Object[1];
115: try {
116: args[0] = new Integer(this .getValue());
117: this .setter.invoke(this .source, args);
118: this .changed = false;
119: } catch (Exception e) {
120: LogMgr.logError("IntegerPropertyEditor.applyChanges()",
121: "Error setting value", e);
122: }
123: }
124:
125: public boolean isChanged() {
126: return this .changed;
127: }
128:
129: public void changedUpdate(DocumentEvent e) {
130: documentChanged();
131: }
132:
133: public void insertUpdate(DocumentEvent e) {
134: documentChanged();
135: }
136:
137: public void removeUpdate(DocumentEvent e) {
138: documentChanged();
139: }
140:
141: private void documentChanged() {
142: this .changed = true;
143: if (this .immediateUpdate) {
144: this .applyChanges();
145: }
146: firePropertyChange(this .propName, null, null);
147: }
148:
149: public void setImmediateUpdate(boolean aFlag) {
150: this .immediateUpdate = aFlag;
151: if (aFlag)
152: this .applyChanges();
153: }
154:
155: public boolean getImmediateUpdate() {
156: return this .immediateUpdate;
157: }
158:
159: /** Invoked when a component gains the keyboard focus.
160: *
161: */
162: public void focusGained(FocusEvent e) {
163: }
164:
165: /** Invoked when a component loses the keyboard focus.
166: *
167: */
168: public void focusLost(FocusEvent e) {
169: if (!this.immediateUpdate)
170: this.applyChanges();
171: }
172:
173: }
|