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 mListenerList = new EventListenerList();
081
082    /** @see CellEditor */
083    public Object getCellEditorValue()
084    {
085        return null;
086    }
087
088    /** @see CellEditor */
089    public boolean isCellEditable(EventObject e)
090    {
091        return true;
092    }
093
094    /** @see CellEditor */
095    public boolean shouldSelectCell(EventObject anEvent)
096    {
097        return false;
098    }
099
100    /** @see CellEditor */
101    public boolean stopCellEditing()
102    {
103        return true;
104    }
105
106    /** @see CellEditor */
107    public void cancelCellEditing()
108    {
109    }
110
111    /** @see CellEditor */
112    public void addCellEditorListener(CellEditorListener l)
113    {
114        mListenerList.add(CellEditorListener.class, l);
115    }
116
117    /** @see CellEditor */
118    public void removeCellEditorListener(CellEditorListener l)
119    {
120        mListenerList.remove(CellEditorListener.class, l);
121    }
122
123    /*
124     * Notify all listeners that have registered interest for
125     * notification on this event type.
126     * @see EventListenerList
127     */
128    protected void fireEditingStopped()
129    {
130        // Guaranteed to return a non-null array
131        final Object[] listeners = mListenerList.getListenerList();
132        // Process the listeners last to first, notifying
133        // those that are interested in this event
134        for (int i = listeners.length - 2; i >= 0; i -= 2) {
135            if (listeners[i] == CellEditorListener.class) {
136                ((CellEditorListener) listeners[i + 1]).editingStopped(new ChangeEvent(this));
137            }
138        }
139    }
140
141    /*
142     * Notify all listeners that have registered interest for
143     * notification on this event type.
144     * @see EventListenerList
145     */
146    protected void fireEditingCanceled()
147    {
148        // Guaranteed to return a non-null array
149        final Object[] listeners = mListenerList.getListenerList();
150        // Process the listeners last to first, notifying
151        // those that are interested in this event
152        for (int i = listeners.length - 2; i >= 0; i -= 2) {
153            if (listeners[i] == CellEditorListener.class) {
154                ((CellEditorListener) listeners[i + 1]).editingCanceled(new ChangeEvent(this));
155            }
156        }
157    }
158}