root / tags / v1_0_2_Build_904 / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / manager / GeoprocessTree.java @ 10724
History | View | Annotate | Download (8.51 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 6490 2006-07-21 09:32:01Z azabala $
|
47 |
* $Log$
|
48 |
* 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 |
* added geoprocess panel opening with user mouse interaction
|
53 |
*
|
54 |
* Revision 1.2 2006/06/23 19:04:23 azabala
|
55 |
* bug for tree creation by namespacies resolved
|
56 |
*
|
57 |
* Revision 1.1 2006/06/22 17:46:30 azabala
|
58 |
* first version in cvs
|
59 |
*
|
60 |
*
|
61 |
*/
|
62 |
package com.iver.cit.gvsig.geoprocess.manager; |
63 |
|
64 |
import java.awt.event.MouseListener; |
65 |
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 |
import com.iver.andami.PluginServices; |
75 |
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 |
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 |
* Its different subnodes represents an organization
|
87 |
* of geoprocesses.
|
88 |
*
|
89 |
* Leaf nodes are IGeoprocessPlugin instances.
|
90 |
*
|
91 |
* @author azabala
|
92 |
*
|
93 |
*/
|
94 |
public class GeoprocessTree extends JScrollPane implements IGeoprocessTree { |
95 |
private static final long serialVersionUID = -6244491453178280294L; |
96 |
private JTree tree; |
97 |
DefaultMutableTreeNode root;
|
98 |
final GeoprocessTreeDirectory ROOT = new GeoprocessTreeDirectory(); |
99 |
|
100 |
public GeoprocessTree() {
|
101 |
super();
|
102 |
root = new DefaultMutableTreeNode(); |
103 |
ROOT.description = "";
|
104 |
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 |
extensionPoints.add("GeoprocessManager","BUFFER", BufferGeoprocessPlugin.class); |
120 |
extensionPoints.add("GeoprocessManager","CLIP", ClipGeoprocessPlugin.class); |
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 |
/**
|
152 |
* Registers a new geoprocess (linked to the plugin)
|
153 |
* in the geoprocess manager.
|
154 |
*/
|
155 |
public void register(IGeoprocessPlugin metadata) { |
156 |
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 |
match = true;//is true if we dont verify is false |
169 |
scanned = |
170 |
(DefaultMutableTreeNode) bestMatch.getChildAt(i);
|
171 |
if(scanned.isLeaf() ||
|
172 |
(scanned.getUserObject() instanceof IGeoprocessPlugin))//this is a geoprocess, not a directory |
173 |
{ |
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 |
if(match){
|
193 |
bestMatch = scanned; |
194 |
break;
|
195 |
} |
196 |
}//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 |
String description = GeoprocessManager.
|
232 |
getDescriptionFor(packageName); |
233 |
path.description = description; |
234 |
prev.add(newNode); |
235 |
prev = newNode; |
236 |
}//for
|
237 |
prev.add(gpNode); |
238 |
}//else
|
239 |
|
240 |
} |
241 |
|
242 |
|
243 |
|
244 |
public IGeoprocessPlugin getGeoprocess() {
|
245 |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree |
246 |
.getLastSelectedPathComponent(); |
247 |
Object nodeInfo = node.getUserObject();
|
248 |
if ((node == null) || !(nodeInfo instanceof IGeoprocessPlugin)) |
249 |
return null; |
250 |
else {
|
251 |
return (IGeoprocessPlugin)nodeInfo;
|
252 |
} |
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 |
return PluginServices.getText(this, |
291 |
"Geoprocesos");
|
292 |
} |
293 |
} |
294 |
|
295 |
private String[] getObjectsInPath(String path) { |
296 |
return path.split(PATH_SEPARATOR);
|
297 |
} |
298 |
|
299 |
public void addTreeSelectionListener(TreeSelectionListener l) { |
300 |
tree.addTreeSelectionListener(l); |
301 |
} |
302 |
|
303 |
public void addMouseListener(MouseListener l){ |
304 |
tree.addMouseListener(l); |
305 |
} |
306 |
} |