View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2002  Oliver Burn
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  /*
21   * %W% %E%
22   *
23   * Copyright 1997, 1998 Sun Microsystems, Inc. All Rights Reserved.
24   *
25   * Redistribution and use in source and binary forms, with or
26   * without modification, are permitted provided that the following
27   * conditions are met:
28   *
29   * - Redistributions of source code must retain the above copyright
30   *   notice, this list of conditions and the following disclaimer.
31   *
32   * - Redistribution in binary form must reproduce the above
33   *   copyright notice, this list of conditions and the following
34   *   disclaimer in the documentation and/or other materials
35   *   provided with the distribution.
36   *
37   * Neither the name of Sun Microsystems, Inc. or the names of
38   * contributors may be used to endorse or promote products derived
39   * from this software without specific prior written permission.
40   *
41   * This software is provided "AS IS," without a warranty of any
42   * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
43   * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
44   * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
45   * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
46   * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
47   * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
48   * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
49   * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
50   * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
51   * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
52   * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
53   * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
54   *
55   * You acknowledge that this software is not designed, licensed or
56   * intended for use in the design, construction, operation or
57   * maintenance of any nuclear facility.
58   */
59  
60  package com.puppycrawl.tools.checkstyle.gui;
61  
62  import javax.swing.event.EventListenerList;
63  import javax.swing.event.TreeModelEvent;
64  import javax.swing.event.TreeModelListener;
65  import javax.swing.tree.TreePath;
66  
67  /**
68   * An abstract implementation of the TreeTableModel interface, handling
69   * the list of listeners.
70   *
71   * <a href="http://java.sun.com/products/jfc/tsc/articles/treetable1/index.html">Original&nbsp;Source&nbsp;Location</a>
72   *
73   * @author Philip Milne
74   */
75  public abstract class AbstractTreeTableModel implements TreeTableModel
76  {
77      private final Object root;
78      private final EventListenerList listenerList = new EventListenerList();
79  
80      public AbstractTreeTableModel(Object root)
81      {
82          this.root = root;
83      }
84  
85      //
86      // Default implmentations for methods in the TreeModel interface.
87      //
88  
89      @Override
90      public Object getRoot()
91      {
92          return root;
93      }
94  
95      @Override
96      public boolean isLeaf(Object node)
97      {
98          return getChildCount(node) == 0;
99      }
100 
101     @Override
102     public void valueForPathChanged(TreePath path, Object newValue)
103     {
104     }
105 
106     // This is not called in the JTree's default mode: use a naive implementation.
107     @Override
108     public int getIndexOfChild(Object parent, Object child)
109     {
110         for (int i = 0; i < getChildCount(parent); i++) {
111             if (getChild(parent, i).equals(child)) {
112                 return i;
113             }
114         }
115         return -1;
116     }
117 
118     @Override
119     public void addTreeModelListener(TreeModelListener l)
120     {
121         listenerList.add(TreeModelListener.class, l);
122     }
123 
124     @Override
125     public void removeTreeModelListener(TreeModelListener l)
126     {
127         listenerList.remove(TreeModelListener.class, l);
128     }
129 
130     /*
131      * Notify all listeners that have registered interest for
132      * notification on this event type.  The event instance
133      * is lazily created using the parameters passed into
134      * the fire method.
135      * @see EventListenerList
136      */
137     protected void fireTreeNodesChanged(Object source, Object[] path,
138             int[] childIndices,
139             Object[] children)
140     {
141         // Guaranteed to return a non-null array
142         final Object[] listeners = listenerList.getListenerList();
143         TreeModelEvent e = null;
144         // Process the listeners last to first, notifying
145         // those that are interested in this event
146         for (int i = listeners.length - 2; i >= 0; i -= 2) {
147             if (listeners[i] == TreeModelListener.class) {
148                 // Lazily create the event:
149                 if (e == null) {
150                     e = new TreeModelEvent(source, path,
151                             childIndices, children);
152                 }
153                 ((TreeModelListener) listeners[i + 1]).treeNodesChanged(e);
154             }
155         }
156     }
157 
158     /*
159      * Notify all listeners that have registered interest for
160      * notification on this event type.  The event instance
161      * is lazily created using the parameters passed into
162      * the fire method.
163      * @see EventListenerList
164      */
165     protected void fireTreeNodesInserted(Object source, Object[] path,
166             int[] childIndices,
167             Object[] children)
168     {
169         // Guaranteed to return a non-null array
170         final Object[] listeners = listenerList.getListenerList();
171         TreeModelEvent e = null;
172         // Process the listeners last to first, notifying
173         // those that are interested in this event
174         for (int i = listeners.length - 2; i >= 0; i -= 2) {
175             if (listeners[i] == TreeModelListener.class) {
176                 // Lazily create the event:
177                 if (e == null) {
178                     e = new TreeModelEvent(source, path,
179                             childIndices, children);
180                 }
181                 ((TreeModelListener) listeners[i + 1]).treeNodesInserted(e);
182             }
183         }
184     }
185 
186     /*
187      * Notify all listeners that have registered interest for
188      * notification on this event type.  The event instance
189      * is lazily created using the parameters passed into
190      * the fire method.
191      * @see EventListenerList
192      */
193     protected void fireTreeNodesRemoved(Object source, Object[] path,
194             int[] childIndices,
195             Object[] children)
196     {
197         // Guaranteed to return a non-null array
198         final Object[] listeners = listenerList.getListenerList();
199         TreeModelEvent e = null;
200         // Process the listeners last to first, notifying
201         // those that are interested in this event
202         for (int i = listeners.length - 2; i >= 0; i -= 2) {
203             if (listeners[i] == TreeModelListener.class) {
204                 // Lazily create the event:
205                 if (e == null) {
206                     e = new TreeModelEvent(source, path,
207                             childIndices, children);
208                 }
209                 ((TreeModelListener) listeners[i + 1]).treeNodesRemoved(e);
210             }
211         }
212     }
213 
214     /*
215      * Notify all listeners that have registered interest for
216      * notification on this event type.  The event instance
217      * is lazily created using the parameters passed into
218      * the fire method.
219      * @see EventListenerList
220      */
221     protected void fireTreeStructureChanged(Object source, Object[] path,
222             int[] childIndices,
223             Object[] children)
224     {
225         // Guaranteed to return a non-null array
226         final Object[] listeners = listenerList.getListenerList();
227         TreeModelEvent e = null;
228         // Process the listeners last to first, notifying
229         // those that are interested in this event
230         for (int i = listeners.length - 2; i >= 0; i -= 2) {
231             if (listeners[i] == TreeModelListener.class) {
232                 // Lazily create the event:
233                 if (e == null) {
234                     e = new TreeModelEvent(source, path,
235                             childIndices, children);
236                 }
237                 ((TreeModelListener) listeners[i + 1]).treeStructureChanged(e);
238             }
239         }
240     }
241 
242     //
243     // Default impelmentations for methods in the TreeTableModel interface.
244     //
245 
246     @Override
247     public Class<?> getColumnClass(int column)
248     {
249         return Object.class;
250     }
251 
252     /** By default, make the column with the Tree in it the only editable one.
253      *  Making this column editable causes the JTable to forward mouse
254      *  and keyboard events in the Tree column to the underlying JTree.
255      */
256     @Override
257     public boolean isCellEditable(Object node, int column)
258     {
259         return getColumnClass(column) == TreeTableModel.class;
260     }
261 
262     @Override
263     public void setValueAt(Object value, Object node, int column)
264     {
265     }
266 
267 
268     // Left to be implemented in the subclass:
269 
270     /*
271      *   public Object getChild(Object parent, int index)
272      *   public int getChildCount(Object parent)
273      *   public int getColumnCount()
274      *   public String getColumnName(Object node, int column)
275      *   public Object getValueAt(Object node, int column)
276      */
277 }