Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1010 / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / manager / GeoprocessTree.java @ 12804

History | View | Annotate | Download (8.51 KB)

1 5999 azabala
/*
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$
47
 * $Log$
48 6490 azabala
 * Revision 1.4  2006-07-21 09:32:01  azabala
49
 * fixed bug 644: disabling ok button untill user select a geoprocess
50
 *
51
 * Revision 1.3  2006/06/27 16:14:29  azabala
52 6057 azabala
 * added geoprocess panel opening with user mouse interaction
53
 *
54
 * Revision 1.2  2006/06/23 19:04:23  azabala
55 6019 azabala
 * bug for tree creation by namespacies resolved
56
 *
57
 * Revision 1.1  2006/06/22 17:46:30  azabala
58 5999 azabala
 * first version in cvs
59
 *
60
 *
61
 */
62
package com.iver.cit.gvsig.geoprocess.manager;
63
64 6057 azabala
import java.awt.event.MouseListener;
65 5999 azabala
import java.util.Iterator;
66
67
import javax.swing.JFrame;
68
import javax.swing.JScrollPane;
69
import javax.swing.JTree;
70
import javax.swing.event.TreeSelectionListener;
71
import javax.swing.tree.DefaultMutableTreeNode;
72
import javax.swing.tree.TreeSelectionModel;
73
74 6057 azabala
import com.iver.andami.PluginServices;
75 6019 azabala
import com.iver.cit.gvsig.geoprocess.core.IGeoprocessPlugin;
76
import com.iver.cit.gvsig.geoprocess.impl.buffer.BufferGeoprocessPlugin;
77
import com.iver.cit.gvsig.geoprocess.impl.clip.ClipGeoprocessPlugin;
78 5999 azabala
import com.iver.utiles.extensionPoints.ExtensionPoint;
79
import com.iver.utiles.extensionPoints.ExtensionPoints;
80
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
81
82
/**
83
 * This component shows all registered geoprocesses in extension point
84
 * "GeoprocessManager", in a tree style.
85
 *
86 6490 azabala
 * Its different subnodes represents an organization
87 5999 azabala
 * of geoprocesses.
88
 *
89 6019 azabala
 * Leaf nodes are IGeoprocessPlugin instances.
90 5999 azabala
 *
91
 * @author azabala
92
 *
93
 */
