Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUI / src / org / gvsig / gui / beans / swing / treeTable / AbstractTreeTableModel.java @ 8238

History | View | Annotate | Download (7.38 KB)

1
package org.gvsig.gui.beans.swing.treeTable;
2

    
3
import javax.swing.event.EventListenerList;
4
import javax.swing.event.TreeModelEvent;
5
import javax.swing.event.TreeModelListener;
6
import javax.swing.tree.TreePath;
7

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

    
256
                /**
257
                 * @return Returns the leafs.
258
                 */
259
                public Object[] getChildren() {
260
                        return leafs;
261
                }
262
        }
263
        
264
}
265