View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.gui;
21  
22  import java.util.EventObject;
23  
24  import javax.swing.CellEditor;
25  import javax.swing.event.CellEditorListener;
26  import javax.swing.event.ChangeEvent;
27  import javax.swing.event.EventListenerList;
28  
29  /**
30   * A base class for CellEditors, providing default implementations for all
31   * methods in the CellEditor interface and support for managing a series of listeners.
32   * <a href=
33   * "https://docs.oracle.com/cd/E48246_01/apirefs.1111/e13403/oracle/ide/controls/TreeTableModel.html">
34   * Original&nbsp;Source&nbsp;Location</a>
35   *
36   */
37  public class BaseCellEditor implements CellEditor {
38  
39      /**
40       * A list of event listeners for the cell editor.
41       */
42      private final EventListenerList listenerList = new EventListenerList();
43  
44      /**
45       * Creates a new {@code BaseCellEditor} instance.
46       */
47      public BaseCellEditor() {
48          // no code by default
49      }
50  
51      @Override
52      public Object getCellEditorValue() {
53          return null;
54      }
55  
56      @Override
57      public boolean isCellEditable(EventObject event) {
58          return true;
59      }
60  
61      @Override
62      public boolean shouldSelectCell(EventObject anEvent) {
63          return false;
64      }
65  
66      @Override
67      public boolean stopCellEditing() {
68          return true;
69      }
70  
71      @Override
72      public void cancelCellEditing() {
73          // No code, tree is read-only
74      }
75  
76      @Override
77      public void addCellEditorListener(CellEditorListener listener) {
78          listenerList.add(CellEditorListener.class, listener);
79      }
80  
81      @Override
82      public void removeCellEditorListener(CellEditorListener listener) {
83          listenerList.remove(CellEditorListener.class, listener);
84      }
85  
86      /**
87       * Notifies all listeners that have registered interest in 'editing stopped' event.
88       *
89       * @see EventListenerList
90       */
91      protected void fireEditingStopped() {
92          // Guaranteed to return a non-null array
93          final Object[] listeners = listenerList.getListenerList();
94          // Process the listeners last to first, notifying
95          // those that are interested in this event
96          for (int index = listeners.length - 2; index >= 0; index -= 2) {
97              if (listeners[index] == CellEditorListener.class) {
98                  ((CellEditorListener) listeners[index + 1]).editingStopped(new ChangeEvent(this));
99              }
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 }