001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2002  Oliver Burn
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020/*
021 * %W% %E%
022 *
023 * Copyright 1997, 1998 Sun Microsystems, Inc. All Rights Reserved.
024 * 
025 * Redistribution and use in source and binary forms, with or
026 * without modification, are permitted provided that the following
027 * conditions are met:
028 * 
029 * - Redistributions of source code must retain the above copyright
030 *   notice, this list of conditions and the following disclaimer. 
031 *   
032 * - Redistribution in binary form must reproduce the above
033 *   copyright notice, this list of conditions and the following
034 *   disclaimer in the documentation and/or other materials
035 *   provided with the distribution. 
036 *   
037 * Neither the name of Sun Microsystems, Inc. or the names of
038 * contributors may be used to endorse or promote products derived
039 * from this software without specific prior written permission.  
040 * 
041 * This software is provided "AS IS," without a warranty of any
042 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
043 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
044 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
045 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
046 * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
047 * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
048 * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE 
049 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,   
050 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER  
051 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF 
052 * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS 
053 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
054 * 
055 * You acknowledge that this software is not designed, licensed or
056 * intended for use in the design, construction, operation or
057 * maintenance of any nuclear facility.
058 */
059
060
061package com.puppycrawl.tools.checkstyle.gui;
062
063import java.util.EventObject;
064import javax.swing.CellEditor;
065import javax.swing.event.CellEditorListener;
066import javax.swing.event.ChangeEvent;
067import javax.swing.event.EventListenerList;
068
069/** 
070 * A base class for CellEditors, providing default implementations for all 
071 * methods in the CellEditor interface and support for managing a series 
072 * of listeners.
073 *
074 * <a href="http://java.sun.com/products/jfc/tsc/articles/treetable1/index.html">Original&nbsp;Source&nbsp;Location</a>
075 * 
076 * @author Philip Milne
077 */
078public class AbstractCellEditor implements CellEditor
079{
080    private final EventListenerList listenerList = new EventListenerList();
081
082    /** @see CellEditor */
083    @Override
084    public Object getCellEditorValue()
085    {
086        return null;
087    }
088
089    /** @see CellEditor */
090    @Override
091    public boolean isCellEditable(EventObject e)
092    {
093        return true;
094    }
095
096    /** @see CellEditor */
097    @Override
098    public boolean shouldSelectCell(EventObject anEvent)
099    {
100        return false;
101    }
102
103    /** @see CellEditor */
104    @Override
105    public boolean stopCellEditing()
106    {
107        return true;
108    }
109
110    /** @see CellEditor */
111    @Override
112    public void cancelCellEditing()
113    {
114    }
115
116    /** @see CellEditor */
117    @Override
118    public void addCellEditorListener(CellEditorListener l)
119    {
120        listenerList.add(CellEditorListener.class, l);
121    }
122
123    /** @see CellEditor */
124    @Override
125    public void removeCellEditorListener(CellEditorListener l)
126    {
127        listenerList.remove(CellEditorListener.class, l);
128    }
129
130    /*
131     * Notify all listeners that have registered interest for
132     * notification on this event type.
133     * @see EventListenerList
134     */
135    protected void fireEditingStopped()
136    {
137        // Guaranteed to return a non-null array
138        final Object[] listeners = listenerList.getListenerList();
139        // Process the listeners last to first, notifying
140        // those that are interested in this event
141        for (int i = listeners.length - 2; i >= 0; i -= 2) {
142            if (listeners[i] == CellEditorListener.class) {
143                ((CellEditorListener) listeners[i + 1]).editingStopped(new ChangeEvent(this));
144            }
145        }
146    }
147
148    /*
149     * Notify all listeners that have registered interest for
150     * notification on this event type.
151     * @see EventListenerList
152     */
153    protected void fireEditingCanceled()
154    {
155        // Guaranteed to return a non-null array
156        final Object[] listeners = listenerList.getListenerList();
157        // Process the listeners last to first, notifying
158        // those that are interested in this event
159        for (int i = listeners.length - 2; i >= 0; i -= 2) {
160            if (listeners[i] == CellEditorListener.class) {
161                ((CellEditorListener) listeners[i + 1]).editingCanceled(new ChangeEvent(this));
162            }
163        }
164    }
165}