Statistics
| Revision:

root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / NetworkExtension.java @ 7882

History | View | Annotate | Download (6.64 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.io.File;
44

    
45
import javax.swing.JOptionPane;
46

    
47
import com.iver.andami.PluginServices;
48
import com.iver.andami.plugins.Extension;
49
import com.iver.andami.ui.mdiManager.IWindow;
50
import com.iver.cit.gvsig.ProjectExtension;
51
import com.iver.cit.gvsig.fmap.DriverException;
52
import com.iver.cit.gvsig.fmap.MapContext;
53
import com.iver.cit.gvsig.fmap.MapControl;
54
import com.iver.cit.gvsig.fmap.core.FShape;
55
import com.iver.cit.gvsig.fmap.edition.EditionException;
56
import com.iver.cit.gvsig.fmap.edition.writers.dbf.DbfWriter;
57
import com.iver.cit.gvsig.fmap.layers.FLayer;
58
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
59
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
60
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
61
import com.iver.cit.gvsig.graph.core.NetworkLoader;
62
import com.iver.cit.gvsig.graph.core.NetworkWriter;
63
import com.iver.cit.gvsig.project.Project;
64
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
65
import com.iver.cit.gvsig.project.documents.view.IProjectView;
66
import com.iver.cit.gvsig.project.documents.view.gui.IView;
67
import com.iver.cit.gvsig.project.documents.view.gui.View;
68

    
69
import edu.uci.ics.jung.graph.Graph;
70

    
71
public class NetworkExtension extends Extension {
72

    
73
//        private String fieldType;
74
//        private String fieldDist;
75
//        private String fieldSense;
76

    
77
        public void initialize() {
78
        }
79

    
80
        public void execute(String actionCommand) {
81
                IView view = (View) PluginServices.getMDIManager().getActiveWindow();
82
                MapControl mapControl = view.getMapControl();
83
                MapContext map = mapControl.getMapContext();
84
                SingleLayerIterator lyrIterator = new SingleLayerIterator(map
85
                                .getLayers());
86
                while (lyrIterator.hasNext()) {
87
                        FLayer lyr = lyrIterator.next();                        
88
                        if ((lyr.isActive()) && (lyr instanceof FLyrVect))
89
                        {
90
                                FLyrVect lyrVect = (FLyrVect) lyr;
91
                                int shapeType;
92
                                try {
93
                                        shapeType = lyrVect.getShapeType();
94
                                        if (shapeType == FShape.LINE)
95
                                        {
96
                                                if (actionCommand.equalsIgnoreCase("GENERAR_RED")) {
97
                                                        generateNetwork(lyrVect);
98
                                                }
99
                                                if (actionCommand.equalsIgnoreCase("LOAD_RED")) {
100
                                                        loadNetwork(lyrVect);
101
                                                }                                                
102
                                        }
103
                                } catch (DriverException e) {
104
                                        // TODO Auto-generated catch block
105
                                        e.printStackTrace();
106
                                }
107
                                        
108
                        }
109
                }
110

    
111

    
112
        }
113

    
114
        /**
115
         * Suponemos que en el proyecto hay 2 tablas, una con los nodos
116
         * y otro con los edges.
117
         * Cargamos la red a partir de esas tablas y se la 
118
         * asociamos a la capa. A partir de ah?, nuestras
119
         * herramientas pueden ver si la capa activa tiene
120
         * asociada o no una red y ponerse visibles / invisibles
121
         * Otra posible soluci?n es llevar nuestra propia lista de capas
122
         * con red (que ser? peque?ita), y as?, en lugar de recorrer
123
         * el MapContext, recorremos nuestra lista para ver la
124
         * capa que est? activa y con red. Me empieza a preocupar
125
         * que todas las herramientas iteren por la colecci?n de
126
         * capas para habilitarse/deshabilitarse:
127
         * 100 herramientas * 100 capas = 10.000 comprobaciones
128
         * Si comprobar algo cuesta 1 mseg => 10 segundos!!! 
129
         * @param lyrVect
130
         */
131
        private void loadNetwork(FLyrVect lyrVect) {
132
                // Aqu? mostrar un di?lgo para seleccionar las tablas
133
                // de nodos y edges
134
                // y hacer un mapping (si es necesario) entre los 
135
                // nombres de campos
136
                String tableNodes = "Nodes";
137
                String tableEdges = "Edges";
138
                
139
                ProjectExtension projectExt = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
140
                
141
                ProjectTable ptNodes = projectExt.getProject().getTable(tableNodes);
142
                ProjectTable ptEdges = projectExt.getProject().getTable(tableEdges);
143
                
144
                SelectableDataSource sdsNodes = ptNodes.getModelo().getRecordset();
145
                SelectableDataSource sdsEdges = ptEdges.getModelo().getRecordset();        
146
                
147
                NetworkLoader netLoader = new NetworkLoader();
148
                
149
                netLoader.setNodeReader(sdsNodes);
150
                netLoader.setEdgeReader(sdsEdges);
151
                
152
                
153
                Graph g = netLoader.loadNetwork();
154
                
155
                System.out.println("Num nodos=" + g.numVertices() + " numEdges = " + g.numEdges());
156
                
157
                lyrVect.setProperty("network", g);
158
                
159
        }
160

    
161
        private void generateNetwork(FLyrVect lyr) {
162
                NetworkWriter netBuilder = new NetworkWriter();
163
                // Por ahora, a pelo, pero hay que sacar un cuadro
164
                // de di?logo para hecer el mapping.
165
                // Tambi?n un cuadro de di?logo para seleccionar
166
                // en qu? tablas quiere escribir, y su formato
167
                // (dbf, postgres, etc)
168
                String fieldType = "tipored"; String fieldDist = "length"; String fieldSense = "sen";
169
                try {
170
                        netBuilder.setLayer(lyr, fieldType, fieldDist, fieldSense);
171
                        DbfWriter nodeWriter = new DbfWriter();
172
                        nodeWriter.setFile(new File("c:/nodes.dbf"));
173
                        
174
                        DbfWriter edgeWriter = new DbfWriter();
175
                        edgeWriter.setFile(new File("c:/edges.dbf"));
176
                        
177
                        netBuilder.setEdgeWriter(edgeWriter);
178
                        netBuilder.setNodeWriter(nodeWriter);
179
                        
180
                        netBuilder.writeNetwork();
181
                } catch (DriverException e1) {
182
                        // TODO Auto-generated catch block
183
                        e1.printStackTrace();
184
                } catch (EditionException e1) {
185
                        // TODO Auto-generated catch block
186
                        e1.printStackTrace();
187
                }
188
                JOptionPane.showMessageDialog(null, "Hecho. ");
189
                
190
                
191
        }
192

    
193
        public boolean isEnabled() {
194
                return true;
195
        }
196

    
197
        public boolean isVisible() {
198
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
199

    
200
                if (f == null) {
201
                        return false;
202
                }
203

    
204
                if (f instanceof View) {
205
                        View vista = (View) f;
206
                        IProjectView model = vista.getModel();
207
                        MapContext mapa = model.getMapContext();
208
                        FLayer[] activeLayers = mapa.getLayers().getActives();
209
                        if (activeLayers.length > 0)
210
                                if (activeLayers[0] instanceof FLyrVect)
211
                                        return true;
212
                }
213
                return false;
214

    
215
        }
216

    
217
}