Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / manager / GeoprocessTree.java @ 6019

History | View | Annotate | Download (8.02 KB)

1
/*
2
 * Created on 21-jun-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id: GeoprocessTree.java 6019 2006-06-23 19:04:23Z azabala $
47
 * $Log$
48
 * Revision 1.2  2006-06-23 19:04:23  azabala
49
 * bug for tree creation by namespacies resolved
50
 *
51
 * Revision 1.1  2006/06/22 17:46:30  azabala
52
 * first version in cvs
53
 *
54
 *
55
 */
56
package com.iver.cit.gvsig.geoprocess.manager;
57

    
58
import java.util.Iterator;
59

    
60
import javax.swing.JFrame;
61
import javax.swing.JScrollPane;
62
import javax.swing.JTree;
63
import javax.swing.event.TreeSelectionListener;
64
import javax.swing.tree.DefaultMutableTreeNode;
65
import javax.swing.tree.TreeSelectionModel;
66

    
67
import com.iver.cit.gvsig.geoprocess.core.IGeoprocessPlugin;
68
import com.iver.cit.gvsig.geoprocess.impl.buffer.BufferGeoprocessPlugin;
69
import com.iver.cit.gvsig.geoprocess.impl.clip.ClipGeoprocessPlugin;
70
import com.iver.utiles.extensionPoints.ExtensionPoint;
71
import com.iver.utiles.extensionPoints.ExtensionPoints;
72
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
73

    
74
/**
75
 * This component shows all registered geoprocesses in extension point
76
 * "GeoprocessManager", in a tree style.
77
 * 
78
 * Its tree hasnt root node, and different subnodes represents an organization
79
 * of geoprocesses.
80
 * 
81
 * Leaf nodes are IGeoprocessPlugin instances.
82
 * 
83
 * @author azabala
84
 * 
85
 */
