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