001: package abbot.editor.editors;
002:
003: import java.awt.Component;
004: import java.awt.event.*;
005: import java.util.EventObject;
006:
007: import javax.swing.*;
008: import javax.swing.table.TableCellEditor;
009: import javax.swing.tree.TreeCellEditor;
010:
011: import abbot.script.XMLifiable;
012:
013: /**
014: * An editor for an XMLifiable object.
015: * It'd be nice to provide a real XML editor here...
016: */
017:
018: public class XMLEditor extends AbstractCellEditor implements
019: TableCellEditor, TreeCellEditor {
020:
021: private JTextField textField = new JTextField();
022: protected JComponent editorComponent = textField;
023: protected EditorDelegate delegate;
024: protected int clickCountToStart = 1;
025:
026: /**
027: * Constructs an XMLEditor that uses a text field.
028: */
029: public XMLEditor() {
030: this .clickCountToStart = 2;
031: delegate = new EditorDelegate() {
032: /** Set the contents of the editor based on the original
033: object. */
034: public void setValue(Object value) {
035: if (value instanceof XMLifiable) {
036: value = ((XMLifiable) value).toEditableString();
037: }
038: textField.setText((value != null) ? value.toString()
039: : "");
040: }
041:
042: public Object getCellEditorValue() {
043: return textField.getText();
044: }
045: };
046: textField.addActionListener(delegate);
047: }
048:
049: /**
050: * Returns the component used to edit this editor's value.
051: *
052: * @return the editor Component
053: */
054: public Component getComponent() {
055: return editorComponent;
056: }
057:
058: /**
059: * Specifies the number of clicks needed to start editing.
060: *
061: * @param count an int specifying the number of clicks needed to start
062: * editing
063: * @see #getClickCountToStart
064: */
065: public void setClickCountToStart(int count) {
066: clickCountToStart = count;
067: }
068:
069: /**
070: * ClickCountToStart controls the number of clicks required to start
071: * editing.
072: */
073: public int getClickCountToStart() {
074: return clickCountToStart;
075: }
076:
077: public Object getCellEditorValue() {
078: return delegate.getCellEditorValue();
079: }
080:
081: public boolean isCellEditable(EventObject anEvent) {
082: return delegate.isCellEditable(anEvent);
083: }
084:
085: public boolean shouldSelectCell(EventObject anEvent) {
086: return delegate.shouldSelectCell(anEvent);
087: }
088:
089: public boolean stopCellEditing() {
090: return delegate.stopCellEditing();
091: }
092:
093: public void cancelCellEditing() {
094: delegate.cancelCellEditing();
095: }
096:
097: //
098: // Implementing the TreeCellEditor Interface
099: //
100:
101: public Component getTreeCellEditorComponent(JTree tree,
102: Object value, boolean isSelected, boolean expanded,
103: boolean leaf, int row) {
104: String stringValue = tree.convertValueToText(value, isSelected,
105: expanded, leaf, row, false);
106:
107: delegate.setValue(stringValue);
108: return editorComponent;
109: }
110:
111: //
112: // Implementing the CellEditor Interface
113: //
114:
115: public Component getTableCellEditorComponent(JTable table,
116: Object value, boolean isSelected, int row, int column) {
117: delegate.setValue(value);
118: return editorComponent;
119: }
120:
121: //
122: // Protected EditorDelegate class
123: //
124:
125: protected class EditorDelegate implements ActionListener,
126: ItemListener {
127:
128: protected Object value;
129:
130: public Object getCellEditorValue() {
131: return value;
132: }
133:
134: public void setValue(Object value) {
135: this .value = value;
136: }
137:
138: public boolean isCellEditable(EventObject anEvent) {
139: if (anEvent instanceof MouseEvent) {
140: return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
141: }
142: return true;
143: }
144:
145: public boolean shouldSelectCell(EventObject anEvent) {
146: return true;
147: }
148:
149: public boolean startCellEditing(EventObject anEvent) {
150: return true;
151: }
152:
153: public boolean stopCellEditing() {
154: fireEditingStopped();
155: return true;
156: }
157:
158: public void cancelCellEditing() {
159: fireEditingCanceled();
160: }
161:
162: public void actionPerformed(ActionEvent e) {
163: XMLEditor.this .stopCellEditing();
164: }
165:
166: public void itemStateChanged(ItemEvent e) {
167: XMLEditor.this.stopCellEditing();
168: }
169: }
170: }
|