001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 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 */
039public class BaseCellEditor implements CellEditor {
040
041    /**
042     * A list of event listeners for the cell editor.
043     */
044    private final EventListenerList listenerList = new EventListenerList();
045
046    @Override
047    public Object getCellEditorValue() {
048        return null;
049    }
050
051    @Override
052    public boolean isCellEditable(EventObject event) {
053        return true;
054    }
055
056    @Override
057    public boolean shouldSelectCell(EventObject anEvent) {
058        return false;
059    }
060
061    @Override
062    public boolean stopCellEditing() {
063        return true;
064    }
065
066    @Override
067    public void cancelCellEditing() {
068        // No code, tree is read-only
069    }
070
071    @Override
072    public void addCellEditorListener(CellEditorListener listener) {
073        listenerList.add(CellEditorListener.class, listener);
074    }
075
076    @Override
077    public void removeCellEditorListener(CellEditorListener listener) {
078        listenerList.remove(CellEditorListener.class, listener);
079    }
080
081    /**
082     * Notifies all listeners that have registered interest for
083     * 'editing stopped' event.
084     *
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     *
103     * @see EventListenerList
104     */
105    protected void fireEditingCanceled() {
106        // Guaranteed to return a non-null array
107        final Object[] listeners = listenerList.getListenerList();
108        // Process the listeners last to first, notifying
109        // those that are interested in this event
110        for (int i = listeners.length - 2; i >= 0; i -= 2) {
111            if (listeners[i] == CellEditorListener.class) {
112                ((CellEditorListener) listeners[i + 1]).editingCanceled(new ChangeEvent(this));
113            }
114        }
115    }
116
117}