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