Statistics
| Revision:

root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / LoadNetworkExtension.java @ 8646

History | View | Annotate | Download (7.96 KB)

1
/* 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 com.iver.cit.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

    
50
import com.iver.andami.PluginServices;
51
import com.iver.andami.messages.NotificationManager;
52
import com.iver.andami.plugins.Extension;
53
import com.iver.andami.ui.mdiManager.IWindow;
54
import com.iver.cit.gvsig.ProjectExtension;
55
import com.iver.cit.gvsig.fmap.DriverException;
56
import com.iver.cit.gvsig.fmap.MapContext;
57
import com.iver.cit.gvsig.fmap.MapControl;
58
import com.iver.cit.gvsig.fmap.core.FShape;
59
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
60
import com.iver.cit.gvsig.fmap.layers.FLayer;
61
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
62
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
63
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
64
import com.iver.cit.gvsig.graph.core.IGraph;
65
import com.iver.cit.gvsig.graph.core.Network;
66
import com.iver.cit.gvsig.graph.core.loaders.NetworkLoader;
67
import com.iver.cit.gvsig.graph.core.loaders.NetworkRedLoader;
68
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
69
import com.iver.cit.gvsig.project.documents.view.IProjectView;
70
import com.iver.cit.gvsig.project.documents.view.gui.IView;
71
import com.iver.cit.gvsig.project.documents.view.gui.View;
72

    
73
public class LoadNetworkExtension extends Extension {
74

    
75
        public void initialize() {
76
        }
77

    
78
        public void execute(String actionCommand) {
79
                IView view = (View) PluginServices.getMDIManager().getActiveWindow();
80
                MapControl mapControl = view.getMapControl();
81
                MapContext map = mapControl.getMapContext();
82
                SingleLayerIterator lyrIterator = new SingleLayerIterator(map
83
                                .getLayers());
84
                while (lyrIterator.hasNext()) {
85
                        FLayer lyr = lyrIterator.next();
86
                        if ((lyr.isActive()) && (lyr instanceof FLyrVect))
87
                        {
88
                                FLyrVect lyrVect = (FLyrVect) lyr;
89
                                int shapeType;
90
                                try {
91
                                        shapeType = lyrVect.getShapeType();
92
                                        if (shapeType == FShape.LINE)
93
                                        {
94
                                                if (actionCommand.equalsIgnoreCase("LOAD_RED")) {
95
                                                        loadNetwork(lyrVect);
96
                                                        return;
97
                                                }
98
                                        }
99
                                } catch (DriverException e) {
100
                                        e.printStackTrace();
101
                                        NotificationManager.addError(e);
102
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
103
                                        e.printStackTrace();
104
                                        NotificationManager.addError(e);
105
                                }
106

    
107
                        }
108
                }
109

    
110

    
111
        }
112

    
113
        /**
114
         * Suponemos que en el proyecto hay 2 tablas, una con los nodos
115
         * y otro con los edges.
116
         * Cargamos la red a partir de esas tablas y se la
117
         * asociamos a la capa. A partir de ah?, nuestras
118
         * herramientas pueden ver si la capa activa tiene
119
         * asociada o no una red y ponerse visibles / invisibles
120
         * Otra posible soluci?n es llevar nuestra propia lista de capas
121
         * con red (que ser? peque?ita), y as?, en lugar de recorrer
122
         * el MapContext, recorremos nuestra lista para ver la
123
         * capa que est? activa y con red. Me empieza a preocupar
124
         * que todas las herramientas iteren por la colecci?n de
125
         * capas para habilitarse/deshabilitarse:
126
         * 100 herramientas * 100 capas = 10.000 comprobaciones
127
         * Si comprobar algo cuesta 1 mseg => 10 segundos!!!
128
         * @param lyrVect
129
         */
130
        private void loadNetworkFromTables(FLyrVect lyrVect) {
131
                // Aqu? mostrar un di?lgo para seleccionar las tablas
132
                // de nodos y edges
133
                // y hacer un mapping (si es necesario) entre los
134
                // nombres de campos
135
                String tableNodes = "Nodes";
136
                String tableEdges = "Edges";
137

    
138
                ProjectExtension projectExt = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
139

    
140
                ProjectTable ptNodes = projectExt.getProject().getTable(tableNodes);
141
                ProjectTable ptEdges = projectExt.getProject().getTable(tableEdges);
142

    
143
                SelectableDataSource sdsNodes = ptNodes.getModelo().getRecordset();
144

    
145

    
146
                SelectableDataSource sdsEdges = ptEdges.getModelo().getRecordset();
147

    
148
                NetworkLoader netLoader = new NetworkLoader(true);
149

    
150
                netLoader.setNodeReader(sdsNodes);
151
                netLoader.setEdgeReader(sdsEdges);
152

    
153
                IGraph g = netLoader.loadNetwork();
154

    
155
                System.out.println("Num nodos=" + g.numVertices() + " numEdges = " + g.numEdges());
156

    
157
                lyrVect.setProperty("network", g);
158

    
159
        }
