View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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      @Override
46      public Object getCellEditorValue() {
47          return null;
48      }
49  
50      @Override
51      public boolean isCellEditable(EventObject event) {
52          return true;
53      }
54  
55      @Override
56      public boolean shouldSelectCell(EventObject anEvent) {
57          return false;
58      }
59  
60      @Override
61      public boolean stopCellEditing() {
62          return true;
63      }
64  
65      @Override
66      public void cancelCellEditing() {
67          // No code, tree is read-only
68      }
69  
70      @Override
71      public void addCellEditorListener(CellEditorListener listener) {
72          listenerList.add(CellEditorListener.class, listener);
73      }
74  
75      @Override
76      public void removeCellEditorListener(CellEditorListener listener) {
77          listenerList.remove(CellEditorListener.class, listener);
78      }
79  
80      /**
81       * Notifies all listeners that have registered interest in
82       * 'editing stopped' event.
83       *
84       * @see EventListenerList
85       */
86      protected void fireEditingStopped() {
87          // Guaranteed to return a non-null array
88          final Object[] listeners = listenerList.getListenerList();
89          // Process the listeners last to first, notifying
90          // those that are interested in this event
91          for (int i = listeners.length - 2; i >= 0; i -= 2) {
92              if (listeners[i] == CellEditorListener.class) {
93                  ((CellEditorListener) listeners[i + 1]).editingStopped(new ChangeEvent(this));
94              }
95          }
96      }
97  
98      /**
99       * 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 }