86
public class GeoprocessTree extends JScrollPane implements IGeoprocessTree {
87
        private JTree tree;
88
        DefaultMutableTreeNode root;
89
        final  GeoprocessTreeDirectory ROOT = new GeoprocessTreeDirectory();
90
        
91
        public GeoprocessTree() {
92
                super();
93
                root = new DefaultMutableTreeNode();
94
                root.setUserObject(ROOT);
95
                tree = new JTree(root);
96
                loadGeoprocesses();
97
                tree.getSelectionModel().setSelectionMode(
98
                                TreeSelectionModel.SINGLE_TREE_SELECTION);
99
//                tree.setRootVisible(false);
100
                tree.setShowsRootHandles(true);
101
                setViewportView(tree);
102
                // tree.setCellRenderer( new JTreeEntidadesRenderer(listeners) );
103
        }
104
        
105
        public static void main(String[] args){
106
                JFrame f = new JFrame();
107
                //ESTO SE HARA EN EL METODO INITIALIZE DE CADA EXTENSION
108
                ExtensionPoints extensionPoints = 
109
                        ExtensionPointsSingleton.getInstance();
110
                extensionPoints.add("GeoprocessManager","BUFFER", BufferGeoprocessPlugin.class);
111
                extensionPoints.add("GeoprocessManager","CLIP", ClipGeoprocessPlugin.class);
112
                //REVISAR ESTO ULTIMO, PUES EST? PETANDO
113
//                GeoprocessTree tree = new GeoprocessTree();
114
                GeoprocessManager tree = new GeoprocessManager();
115
                f.getContentPane().add(tree);
116
                f.setSize(800,600);
117
                f.setVisible(true);
118
        }
119

    
120
        private void loadGeoprocesses() {
121
                ExtensionPoints extensionPoints = 
122
                        ExtensionPointsSingleton.getInstance();
123
                ExtensionPoint geoprocessManager = 
124
                        (ExtensionPoint)extensionPoints.get("GeoprocessManager"); 
125
                if(geoprocessManager == null)
126
                        return;
127
                Iterator i = geoprocessManager.keySet().iterator();
128
                while( i.hasNext() ) { 
129
                        String nombre = (String)i.next(); 
130
                        Class metadataClass =
131
                                (Class) geoprocessManager.get(nombre);
132
                        try {
133
                                register((IGeoprocessPlugin)metadataClass.newInstance());
134
                        } catch (InstantiationException e) {
135
                                // TODO Auto-generated catch block
136
                                e.printStackTrace();
137
                        } catch (IllegalAccessException e) {
138
                                // TODO Auto-generated catch block
139
                                e.printStackTrace();
140
                        }
141
                }
142
        }
143

    
144
        public void register(IGeoprocessPlugin metadata) {
145
                String namespace = metadata.getNamespace();
146
                String[] directories = getObjectsInPath(namespace);
147
                DefaultMutableTreeNode bestMatch = root;
148
                DefaultMutableTreeNode scanned = null;
149
                int levelNum = 0;
150
                boolean doit = true;
151
                while(doit){
152
                        int numChilds = bestMatch.getChildCount();
153
                        if(numChilds == 0)
154
                                break;
155
                        boolean match = true;
156
                        for(int i = 0; i < numChilds; i++){
157
                                scanned =
158
                                        (DefaultMutableTreeNode) bestMatch.getChildAt(i);
159
                                if(scanned.isLeaf() || 
160
                                                (scanned.getUserObject() instanceof IGeoprocessPlugin))//this is a geoprocess, not a directory
161
                                {
162
                                        doit = false;
163
                                        if(scanned.getUserObject().getClass() == metadata.getClass()){
164
                                                //we are trying to add the same geoprocess twice
165
                                                return;
166
                                        }        
167
                                        break;
168
                                }
169
                                
170
                                GeoprocessTreeDirectory path =
171
                                        (GeoprocessTreeDirectory) scanned.getUserObject();
172
                                String[] pathStr = path.getPath();
173
                                //verify the length of the path
174
                                for(int j = 0; j < pathStr.length; j++){
175
                                        if(!pathStr[j].equalsIgnoreCase(directories[j])){
176
                                                match = false;
177
                                                break;
178
                                        }
179
                                }//for num paths
180
                                if(match){
181
                                        bestMatch = scanned;
182
                                        break;
183
                                }
184
                        }//for numChilds
185
                        
186
                        if(! match)//si en el escaneo de nivel no se encontro nada, paramos
187
                                doit = false;
188
                        else
189
                                levelNum++;
190
                }//while
191
                
192
                //Llegados a este punto, tenemos el nodo que mejor casa
193
                //si el numero de niveles recorridos es directories.length -1
194
                //lo a?adimos directamente. Si no, hay que crear nuevos niveles
195
                DefaultMutableTreeNode gpNode
196
                        = new DefaultMutableTreeNode();
197
                gpNode.setUserObject(metadata);
198
                if(levelNum == directories.length -1){
199
                        bestMatch.add(gpNode);
200
                }else{
201
                        int numNewNodes = (directories.length - 1) - levelNum;
202
                        DefaultMutableTreeNode prev = bestMatch;
203
                        for(int i = 0; i < numNewNodes; i++){
204
                                DefaultMutableTreeNode newNode = 
205
                                        new DefaultMutableTreeNode();
206
                                GeoprocessTreeDirectory path
207
                                        = new GeoprocessTreeDirectory();
208
                                String[] newPath = new String[levelNum + i + 1];
209
                                System.arraycopy(directories, 0, newPath, 0, (levelNum + i + 1));
210
                                path.path = newPath;
211
                                newNode.setUserObject(path);
212
                                String packageName = "";
213
                                for(int j = 0; j < newPath.length -1; j++){
214
                                        packageName += newPath[j];
215
                                        packageName += "/";
216
                                        
217
                                }
218
                                packageName += newPath[newPath.length -1];
219
                                String description = metadata.
220
                                        getDescriptionForPackage(packageName);
221
                                path.description = description;
222
                                prev.add(newNode);
223
                                prev = newNode;
224
                        }//for
225
                        prev.add(gpNode);
226
                }//else
227
                
228
        }
229

    
230
        
231

    
232
        public IGeoprocessPlugin getGeoprocess() {
233
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
234
                                .getLastSelectedPathComponent();
235
                Object nodeInfo = node.getUserObject();
236
                if ((node == null) || !(nodeInfo instanceof IGeoprocessPlugin))
237
                        return null;
238
                else {
239
                        return (IGeoprocessPlugin)nodeInfo;
240
                }
241
        }
242

    
243

    
244
        /**
245
         * A directory of the geoprocesses path.
246
         * 
247
         * @author azabala
248
         * 
249
         */
250
        class GeoprocessTreeDirectory {
251
                String[] path;
252

    
253
                String description;
254
                
255
                public boolean equals(Object o) {
256
                        if (!(o instanceof GeoprocessTreeDirectory))
257
                                return false;
258
                        GeoprocessTreeDirectory d = (GeoprocessTreeDirectory) o;
259
                        for(int i = 0; i < path.length; i++){
260
                                if(!path[i].equalsIgnoreCase(d.path[i]))
261
                                        return false;
262
                        }
263
                        return true;
264
                }
265

    
266
                public String[] getPath() {
267
                        return path;
268
                }
269

    
270
                public String getDescription() {
271
                        return description;
272
                }
273
                
274
                public String toString(){
275
                        if(path != null && path.length > 0)
276
                                return path[path.length-1];
277
                        else
278
                                return "GpGvSig";
279
                }
280
        }
281

    
282
        private String[] getObjectsInPath(String path) {
283
                return path.split(PATH_SEPARATOR);
284
        }
285

    
286
        public void addTreeSelectionListener(TreeSelectionListener l) {
287
                tree.addTreeSelectionListener(l);
288
        }
289
}