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