Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / gui / RouteReportPanel.java @ 8264

History | View | Annotate | Download (11.3 KB)

1
/*
2
 * Created on 19-oct-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
*
46
* $Id: RouteReportPanel.java 8264 2006-10-23 18:51:42Z azabala $
47
* $Log$
48
* Revision 1.3  2006-10-23 18:51:42  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.2  2006/10/20 19:54:01  azabala
52
* *** empty log message ***
53
*
54
* Revision 1.1  2006/10/19 19:09:43  azabala
55
* *** empty log message ***
56
*
57
*
58
*/
59
package com.iver.cit.gvsig.graph.gui;
60

    
61
import java.awt.BasicStroke;
62
import java.awt.BorderLayout;
63
import java.awt.Color;
64
import java.awt.geom.Rectangle2D;
65
import java.text.NumberFormat;
66
import java.util.ArrayList;
67
import java.util.Collection;
68
import java.util.Iterator;
69
import java.util.StringTokenizer;
70

    
71
import javax.swing.JEditorPane;
72
import javax.swing.JPanel;
73
import javax.swing.JScrollPane;
74
import javax.swing.event.HyperlinkEvent;
75
import javax.swing.event.HyperlinkListener;
76

    
77

    
78

    
79
import com.hardcode.gdbms.engine.values.DoubleValue;
80
import com.iver.andami.ui.mdiManager.IWindow;
81
import com.iver.andami.ui.mdiManager.WindowInfo;
82
import com.iver.cit.gvsig.fmap.MapControl;
83
import com.iver.cit.gvsig.fmap.core.FShape;
84
import com.iver.cit.gvsig.fmap.core.IFeature;
85
import com.iver.cit.gvsig.fmap.core.IGeometry;
86
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
87
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
88
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
89
import com.iver.cit.gvsig.graph.core.TurnUtil;
90
import com.iver.cit.gvsig.graph.solvers.Route;
91

    
92
public class RouteReportPanel extends JPanel implements IWindow{
93
        
94
        private final String START_IMAGE = "<img src=\"images/drapeau_depart.gif\">";
95
        private final String STOP_IMAGE = "<img src=\"images/drapeau_arrivee.gif\">";
96
        private final String LEFT_IMAGE = "<img src=\"images/gtk-go-left.png\">";
97
        private final String RIGHT_IMAGE = "<img src=\"images/gtk-go-right.png\">";
98
        private final String STRAIGHT_IMAGE = "<img src=\"images/gtk-go-up.png\">";
99
        
100
        private final String LINE_SEPARATOR = "<hr width =\"70%\">";
101
        
102
        private Route route;
103
        
104
        private JScrollPane scrollPanel;
105
        private JEditorPane htmlPanel;
106
        private WindowInfo viewInfo;
107
        
108
        
109
        private String htmlText;
110
        private MapControl mapControl;
111
        
112
        //Para poder poner duraci?n, etc.
113
        private String weightText = "Longitud:";
114
        
115
        //Distancias y costes acumulados (entre dos cambios de calles)
116
        private double acumuledLenght = 0d;
117
        private double acumuledWeight = 0d;
118
        
119
        private double totalLenght = 0d;
120
        private double totalWeight = 0d;
121
        
122
        
123
        
124
        private int numberOfStreets = 1;//partimos de 1 porque consideramos la salida
125
        private ArrayList tramesOfSameStreet = new ArrayList();
126
        
127
        NumberFormat nf = NumberFormat.getInstance();
128
        
129
        
130
        public RouteReportPanel(Route route, MapControl mapControl){
131
                super();
132
                setLayout(new BorderLayout());
133
                this.route = route;
134
                this.mapControl = mapControl;
135
                scrollPanel = new JScrollPane();
136
                htmlPanel = new JEditorPane();
137
                htmlPanel.setEditable(false);
138
                htmlPanel.setEditorKit(new HTMLEditorKit());
139
                
140
                nf.setMaximumFractionDigits(2);
141
                
142
                final MapControl map = mapControl;
143
                final Route routeTemp = route;
144
                htmlPanel.addHyperlinkListener(new HyperlinkListener(){
145
                        public void hyperlinkUpdate(HyperlinkEvent e) {
146
                                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
147
                                        Rectangle2D bounds = null; 
148
                                        StringTokenizer st = new StringTokenizer(e.getDescription(),",");
149
                                        ArrayList features = new ArrayList();
150
                                     while (st.hasMoreTokens()) {
151
                                             int indexOfFeature = Integer.parseInt(st.nextToken());
152
                                             IFeature feature = (IFeature) routeTemp.getFeatureList().get(indexOfFeature);
153
                                             if(bounds == null)
154
                                                     bounds = feature.getGeometry().getBounds2D();
155
                                             else
156
                                                     bounds.add(feature.getGeometry().getBounds2D());
157
                                             features.add(feature);
158
                                     }
159
                                        if(bounds != null){
160
                                                graphicSelection(features);
161
                                                map.getMapContext().getViewPort().setExtent(bounds);
162
                                        }        
163
                              }
164
                        }});
165
                initialize();
166
                scrollPanel.setViewportView(htmlPanel);
167
                add(scrollPanel, BorderLayout.CENTER);
168
        }