160
        private void loadNetwork(FLyrVect lyrVect) throws com.hardcode.gdbms.engine.data.driver.DriverException, DriverException {
161
                // Aqu? mostrar un di?lgo para seleccionar las tablas
162
                // de nodos y edges
163
                // y hacer un mapping (si es necesario) entre los
164
                // nombres de campos
165

    
166
                // TODO: MOSTRAR UN CUADRO DE DI?LOGO CON UN COMBOBOX PARA QUE ESCOJA EL CAMPO DE NOMBRE DE CALLE.
167
                ArrayList aux = new ArrayList();
168
                FieldDescription[] fields = lyrVect.getRecordset().getFieldsDescription();
169
                for (int i=0; i<fields.length; i++)
170
                {
171
                        if (fields[i].getFieldType() == Types.VARCHAR)
172
                        {
173
                                aux.add(fields[i].getFieldName());
174
                        }
175
                }
176
                String fieldStreetName = (String) JOptionPane.showInputDialog((Component) PluginServices.getMainFrame(),
177
                                PluginServices.getText(this, "select_street_route_field_name"),
178
                                "gvSIG",
179
                                JOptionPane.QUESTION_MESSAGE, 
180
                                null,
181
                                (Object[]) aux.toArray(new String[0]), 
182
                                "NOMBRE");
183
                
184
                if (fieldStreetName == null)
185
                        return;
186

    
187
                
188
                NetworkRedLoader netLoader = new NetworkRedLoader();
189
                File redFile = NetworkUtils.getNetworkFile(lyrVect);
190
                netLoader.setNetFile(redFile);
191

    
192
                IGraph g = netLoader.loadNetwork();
193
                
194
                System.out.println("Num nodos=" + g.numVertices() + " numEdges = " + g.numEdges());
195

    
196
                Network net = new Network();
197
                // lyrVect.createSpatialIndex();
198
                net.setGraph(g);
199
                net.setLayer(lyrVect);
200
                ShortestPathExtension.solver.setNetwork(net);
201
                ShortestPathExtension.solver.setFielStreetName(fieldStreetName);
202

    
203
                lyrVect.setProperty("network", net);
204

    
205
        }
206

    
207
        public boolean isEnabled() {
208
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
209

    
210
                if (f == null) {
211
                        return false;
212
                }
213

    
214
                if (f instanceof View) {
215
                        View vista = (View) f;
216
                        IProjectView model = vista.getModel();
217
                        MapContext mapa = model.getMapContext();
218
                        FLayer[] activeLayers = mapa.getLayers().getActives();
219
                        if (activeLayers.length > 0)
220
                                if (activeLayers[0] instanceof FLyrVect){
221
                                        FLyrVect lyrVect = (FLyrVect) activeLayers[0];
222
                                        File netFile = NetworkUtils.getNetworkFile(lyrVect);
223
                                        if (netFile.exists())
224
                                                return true;
225

    
226
                                }
227
                }
228
                return false;
229
        }
230

    
231
        public boolean isVisible() {
232
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
233

    
234
                if (f == null) {
235
                        return false;
236
                }
237

    
238
                if (f instanceof View) {
239
                        View vista = (View) f;
240
                        IProjectView model = vista.getModel();
241
                        MapContext mapa = model.getMapContext();
242
                        FLayer[] activeLayers = mapa.getLayers().getActives();
243
                        if (activeLayers.length > 0)
244
                                if (activeLayers[0] instanceof FLyrVect){
245
                                        FLyrVect lyrVect = (FLyrVect) activeLayers[0];
246
                                        int shapeType ;
247
                                        try {
248
                                                shapeType = lyrVect.getShapeType();
249
                                                if (shapeType == FShape.LINE)
250
                                                        return true;
251
                                        } catch (DriverException e) {
252
                                                // TODO Auto-generated catch block
253
                                                e.printStackTrace();
254
                                        }
255
                                }        
256
                }
257
                return false;
258

    
259
        }
260

    
261

    
262
}