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
32   * of listeners.
33   * <a href=
34   * "https://docs.oracle.com/cd/E48246_01/apirefs.1111/e13403/oracle/ide/controls/TreeTableModel.html">
35   * Original&nbsp;Source&nbsp;Location</a>
36   *
37   */
38  public class BaseCellEditor implements CellEditor {
39  
40      /**
41       * A list of event listeners for the cell editor.
42       */
43      private final EventListenerList listenerList = new EventListenerList();
44  
45      /**
46       * Creates a new {@code BaseCellEditor} instance.
47       */
48      public BaseCellEditor() {
49          // no code by default
50      }
51  
52      @Override
53      public Object getCellEditorValue() {
54          return null;
55      }
56  
57      @Override
58      public boolean isCellEditable(EventObject event) {
59          return true;
60      }
61  
62      @Override
63      public boolean shouldSelectCell(EventObject anEvent) {
64          return false;
65      }
66  
67      @Override
68      public boolean stopCellEditing() {
69          return true;
70      }
71  
72      @Override
73      public void cancelCellEditing() {
74          // No code, tree is read-only
75      }
76  
77      @Override
78      public void addCellEditorListener(CellEditorListener listener) {
79          listenerList.add(CellEditorListener.class, listener);
80      }
81  
82      @Override
83      public void removeCellEditorListener(CellEditorListener listener) {
84          listenerList.remove(CellEditorListener.class, listener);
85      }
86  
87      /**
88       * Notifies all listeners that have registered interest in
89       * 'editing stopped' event.
90       *
91       * @see EventListenerList
92       */
93      protected void fireEditingStopped() {
94          // Guaranteed to return a non-null array
95          final Object[] listeners = listenerList.getListenerList();
96          // Process the listeners last to first, notifying
97          // those that are interested in this event
98          for (int i = listeners.length - 2; i >= 0; i -= 2) {
99              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 }