Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / manager / GeoprocessTree.java @ 6057

History | View | Annotate | Download (8.47 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 6057 2006-06-27 16:14:29Z azabala $
47
 * $Log$
48
 * Revision 1.3  2006-06-27 16:14:29  azabala
49
 * added geoprocess panel opening with user mouse interaction
50
 *
51
 * Revision 1.2  2006/06/23 19:04:23  azabala
52
 * bug for tree creation by namespacies resolved
53
 *
54
 * Revision 1.1  2006/06/22 17:46:30  azabala
55
 * first version in cvs
56
 *
57
 *
58
 */
59
package com.iver.cit.gvsig.geoprocess.manager;
60

    
61
import java.awt.event.MouseListener;
62
import java.util.Iterator;
63

    
64
import javax.swing.JFrame;
65
import javax.swing.JScrollPane;
66
import javax.swing.JTree;
67
import javax.swing.event.TreeSelectionListener;
68
import javax.swing.tree.DefaultMutableTreeNode;
69
import javax.swing.tree.TreeSelectionModel;
70

    
71
import com.iver.andami.PluginServices;
72
import com.iver.cit.gvsig.geoprocess.core.IGeoprocessPlugin;
73
import com.iver.cit.gvsig.geoprocess.impl.buffer.BufferGeoprocessPlugin;
74
import com.iver.cit.gvsig.geoprocess.impl.clip.ClipGeoprocessPlugin;
75
import com.iver.utiles.extensionPoints.ExtensionPoint;
76
import com.iver.utiles.extensionPoints.ExtensionPoints;
77
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
78

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

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

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

    
238
        
239

    
240
        public IGeoprocessPlugin getGeoprocess() {
241
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
242
                                .getLastSelectedPathComponent();
243
                Object nodeInfo = node.getUserObject();
244
                if ((node == null) || !(nodeInfo instanceof IGeoprocessPlugin))
245
                        return null;
246
                else {
247
                        return (IGeoprocessPlugin)nodeInfo;
248
                }
249
        }
250

    
251

    
252
        /**
253
         * A directory of the geoprocesses path.
254
         * 
255
         * @author azabala
256
         * 
257
         */
258
        class GeoprocessTreeDirectory {
259
                String[] path;
260

    
261
                String description;
262
                
263
                public boolean equals(Object o) {
264
                        if (!(o instanceof GeoprocessTreeDirectory))
265
                                return false;
266
                        GeoprocessTreeDirectory d = (GeoprocessTreeDirectory) o;
267
                        for(int i = 0; i < path.length; i++){
268
                                if(!path[i].equalsIgnoreCase(d.path[i]))
269
                                        return false;
270
                        }
271
                        return true;
272
                }
273

    
274
                public String[] getPath() {
275
                        return path;
276
                }
277

    
278
                public String getDescription() {
279
                        return description;
280
                }
281
                
282
                public String toString(){
283
                        if(path != null && path.length > 0)
284
                                return path[path.length-1];
285
                        else
286
                                return PluginServices.getText(this, 
287
                                                        "Geoprocesos");
288
                }
289
        }
290

    
291
        private String[] getObjectsInPath(String path) {
292
                return path.split(PATH_SEPARATOR);
293
        }
294

    
295
        public void addTreeSelectionListener(TreeSelectionListener l) {
296
                tree.addTreeSelectionListener(l);
297
        }
298
        
299
        public void addMouseListener(MouseListener l){
300
                tree.addMouseListener(l);
301
        }
302
}