94
public class GeoprocessTree extends JScrollPane implements IGeoprocessTree {
95 6057 azabala
        private static final long serialVersionUID = -6244491453178280294L;
96 5999 azabala
        private JTree tree;
97
        DefaultMutableTreeNode root;
98
        final  GeoprocessTreeDirectory ROOT = new GeoprocessTreeDirectory();
99
100
        public GeoprocessTree() {
101
                super();
102
                root = new DefaultMutableTreeNode();
103 6057 azabala
                ROOT.description = "";
104 5999 azabala
                root.setUserObject(ROOT);
105
                tree = new JTree(root);
106
                loadGeoprocesses();
107
                tree.getSelectionModel().setSelectionMode(
108
                                TreeSelectionModel.SINGLE_TREE_SELECTION);
109
//                tree.setRootVisible(false);
110
                tree.setShowsRootHandles(true);
111
                setViewportView(tree);
112
                // tree.setCellRenderer( new JTreeEntidadesRenderer(listeners) );
113
        }
114
115
        public static void main(String[] args){
116
                JFrame f = new JFrame();
117
                ExtensionPoints extensionPoints =
118
                        ExtensionPointsSingleton.getInstance();
119 6019 azabala
                extensionPoints.add("GeoprocessManager","BUFFER", BufferGeoprocessPlugin.class);
120
                extensionPoints.add("GeoprocessManager","CLIP", ClipGeoprocessPlugin.class);
121
                GeoprocessManager tree = new GeoprocessManager();
122 5999 azabala
                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 6019 azabala
                                register((IGeoprocessPlugin)metadataClass.newInstance());
141 5999 azabala
                        } 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 6490 azabala
        /**
152
         * Registers a new geoprocess (linked to the plugin)
153
         * in the geoprocess manager.
154
         */
155 6019 azabala
        public void register(IGeoprocessPlugin metadata) {
156 5999 azabala
                String namespace = metadata.getNamespace();
157
                String[] directories = getObjectsInPath(namespace);
158
                DefaultMutableTreeNode bestMatch = root;
159
                DefaultMutableTreeNode scanned = null;
160
                int levelNum = 0;
161
                boolean doit = true;
162
                while(doit){
163
                        int numChilds = bestMatch.getChildCount();
164
                        if(numChilds == 0)
165
                                break;
166
                        boolean match = true;
167
                        for(int i = 0; i < numChilds; i++){
168 6057 azabala
                                match = true;//is true if we dont verify is false
169 5999 azabala
                                scanned =
170
                                        (DefaultMutableTreeNode) bestMatch.getChildAt(i);
171
                                if(scanned.isLeaf() ||
172 6019 azabala
                                                (scanned.getUserObject() instanceof IGeoprocessPlugin))//this is a geoprocess, not a directory
173 5999 azabala
                                {
174
                                        doit = false;
175
                                        if(scanned.getUserObject().getClass() == metadata.getClass()){
176
                                                //we are trying to add the same geoprocess twice
177
                                                return;
178
                                        }
179
                                        break;
180
                                }
181
182
                                GeoprocessTreeDirectory path =
183
                                        (GeoprocessTreeDirectory) scanned.getUserObject();
184
                                String[] pathStr = path.getPath();
185
                                //verify the length of the path
186
                                for(int j = 0; j < pathStr.length; j++){
187
                                        if(!pathStr[j].equalsIgnoreCase(directories[j])){
188
                                                match = false;
189
                                                break;
190
                                        }
191
                                }//for num paths
192 6019 azabala
                                if(match){
193 5999 azabala
                                        bestMatch = scanned;
194 6019 azabala
                                        break;
195
                                }
196 5999 azabala
                        }//for numChilds
197
198
                        if(! match)//si en el escaneo de nivel no se encontro nada, paramos
199
                                doit = false;
200
                        else
201
                                levelNum++;
202
                }//while
203
204
                //Llegados a este punto, tenemos el nodo que mejor casa
205
                //si el numero de niveles recorridos es directories.length -1
206
                //lo a?adimos directamente. Si no, hay que crear nuevos niveles
207
                DefaultMutableTreeNode gpNode
208
                        = new DefaultMutableTreeNode();
209
                gpNode.setUserObject(metadata);
210
                if(levelNum == directories.length -1){
211
                        bestMatch.add(gpNode);
212
                }else{
213
                        int numNewNodes = (directories.length - 1) - levelNum;
214
                        DefaultMutableTreeNode prev = bestMatch;
215
                        for(int i = 0; i < numNewNodes; i++){
216
                                DefaultMutableTreeNode newNode =
217
                                        new DefaultMutableTreeNode();
218
                                GeoprocessTreeDirectory path
219
                                        = new GeoprocessTreeDirectory();
220
                                String[] newPath = new String[levelNum + i + 1];
221
                                System.arraycopy(directories, 0, newPath, 0, (levelNum + i + 1));
222
                                path.path = newPath;
223
                                newNode.setUserObject(path);
224
                                String packageName = "";
225
                                for(int j = 0; j < newPath.length -1; j++){
226
                                        packageName += newPath[j];
227
                                        packageName += "/";
228
229
                                }
230
                                packageName += newPath[newPath.length -1];
231 6057 azabala
                                String description = GeoprocessManager.
232
                                        getDescriptionFor(packageName);
233 5999 azabala
                                path.description = description;
234
                                prev.add(newNode);
235
                                prev = newNode;
236
                        }//for
237
                        prev.add(gpNode);
238
                }//else
239
240
        }
241
242
243
244 6019 azabala
        public IGeoprocessPlugin getGeoprocess() {
245 5999 azabala
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
246
                                .getLastSelectedPathComponent();
247
                Object nodeInfo = node.getUserObject();
248 6019 azabala
                if ((node == null) || !(nodeInfo instanceof IGeoprocessPlugin))
249 5999 azabala
                        return null;
250
                else {
251 6019 azabala
                        return (IGeoprocessPlugin)nodeInfo;
252 5999 azabala
                }
253
        }
254
255
256
        /**
257
         * A directory of the geoprocesses path.
258
         *
259
         * @author azabala
260
         *
261
         */
262
        class GeoprocessTreeDirectory {
263
                String[] path;
264
265
                String description;
266
267
                public boolean equals(Object o) {
268
                        if (!(o instanceof GeoprocessTreeDirectory))
269
                                return false;
270
                        GeoprocessTreeDirectory d = (GeoprocessTreeDirectory) o;
271
                        for(int i = 0; i < path.length; i++){
272
                                if(!path[i].equalsIgnoreCase(d.path[i]))
273
                                        return false;
274
                        }
275
                        return true;
276
                }
277
278
                public String[] getPath() {
279
                        return path;
280
                }
281
282
                public String getDescription() {
283
                        return description;
284
                }
285
286
                public String toString(){
287
                        if(path != null && path.length > 0)
288
                                return path[path.length-1];
289
                        else
290 6057 azabala
                                return PluginServices.getText(this,
291
                                                        "Geoprocesos");
292 5999 azabala
                }
293
        }
294
295
        private String[] getObjectsInPath(String path) {
296
                return path.split(PATH_SEPARATOR);
297
        }
298
299 6019 azabala
        public void addTreeSelectionListener(TreeSelectionListener l) {
300
                tree.addTreeSelectionListener(l);
301
        }
302 6057 azabala
303
        public void addMouseListener(MouseListener l){
304
                tree.addMouseListener(l);
305
        }
306 5999 azabala
}