001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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    @Override
046    public Object getCellEditorValue() {
047        return null;
048    }
049
050    @Override
051    public boolean isCellEditable(EventObject event) {
052        return true;
053    }
054
055    @Override
056    public boolean shouldSelectCell(EventObject anEvent) {
057        return false;
058    }
059
060    @Override
061    public boolean stopCellEditing() {
062        return true;
063    }
064
065    @Override
066    public void cancelCellEditing() {
067        // No code, tree is read-only
068    }
069
070    @Override
071    public void addCellEditorListener(CellEditorListener listener) {
072        listenerList.add(CellEditorListener.class, listener);
073    }
074
075    @Override
076    public void removeCellEditorListener(CellEditorListener listener) {
077        listenerList.remove(CellEditorListener.class, listener);
078    }
079
080    /**
081     * Notifies all listeners that have registered interest in
082     * 'editing stopped' event.
083     *
084     * @see EventListenerList
085     */
086    protected void fireEditingStopped() {
087        // Guaranteed to return a non-null array
088        final Object[] listeners = listenerList.getListenerList();
089        // Process the listeners last to first, notifying
090        // those that are interested in this event
091        for (int i = listeners.length - 2; i >= 0; i -= 2) {
092            if (listeners[i] == CellEditorListener.class) {
093                ((CellEditorListener) listeners[i + 1]).editingStopped(new ChangeEvent(this));
094            }
095        }
096    }
097
098    /**
099     * Notifies all listeners that have registered interest in
100     * 'editing canceled' event.
101     *
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}