Statistics
| Revision:

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

History | View | Annotate | Download (7.13 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 8226 2006-10-23 08:18:39Z jorpiell $
51
 * $Log$
52
 * Revision 1.1  2006-10-23 08:18:39  jorpiell
53
 * A?adida la clase treetable
54
 *
55
 *
56
 */
57
/**
58
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
59
 */
60
public abstract class AbstractTreeTableModel implements TreeTableModel {
61
        protected Object root;     
62
        protected EventListenerList listenerList = new EventListenerList();
63
        
64
        public AbstractTreeTableModel(Object root) {
65
                if (root instanceof Object[]){
66
                        this.root = new Root((Object[])root);
67
                }else{
68
                        this.root = root; 
69
                }
70
        }
71
        
72
        //
73
        // Default implmentations for methods in the TreeModel interface. 
74
        //
75
        
76
        public Object getRoot() {
77
                return root;
78
        }
79
        
80
        public boolean isLeaf(Object node) {
81
                return getChildCount(node) == 0; 
82
        }
83
        
84
        public void valueForPathChanged(TreePath path, Object newValue) {}
85
        
86
        // This is not called in the JTree's default mode: use a naive implementation. 
87
        public int getIndexOfChild(Object parent, Object child) {
88
                for (int i = 0; i < getChildCount(parent); i++) {
89
                        if (getChild(parent, i).equals(child)) { 
90
                                return i; 
91
                        }
92
                }
93
                return -1; 
94
        }
95
        
96
        public void addTreeModelListener(TreeModelListener l) {
97
                listenerList.add(TreeModelListener.class, l);
98
        }
99
        
100
        public void removeTreeModelListener(TreeModelListener l) {
101
                listenerList.remove(TreeModelListener.class, l);
102
        }
103
        
104
        /*
105
         * Notify all listeners that have registered interest for
106
         * notification on this event type.  The event instance 
107
         * is lazily created using the parameters passed into 
108
         * the fire method.
109
         * @see EventListenerList
110
         */
111
        protected void fireTreeNodesChanged(Object source, Object[] path, 
112
                        int[] childIndices, 
113
                        Object[] children) {
114
                // Guaranteed to return a non-null array
115
                Object[] listeners = listenerList.getListenerList();
116
                TreeModelEvent e = null;
117
                // Process the listeners last to first, notifying
118
                // those that are interested in this event
119
                for (int i = listeners.length-2; i>=0; i-=2) {
120
                        if (listeners[i]==TreeModelListener.class) {
121
                                // Lazily create the event:
122
                                if (e == null)
123
                                        e = new TreeModelEvent(source, path, 
124
                                                        childIndices, children);
125
                                ((TreeModelListener)listeners[i+1]).treeNodesChanged(e);
126
                        }          
127
                }
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 fireTreeNodesInserted(Object source, Object[] path, 
138
                        int[] childIndices, 
139
                        Object[] children) {
140
                // Guaranteed to return a non-null array
141
                Object[] listeners = listenerList.getListenerList();
142
                TreeModelEvent e = null;
143
                // Process the listeners last to first, notifying
144
                // those that are interested in this event
145
                for (int i = listeners.length-2; i>=0; i-=2) {
146
                        if (listeners[i]==TreeModelListener.class) {
147
                                // Lazily create the event:
148
                                if (e == null)
149
                                        e = new TreeModelEvent(source, path, 
150
                                                        childIndices, children);
151
                                ((TreeModelListener)listeners[i+1]).treeNodesInserted(e);
152
                        }          
153
                }
154
        }
155
        
156
        /*
157
         * Notify all listeners that have registered interest for
158
         * notification on this event type.  The event instance 
159
         * is lazily created using the parameters passed into 
160
         * the fire method.
161
         * @see EventListenerList
162
         */
163
        protected void fireTreeNodesRemoved(Object source, Object[] path, 
164
                        int[] childIndices, 
165
                        Object[] children) {
166
                // Guaranteed to return a non-null array
167
                Object[] listeners = listenerList.getListenerList();
168
                TreeModelEvent e = null;
169
                // Process the listeners last to first, notifying
170
                // those that are interested in this event
171
                for (int i = listeners.length-2; i>=0; i-=2) {
172
                        if (listeners[i]==TreeModelListener.class) {
173
                                // Lazily create the event:
174
                                if (e == null)
175
                                        e = new TreeModelEvent(source, path, 
176
                                                        childIndices, children);
177
                                ((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);
178
                        }          
179
                }
180
        }
181
        
182
        /*
183
         * Notify all listeners that have registered interest for
184
         * notification on this event type.  The event instance 
185
         * is lazily created using the parameters passed into 
186
         * the fire method.
187
         * @see EventListenerList
188
         */
189
        protected void fireTreeStructureChanged(Object source, Object[] path, 
190
                        int[] childIndices, 
191
                        Object[] children) {
192
                // Guaranteed to return a non-null array
193
                Object[] listeners = listenerList.getListenerList();
194
                TreeModelEvent e = null;
195
                // Process the listeners last to first, notifying
196
                // those that are interested in this event
197
                for (int i = listeners.length-2; i>=0; i-=2) {
198
                        if (listeners[i]==TreeModelListener.class) {
199
                                // Lazily create the event:
200
                                if (e == null)
201
                                        e = new TreeModelEvent(source, path, 
202
                                                        childIndices, children);
203
                                ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
204
                        }          
205
                }
206
        }
207
        
208
        //
209
        // Default impelmentations for methods in the TreeTableModel interface. 
210
        //
211
        
212
        public Class getColumnClass(int column) { return Object.class; }
213
        
214
        /** By default, make the column with the Tree in it the only editable one. 
215
         *  Making this column editable causes the JTable to forward mouse 
216
         *  and keyboard events in the Tree column to the underlying JTree. 
217
         */ 
218
        public boolean isCellEditable(Object node, int column) { 
219
                return getColumnClass(column) == TreeTableModel.class; 
220
        }
221
        
222
        public void setValueAt(Object aValue, Object node, int column) {}
223
        
224
        /**
225
         * This class will be the root for the trees "without root"
226
         * @author Jorge Piera Llodr? (piera_jor@gva.es)
227
         *
228
         */
229
        public class Root{
230
                private Object[] leafs = null;
231
                
232
                public Root(Object[] leafs){
233
                        this.leafs = leafs;
234
                }
235
                
236
                public String toString(){
237
                        return "";
238
                }
239
                
240
                public int getChildCount(){
241
                        if (leafs == null){
242
                                return 0;
243
                        }
244
                        return leafs.length;
245
                }
246

    
247
                /**
248
                 * @return Returns the leafs.
249
                 */
250
                public Object[] getChildren() {
251
                        return leafs;
252
                }
253
        }
254
        
255
}
256