GNU Classpath (0.18) | ||
Frames | No Frames |
1: /* DefaultCellEditor.java -- 2: Copyright (C) 2002, 2004 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package javax.swing; 40: 41: import java.awt.Component; 42: import java.awt.event.ActionEvent; 43: import java.awt.event.ActionListener; 44: import java.awt.event.ItemEvent; 45: import java.awt.event.ItemListener; 46: import java.awt.event.MouseEvent; 47: import java.io.Serializable; 48: import java.util.EventObject; 49: 50: import javax.swing.JTable; 51: import javax.swing.JTextField; 52: import javax.swing.event.CellEditorListener; 53: import javax.swing.table.TableCellEditor; 54: import javax.swing.tree.TreeCellEditor; 55: 56: /** 57: * The default implementation of {@link TableCellEditor} and 58: * {@link TreeCellEditor}. It provides editor components for 59: * some standard object types. 60: * 61: * @author Andrew Selkirk 62: * 63: * @status mostly unimplemented 64: */ 65: public class DefaultCellEditor 66: extends AbstractCellEditor 67: implements TableCellEditor, TreeCellEditor 68: { 69: private static final long serialVersionUID = 3564035141373880027L; 70: 71: /** 72: * Delegates a couple of method calls (such as {@link #isCellEditable) 73: * to the component it contains and listens for events that indicate 74: * that editing has stopped. 75: */ 76: protected class EditorDelegate 77: implements ActionListener, ItemListener, Serializable 78: { 79: private static final long serialVersionUID = -1420007406015481933L; 80: 81: /** 82: * value 83: */ 84: protected Object value; 85: 86: /** 87: * Constructor EditorDelegate 88: */ 89: protected EditorDelegate() 90: { 91: } 92: 93: /** 94: * setValue 95: * 96: * @param event TODO 97: */ 98: public void setValue(Object value) 99: { 100: // TODO: should be setting the value in the editorComp 101: this.value = value; 102: } 103: 104: /** 105: * getCellEditorValue 106: * 107: * @returns Object 108: */ 109: public Object getCellEditorValue() 110: { 111: // TODO: should be getting the updated value from the editorComp 112: return value; 113: } // getCellEditorValue() 114: 115: /** 116: * isCellEditable 117: * 118: * @param event TODO 119: * 120: * @returns boolean 121: */ 122: public boolean isCellEditable(EventObject event) 123: { 124: if (event == null || !(event instanceof MouseEvent) || 125: (((MouseEvent) event).getClickCount() >= getClickCountToStart())) 126: return true; 127: return false; 128: } // isCellEditable() 129: 130: /** 131: * shouldSelectCell 132: * 133: * @param event TODO 134: * 135: * @returns boolean 136: */ 137: public boolean shouldSelectCell(EventObject event) 138: { 139: // return true to indicate that the editing cell may be selected 140: return true; 141: } // shouldSelectCell() 142: 143: /** 144: * stopCellEditing 145: * 146: * @returns boolean 147: */ 148: public boolean stopCellEditing() 149: { 150: fireEditingStopped(); 151: return true; 152: } // stopCellEditing() 153: 154: /** 155: * cancelCellEditing 156: */ 157: public void cancelCellEditing() 158: { 159: fireEditingCanceled(); 160: } // cancelCellEditing() 161: 162: /** 163: * startCellEditing 164: * 165: * @param event TODO 166: * 167: * @returns boolean 168: */ 169: public boolean startCellEditing(EventObject event) 170: { 171: // return true to indicate that editing has begun 172: return true; 173: } // startCellEditing() 174: 175: /** 176: * actionPerformed 177: * 178: * @param event TODO 179: */ 180: public void actionPerformed(ActionEvent event) 181: { 182: stopCellEditing(); 183: } // actionPerformed() 184: 185: /** 186: * itemStateChanged 187: * 188: * @param event TODO 189: */ 190: public void itemStateChanged(ItemEvent event) 191: { 192: stopCellEditing(); 193: } // itemStateChanged() 194: 195: void fireEditingStopped() 196: { 197: CellEditorListener[] listeners = getCellEditorListeners(); 198: for (int index = 0; index < listeners.length; index++) 199: listeners[index].editingStopped(changeEvent); 200: 201: } 202: 203: void fireEditingCanceled() 204: { 205: CellEditorListener[] listeners = getCellEditorListeners(); 206: for (int index = 0; index < listeners.length; index++) 207: listeners[index].editingCanceled(changeEvent); 208: } 209: } // EditorDelegate 210: 211: /** 212: * editorComponent 213: */ 214: protected JComponent editorComponent; 215: 216: /** 217: * delegate 218: */ 219: protected EditorDelegate delegate; 220: 221: /** 222: * clickCountToStart 223: */ 224: protected int clickCountToStart; 225: 226: /** 227: * Constructor DefaultCellEditor 228: * 229: * @param textfield TODO 230: */ 231: public DefaultCellEditor(JTextField textfield) 232: { 233: editorComponent = textfield; 234: clickCountToStart = 3; 235: } // DefaultCellEditor() 236: 237: /** 238: * Constructor DefaultCellEditor 239: * 240: * @param checkbox TODO 241: */ 242: public DefaultCellEditor(JCheckBox checkbox) 243: { 244: editorComponent = checkbox; 245: clickCountToStart = 1; 246: } // DefaultCellEditor() 247: 248: /** 249: * Constructor DefaultCellEditor 250: * 251: * @param combobox TODO 252: */ 253: public DefaultCellEditor(JComboBox combobox) 254: { 255: editorComponent = combobox; 256: clickCountToStart = 1; 257: } // DefaultCellEditor() 258: 259: /** 260: * getComponent 261: * 262: * @returns Component 263: */ 264: public Component getComponent() 265: { 266: return editorComponent; 267: } // getComponent() 268: 269: /** 270: * getClickCountToStart 271: * 272: * @returns int 273: */ 274: public int getClickCountToStart() 275: { 276: return clickCountToStart; 277: } // getClickCountToStart() 278: 279: /** 280: * setClickCountToStart 281: * 282: * @param count TODO 283: */ 284: public void setClickCountToStart(int count) 285: { 286: clickCountToStart = count; 287: } // setClickCountToStart() 288: 289: /** 290: * getCellEditorValue 291: * 292: * @returns Object 293: */ 294: public Object getCellEditorValue() 295: { 296: return delegate.getCellEditorValue(); 297: } // getCellEditorValue() 298: 299: /** 300: * isCellEditable 301: * 302: * @param event TODO 303: * 304: * @returns boolean 305: */ 306: public boolean isCellEditable(EventObject event) 307: { 308: return delegate.isCellEditable(event); 309: } // isCellEditable() 310: 311: /** 312: * shouldSelectCell 313: * 314: * @param event TODO 315: * 316: * @returns boolean 317: */ 318: public boolean shouldSelectCell(EventObject event) 319: { 320: return delegate.shouldSelectCell(event); 321: } // shouldSelectCell() 322: 323: /** 324: * stopCellEditing 325: * 326: * @returns boolean 327: */ 328: public boolean stopCellEditing() 329: { 330: return delegate.stopCellEditing(); 331: } // stopCellEditing() 332: 333: /** 334: * cancelCellEditing 335: */ 336: public void cancelCellEditing() 337: { 338: delegate.cancelCellEditing(); 339: } // cancelCellEditing() 340: 341: /** 342: * Sets an initial value for the editor. 343: * This will cause the editor to stopEditing and lose any partially 344: * edited value if the editor is editing when this method is called. 345: * Returns the component that should be added to the client's Component 346: * hierarchy. Once installed in the client's hierarchy this component will 347: * then be able to draw and receive user input. 348: * 349: * @param tree - the JTree that is asking the editor to edit; this 350: * parameter can be null 351: * @param value - the value of the cell to be edited 352: * @param isSelected - true is the cell is to be renderer with selection 353: * highlighting 354: * @param expanded - true if the node is expanded 355: * @param leaf - true if the node is a leaf node 356: * @param row - the row index of the node being edited 357: * 358: * @returns Component the component for editing 359: */ 360: public Component getTreeCellEditorComponent(JTree tree, Object value, 361: boolean isSelected, 362: boolean expanded, boolean leaf, 363: int row) 364: { 365: if (editorComponent instanceof JTextField) 366: { 367: ((JTextField)editorComponent).setText(value.toString()); 368: delegate = new EditorDelegate(); 369: ((JTextField)editorComponent).addActionListener(delegate); 370: } 371: else if (editorComponent instanceof JCheckBox) 372: { 373: ((JCheckBox)editorComponent).setText(value.toString()); 374: delegate = new EditorDelegate(); 375: ((JCheckBox)editorComponent).addActionListener(delegate); 376: } 377: else if (editorComponent instanceof JComboBox) 378: { 379: ((JComboBox)editorComponent).setSelectedItem(value.toString()); 380: delegate = new EditorDelegate(); 381: ((JComboBox)editorComponent).addActionListener(delegate); 382: } 383: 384: return editorComponent; 385: } // getTreeCellEditorComponent() 386: 387: /** 388: * getTableCellEditorComponent 389: * 390: * @param tree TODO 391: * @param value TODO 392: * @param isSelected TODO 393: * @param row TODO 394: * @param column TODO 395: * 396: * @returns Component 397: */ 398: public Component getTableCellEditorComponent(JTable table, Object value, 399: boolean isSelected, int row, 400: int column) 401: { 402: // NOTE: as specified by Sun, we don't call new() everytime, we return 403: // editorComponent on each call to getTableCellEditorComponent or 404: // getTreeCellEditorComponent. However, currently JTextFields have a 405: // problem with getting rid of old text, so without calling new() there 406: // are some strange results. If you edit more than one cell in the table 407: // text from previously edited cells may unexpectedly show up in the 408: // cell you are currently editing. This will be fixed automatically 409: // when JTextField is fixed. 410: if (editorComponent instanceof JTextField) 411: { 412: ((JTextField)editorComponent).setText(value.toString()); 413: delegate = new EditorDelegate(); 414: ((JTextField)editorComponent).addActionListener(delegate); 415: } 416: else 417: { 418: // TODO 419: } 420: return editorComponent; 421: } // getTableCellEditorComponent() 422: 423: 424: }
GNU Classpath (0.18) |