001: /*
002: * ComboStringPropertyEditor.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.ActionEvent;
015: import java.awt.event.ActionListener;
016: import java.awt.event.FocusEvent;
017: import java.awt.event.FocusListener;
018: import java.awt.event.ItemEvent;
019: import java.awt.event.ItemListener;
020: import java.lang.reflect.Method;
021:
022: import javax.swing.ComboBoxModel;
023: import javax.swing.JComboBox;
024: import javax.swing.JTextField;
025: import javax.swing.event.DocumentEvent;
026: import javax.swing.event.DocumentListener;
027:
028: import workbench.interfaces.SimplePropertyEditor;
029: import workbench.log.LogMgr;
030:
031: /**
032: * A property editor for a String property, where the values
033: * for the field can also be selected by a dropdown
034: *
035: * @author support@sql-workbench.net
036: */
037: public class ComboStringPropertyEditor extends JComboBox implements
038: ItemListener, SimplePropertyEditor, FocusListener,
039: DocumentListener {
040: private Object source;
041: private Method setter;
042: private Method getter;
043: protected boolean changed;
044: private boolean immediateUpdate = false;
045: private ActionListener listener;
046:
047: public ComboStringPropertyEditor() {
048: super ();
049: }
050:
051: public void setSourceObject(Object aSource, String aProperty) {
052: this .source = aSource;
053: this .changed = false;
054: String propertyName = Character
055: .toUpperCase(aProperty.charAt(0))
056: + aProperty.substring(1);
057: stopEvents();
058:
059: try {
060: String name = "get" + propertyName;
061: Class cls = aSource.getClass();
062: this .getter = cls.getMethod(name, (Class[]) null);
063:
064: name = "set" + propertyName;
065: Class[] parms = { String.class };
066:
067: this .setter = cls.getMethod(name, parms);
068: //this.setEditable(true);
069: if (this .getModel() != null) {
070: this .initData();
071: }
072: } catch (Exception e) {
073: System.out.println("Error on init");
074: e.printStackTrace();
075: }
076: startEvents();
077: }
078:
079: private ActionListener getListener() {
080: if (listener == null) {
081: listener = new ActionListener() {
082: public void actionPerformed(ActionEvent evt) {
083: changed = true;
084: applyChanges();
085: }
086: };
087: }
088: return listener;
089: }
090:
091: private void stopEvents() {
092: this .removeItemListener(this );
093: if (this .isEditable()) {
094: removeActionListener(getListener());
095: JTextField text = (JTextField) getEditor()
096: .getEditorComponent();
097: //text.removeFocusListener(this);
098: text.getDocument().removeDocumentListener(this );
099: }
100: }
101:
102: private void startEvents() {
103: this .addItemListener(this );
104: if (this .isEditable()) {
105: addActionListener(getListener());
106: JTextField text = (JTextField) getEditor()
107: .getEditorComponent();
108: //text.addFocusListener(this);
109: text.getDocument().addDocumentListener(this );
110: }
111: }
112:
113: public void setModel(ComboBoxModel m) {
114: stopEvents();
115: super .setModel(m);
116: if (this .isEditable()) {
117: this .initData();
118: }
119: startEvents();
120: }
121:
122: private void initData() {
123: if (this .getter == null || this .source == null)
124: return;
125: try {
126:
127: Object value = this .getter.invoke(this .source,
128: (Object[]) null);
129: this .setSelectedItem(value);
130: } catch (Exception e) {
131: LogMgr.logError("ComboProperty.intiData", "Error", e);
132: }
133: }
134:
135: public boolean isChanged() {
136: return this .changed;
137: }
138:
139: public void applyChanges() {
140: if (!this .changed)
141: return;
142: Object args[] = new Object[1];
143: if (this .isEditable()) {
144: args[0] = this .getEditor().getItem().toString();
145: } else {
146: args[0] = this .getSelectedItem().toString();
147: }
148: try {
149: this .setter.invoke(this .source, args);
150: } catch (Exception e) {
151: e.printStackTrace();
152: }
153: }
154:
155: /** Invoked when an item has been selected or deselected by the user.
156: * The code written for this method performs the operations
157: * that need to occur when an item is selected (or deselected).
158: *
159: */
160: public void itemStateChanged(ItemEvent e) {
161: if (e.getStateChange() == ItemEvent.SELECTED) {
162: this .changed = true;
163: }
164: if (this .immediateUpdate) {
165: this .applyChanges();
166: }
167: }
168:
169: public void setImmediateUpdate(boolean aFlag) {
170: this .immediateUpdate = aFlag;
171: if (aFlag)
172: this .applyChanges();
173: }
174:
175: public boolean getImmediateUpdate() {
176: return this .immediateUpdate;
177: }
178:
179: public void focusGained(FocusEvent e) {
180: }
181:
182: public void focusLost(FocusEvent e) {
183: this .applyChanges();
184: }
185:
186: public void changedUpdate(DocumentEvent e) {
187: documentChanged();
188: }
189:
190: public void insertUpdate(DocumentEvent e) {
191: documentChanged();
192: }
193:
194: public void removeUpdate(DocumentEvent e) {
195: documentChanged();
196: }
197:
198: private void documentChanged() {
199: this .changed = true;
200: this.applyChanges();
201: }
202: }
|