svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / treeTable / AbstractTreeTableModel.java @ 40561
History | View | Annotate | Download (7.92 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.gui.beans.swing.treeTable; |
25 |
|
26 |
import javax.swing.event.EventListenerList; |
27 |
import javax.swing.event.TreeModelEvent; |
28 |
import javax.swing.event.TreeModelListener; |
29 |
import javax.swing.tree.TreePath; |
30 |
|
31 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
32 |
*
|
33 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
34 |
*
|
35 |
* This program is free software; you can redistribute it and/or
|
36 |
* modify it under the terms of the GNU General Public License
|
37 |
* as published by the Free Software Foundation; either version 2
|
38 |
* of the License, or (at your option) any later version.
|
39 |
*
|
40 |
* This program is distributed in the hope that it will be useful,
|
41 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
42 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
43 |
* GNU General Public License for more details.
|
44 |
*
|
45 |
* You should have received a copy of the GNU General Public License
|
46 |
* along with this program; if not, write to the Free Software
|
47 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
48 |
*
|
49 |
* For more information, contact:
|
50 |
*
|
51 |
* Generalitat Valenciana
|
52 |
* Conselleria d'Infraestructures i Transport
|
53 |
* Av. Blasco Ib??ez, 50
|
54 |
* 46010 VALENCIA
|
55 |
* SPAIN
|
56 |
*
|
57 |
* +34 963862235
|
58 |
* gvsig@gva.es
|
59 |
* www.gvsig.gva.es
|
60 |
*
|
61 |
* or
|
62 |
*
|
63 |
* IVER T.I. S.A
|
64 |
* Salamanca 50
|
65 |
* 46005 Valencia
|
66 |
* Spain
|
67 |
*
|
68 |
* +34 963163400
|
69 |
* dac@iver.es
|
70 |
*/
|
71 |
/* CVS MESSAGES:
|
72 |
*
|
73 |
* $Id: AbstractTreeTableModel.java 13136 2007-08-20 08:38:34Z evercher $
|
74 |
* $Log$
|
75 |
* Revision 1.1 2007-08-20 08:34:46 evercher
|
76 |
* He fusionado LibUI con LibUIComponents
|
77 |
*
|
78 |
* Revision 1.3 2006/11/01 17:27:44 jorpiell
|
79 |
* Se elimina el nodo "virtual" de la raiz
|
80 |
*
|
81 |
* Revision 1.2 2006/10/23 11:13:55 jorpiell
|
82 |
* A?adida la posibilidad de recargar el ?rbol tantas veces como queramos
|
83 |
*
|
84 |
* Revision 1.1 2006/10/23 08:18:39 jorpiell
|
85 |
* A?adida la clase treetable
|
86 |
*
|
87 |
*
|
88 |
*/
|
89 |
/**
|
90 |
* @author Jorge Piera Llodr? (piera_jor@gva.es)
|
91 |
*/
|
92 |
public abstract class AbstractTreeTableModel implements TreeTableModel { |
93 |
protected Object root; |
94 |
protected EventListenerList listenerList = new EventListenerList(); |
95 |
|
96 |
public AbstractTreeTableModel(Object root) { |
97 |
setNodes(root); |
98 |
} |
99 |
|
100 |
public void setNodes(Object root){ |
101 |
this.root = root;
|
102 |
} |
103 |
|
104 |
//
|
105 |
// Default implmentations for methods in the TreeModel interface.
|
106 |
//
|
107 |
|
108 |
public Object getRoot() { |
109 |
return root;
|
110 |
} |
111 |
|
112 |
public boolean isLeaf(Object node) { |
113 |
return getChildCount(node) == 0; |
114 |
} |
115 |
|
116 |
public void valueForPathChanged(TreePath path, Object newValue) {} |
117 |
|
118 |
// This is not called in the JTree's default mode: use a naive implementation.
|
119 |
public int getIndexOfChild(Object parent, Object child) { |
120 |
for (int i = 0; i < getChildCount(parent); i++) { |
121 |
if (getChild(parent, i).equals(child)) {
|
122 |
return i;
|
123 |
} |
124 |
} |
125 |
return -1; |
126 |
} |
127 |
|
128 |
public void addTreeModelListener(TreeModelListener l) { |
129 |
listenerList.add(TreeModelListener.class, l);
|
130 |
} |
131 |
|
132 |
public void removeTreeModelListener(TreeModelListener l) { |
133 |
listenerList.remove(TreeModelListener.class, l);
|
134 |
} |
135 |
|
136 |
/*
|
137 |
* Notify all listeners that have registered interest for
|
138 |
* notification on this event type. The event instance
|
139 |
* is lazily created using the parameters passed into
|
140 |
* the fire method.
|
141 |
* @see EventListenerList
|
142 |
*/
|
143 |
protected void fireTreeNodesChanged(Object source, Object[] path, |
144 |
int[] childIndices, |
145 |
Object[] children) { |
146 |
// Guaranteed to return a non-null array
|
147 |
Object[] listeners = listenerList.getListenerList(); |
148 |
TreeModelEvent e = null; |
149 |
// Process the listeners last to first, notifying
|
150 |
// those that are interested in this event
|
151 |
for (int i = listeners.length-2; i>=0; i-=2) { |
152 |
if (listeners[i]==TreeModelListener.class) { |
153 |
// Lazily create the event:
|
154 |
if (e == null) |
155 |
e = new TreeModelEvent(source, path, |
156 |
childIndices, children); |
157 |
((TreeModelListener)listeners[i+1]).treeNodesChanged(e); |
158 |
} |
159 |
} |
160 |
} |
161 |
|
162 |
/*
|
163 |
* Notify all listeners that have registered interest for
|
164 |
* notification on this event type. The event instance
|
165 |
* is lazily created using the parameters passed into
|
166 |
* the fire method.
|
167 |
* @see EventListenerList
|
168 |
*/
|
169 |
protected void fireTreeNodesInserted(Object source, Object[] path, |
170 |
int[] childIndices, |
171 |
Object[] children) { |
172 |
// Guaranteed to return a non-null array
|
173 |
Object[] listeners = listenerList.getListenerList(); |
174 |
TreeModelEvent e = null; |
175 |
// Process the listeners last to first, notifying
|
176 |
// those that are interested in this event
|
177 |
for (int i = listeners.length-2; i>=0; i-=2) { |
178 |
if (listeners[i]==TreeModelListener.class) { |
179 |
// Lazily create the event:
|
180 |
if (e == null) |
181 |
e = new TreeModelEvent(source, path, |
182 |
childIndices, children); |
183 |
((TreeModelListener)listeners[i+1]).treeNodesInserted(e); |
184 |
} |
185 |
} |
186 |
} |
187 |
|
188 |
/*
|
189 |
* Notify all listeners that have registered interest for
|
190 |
* notification on this event type. The event instance
|
191 |
* is lazily created using the parameters passed into
|
192 |
* the fire method.
|
193 |
* @see EventListenerList
|
194 |
*/
|
195 |
protected void fireTreeNodesRemoved(Object source, Object[] path, |
196 |
int[] childIndices, |
197 |
Object[] children) { |
198 |
// Guaranteed to return a non-null array
|
199 |
Object[] listeners = listenerList.getListenerList(); |
200 |
TreeModelEvent e = null; |
201 |
// Process the listeners last to first, notifying
|
202 |
// those that are interested in this event
|
203 |
for (int i = listeners.length-2; i>=0; i-=2) { |
204 |
if (listeners[i]==TreeModelListener.class) { |
205 |
// Lazily create the event:
|
206 |
if (e == null) |
207 |
e = new TreeModelEvent(source, path, |
208 |
childIndices, children); |
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 |
// Guaranteed to return a non-null array
|
225 |
Object[] listeners = listenerList.getListenerList(); |
226 |
TreeModelEvent e = null; |
227 |
// Process the listeners last to first, notifying
|
228 |
// those that are interested in this event
|
229 |
for (int i = listeners.length-2; i>=0; i-=2) { |
230 |
if (listeners[i]==TreeModelListener.class) { |
231 |
// Lazily create the event:
|
232 |
if (e == null) |
233 |
e = new TreeModelEvent(source, path, |
234 |
childIndices, children); |
235 |
((TreeModelListener)listeners[i+1]).treeStructureChanged(e); |
236 |
} |
237 |
} |
238 |
} |
239 |
|
240 |
//
|
241 |
// Default impelmentations for methods in the TreeTableModel interface.
|
242 |
//
|
243 |
|
244 |
public Class getColumnClass(int column) { return Object.class; } |
245 |
|
246 |
/** By default, make the column with the Tree in it the only editable one.
|
247 |
* Making this column editable causes the JTable to forward mouse
|
248 |
* and keyboard events in the Tree column to the underlying JTree.
|
249 |
*/
|
250 |
public boolean isCellEditable(Object node, int column) { |
251 |
return getColumnClass(column) == TreeTableModel.class;
|
252 |
} |
253 |
|
254 |
public void setValueAt(Object aValue, Object node, int column) {} |
255 |
} |
256 |
|