Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / ShortestPathExtension.java @ 8659

History | View | Annotate | Download (6.02 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.ArrayList;
46
import java.util.Collection;
47
import java.util.Iterator;
48
import java.util.List;
49

    
50
import javax.swing.JComponent;
51
import javax.swing.JOptionPane;
52

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

    
73
public class ShortestPathExtension extends Extension {
74

    
75
        public static ShortestPathSolverAStar solver = new ShortestPathSolverAStar();
76
        private int idSymbolLine = -1;
77

    
78
        public void initialize() {
79
        }
80

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

    
93
                        if ( net != null)
94
                        {
95
                                Route route;
96
                                try {
97
                                        solver.setNetwork(net);
98
//                                        solver.setFielStreetName("STREET_NAM");
99
                                        route = solver.calculateRoute();
100
                                        if (route.getFeatureList().size() == 0)
101
                                        {
102
                                                JOptionPane.showMessageDialog((JComponent) PluginServices.getMDIManager().getActiveWindow(),
103
                                                                PluginServices.getText(this, "shortest_path_not_found"));
104
                                                return;
105
                                        }
106
                                        List routes = (List) GvSession.getInstance().get(mapCtrl, "Route");
107
                                        if(routes == null){
108
                                                routes = new ArrayList();
109
                                                GvSession.getInstance().put(mapCtrl, "Route", routes);
110
                                        }
111
                                        routes.add(route);
112
                                        
113
                                        createGraphicsFrom(route.getFeatureList(), v.getMapControl());
114
                                        RouteReportPanel routeReport = new RouteReportPanel(route, v.getMapControl());
115
                                        PluginServices.getMDIManager().addWindow(routeReport);
116
                                        List reportsPanels = (List) GvSession.getInstance().get(mapCtrl, "RouteReport");
117
                                        if(reportsPanels == null){
118
                                                reportsPanels = new ArrayList();
119
                                                GvSession.getInstance().put(mapCtrl, "RouteReport", reportsPanels);
120
                                        }        
121
                                        reportsPanels.add(routeReport);
122
                                        
123
                                } catch (GraphException e) {
124
                                        // TODO Auto-generated catch block
125
                                        e.printStackTrace();
126
                                }
127
                        }
128
                }
129

    
130

    
131

    
132
        }
133

    
134
        private void createGraphicsFrom(Collection featureList, MapControl mapControl) {
135
                Iterator it = featureList.iterator();
136
                GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer();
137
//                if (idSymbolLine == -1)
138
                {
139
                        FArrowSymbol arrowSymbol = new FArrowSymbol(Color.RED);
140
                        // FSymbol arrowSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
141
                        arrowSymbol.setStroke(new BasicStroke(3.0f));
142
                        idSymbolLine = graphicLayer.addSymbol(arrowSymbol);
143
                        
144
                }
145
                while (it.hasNext()) {
146
                        IFeature feat = (IFeature) it.next();
147
                        IGeometry gAux = feat.getGeometry();
148
                        FGraphic graphic = new FGraphic(gAux, idSymbolLine);
149
                        graphic.setTag("ROUTE");
150
                        // Lo insertamos al principio de la lista para que los
151
                        // pushpins se dibujen despu?s.
152
//                        graphicLayer.addGraphic(graphic);
153
                        graphicLayer.insertGraphic(0, graphic);
154
                }
155
                mapControl.drawGraphics();
156

    
157
        }
158

    
159
        public boolean isEnabled() {
160
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
161
                 .getActiveWindow();
162
                if (f == null) {
163
                    return false;
164
                }
165
                if (f instanceof View) {
166
                    View v = (View) f;
167
                        MapContext map = v.getMapControl().getMapContext();
168
                        SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
169
                        while (it.hasNext())
170
                        {
171
                                FLayer aux = it.next();
172
                                if (!aux.isActive())
173
                                        continue;
174
                                Network net = (Network) aux.getProperty("network");
175

    
176
                                if ( net != null)
177
                                {
178
                                        int count = 0;
179
                                        for (int i=0; i < net.getOriginaFlags().size(); i++)
180
                                        {
181
                                                GvFlag flag = (GvFlag) net.getOriginaFlags().get(i);
182
                                                if (flag.isEnabled()) count++;
183
                                        }
184
                                        if (count > 1) return true;
185
                                }
186
                        }
187
                        return false;
188

    
189
                }
190
                return false;
191
        }
192

    
193
        public boolean isVisible() {
194
                IWindow f = PluginServices.getMDIManager()
195
                 .getActiveWindow();
196
                if (f == null) {
197
                    return false;
198
                }
199
                if (f instanceof View) {
200
                        return true;
201
                }
202
                return false;
203

    
204
        }
205

    
206
}
207

    
208