169
        
170
        
171
        public void setRoute(Route route){
172
                this.route = route;
173
                initialize();
174
        }
175
        
176
        private void initialize(){
177
                htmlText = "";
178
                ArrayList features = route.getFeatureList();
179
                
180
                //Route is ordered from the end to the start
181
                IFeature firstFeature = (IFeature) features.get(features.size() - 1);
182
                IFeature lastFeature = (IFeature) features.get(0);
183
                renderHeader(firstFeature, lastFeature);
184
                renderFirstStrech(firstFeature);
185
                IFeature previousFeature = firstFeature;
186
                int counter = 1;
187
                for (int i = features.size() - 2; i > 0; i--) {
188
                        IFeature feature = (IFeature) features.get(i);
189
                        renderStrech(feature , previousFeature, counter);
190
                        previousFeature = feature;
191
                        counter++;
192
                }
193
                
194
                //TODO
195
                //Invertir el FIRST y el LAST
196
                //Borrar el graphics resaltado cuando se resalte otro
197
                
198
                renderLastStretch((IFeature)features.get(features.size() -1));
199
                htmlPanel.setText(htmlText);
200
        }
201
        
202
        private void graphicSelection(ArrayList selectedFeature) {
203
                GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer();
204
                FSymbol lineSymbol = new FSymbol(FShape.LINE, Color.YELLOW);
205
                lineSymbol.setStroke(new BasicStroke(3.0f));
206
                int idSymbolLine = graphicLayer.addSymbol(lineSymbol);
207
                for(int i = 0; i < selectedFeature.size(); i++ ){
208
                        IGeometry gAux = ((IFeature)selectedFeature.get(i)).getGeometry();
209
                        FGraphic graphic = new FGraphic(gAux, idSymbolLine);
210
                        graphicLayer.addGraphic( graphic);
211
                }
212
                mapControl.drawGraphics();
213

    
214
        }
215
        
216
        
217
        
218
        private void renderHeader(IFeature firstFeature, IFeature lastFeature){
219
                String startName = firstFeature.getAttribute(Route.TEXT_INDEX).toString();
220
                String stopName = lastFeature.getAttribute(Route.TEXT_INDEX).toString();
221
                htmlText += "<h1>";
222
                htmlText += "Informe de Ruta:" + startName + "-" + stopName ;//TODO INTERNACIONALIZAR ESTO
223
                htmlText += "</h1><br>";
224
                htmlText += "Salida desde: <b>";//TODO INTERNAC
225
                htmlText += startName;
226
                htmlText += "</b><br>";
227
                htmlText += "Llegada a:<b> ";//TODO INTERNAC
228
                htmlText += stopName + "</b><br>";
229
                
230
                //TODO METER LA LONGITUD TOTAL DEL TRAYECTO AQUI
231
                
232
                htmlText += LINE_SEPARATOR;
233
        }
234
        
235
        private void renderFirstStrech(IFeature feature){
236
                htmlText += "<table>";
237
                htmlText += "<tr>";
238
                htmlText += "<td>";
239
                htmlText += START_IMAGE;
240
                htmlText += "</td>";
241
                htmlText += "<td>";
242
                htmlText += "1. Salir de:<b> ";//TODO INTERNAC
243
                htmlText += feature.getAttribute(Route.TEXT_INDEX);
244
                htmlText += "</b></td></tr>";
245
                htmlText += "<tr><td><a href=\""+0+"\">Ver sobre el mapa</a><td></tr>";
246
                htmlText += "</table>";
247
                htmlText += LINE_SEPARATOR;
248
                
249
                double length = ((DoubleValue)feature.getAttribute(Route.LENGTH_INDEX)).getValue();
250
                double weight = ((DoubleValue)feature.getAttribute(Route.WEIGHT_INDEX)).getValue();
251
                acumuledLenght += length;
252
                acumuledWeight += weight;
253
                
254
                totalLenght += length;
255
                totalWeight += weight;
256
                
257
                tramesOfSameStreet.add(new Integer(0));
258
        }
259
        
