Statistics
| Revision:

root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / ShortestPathExtension.java @ 8567

History | View | Annotate | Download (4.92 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.BasicStroke;
44
import java.awt.Color;
45
import java.util.Collection;
46
import java.util.Iterator;
47

    
48
import com.iver.andami.PluginServices;
49
import com.iver.andami.plugins.Extension;
50
import com.iver.andami.ui.mdiManager.IWindow;
51
import com.iver.cit.gvsig.fmap.MapContext;
52
import com.iver.cit.gvsig.fmap.MapControl;
53
import com.iver.cit.gvsig.fmap.core.IFeature;
54
import com.iver.cit.gvsig.fmap.core.IGeometry;
55
import com.iver.cit.gvsig.fmap.core.v02.FArrowSymbol;
56
import com.iver.cit.gvsig.fmap.layers.FLayer;
57
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
58
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
59
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
60
import com.iver.cit.gvsig.graph.core.Network;
61
import com.iver.cit.gvsig.graph.gui.RouteReportPanel;
62
import com.iver.cit.gvsig.graph.solvers.Route;
63
import com.iver.cit.gvsig.graph.solvers.ShortestPathSolverAStar;
64
import com.iver.cit.gvsig.project.documents.view.gui.View;
65

    
66
public class ShortestPathExtension extends Extension {
67

    
68
        public static ShortestPathSolverAStar solver = new ShortestPathSolverAStar();
69
        private int idSymbolLine = -1;
70

    
71
        public void initialize() {
72
        }
73

    
74
        public void execute(String actionCommand) {
75
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
76
                MapContext map = v.getMapControl().getMapContext();
77
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
78
                while (it.hasNext())
79
                {
80
                        FLayer aux = it.next();
81
                        if (!aux.isActive())
82
                                continue;
83
                        Network net = (Network) aux.getProperty("network");
84

    
85
                        if ( net != null)
86
                        {
87
                                Route route;
88
                                try {
89
                                        solver.setNetwork(net);
90
//                                        solver.setFielStreetName("STREET_NAM");
91
                                        route = solver.calculateRoute();
92

    
93
                                        
94
                                        createGraphicsFrom(route.getFeatureList(), v.getMapControl());
95
                                        
96
                                        RouteReportPanel routeReport = new RouteReportPanel(route, v.getMapControl());
97
                                        PluginServices.getMDIManager().addWindow(routeReport);
98
                                        
99

    
100
                                } catch (GraphException e) {
101
                                        // TODO Auto-generated catch block
102
                                        e.printStackTrace();
103
                                }
104
                        }
105
                }
106

    
107

    
108

    
109
        }
110

    
111
        private void createGraphicsFrom(Collection featureList, MapControl mapControl) {
112
                Iterator it = featureList.iterator();
113
                GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer();
114
//                if (idSymbolLine == -1)
115
                {
116
                        FArrowSymbol arrowSymbol = new FArrowSymbol(Color.RED);
117
                        // FSymbol arrowSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
118
                        arrowSymbol.setStroke(new BasicStroke(3.0f));
119
                        idSymbolLine = graphicLayer.addSymbol(arrowSymbol);
120
                        
121
                }
122
                while (it.hasNext()) {
123
                        IFeature feat = (IFeature) it.next();
124
                        IGeometry gAux = feat.getGeometry();
125
                        FGraphic graphic = new FGraphic(gAux, idSymbolLine);
126
                        graphic.setTag("ROUTE");
127
                        // Lo insertamos al principio de la lista para que los
128
                        // pushpins se dibujen despu?s.
129
//                        graphicLayer.addGraphic(graphic);
130
                        graphicLayer.insertGraphic(0, graphic);
131
                }
132
                mapControl.drawGraphics();
133

    
134
        }
135

    
136
        public boolean isEnabled() {
137
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
138
                 .getActiveWindow();
139
                if (f == null) {
140
                    return false;
141
                }
142
                if (f instanceof View) {
143
                    View v = (View) f;
144
                        MapContext map = v.getMapControl().getMapContext();
145
                        SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
146
                        while (it.hasNext())
147
                        {
148
                                FLayer aux = it.next();
149
                                if (!aux.isActive())
150
                                        continue;
151
                                Network net = (Network) aux.getProperty("network");
152

    
153
                                if ( net != null)
154
                                {
155
                                        if(net.getFlags().length >= 2)
156
                                                return true;
157
                                }
158
                        }
159
                        return false;
160

    
161
                }
162
                return false;
163
        }
164

    
165
        public boolean isVisible() {
166
                IWindow f = PluginServices.getMDIManager()
167
                 .getActiveWindow();
168
                if (f == null) {
169
                    return false;
170
                }
171
                if (f instanceof View) {
172
                        return true;
173
                }
174
                return false;
175

    
176
        }
177

    
178
}
179

    
180