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