Source code: com/puppycrawl/tools/checkstyle/gui/AbstractTreeTableModel.java
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 * @(#)AbstractTreeTableModel.java 1.2 98/10/27
22 *
23 * Copyright 1997, 1998 by Sun Microsystems, Inc.,
24 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
25 * All rights reserved.
26 *
27 * This software is the confidential and proprietary information
28 * of Sun Microsystems, Inc. ("Confidential Information"). You
29 * shall not disclose such Confidential Information and shall use
30 * it only in accordance with the terms of the license agreement
31 * you entered into with Sun.
32 */
33
34 package com.puppycrawl.tools.checkstyle.gui;
35
36 import javax.swing.tree.*;
37 import javax.swing.event.*;
38
39 /**
40 * @version 1.2 10/27/98
41 * An abstract implementation of the TreeTableModel interface, handling the list
42 * of listeners.
43 * @author Philip Milne
44 */
45
46 public abstract class AbstractTreeTableModel implements TreeTableModel
47 {
48 private Object mRoot;
49 private EventListenerList mListenerList = new EventListenerList();
50
51 public AbstractTreeTableModel(Object root)
52 {
53 this.mRoot = root;
54 }
55
56 //
57 // Default implmentations for methods in the TreeModel interface.
58 //
59
60 public Object getRoot()
61 {
62 return mRoot;
63 }
64
65 public boolean isLeaf(Object node)
66 {
67 return getChildCount(node) == 0;
68 }
69
70 public void valueForPathChanged(TreePath path, Object newValue)
71 {
72 }
73
74 // This is not called in the JTree's default mode: use a naive implementation.
75 public int getIndexOfChild(Object parent, Object child)
76 {
77 for (int i = 0; i < getChildCount(parent); i++) {
78 if (getChild(parent, i).equals(child)) {
79 return i;
80 }
81 }
82 return -1;
83 }
84
85 public void addTreeModelListener(TreeModelListener l)
86 {
87 mListenerList.add(TreeModelListener.class, l);
88 }
89
90 public void removeTreeModelListener(TreeModelListener l)
91 {
92 mListenerList.remove(TreeModelListener.class, l);
93 }
94
95 /*
96 * Notify all listeners that have registered interest for
97 * notification on this event type. The event instance
98 * is lazily created using the parameters passed into
99 * the fire method.
100 * @see EventListenerList
101 */
102 protected void fireTreeNodesChanged(Object source, Object[] path,
103 int[] childIndices,
104 Object[] children)
105 {
106 // Guaranteed to return a non-null array
107 Object[] listeners = mListenerList.getListenerList();
108 TreeModelEvent e = null;
109 // Process the listeners last to first, notifying
110 // those that are interested in this event
111 for (int i = listeners.length - 2; i >= 0; i -= 2) {
112 if (listeners[i] == TreeModelListener.class) {
113 // Lazily create the event:
114 if (e == null)
115 e = new TreeModelEvent(source, path,
116 childIndices, children);
117 ((TreeModelListener) listeners[i + 1]).treeNodesChanged(e);
118 }
119 }
120 }
121
122 /*
123 * Notify all listeners that have registered interest for
124 * notification on this event type. The event instance
125 * is lazily created using the parameters passed into
126 * the fire method.
127 * @see EventListenerList
128 */
129 protected void fireTreeNodesInserted(Object source, Object[] path,
130 int[] childIndices,
131 Object[] children)
132 {
133 // Guaranteed to return a non-null array
134 Object[] listeners = mListenerList.getListenerList();
135 TreeModelEvent e = null;
136 // Process the listeners last to first, notifying
137 // those that are interested in this event
138 for (int i = listeners.length - 2; i >= 0; i -= 2) {
139 if (listeners[i] == TreeModelListener.class) {
140 // Lazily create the event:
141 if (e == null)
142 e = new TreeModelEvent(source, path,
143 childIndices, children);
144 ((TreeModelListener) listeners[i + 1]).treeNodesInserted(e);
145 }
146 }
147 }
148
149 /*
150 * Notify all listeners that have registered interest for
151 * notification on this event type. The event instance
152 * is lazily created using the parameters passed into
153 * the fire method.
154 * @see EventListenerList
155 */
156 protected void fireTreeNodesRemoved(Object source, Object[] path,
157 int[] childIndices,
158 Object[] children)
159 {
160 // Guaranteed to return a non-null array
161 Object[] listeners = mListenerList.getListenerList();
162 TreeModelEvent e = null;
163 // Process the listeners last to first, notifying
164 // those that are interested in this event
165 for (int i = listeners.length - 2; i >= 0; i -= 2) {
166 if (listeners[i] == TreeModelListener.class) {
167 // Lazily create the event:
168 if (e == null)
169 e = new TreeModelEvent(source, path,
170 childIndices, children);
171 ((TreeModelListener) listeners[i + 1]).treeNodesRemoved(e);
172 }
173 }
174 }
175
176 /*
177 * Notify all listeners that have registered interest for
178 * notification on this event type. The event instance
179 * is lazily created using the parameters passed into
180 * the fire method.
181 * @see EventListenerList
182 */
183 protected void fireTreeStructureChanged(Object source, Object[] path,
184 int[] childIndices,
185 Object[] children)
186 {
187 // Guaranteed to return a non-null array
188 Object[] listeners = mListenerList.getListenerList();
189 TreeModelEvent e = null;
190 // Process the listeners last to first, notifying
191 // those that are interested in this event
192 for (int i = listeners.length - 2; i >= 0; i -= 2) {
193 if (listeners[i] == TreeModelListener.class) {
194 // Lazily create the event:
195 if (e == null)
196 e = new TreeModelEvent(source, path,
197 childIndices, children);
198 ((TreeModelListener) listeners[i + 1]).treeStructureChanged(e);
199 }
200 }
201 }
202
203 //
204 // Default impelmentations for methods in the TreeTableModel interface.
205 //
206
207 public Class getColumnClass(int column)
208 {
209 return Object.class;
210 }
211
212 /** By default, make the column with the Tree in it the only editable one.
213 * Making this column editable causes the JTable to forward mouse
214 * and keyboard events in the Tree column to the underlying JTree.
215 */
216 public boolean isCellEditable(Object node, int column)
217 {
218 return getColumnClass(column) == TreeTableModel.class;
219 }
220
221 public void setValueAt(Object aValue, Object node, int column)
222 {
223 }
224
225
226 // Left to be implemented in the subclass:
227
228 /*
229 * public Object getChild(Object parent, int index)
230 * public int getChildCount(Object parent)
231 * public int getColumnCount()
232 * public String getColumnName(Object node, int column)
233 * public Object getValueAt(Object node, int column)
234 */
235 }