GNU Classpath (0.91) | |
Frames | No Frames |
1: /* DefaultCellEditor.java -- 2: Copyright (C) 2002, 2004, 2006, 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: * @author Audrius Meskauskas 63: */ 64: public class DefaultCellEditor 65: extends AbstractCellEditor 66: implements TableCellEditor, TreeCellEditor 67: { 68: private static final long serialVersionUID = 3564035141373880027L; 69: 70: /** 71: * This changeable module access the editor component in the component 72: * specific way. For instance, to set the value for JTextField, we need to 73: * call setText(String), and for JCheckBox we need to call 74: * setSelected(boolean). Each default editor has the component specific 75: * derivative of this class. These derivatives are private inner classes of 76: * the DefaultCellEditor. 77: * 78: * The editor delegate is also set for the editor component as the action 79: * listener. It listens for the events that indicate that editing has stopped. 80: */ 81: protected class EditorDelegate 82: implements ActionListener, ItemListener, Serializable 83: { 84: /** 85: * Use the serial version UID for interoperability. 86: */ 87: private static final long serialVersionUID = -1420007406015481933L; 88: 89: /** 90: * The object value (updated when getting and setting the value). 91: */ 92: protected Object value; 93: 94: /** 95: * Constructor EditorDelegate 96: */ 97: protected EditorDelegate() 98: { 99: // Nothing to do here. 100: } 101: 102: /** 103: * Set the value for the editor component. This method is normally 104: * overridden to set the value in the way, specific for the text 105: * component, check box or combo box. 106: * 107: * @param aValue the value to set (String, Boolean or Number). 108: */ 109: public void setValue(Object aValue) 110: { 111: value = aValue; 112: } 113: 114: /** 115: * Get the value for the editor component. This method is normally 116: * overridden to obtain the value in the way, specific for the text 117: * component, check box or combo box. 118: * 119: * @return value the value of the component (String, Boolean or Number). 120: */ 121: public Object getCellEditorValue() 122: { 123: return value; 124: } 125: 126: /** 127: * The default method returns true for the {@link MouseEvent} and false 128: * for any other events. 129: * 130: * @param event the event to check 131: * 132: * @return true if the passed event is the mouse event and false otherwise. 133: */ 134: public boolean isCellEditable(EventObject event) 135: { 136: if (event == null || !(event instanceof MouseEvent) || 137: (((MouseEvent) event).getClickCount() >= getClickCountToStart())) 138: return true; 139: return false; 140: } // isCellEditable() 141: 142: /** 143: * Returns true to indicate that the editing cell can be selected. 144: * 145: * The default method returns true without action but may be overridden 146: * in derived classes for more specific behavior. 147: * 148: * @param event unused in default method 149: * 150: * @return true always 151: */ 152: public boolean shouldSelectCell(EventObject event) 153: { 154: // return true to indicate that the editing cell may be selected 155: return true; 156: } 157: 158: /** 159: * Finish the cell editing session. This method notifies the registered 160: * cell editor listeners (including the table) that the editing has been 161: * stopped. 162: * 163: * @return boolean 164: */ 165: public boolean stopCellEditing() 166: { 167: fireEditingStopped(); 168: return true; 169: } // stopCellEditing() 170: 171: /** 172: * Cancel the cell editing session. This method notifies the registered 173: * cell editor listeners (including the table) that the editing has been 174: * canceled. 175: */ 176: public void cancelCellEditing() 177: { 178: fireEditingCanceled(); 179: } // cancelCellEditing() 180: 181: /** 182: * Start editing session and returns true to indicate the editing has begun. 183: * The default method returns true without action but may be overridden 184: * in derived classes for more specific behavior. 185: * 186: * @param event the event. 187: * 188: * @return true, always 189: */ 190: public boolean startCellEditing(EventObject event) 191: { 192: // return true to indicate that editing has begun 193: return true; 194: } // startCellEditing() 195: 196: /** 197: * This event is fired by the editor component (for instance, by pressing 198: * ENTER in the {@link JTextField}. The default method delegates call to 199: * the {@link #stopCellEditing}, finishing the editing session. 200: * 201: * @param event unused in default method 202: */ 203: public void actionPerformed(ActionEvent event) 204: { 205: stopCellEditing(); 206: } // actionPerformed() 207: 208: /** 209: * This event is fired by the editor component.The default method delegates 210: * call to the {@link #stopCellEditing}, finishing the editing session. 211: * 212: * @param event unused in default method 213: */ 214: public void itemStateChanged(ItemEvent event) 215: { 216: stopCellEditing(); 217: } // itemStateChanged() 218: 219: /** 220: * Notify the registered listeners (including the table) that the editing 221: * has been completed. 222: */ 223: void fireEditingStopped() 224: { 225: CellEditorListener[] listeners = getCellEditorListeners(); 226: for (int index = 0; index < listeners.length; index++) 227: listeners[index].editingStopped(changeEvent); 228: 229: } 230: 231: /** 232: * Notify the registered listeners (including the table) that the editing 233: * has been canceled. 234: */ 235: void fireEditingCanceled() 236: { 237: CellEditorListener[] listeners = getCellEditorListeners(); 238: for (int index = 0; index < listeners.length; index++) 239: listeners[index].editingCanceled(changeEvent); 240: } 241: } // EditorDelegate 242: 243: /** 244: * Provides getter and setter methods to work with the text component. 245: * 246: * @author Audrius Meskauskas (audriusa@Bioinformatics.org) 247: */ 248: private class JTextFieldDelegate extends EditorDelegate 249: { 250: /** 251: * Use the serial version UID for interoperability. 252: */ 253: private static final long serialVersionUID = 1; 254: 255: /** 256: * Set the value for the editor component. 257: * 258: * @param aValue the value to set (toString() will be called). 259: */ 260: public void setValue(Object aValue) 261: { 262: value = aValue; 263: JTextField f = (JTextField) editorComponent; 264: if (value == null) 265: f.setText(""); 266: else 267: f.setText(value.toString()); 268: } 269: 270: /** 271: * Get the value for the editor component. 272: * 273: * @return value the value of the component (String) 274: */ 275: public Object getCellEditorValue() 276: { 277: JTextField f = (JTextField) editorComponent; 278: return value = f.getText(); 279: } 280: } 281: 282: /** 283: * Provides getter and setter methods to work with the combo box. 284: * 285: * @author Audrius Meskauskas (audriusa@Bioinformatics.org) 286: */ 287: private class JComboBoxDelegate extends EditorDelegate 288: { 289: /** 290: * Use the serial version UID for interoperability. 291: */ 292: private static final long serialVersionUID = 1; 293: 294: /** 295: * Set the value for the editor component. 296: * 297: * @param aValue the value to set. 298: */ 299: public void setValue(Object aValue) 300: { 301: value = aValue; 302: JComboBox c = (JComboBox) editorComponent; 303: if (value != null) 304: c.setSelectedItem(value); 305: } 306: 307: /** 308: * Get the value for the editor component. 309: * 310: * @return value the value of the component (as String) 311: */ 312: public Object getCellEditorValue() 313: { 314: JComboBox c = (JComboBox) editorComponent; 315: return value = c.getSelectedItem(); 316: } 317: } 318: 319: /** 320: * Provides getter and setter methods to work with the check box. 321: * 322: * @author Audrius Meskauskas (audriusa@Bioinformatics.org) 323: */ 324: private class JCheckBoxDelegate extends EditorDelegate 325: { 326: /** 327: * Use the serial version UID for interoperability. 328: */ 329: private static final long serialVersionUID = 1; 330: 331: /** 332: * Set the value for the editor component. 333: * 334: * @param value the value to set (must be Boolean). 335: */ 336: public void setValue(Object value) 337: { 338: JCheckBox c = (JCheckBox) editorComponent; 339: 340: if (value == null) 341: c.setSelected(false); 342: else 343: c.setSelected( ((Boolean) value).booleanValue()); 344: } 345: 346: /** 347: * Get the value for the editor component. 348: * 349: * @return value the value of the component (must be CharSequence) 350: */ 351: public Object getCellEditorValue() 352: { 353: JCheckBox c = (JCheckBox) editorComponent; 354: value = c.isSelected() ? Boolean.TRUE : Boolean.FALSE; 355: return value; 356: } 357: } 358: 359: /** 360: * The Swing JComponent, performing the editing session. 361: */ 362: protected JComponent editorComponent; 363: 364: /** 365: * The editor delegate, responsible for listening the {@link #editorComponent} 366: * events and getting/setting its value. 367: */ 368: protected EditorDelegate delegate; 369: 370: /** 371: * The number of the mouse clicks, required to start the editing session. 372: */ 373: protected int clickCountToStart; 374: 375: /** 376: * Create the DefaultCellEditor that uses the text field as its editor 377: * component (appropriate for the text content) 378: * 379: * @param textfield the text field as will be used as the editor component 380: */ 381: public DefaultCellEditor(JTextField textfield) 382: { 383: editorComponent = textfield; 384: clickCountToStart = 2; 385: delegate = new JTextFieldDelegate(); 386: textfield.addActionListener(delegate); 387: } // DefaultCellEditor() 388: 389: /** 390: * Constructor DefaultCellEditor that uses the checkbox (appropriate 391: * for boolean values) 392: * 393: * @param checkbox the checkbox that will be used with this editor. 394: */ 395: public DefaultCellEditor(JCheckBox checkbox) 396: { 397: editorComponent = checkbox; 398: clickCountToStart = 1; 399: delegate = new JCheckBoxDelegate(); 400: checkbox.addActionListener(delegate); 401: } // DefaultCellEditor() 402: 403: /** 404: * Constructor DefaultCellEditor that uses the combo box. 405: * 406: * @param combobox the combo box that will be used with this editor. 407: */ 408: public DefaultCellEditor(JComboBox combobox) 409: { 410: editorComponent = combobox; 411: clickCountToStart = 1; 412: delegate = new JComboBoxDelegate(); 413: combobox.addActionListener(delegate); 414: } // DefaultCellEditor() 415: 416: /** 417: * Get the component that performs the editing sessions. It is the same 418: * component that was passed in constructor. 419: * 420: * @return the component, performing the editing sessions. 421: */ 422: public Component getComponent() 423: { 424: return editorComponent; 425: } // getComponent() 426: 427: /** 428: * Get the number of mouse clicks, required to start the editing session. 429: * 430: * @return int the number of mouse clicks, required to start the session 431: */ 432: public int getClickCountToStart() 433: { 434: return clickCountToStart; 435: } // getClickCountToStart() 436: 437: /** 438: * Set the number of mouse clicks, required to start the editing session. 439: * 440: * @param count the number of clicks, required to start the session 441: */ 442: public void setClickCountToStart(int count) 443: { 444: clickCountToStart = count; 445: } // setClickCountToStart() 446: 447: /** 448: * Get the value, currently being displayed by the editor component. The 449: * call is forwarded to the {@link #delegate}. 450: * 451: * @return Object the value (class depends on the editor component) 452: */ 453: public Object getCellEditorValue() 454: { 455: return delegate.getCellEditorValue(); 456: } // getCellEditorValue() 457: 458: /** 459: * Forwards call to the {@link #delegate}. 460: * 461: * @param event forwarded to the delegate. 462: * 463: * @return boolean returned by delegate 464: */ 465: public boolean isCellEditable(EventObject event) 466: { 467: return delegate.isCellEditable(event); 468: } // isCellEditable() 469: 470: /** 471: * Forwards call to the {@link #delegate}. 472: * 473: * @param event forwarded to the delegate. 474: * 475: * @return boolean returned by delegate 476: */ 477: public boolean shouldSelectCell(EventObject event) 478: { 479: return delegate.shouldSelectCell(event); 480: } // shouldSelectCell() 481: 482: /** 483: * Forwards call to the {@link #delegate}. 484: * 485: * @return boolean returned by delegate 486: */ 487: public boolean stopCellEditing() 488: { 489: return delegate.stopCellEditing(); 490: } // stopCellEditing() 491: 492: /** 493: * Forwards call to the {@link #delegate}. 494: */ 495: public void cancelCellEditing() 496: { 497: delegate.cancelCellEditing(); 498: } // cancelCellEditing() 499: 500: /** 501: * Sets an initial value for the editor. 502: * This will cause the editor to stopEditing and lose any partially 503: * edited value if the editor is editing when this method is called. 504: * Returns the component that should be added to the client's Component 505: * hierarchy. Once installed in the client's hierarchy this component will 506: * then be able to draw and receive user input. 507: * 508: * @param tree - the JTree that is asking the editor to edit; this 509: * parameter can be null 510: * @param value - the value of the cell to be edited 511: * @param isSelected - true is the cell is to be renderer with selection 512: * highlighting 513: * @param expanded - true if the node is expanded 514: * @param leaf - true if the node is a leaf node 515: * @param row - the row index of the node being edited 516: * 517: * @return Component the component for editing 518: */ 519: public Component getTreeCellEditorComponent(JTree tree, Object value, 520: boolean isSelected, 521: boolean expanded, boolean leaf, 522: int row) 523: { 524: delegate.setValue(value); 525: return editorComponent; 526: } // getTreeCellEditorComponent() 527: 528: /** 529: * Get the cell editor component that will perform the editing session. If 530: * returned once, the same component is also returned on the repetetive calls 531: * again (reused). 532: * 533: * @param table the table where the editing is performed 534: * @param value the current value of the table. It is set as the initial 535: * component value. 536: * @param isSelected if true, the cell is currently selected 537: * @param row the row of the cell being edited 538: * @param column the column of the cell being edited 539: * 540: * @return Component the component that will perform the editing session 541: */ 542: public Component getTableCellEditorComponent(JTable table, Object value, 543: boolean isSelected, int row, 544: int column) 545: { 546: // NOTE: as specified by Sun, we don't call new() everytime, we return 547: // editorComponent on each call to getTableCellEditorComponent or 548: // getTreeCellEditorComponent. 549: delegate.setValue(value); 550: return editorComponent; 551: } // getTableCellEditorComponent() 552: 553: }
GNU Classpath (0.91) |