svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / LoadDefaultNetworkExtension.java @ 29994
History | View | Annotate | Download (9.52 KB)
1 | 27601 | fpenarrubia | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | package org.gvsig.graph; |
||
42 | |||
43 | import java.awt.Component; |
||
44 | import java.io.File; |
||
45 | import java.sql.Types; |
||
46 | import java.util.ArrayList; |
||
47 | |||
48 | import javax.swing.JOptionPane; |
||
49 | import javax.swing.filechooser.FileFilter; |
||
50 | |||
51 | import org.gvsig.exceptions.BaseException; |
||
52 | import org.gvsig.graph.core.IGraph; |
||
53 | import org.gvsig.graph.core.Network; |
||
54 | import org.gvsig.graph.core.NetworkUtils; |
||
55 | import org.gvsig.graph.core.loaders.NetworkLoader; |
||
56 | import org.gvsig.graph.core.loaders.NetworkRedLoader; |
||
57 | import org.gvsig.gui.beans.swing.JFileChooser; |
||
58 | |||
59 | import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
||
60 | import com.iver.andami.PluginServices; |
||
61 | import com.iver.andami.messages.NotificationManager; |
||
62 | import com.iver.andami.plugins.Extension; |
||
63 | import com.iver.andami.ui.mdiManager.IWindow; |
||
64 | import com.iver.cit.gvsig.ProjectExtension; |
||
65 | import com.iver.cit.gvsig.fmap.MapContext; |
||
66 | import com.iver.cit.gvsig.fmap.MapControl; |
||
67 | import com.iver.cit.gvsig.fmap.core.FShape; |
||
68 | import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
||
69 | import com.iver.cit.gvsig.fmap.layers.FLayer; |
||
70 | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
||
71 | import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
||
72 | import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator; |
||
73 | import com.iver.cit.gvsig.project.documents.table.ProjectTable; |
||
74 | import com.iver.cit.gvsig.project.documents.view.IProjectView; |
||
75 | import com.iver.cit.gvsig.project.documents.view.gui.IView; |
||
76 | import com.iver.cit.gvsig.project.documents.view.gui.View; |
||
77 | |||
78 | public class LoadDefaultNetworkExtension extends Extension { |
||
79 | |||
80 | public void initialize() { |
||
81 | PluginServices.getIconTheme().registerDefault( |
||
82 | "network",
|
||
83 | this.getClass().getClassLoader().getResource("images/network.png") |
||
84 | ); |
||
85 | |||
86 | } |
||
87 | |||
88 | public void execute(String actionCommand) { |
||
89 | IView view = (View) PluginServices.getMDIManager().getActiveWindow();
|
||
90 | MapControl mapControl = view.getMapControl(); |
||
91 | MapContext map = mapControl.getMapContext(); |
||
92 | SingleLayerIterator lyrIterator = new SingleLayerIterator(map
|
||
93 | .getLayers()); |
||
94 | while (lyrIterator.hasNext()) {
|
||
95 | FLayer lyr = lyrIterator.next(); |
||
96 | if ((lyr.isActive()) && (lyr instanceof FLyrVect)) |
||
97 | { |
||
98 | FLyrVect lyrVect = (FLyrVect) lyr; |
||
99 | int shapeType;
|
||
100 | try {
|
||
101 | shapeType = lyrVect.getShapeType(); |
||
102 | if ((shapeType & FShape.LINE) == FShape.LINE)
|
||
103 | // if (shapeType == FShape.LINE)
|
||
104 | { |
||
105 | if (actionCommand.equalsIgnoreCase("LOAD_NET")) { |
||
106 | File netFile = NetworkUtils.getNetworkFile(lyrVect);
|
||
107 | loadNetwork(lyrVect, netFile); |
||
108 | return;
|
||
109 | } |
||
110 | // if (actionCommand.equalsIgnoreCase("LOAD_NET_FROM_FILE")) {
|
||
111 | // String curDir = System.getProperty("user.dir");
|
||
112 | //
|
||
113 | // JFileChooser fileChooser = new JFileChooser("NET_FILES", new File(curDir));
|
||
114 | // fileChooser.setFileFilter(new FileFilter() {
|
||
115 | //
|
||
116 | // @Override
|
||
117 | // public boolean accept(File f) {
|
||
118 | // String path = f.getPath().toLowerCase();
|
||
119 | // if (path.endsWith(".net"))
|
||
120 | // return true;
|
||
121 | // return false;
|
||
122 | // }
|
||
123 | //
|
||
124 | // @Override
|
||
125 | // public String getDescription() {
|
||
126 | // return ".net files";
|
||
127 | // }
|
||
128 | //
|
||
129 | // });
|
||
130 | // int res = fileChooser.showOpenDialog((Component) PluginServices.getMainFrame());
|
||
131 | // if (res==JFileChooser.APPROVE_OPTION) {
|
||
132 | // File netFile =fileChooser.getSelectedFile();
|
||
133 | // loadNetwork(lyrVect, netFile);
|
||
134 | // }
|
||
135 | //
|
||
136 | // return;
|
||
137 | // }
|
||
138 | |||
139 | } |
||
140 | } catch (BaseException e) {
|
||
141 | e.printStackTrace(); |
||
142 | NotificationManager.addError(e); |
||
143 | } |
||
144 | |||
145 | } |
||
146 | } |
||
147 | |||
148 | |||
149 | } |
||
150 | |||
151 | /**
|
||
152 | * Suponemos que en el proyecto hay 2 tablas, una con los nodos
|
||
153 | * y otro con los edges.
|
||
154 | * Cargamos la red a partir de esas tablas y se la
|
||
155 | * asociamos a la capa. A partir de ah?, nuestras
|
||
156 | * herramientas pueden ver si la capa activa tiene
|
||
157 | * asociada o no una red y ponerse visibles / invisibles
|
||
158 | * Otra posible soluci?n es llevar nuestra propia lista de capas
|
||
159 | * con red (que ser? peque?ita), y as?, en lugar de recorrer
|
||
160 | * el MapContext, recorremos nuestra lista para ver la
|
||
161 | * capa que est? activa y con red. Me empieza a preocupar
|
||
162 | * que todas las herramientas iteren por la colecci?n de
|
||
163 | * capas para habilitarse/deshabilitarse:
|
||
164 | * 100 herramientas * 100 capas = 10.000 comprobaciones
|
||
165 | * Si comprobar algo cuesta 1 mseg => 10 segundos!!!
|
||
166 | * @param lyrVect
|
||
167 | * @throws ReadDriverException
|
||
168 | */
|
||
169 | private void loadNetworkFromTables(FLyrVect lyrVect) throws ReadDriverException { |
||
170 | // Aqu? mostrar un di?lgo para seleccionar las tablas
|
||
171 | // de nodos y edges
|
||
172 | // y hacer un mapping (si es necesario) entre los
|
||
173 | // nombres de campos
|
||
174 | String tableNodes = "Nodes"; |
||
175 | String tableEdges = "Edges"; |
||
176 | |||
177 | ProjectExtension projectExt = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class); |
||
178 | |||
179 | ProjectTable ptNodes = projectExt.getProject().getTable(tableNodes); |
||
180 | ProjectTable ptEdges = projectExt.getProject().getTable(tableEdges); |
||
181 | |||
182 | SelectableDataSource sdsNodes = ptNodes.getModelo().getRecordset(); |
||
183 | |||
184 | |||
185 | SelectableDataSource sdsEdges = ptEdges.getModelo().getRecordset(); |
||
186 | |||
187 | NetworkLoader netLoader = new NetworkLoader(true); |
||
188 | |||
189 | netLoader.setNodeReader(sdsNodes); |
||
190 | netLoader.setEdgeReader(sdsEdges); |
||
191 | |||
192 | IGraph g = netLoader.loadNetwork(); |
||
193 | |||
194 | System.out.println("Num nodos=" + g.numVertices() + " numEdges = " + g.numEdges()); |
||
195 | |||
196 | lyrVect.setProperty("network", g);
|
||
197 | |||
198 | } |
||
199 | private void loadNetwork(FLyrVect lyrVect, File netFile) throws BaseException { |
||
200 | // Aqu? mostrar un di?lgo para seleccionar las tablas
|
||
201 | // de nodos y edges
|
||
202 | // y hacer un mapping (si es necesario) entre los
|
||
203 | // nombres de campos
|
||
204 | |||
205 | // TODO: MOSTRAR UN CUADRO DE DI?LOGO CON UN COMBOBOX PARA QUE ESCOJA EL CAMPO DE NOMBRE DE CALLE.
|
||
206 | ArrayList aux = new ArrayList(); |
||
207 | FieldDescription[] fields = lyrVect.getRecordset().getFieldsDescription();
|
||
208 | for (int i=0; i<fields.length; i++) |
||
209 | { |
||
210 | if (fields[i].getFieldType() == Types.VARCHAR) |
||
211 | { |
||
212 | aux.add(fields[i].getFieldName()); |
||
213 | } |
||
214 | } |
||
215 | String fieldStreetName = (String) JOptionPane.showInputDialog((Component) PluginServices.getMainFrame(), |
||
216 | PluginServices.getText(this, "select_street_route_field_name"), |
||
217 | "gvSIG",
|
||
218 | JOptionPane.QUESTION_MESSAGE,
|
||
219 | null,
|
||
220 | (Object[]) aux.toArray(new String[0]), |
||
221 | "NOMBRE");
|
||
222 | |||
223 | if (fieldStreetName == null) |
||
224 | return;
|
||
225 | |||
226 | |||
227 | NetworkRedLoader netLoader = new NetworkRedLoader();
|
||
228 | |||
229 | netLoader.setNetFile(netFile); |
||
230 | |||
231 | IGraph g = netLoader.loadNetwork(); |
||
232 | |||
233 | System.out.println("Num nodos=" + g.numVertices() + " numEdges = " + g.numEdges()); |
||
234 | |||
235 | Network net = new Network();
|
||
236 | // lyrVect.createSpatialIndex();
|
||
237 | net.setGraph(g); |
||
238 | net.setLayer(lyrVect); |
||
239 | 28552 | fpenarrubia | // ShortestPathExtension.solver.setNetwork(net);
|
240 | // ShortestPathExtension.solver.setFielStreetName(fieldStreetName);
|
||
241 | 27601 | fpenarrubia | |
242 | lyrVect.setProperty("network", net);
|
||
243 | 28552 | fpenarrubia | lyrVect.setProperty("network_fieldStreetName", fieldStreetName);
|
244 | 27601 | fpenarrubia | |
245 | } |
||
246 | |||
247 | public boolean isEnabled() { |
||
248 | IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
||
249 | |||
250 | if (f == null) { |
||
251 | return false; |
||
252 | } |
||
253 | |||
254 | if (f instanceof View) { |
||
255 | View vista = (View) f; |
||
256 | IProjectView model = vista.getModel(); |
||
257 | MapContext mapa = model.getMapContext(); |
||
258 | FLayer[] activeLayers = mapa.getLayers().getActives();
|
||
259 | if (activeLayers.length > 0) |
||
260 | if (activeLayers[0] instanceof FLyrVect){ |
||
261 | FLyrVect lyrVect = (FLyrVect) activeLayers[0];
|
||
262 | File netFile = NetworkUtils.getNetworkFile(lyrVect);
|
||
263 | if (netFile.exists())
|
||
264 | return true; |
||
265 | |||
266 | } |
||
267 | } |
||
268 | return false; |
||
269 | } |
||
270 | |||
271 | public boolean isVisible() { |
||
272 | IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
||
273 | |||
274 | if (f == null) { |
||
275 | return false; |
||
276 | } |
||
277 | |||
278 | if (f instanceof View) { |
||
279 | View vista = (View) f; |
||
280 | IProjectView model = vista.getModel(); |
||
281 | MapContext mapa = model.getMapContext(); |
||
282 | FLayer[] activeLayers = mapa.getLayers().getActives();
|
||
283 | if (activeLayers.length > 0) |
||
284 | if (activeLayers[0] instanceof FLyrVect){ |
||
285 | FLyrVect lyrVect = (FLyrVect) activeLayers[0];
|
||
286 | int shapeType ;
|
||
287 | try {
|
||
288 | shapeType = lyrVect.getShapeType(); |
||
289 | // if (shapeType == FShape.LINE)
|
||
290 | if ((shapeType & FShape.LINE) == FShape.LINE)
|
||
291 | return true; |
||
292 | } catch (ReadDriverException e) {
|
||
293 | // TODO Auto-generated catch block
|
||
294 | e.printStackTrace(); |
||
295 | } |
||
296 | } |
||
297 | } |
||
298 | return false; |
||
299 | |||
300 | } |
||
301 | |||
302 | |||
303 | } |