01: package net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent;
02:
03: import javax.swing.DefaultCellEditor;
04: import javax.swing.JTextField;
05:
06: /**
07: * @author gwg
08: *
09: * Class that extends DefaultCellEditor by replacing the
10: * default approach to creating the editable text with
11: * using the renderer from the appropriate DataType class.
12: */
13: public class CellEditorUsingRenderer extends DefaultCellEditor {
14:
15: // place to save reference to DataType object needed
16: // for calling the appropriate renderer on the object value
17: private IDataTypeComponent _dataTypeObject;
18:
19: /**
20: * Constructs a <code>DefaultCellEditor</code> that uses a
21: * restorable text field and fills in the value using the same
22: * renderer as is used to fill in the non-editable cell.
23: *
24: * @param x a <code>JTextField</code> object
25: */
26: public CellEditorUsingRenderer(final JTextField textField,
27: IDataTypeComponent dataTypeObject) {
28:
29: super (textField);
30:
31: // save pointer to object needed to render value for use
32: // in the inner class delegate
33: _dataTypeObject = dataTypeObject;
34:
35: editorComponent = textField;
36: this .clickCountToStart = 2;
37: delegate = new EditorDelegate() {
38: public void setValue(Object value) {
39:
40: // If the cell is editable, we really must have a valid
41: // DataType object, so the 'else' clause should never
42: // be used. It is just there as defensive programming.
43: if (CellEditorUsingRenderer.this ._dataTypeObject != null)
44: textField.setText(_dataTypeObject
45: .renderObject(value));
46: else
47: textField.setText((value != null) ? value
48: .toString() : "<null>");
49: }
50:
51: public Object getCellEditorValue() {
52: return textField.getText();
53: }
54: };
55: textField.addActionListener(delegate);
56: }
57:
58: }
|