001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 the original author or authors.
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
020package com.puppycrawl.tools.checkstyle.gui;
021
022import java.util.EventObject;
023
024import javax.swing.CellEditor;
025import javax.swing.event.CellEditorListener;
026import javax.swing.event.ChangeEvent;
027import javax.swing.event.EventListenerList;
028
029/**
030 * A base class for CellEditors, providing default implementations for all
031 * methods in the CellEditor interface and support for managing a series
032 * of listeners.
033 *
034 * <a href=
035 * "https://docs.oracle.com/cd/E48246_01/apirefs.1111/e13403/oracle/ide/controls/TreeTableModel.html">
036 * Original&nbsp;Source&nbsp;Location</a>
037 *
038 * @author Philip Milne
039 */
040public class BaseCellEditor implements CellEditor {
041
042    /**
043     * A list of event listeners for the cell editor.
044     */
045    private final EventListenerList listenerList = new EventListenerList();
046
047    @Override
048    public Object getCellEditorValue() {
049        return null;
050    }
051
052    @Override
053    public boolean isCellEditable(EventObject event) {
054        return true;
055    }
056
057    @Override
058    public boolean shouldSelectCell(EventObject anEvent) {
059        return false;
060    }
061
062    @Override
063    public boolean stopCellEditing() {
064        return true;
065    }
066
067    @Override
068    public void cancelCellEditing() {
069        // No code, tree is read-only
070    }
071
072    @Override
073    public void addCellEditorListener(CellEditorListener listener) {
074        listenerList.add(CellEditorListener.class, listener);
075    }
076
077    @Override
078    public void removeCellEditorListener(CellEditorListener listener) {
079        listenerList.remove(CellEditorListener.class, listener);
080    }
081
082    /**
083     * Notifies all listeners that have registered interest for
084     * 'editing stopped' event.
085     * @see EventListenerList
086     */
087    protected void fireEditingStopped() {
088        // Guaranteed to return a non-null array
089        final Object[] listeners = listenerList.getListenerList();
090        // Process the listeners last to first, notifying
091        // those that are interested in this event
092        for (int i = listeners.length - 2; i >= 0; i -= 2) {
093            if (listeners[i] == CellEditorListener.class) {
094                ((CellEditorListener) listeners[i + 1]).editingStopped(new ChangeEvent(this));
095            }
096        }
097    }
098
099    /**
100     * Notifies all listeners that have registered interest for
101     * 'editing canceled' event.
102     * @see EventListenerList
103     */
104    protected void fireEditingCanceled() {
105        // Guaranteed to return a non-null array
106        final Object[] listeners = listenerList.getListenerList();
107        // Process the listeners last to first, notifying
108        // those that are interested in this event
109        for (int i = listeners.length - 2; i >= 0; i -= 2) {
110            if (listeners[i] == CellEditorListener.class) {
111                ((CellEditorListener) listeners[i + 1]).editingCanceled(new ChangeEvent(this));
112            }
113        }
114    }
115
116}