260
        private void renderStrech(IFeature feature, IFeature previousFeature, int index){
261
                String street1 =  previousFeature.getAttribute(Route.TEXT_INDEX).toString();
262
                String street2 = feature.getAttribute(Route.TEXT_INDEX).toString();
263
                boolean changeStreet = ! street1.equalsIgnoreCase(street2);
264
                double length = ((DoubleValue)feature.getAttribute(Route.LENGTH_INDEX)).getValue();
265
                double weight = ((DoubleValue)feature.getAttribute(Route.WEIGHT_INDEX)).getValue();
266
                
267
                String textoTramo = null;
268
                String imageTurn = null;
269
                
270
                if(changeStreet){
271
                        numberOfStreets++;
272
                        String prefix = "Continue durante  "+
273
                        nf.format(acumuledLenght)+" y ";
274
                        int direction = TurnUtil.getDirection(previousFeature, feature);
275
                        if(direction == TurnUtil.GO_STRAIGH_ON){
276
                                textoTramo += prefix + " prosiga por<b>"+ street2 + "</b>";
277
                                imageTurn = STRAIGHT_IMAGE;
278
                                
279
                        }else if(direction == TurnUtil.TURN_LEFT){
280
                                textoTramo = prefix += " gire a la izquierda por <b>" + street2 + "</b>";
281
                                imageTurn = LEFT_IMAGE;
282
                                
283
                        }else if(direction == TurnUtil.TURN_RIGHT){
284
                                textoTramo = prefix += " gire a la derecha por <b>" + street2 + "</b>";
285
                                imageTurn = RIGHT_IMAGE;        
286
                        }
287
                        htmlText += "<table>";
288
                        htmlText += "<tr>";
289
                        htmlText += "<td>";
290
                        htmlText += imageTurn;
291
                        htmlText += "</td>";
292
                        htmlText += "<td>";
293
                        htmlText += numberOfStreets+" "+textoTramo;//TODO INTERNAC
294
                        htmlText += "</td></tr>";
295
                        htmlText += "<tr><td>Distancia acumulada:</td><td>"+nf.format(totalLenght)+"</td></tr>";
296
                        if(!weightText.equalsIgnoreCase("Longitud:"))
297
                                htmlText += "<tr><td>Coste:</td><td>"+nf.format(totalWeight)+"</td></tr>";
298
                        String features = "";
299
                        for(int i = 0; i < tramesOfSameStreet.size() -1; i++){
300
                                int featureIndex = ((Integer) tramesOfSameStreet.get(i)).intValue();
301
                                features += featureIndex + ",";
302
                        }
303
                        features += ((Integer)tramesOfSameStreet.get(tramesOfSameStreet.size()-1)).intValue();
304
                        htmlText += "<tr><td><a href=\""+features+"\">Ver sobre el mapa</a><td></tr>";
305
                        htmlText += "</table>";
306
                        htmlText += LINE_SEPARATOR;
307
                        
308
                        acumuledLenght = length;
309
                        acumuledWeight = weight;
310
                        tramesOfSameStreet.clear();
311
        
312
                        
313
                }else{
314
                        acumuledLenght += length;
315
                        acumuledWeight += weight;
316
                }
317
                
318
                totalLenght += length;
319
                totalWeight += weight;
320
                tramesOfSameStreet.add(new Integer(index));
321
        }
322
        
323
        
324
        
325
        private void renderLastStretch(IFeature feature){
326
                
327
                double length = ((DoubleValue)feature.getAttribute(Route.LENGTH_INDEX)).getValue();
328
                double weight = ((DoubleValue)feature.getAttribute(Route.WEIGHT_INDEX)).getValue();
329
                
330
                
331
                totalLenght += length;
332
                totalWeight += weight;
333
                
334
                htmlText += "<table>";
335
                htmlText += "<tr>";
336
                htmlText += "<td>";
337
                htmlText += STOP_IMAGE;
338
                htmlText += "</td>";
339
                htmlText += "<td>";
340
                htmlText += numberOfStreets+". Llegada: ";//TODO INTERNAC
341
                htmlText += feature.getAttribute(Route.TEXT_INDEX);
342
                htmlText += "</td></tr>";
343
                htmlText += "<tr><td>Longitud:</td><td>"+nf.format(totalLenght)+"</td></tr>";
344
                
345
                if(!weightText.equalsIgnoreCase("Longitud:"))
346
                        htmlText += "<tr><td>Coste:</td><td>"+totalWeight+"</td></tr>";
347
                htmlText += "<tr><td><a href=\""+(route.getFeatureList().size()-1)+"\">Ver sobre el mapa</a><td></tr>";
348
                htmlText += "</table>";
349
        }
350

    
351
        public WindowInfo getWindowInfo() {
352
                if (viewInfo == null) {
353
                        viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG | 
354
                                                                                WindowInfo.RESIZABLE);
355
                        viewInfo.setTitle("Informe de la ruta calculada");//Internacionalizar esto
356
                        viewInfo.setWidth(400);
357
                        viewInfo.setHeight(350);
358
                }
359
                return viewInfo;
360
        }
361
        
362
        
363
        
364
        
365
        
366
}
367