Statistics
| Revision:

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

History | View | Annotate | Download (5.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 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 javax.swing.JComponent;
49
import javax.swing.JOptionPane;
50

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

    
71
public class ShortestPathExtension extends Extension {
72

    
73
        public static ShortestPathSolverAStar solver = new ShortestPathSolverAStar();
74
        private int idSymbolLine = -1;
75

    
76
        public void initialize() {
77
        }
78

    
79
        public void execute(String actionCommand) {
80
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
81
                MapControl mapCtrl = v.getMapControl();
82
                MapContext map = mapCtrl.getMapContext();
83
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
84
                while (it.hasNext())
85
                {
86
                        FLayer aux = it.next();
87
                        if (!aux.isActive())
88
                                continue;
89
                        Network net = (Network) aux.getProperty("network");
90

    
91
                        if ( net != null)
92
                        {
93
                                Route route;
94
                                try {
95
                                        solver.setNetwork(net);
96
//                                        solver.setFielStreetName("STREET_NAM");
97
                                        route = solver.calculateRoute();
98
                                        if (route.getFeatureList().size() == 0)
99
                                        {
100
                                                JOptionPane.showMessageDialog((JComponent) PluginServices.getMDIManager().getActiveWindow(),
101
                                                                PluginServices.getText(this, "shortest_path_not_found"));
102
                                                return;
103
                                        }
104
                                        GvSession.getInstance().put(mapCtrl, "Route", route);
105
                                        
106
                                        createGraphicsFrom(route.getFeatureList(), v.getMapControl());
107
                                        
108
                                        RouteReportPanel routeReport = new RouteReportPanel(route, v.getMapControl());
109
                                        PluginServices.getMDIManager().addWindow(routeReport);
110
                                        
111

    
112
                                } catch (GraphException e) {
113
                                        // TODO Auto-generated catch block
114
                                        e.printStackTrace();
115
                                }
116
                        }
117
                }
118

    
119

    
120

    
121
        }
122

    
123
        private void createGraphicsFrom(Collection featureList, MapControl mapControl) {
124
                Iterator it = featureList.iterator();
125
                GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer();
126
//                if (idSymbolLine == -1)
127
                {
128
                        FArrowSymbol arrowSymbol = new FArrowSymbol(Color.RED);
129
                        // FSymbol arrowSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
130
                        arrowSymbol.setStroke(new BasicStroke(3.0f));
131
                        idSymbolLine = graphicLayer.addSymbol(arrowSymbol);
132
                        
133
                }
134
                while (it.hasNext()) {
135
                        IFeature feat = (IFeature) it.next();
136
                        IGeometry gAux = feat.getGeometry();
137
                        FGraphic graphic = new FGraphic(gAux, idSymbolLine);
138
                        graphic.setTag("ROUTE");
139
                        // Lo insertamos al principio de la lista para que los
140
                        // pushpins se dibujen despu?s.
141
//                        graphicLayer.addGraphic(graphic);
142
                        graphicLayer.insertGraphic(0, graphic);
143
                }
144
                mapControl.drawGraphics();
145

    
146
        }
147

    
148
        public boolean isEnabled() {
149
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
150
                 .getActiveWindow();
151
                if (f == null) {
152
                    return false;
153
                }
154
                if (f instanceof View) {
155
                    View v = (View) f;
156
                        MapContext map = v.getMapControl().getMapContext();
157
                        SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
158
                        while (it.hasNext())
159
                        {
160
                                FLayer aux = it.next();
161
                                if (!aux.isActive())
162
                                        continue;
163
                                Network net = (Network) aux.getProperty("network");
164

    
165
                                if ( net != null)
166
                                {
167
                                        int count = 0;
168
                                        for (int i=0; i < net.getOriginaFlags().size(); i++)
169
                                        {
170
                                                GvFlag flag = (GvFlag) net.getOriginaFlags().get(i);
171
                                                if (flag.isEnabled()) count++;
172
                                        }
173
                                        if (count > 1) return true;
174
                                }
175
                        }
176
                        return false;
177

    
178
                }
179
                return false;
180
        }
181

    
182
        public boolean isVisible() {
183
                IWindow f = PluginServices.getMDIManager()
184
                 .getActiveWindow();
185
                if (f == null) {
186
                    return false;
187
                }
188
                if (f instanceof View) {
189
                        return true;
190
                }
191
                return false;
192

    
193
        }
194

    
195
}
196

    
197