001: /*
002: * BooleanPropertyEditor.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.EventQueue;
015: import java.awt.event.ItemEvent;
016: import java.awt.event.ItemListener;
017: import java.lang.reflect.Method;
018: import javax.swing.JCheckBox;
019: import workbench.interfaces.SimplePropertyEditor;
020: import workbench.log.LogMgr;
021:
022: /**
023: *
024: * @author support@sql-workbench.net
025: */
026: public class BooleanPropertyEditor extends JCheckBox implements
027: ItemListener, SimplePropertyEditor {
028: private Object source;
029: private Method setter;
030: private Method getter;
031: private boolean changed;
032: private String propName;
033: private boolean immediateUpdate = false;
034:
035: public BooleanPropertyEditor() {
036: super ();
037: }
038:
039: public void setSourceObject(Object aSource, String aProperty) {
040: this .source = aSource;
041: this .changed = false;
042: this .propName = aProperty;
043: this .removeItemListener(this );
044:
045: if (aSource == null) {
046: this .getter = null;
047: this .setter = null;
048: return;
049: }
050:
051: String propertyName = Character
052: .toUpperCase(aProperty.charAt(0))
053: + aProperty.substring(1);
054:
055: try {
056: String name = "get" + propertyName;
057: Class cls = aSource.getClass();
058: try {
059: this .getter = cls.getMethod(name, (Class[]) null);
060: } catch (NoSuchMethodException e) {
061: this .getter = null;
062: }
063: if (this .getter == null) {
064: name = "is" + propertyName;
065: this .getter = cls.getMethod(name, (Class[]) null);
066: }
067:
068: name = "set" + propertyName;
069: Class[] parms = { boolean.class };
070: this .setter = cls.getMethod(name, parms);
071:
072: Boolean value = (Boolean) this .getter.invoke(this .source,
073: (Object[]) null);
074: this .setSelected(value.booleanValue());
075: this .addItemListener(this );
076: } catch (Exception e) {
077: LogMgr.logError("BooleanPropertyEditor.setSourceObject()",
078: "Error during init", e);
079: }
080: }
081:
082: public void itemStateChanged(ItemEvent evt) {
083: this .changed = true;
084: if (this .immediateUpdate) {
085: this .applyChanges();
086: }
087: EventQueue.invokeLater(new Runnable() {
088: public void run() {
089: firePropertyChange(propName, null, Boolean
090: .valueOf(isSelected()));
091: }
092: });
093: }
094:
095: public void applyChanges() {
096: if (!this .changed)
097: return;
098: if (this .setter == null)
099: return;
100: Object args[] = new Object[1];
101: args[0] = Boolean.valueOf(this .isSelected());
102: try {
103: this .setter.invoke(this .source, args);
104: this .changed = false;
105: } catch (Exception e) {
106: e.printStackTrace();
107: }
108: }
109:
110: public boolean isChanged() {
111: return this .changed;
112: }
113:
114: public void setImmediateUpdate(boolean aFlag) {
115: this .immediateUpdate = aFlag;
116: }
117:
118: public boolean getImmediateUpdate() {
119: return this.immediateUpdate;
120: }
121:
122: }
|