Source for javax.swing.AbstractCellEditor

   1: /* AbstractCellEditor.java --
   2:    Copyright (C) 2002, 2004, 2005 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.io.Serializable;
  42: import java.util.EventObject;
  43: 
  44: import javax.swing.event.CellEditorListener;
  45: import javax.swing.event.ChangeEvent;
  46: import javax.swing.event.EventListenerList;
  47: 
  48: /**
  49:  * The abstract superclass for table and tree cells. This provides some
  50:  * common shared functionality.
  51:  *
  52:  * @author    Andrew Selkirk
  53:  * @version    1.0
  54:  */
  55: public abstract class AbstractCellEditor
  56:   implements CellEditor, Serializable
  57: {
  58:   private static final long serialVersionUID = -1048006551406220959L;
  59: 
  60:   /**
  61:    * Our Swing event listeners.
  62:    */
  63:   protected EventListenerList listenerList;
  64: 
  65:   /**
  66:    * The cached ChangeEvent.
  67:    */
  68:   protected transient ChangeEvent changeEvent;
  69: 
  70:   /**
  71:    * Creates a new instance of AbstractCellEditor.
  72:    */
  73:   public AbstractCellEditor() {
  74:     listenerList = new EventListenerList();
  75:     changeEvent = new ChangeEvent(this);
  76:   } // AbstractCellEditor()
  77: 
  78:   /**
  79:    * Returns <code>true</code> if the cell is editable using
  80:    * <code>event</code>, <code>false</code>
  81:    * if it's not. The default behaviour is to return <code>true</code>.
  82:    *
  83:    * @param event an event
  84:    *
  85:    * @return <code>true</code> if the cell is editable using
  86:    *     <code>event</code>, <code>false</code> if it's not
  87:    */
  88:   public boolean isCellEditable(EventObject event) {
  89:     return true;
  90:   } // isCellEditable()
  91: 
  92:   /**
  93:    * Returns <code>true</code> if the editing cell should be selected,
  94:    * <code>false</code> otherwise. This is usually returning <code>true</code>,
  95:    * but in some special cases it might be useful not to switch cell selection
  96:    * when editing one cell.
  97:    *
  98:    * @param event an event
  99:    *
 100:    * @return <code>true</code> if the editing cell should be selected,
 101:    *     <code>false</code> otherwise
 102:    */
 103:   public boolean shouldSelectCell(EventObject event) {
 104:     return true;
 105:   } // shouldSelectCell()
 106: 
 107:   /**
 108:    * Stop editing the cell and accept any partial value that has been entered
 109:    * into the cell.
 110:    *
 111:    * @returns <code>true</code> if editing has been stopped successfully,
 112:    *     <code>false</code>otherwise
 113:    */
 114:   public boolean stopCellEditing() {
 115:     fireEditingStopped();
 116:     return true;
 117:   } // stopCellEditing()
 118: 
 119:   /**
 120:    * Stop editing the cell and do not accept any partial value that has
 121:    * been entered into the cell.
 122:    */
 123:   public void cancelCellEditing() {
 124:     fireEditingCanceled();
 125:   } // cancelCellEditing()
 126: 
 127:   /**
 128:    * Adds a CellEditorListener to the list of CellEditorListeners of this
 129:    * CellEditor.
 130:    *
 131:    * @param listener the CellEditorListener to add
 132:    */
 133:   public void addCellEditorListener (CellEditorListener listener)
 134:   {
 135:     listenerList.add (CellEditorListener.class, listener);
 136:   }
 137: 
 138:   /**
 139:    * Removes the specified CellEditorListener from the list of the
 140:    * CellEditorListeners of this CellEditor.
 141:    *
 142:    * @param listener the CellEditorListener to remove
 143:    */
 144:   public void removeCellEditorListener (CellEditorListener listener)
 145:   {
 146:     listenerList.remove (CellEditorListener.class, listener);
 147:   }
 148:     
 149:   /**
 150:    * Returns the list of CellEditorListeners that have been registered
 151:    * in this CellEditor.
 152:    *
 153:    * @return the list of CellEditorListeners that have been registered
 154:    *     in this CellEditor
 155:    *
 156:    * @since 1.4
 157:    */
 158:   public CellEditorListener[] getCellEditorListeners()
 159:   {
 160:     return (CellEditorListener[]) listenerList.getListeners
 161:       (CellEditorListener.class);
 162:   }
 163: 
 164:   /**
 165:    * Notifies all registered listeners that the editing of the cell has has been
 166:    * stopped.
 167:    */
 168:   protected void fireEditingStopped()
 169:   {
 170:     CellEditorListener[] listeners = getCellEditorListeners();
 171: 
 172:     for (int index = 0; index < listeners.length; index++)
 173:       {
 174:         listeners[index].editingStopped(changeEvent);
 175:       }
 176:   }
 177: 
 178:   /**
 179:    * Notifies all registered listeners that the editing of the cell has
 180:    * has been canceled.
 181:    */
 182:   protected void fireEditingCanceled()
 183:   {
 184:     CellEditorListener[] listeners = getCellEditorListeners();
 185: 
 186:     for (int index = 0; index < listeners.length; index++)
 187:       {
 188:         listeners[index].editingCanceled(changeEvent);
 189:       }
 190:   }
 191: }