Statistics
| Revision:

root / trunk / extensions / extGraph / src / org / gvsig / graph / LoadDefaultNetworkExtension.java @ 39203

History | View | Annotate | Download (9.54 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 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

    
50
import org.gvsig.exceptions.BaseException;
51
import org.gvsig.graph.core.IGraph;
52
import org.gvsig.graph.core.Network;
53
import org.gvsig.graph.core.NetworkUtils;
54
import org.gvsig.graph.core.loaders.NetworkLoader;
55
import org.gvsig.graph.core.loaders.NetworkRedLoader;
56

    
57
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
58
import com.iver.andami.PluginServices;
59
import com.iver.andami.messages.NotificationManager;
60
import com.iver.andami.plugins.Extension;
61
import com.iver.andami.ui.mdiManager.IWindow;
62
import com.iver.cit.gvsig.ProjectExtension;
63
import com.iver.cit.gvsig.fmap.MapContext;
64
import com.iver.cit.gvsig.fmap.MapControl;
65
import com.iver.cit.gvsig.fmap.core.FShape;
66
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
67
import com.iver.cit.gvsig.fmap.layers.FLayer;
68
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
69
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
70
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
71
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
72
import com.iver.cit.gvsig.project.documents.view.IProjectView;
73
import com.iver.cit.gvsig.project.documents.view.gui.IView;
74
import com.iver.cit.gvsig.project.documents.view.gui.View;
75

    
76
public class LoadDefaultNetworkExtension extends Extension {
77

    
78
        public void initialize() {
79
                PluginServices.getIconTheme().registerDefault(
80
                                "network",
81
                                this.getClass().getClassLoader().getResource("images/network.png")
82
                        );                
83

    
84
        }
85

    
86
        public void execute(String actionCommand) {
87
                IView view = (View) PluginServices.getMDIManager().getActiveWindow();
88
                MapControl mapControl = view.getMapControl();
89
                MapContext map = mapControl.getMapContext();
90
                SingleLayerIterator lyrIterator = new SingleLayerIterator(map
91
                                .getLayers());
92
                while (lyrIterator.hasNext()) {
93
                        FLayer lyr = lyrIterator.next();
94
                        if ((lyr.isActive()) && (lyr instanceof FLyrVect))
95
                        {
96
                                FLyrVect lyrVect = (FLyrVect) lyr;
97
                                int shapeType;
98
                                try {
99
                                        shapeType = lyrVect.getShapeType();
100
                                        if ((shapeType & FShape.LINE) == FShape.LINE) 
101
//                                                if (shapeType == FShape.LINE)
102
                                        {
103
                                                if (actionCommand.equalsIgnoreCase("LOAD_NET")) {
104
                                                        File netFile = NetworkUtils.getNetworkFile(lyrVect);
105
                                                        loadNetwork(lyrVect, netFile);
106
                                                        return;
107
                                                }
108
//                                                if (actionCommand.equalsIgnoreCase("LOAD_NET_FROM_FILE")) {
109
//                                                        String curDir = System.getProperty("user.dir");
110
//
111
//                                                        JFileChooser fileChooser = new JFileChooser("NET_FILES", new File(curDir));
112
//                                                        fileChooser.setFileFilter(new FileFilter() {
113
//
114
//                                                                @Override
115
//                                                                public boolean accept(File f) {
116
//                                                                        String path = f.getPath().toLowerCase();
117
//                                                                        if (path.endsWith(".net"))
118
//                                                                                return true;
119
//                                                                        return false;
120
//                                                                }
121
//
122
//                                                                @Override
123
//                                                                public String getDescription() {
124
//                                                                        return ".net files";
125
//                                                                }
126
//                                                                
127
//                                                        });
128
//                                                        int res = fileChooser.showOpenDialog((Component) PluginServices.getMainFrame());
129
//                                                        if (res==JFileChooser.APPROVE_OPTION) {
130
//                                                                File netFile =fileChooser.getSelectedFile();
131
//                                                                loadNetwork(lyrVect, netFile);
132
//                                                        }
133
//                                                        
134
//                                                        return;
135
//                                                }
136
                                                
137
                                        }
138
                                } catch (BaseException e) {
139
                                        e.printStackTrace();
140
                                        NotificationManager.addError(e);
141
                                }
142

    
143
                        }
144
                }
145

    
146

    
147
        }
148

    
149
        /**
150
         * Suponemos que en el proyecto hay 2 tablas, una con los nodos
151
         * y otro con los edges.
152
         * Cargamos la red a partir de esas tablas y se la
153
         * asociamos a la capa. A partir de ah?, nuestras
154
         * herramientas pueden ver si la capa activa tiene
155
         * asociada o no una red y ponerse visibles / invisibles
156
         * Otra posible soluci?n es llevar nuestra propia lista de capas
157
         * con red (que ser? peque?ita), y as?, en lugar de recorrer
158
         * el MapContext, recorremos nuestra lista para ver la
159
         * capa que est? activa y con red. Me empieza a preocupar
160
         * que todas las herramientas iteren por la colecci?n de
161
         * capas para habilitarse/deshabilitarse:
162
         * 100 herramientas * 100 capas = 10.000 comprobaciones
163
         * Si comprobar algo cuesta 1 mseg => 10 segundos!!!
164
         * @param lyrVect
165
         * @throws ReadDriverException 
166
         */
167
        private void loadNetworkFromTables(FLyrVect lyrVect) throws ReadDriverException {
168
                // Aqu? mostrar un di?lgo para seleccionar las tablas
169
                // de nodos y edges
170
                // y hacer un mapping (si es necesario) entre los
171
                // nombres de campos
172
                String tableNodes = "Nodes";
173
                String tableEdges = "Edges";
174

    
175
                ProjectExtension projectExt = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
176

    
177
                ProjectTable ptNodes = projectExt.getProject().getTable(tableNodes);
178
                ProjectTable ptEdges = projectExt.getProject().getTable(tableEdges);
179

    
180
                SelectableDataSource sdsNodes = ptNodes.getModelo().getRecordset();
181

    
182

    
183
                SelectableDataSource sdsEdges = ptEdges.getModelo().getRecordset();
184

    
185
                NetworkLoader netLoader = new NetworkLoader(true);
186

    
187
                netLoader.setNodeReader(sdsNodes);
188
                netLoader.setEdgeReader(sdsEdges);
189

    
190
                IGraph g = netLoader.loadNetwork();
191

    
192
                System.out.println("Num nodos=" + g.numVertices() + " numEdges = " + g.numEdges());
193

    
194
                lyrVect.setProperty("network", g);
195

    
196
        }
197
        public void loadNetwork(FLyrVect lyrVect, File netFile) throws BaseException {
198
                // Aqu? mostrar un di?lgo para seleccionar las tablas
199
                // de nodos y edges
200
                // y hacer un mapping (si es necesario) entre los
201
                // nombres de campos
202

    
203
                // TODO: MOSTRAR UN CUADRO DE DI?LOGO CON UN COMBOBOX PARA QUE ESCOJA EL CAMPO DE NOMBRE DE CALLE.
204
                ArrayList aux = new ArrayList();
205
                FieldDescription[] fields = lyrVect.getRecordset().getFieldsDescription();
206
                for (int i=0; i<fields.length; i++)
207
                {
208
                        if ((fields[i].getFieldType() == Types.VARCHAR) || (fields[i].getFieldType() == Types.LONGVARCHAR))
209
                        {
210
                                aux.add(fields[i].getFieldName());
211
                        }
212
                }
213
                String fieldStreetName = (String) JOptionPane.showInputDialog((Component) PluginServices.getMainFrame(),
214
                                PluginServices.getText(this, "select_street_route_field_name"),
215
                                "gvSIG",
216
                                JOptionPane.QUESTION_MESSAGE, 
217
                                null,
218
                                (Object[]) aux.toArray(new String[0]), 
219
                                "NOMBRE");
220
                
221
                if (fieldStreetName == null)
222
                        return;
223

    
224
                
225
                NetworkRedLoader netLoader = new NetworkRedLoader();
226
                
227
                netLoader.setNetFile(netFile);
228

    
229
                IGraph g = netLoader.loadNetwork();
230
                
231
                System.out.println("Num nodos=" + g.numVertices() + " numEdges = " + g.numEdges());
232

    
233
                Network net = new Network();
234
                // lyrVect.createSpatialIndex();
235
                net.setGraph(g);
236
                net.setLayer(lyrVect);
237
//                ShortestPathExtension.solver.setNetwork(net);
238
//                ShortestPathExtension.solver.setFielStreetName(fieldStreetName);
239

    
240
                lyrVect.setProperty("network", net);
241
                lyrVect.setProperty("network_fieldStreetName", fieldStreetName);
242

    
243
        }
244

    
245
        public boolean isEnabled() {
246
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
247

    
248
                if (f == null) {
249
                        return false;
250
                }
251

    
252
                if (f instanceof View) {
253
                        View vista = (View) f;
254
                        IProjectView model = vista.getModel();
255
                        MapContext mapa = model.getMapContext();
256
                        FLayer[] activeLayers = mapa.getLayers().getActives();
257
                        if (activeLayers.length > 0)
258
                                if (activeLayers[0] instanceof FLyrVect){
259
                                        FLyrVect lyrVect = (FLyrVect) activeLayers[0];
260
                                        File netFile = NetworkUtils.getNetworkFile(lyrVect);
261
                                        if (netFile.exists())
262
                                                return true;
263

    
264
                                }
265
                }
266
                return false;
267
        }
268

    
269
        public boolean isVisible() {
270
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
271

    
272
                if (f == null) {
273
                        return false;
274
                }
275

    
276
                if (f instanceof View) {
277
                        View vista = (View) f;
278
                        IProjectView model = vista.getModel();
279
                        MapContext mapa = model.getMapContext();
280
                        FLayer[] activeLayers = mapa.getLayers().getActives();
281
                        if (activeLayers.length > 0)
282
                                if (activeLayers[0] instanceof FLyrVect){
283
                                        FLyrVect lyrVect = (FLyrVect) activeLayers[0];
284
                                        int shapeType ;
285
                                        try {
286
                                                if (!lyrVect.isAvailable())
287
                                                        return false;
288
                                                
289
                                                shapeType = lyrVect.getShapeType();
290
//                                                        if (shapeType == FShape.LINE)
291
                                                if ((shapeType & FShape.LINE) == FShape.LINE) 
292
                                                        return true;
293
                                        } catch (ReadDriverException e) {
294
                                                // TODO Auto-generated catch block
295
                                                e.printStackTrace();
296
                                        }
297
                                }        
298
                }
299
                return false;
300

    
301
        }
302

    
303

    
304
}