GNU Classpath (0.20) | |
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: // Nothing to do here. 92: } 93: 94: /** 95: * setValue 96: * 97: * @param value TODO 98: */ 99: public void setValue(Object value) 100: { 101: // TODO: should be setting the value in the editorComp 102: this.value = value; 103: } 104: 105: /** 106: * getCellEditorValue 107: * 108: * @returns Object 109: */ 110: public Object getCellEditorValue() 111: { 112: // TODO: should be getting the updated value from the editorComp 113: return value; 114: } // getCellEditorValue() 115: 116: /** 117: * isCellEditable 118: * 119: * @param event TODO 120: * 121: * @returns boolean 122: */ 123: public boolean isCellEditable(EventObject event) 124: { 125: if (event == null || !(event instanceof MouseEvent) || 126: (((MouseEvent) event).getClickCount() >= getClickCountToStart())) 127: return true; 128: return false; 129: } // isCellEditable() 130: 131: /** 132: * shouldSelectCell 133: * 134: * @param event TODO 135: * 136: * @returns boolean 137: */ 138: public boolean shouldSelectCell(EventObject event) 139: { 140: // return true to indicate that the editing cell may be selected 141: return true; 142: } // shouldSelectCell() 143: 144: /** 145: * stopCellEditing 146: * 147: * @returns boolean 148: */ 149: public boolean stopCellEditing() 150: { 151: fireEditingStopped(); 152: return true; 153: } // stopCellEditing() 154: 155: /** 156: * cancelCellEditing 157: */ 158: public void cancelCellEditing() 159: { 160: fireEditingCanceled(); 161: } // cancelCellEditing() 162: 163: /** 164: * startCellEditing 165: * 166: * @param event TODO 167: * 168: * @returns boolean 169: */ 170: public boolean startCellEditing(EventObject event) 171: { 172: // return true to indicate that editing has begun 173: return true; 174: } // startCellEditing() 175: 176: /** 177: * actionPerformed 178: * 179: * @param event TODO 180: */ 181: public void actionPerformed(ActionEvent event) 182: { 183: stopCellEditing(); 184: } // actionPerformed() 185: 186: /** 187: * itemStateChanged 188: * 189: * @param event TODO 190: */ 191: public void itemStateChanged(ItemEvent event) 192: { 193: stopCellEditing(); 194: } // itemStateChanged() 195: 196: void fireEditingStopped() 197: { 198: CellEditorListener[] listeners = getCellEditorListeners(); 199: for (int index = 0; index < listeners.length; index++) 200: listeners[index].editingStopped(changeEvent); 201: 202: } 203: 204: void fireEditingCanceled() 205: { 206: CellEditorListener[] listeners = getCellEditorListeners(); 207: for (int index = 0; index < listeners.length; index++) 208: listeners[index].editingCanceled(changeEvent); 209: } 210: } // EditorDelegate 211: 212: /** 213: * editorComponent 214: */ 215: protected JComponent editorComponent; 216: 217: /** 218: * delegate 219: */ 220: protected EditorDelegate delegate; 221: 222: /** 223: * clickCountToStart 224: */ 225: protected int clickCountToStart; 226: 227: /** 228: * Constructor DefaultCellEditor 229: * 230: * @param textfield TODO 231: */ 232: public DefaultCellEditor(JTextField textfield) 233: { 234: editorComponent = textfield; 235: clickCountToStart = 3; 236: } // DefaultCellEditor() 237: 238: /** 239: * Constructor DefaultCellEditor 240: * 241: * @param checkbox TODO 242: */ 243: public DefaultCellEditor(JCheckBox checkbox) 244: { 245: editorComponent = checkbox; 246: clickCountToStart = 1; 247: } // DefaultCellEditor() 248: 249: /** 250: * Constructor DefaultCellEditor 251: * 252: * @param combobox TODO 253: */ 254: public DefaultCellEditor(JComboBox combobox) 255: { 256: editorComponent = combobox; 257: clickCountToStart = 1; 258: } // DefaultCellEditor() 259: 260: /** 261: * getComponent 262: * 263: * @returns Component 264: */ 265: public Component getComponent() 266: { 267: return editorComponent; 268: } // getComponent() 269: 270: /** 271: * getClickCountToStart 272: * 273: * @returns int 274: */ 275: public int getClickCountToStart() 276: { 277: return clickCountToStart; 278: } // getClickCountToStart() 279: 280: /** 281: * setClickCountToStart 282: * 283: * @param count TODO 284: */ 285: public void setClickCountToStart(int count) 286: { 287: clickCountToStart = count; 288: } // setClickCountToStart() 289: 290: /** 291: * getCellEditorValue 292: * 293: * @returns Object 294: */ 295: public Object getCellEditorValue() 296: { 297: return delegate.getCellEditorValue(); 298: } // getCellEditorValue() 299: 300: /** 301: * isCellEditable 302: * 303: * @param event TODO 304: * 305: * @returns boolean 306: */ 307: public boolean isCellEditable(EventObject event) 308: { 309: return delegate.isCellEditable(event); 310: } // isCellEditable() 311: 312: /** 313: * shouldSelectCell 314: * 315: * @param event TODO 316: * 317: * @returns boolean 318: */ 319: public boolean shouldSelectCell(EventObject event) 320: { 321: return delegate.shouldSelectCell(event); 322: } // shouldSelectCell() 323: 324: /** 325: * stopCellEditing 326: * 327: * @returns boolean 328: */ 329: public boolean stopCellEditing() 330: { 331: return delegate.stopCellEditing(); 332: } // stopCellEditing() 333: 334: /** 335: * cancelCellEditing 336: */ 337: public void cancelCellEditing() 338: { 339: delegate.cancelCellEditing(); 340: } // cancelCellEditing() 341: 342: /** 343: * Sets an initial value for the editor. 344: * This will cause the editor to stopEditing and lose any partially 345: * edited value if the editor is editing when this method is called. 346: * Returns the component that should be added to the client's Component 347: * hierarchy. Once installed in the client's hierarchy this component will 348: * then be able to draw and receive user input. 349: * 350: * @param tree - the JTree that is asking the editor to edit; this 351: * parameter can be null 352: * @param value - the value of the cell to be edited 353: * @param isSelected - true is the cell is to be renderer with selection 354: * highlighting 355: * @param expanded - true if the node is expanded 356: * @param leaf - true if the node is a leaf node 357: * @param row - the row index of the node being edited 358: * 359: * @returns Component the component for editing 360: */ 361: public Component getTreeCellEditorComponent(JTree tree, Object value, 362: boolean isSelected, 363: boolean expanded, boolean leaf, 364: int row) 365: { 366: if (editorComponent instanceof JTextField) 367: { 368: ((JTextField)editorComponent).setText(value.toString()); 369: delegate = new EditorDelegate(); 370: ((JTextField)editorComponent).addActionListener(delegate); 371: } 372: else if (editorComponent instanceof JCheckBox) 373: { 374: ((JCheckBox)editorComponent).setText(value.toString()); 375: delegate = new EditorDelegate(); 376: ((JCheckBox)editorComponent).addActionListener(delegate); 377: } 378: else if (editorComponent instanceof JComboBox) 379: { 380: ((JComboBox)editorComponent).setSelectedItem(value.toString()); 381: delegate = new EditorDelegate(); 382: ((JComboBox)editorComponent).addActionListener(delegate); 383: } 384: 385: return editorComponent; 386: } // getTreeCellEditorComponent() 387: 388: /** 389: * getTableCellEditorComponent 390: * 391: * @param table TODO 392: * @param value TODO 393: * @param isSelected TODO 394: * @param row TODO 395: * @param column TODO 396: * 397: * @returns Component 398: */ 399: public Component getTableCellEditorComponent(JTable table, Object value, 400: boolean isSelected, int row, 401: int column) 402: { 403: // NOTE: as specified by Sun, we don't call new() everytime, we return 404: // editorComponent on each call to getTableCellEditorComponent or 405: // getTreeCellEditorComponent. However, currently JTextFields have a 406: // problem with getting rid of old text, so without calling new() there 407: // are some strange results. If you edit more than one cell in the table 408: // text from previously edited cells may unexpectedly show up in the 409: // cell you are currently editing. This will be fixed automatically 410: // when JTextField is fixed. 411: if (editorComponent instanceof JTextField) 412: { 413: ((JTextField)editorComponent).setText(value.toString()); 414: delegate = new EditorDelegate(); 415: ((JTextField)editorComponent).addActionListener(delegate); 416: } 417: else 418: { 419: // TODO 420: } 421: return editorComponent; 422: } // getTableCellEditorComponent() 423: 424: 425: }
GNU Classpath (0.20) |