Statistics
| Revision:

root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / gui / RouteReportPanel.java @ 8531

History | View | Annotate | Download (13.5 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 8531 2006-11-06 13:21:38Z fjp $
47
* $Log$
48
* Revision 1.10  2006-11-06 13:21:38  fjp
49
* detalles
50
*
51
* Revision 1.9  2006/10/27 10:17:27  fjp
52
* Correcto. Falta ponerlo bonito.
53
*
54
* Revision 1.8  2006/10/26 17:47:14  fjp
55
* previo a formato
56
*
57
* Revision 1.7  2006/10/26 11:42:42  fjp
58
* Ya pita, ya.
59
*
60
* Revision 1.6  2006/10/25 15:51:20  fjp
61
* por terminar lo de los giros
62
*
63
* Revision 1.5  2006/10/25 14:47:34  fjp
64
* *** empty log message ***
65
*
66
* Revision 1.4  2006/10/24 18:42:05  azabala
67
* *** empty log message ***
68
*
69
* Revision 1.3  2006/10/23 18:51:42  azabala
70
* *** empty log message ***
71
*
72
* Revision 1.2  2006/10/20 19:54:01  azabala
73
* *** empty log message ***
74
*
75
* Revision 1.1  2006/10/19 19:09:43  azabala
76
* *** empty log message ***
77
*
78
*
79
*/
80
package com.iver.cit.gvsig.graph.gui;
81

    
82
import java.awt.BasicStroke;
83
import java.awt.BorderLayout;
84
import java.awt.Color;
85
import java.awt.geom.Rectangle2D;
86
import java.text.NumberFormat;
87
import java.util.ArrayList;
88
import java.util.Collection;
89
import java.util.Iterator;
90
import java.util.StringTokenizer;
91

    
92
import javax.swing.JEditorPane;
93
import javax.swing.JPanel;
94
import javax.swing.JScrollPane;
95
import javax.swing.event.HyperlinkEvent;
96
import javax.swing.event.HyperlinkListener;
97

    
98

    
99

    
100
import com.hardcode.gdbms.engine.values.DoubleValue;
101
import com.iver.andami.ui.mdiManager.IWindow;
102
import com.iver.andami.ui.mdiManager.WindowInfo;
103
import com.iver.cit.gvsig.fmap.MapControl;
104
import com.iver.cit.gvsig.fmap.core.FShape;
105
import com.iver.cit.gvsig.fmap.core.IFeature;
106
import com.iver.cit.gvsig.fmap.core.IGeometry;
107
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
108
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
109
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
110
import com.iver.cit.gvsig.graph.core.TurnUtil;
111
import com.iver.cit.gvsig.graph.solvers.Route;
112

    
113
public class RouteReportPanel extends JPanel implements IWindow{
114
        
115
        private final String START_IMAGE = "<img src=\"images/drapeau_depart.gif\">";
116
        private final String STOP_IMAGE = "<img src=\"images/drapeau_arrivee.gif\">";
117
        private final String LEFT_IMAGE = "<img src=\"images/turn-left.png\">";
118
        private final String RIGHT_IMAGE = "<img src=\"images/turn-right.png\">";
119
        private final String STRAIGHT_IMAGE = "<img src=\"images/gtk-go-up.png\">";
120
        
121
        private final String LINE_SEPARATOR = "<hr width =\"70%\">";
122
        
123
        private Route route;
124
        
125
        private JScrollPane scrollPanel;
126
        private JEditorPane htmlPanel;
127
        private WindowInfo viewInfo;
128
        
129
        
130
        private String htmlText;
131
        private MapControl mapControl;
132
        
133
        //Para poder poner duraci?n, etc.
134
        private String weightText = "Longitud:";
135
        
136
        //Distancias y costes acumulados (entre dos cambios de calles)
137
        private double acumuledLenght = 0d;
138
        private double acumuledWeight = 0d;
139
        
140
        private double totalLenght = 0d;
141
        private double totalWeight = 0d;
142
        
143
        
144
        
145
        private int numberOfStreets = 1;//partimos de 1 porque consideramos la salida
146
        private ArrayList tramesOfSameStreet = new ArrayList();
147
        
148
        NumberFormat nf = NumberFormat.getInstance();
149
        
150
        
151
        public RouteReportPanel(Route route, MapControl mapControl){
152
                super();
153
                setLayout(new BorderLayout());
154
                this.route = route;
155
                this.mapControl = mapControl;
156
                scrollPanel = new JScrollPane();
157
                htmlPanel = new JEditorPane();
158
                htmlPanel.setEditable(false);
159
                htmlPanel.setEditorKit(new HTMLEditorKit());
160
                
161
                nf.setMaximumFractionDigits(2);
162
                
163
                final MapControl map = mapControl;
164
                final Route routeTemp = route;
165
                final MapControl mapControlTemp = mapControl;
166
                htmlPanel.addHyperlinkListener(new HyperlinkListener(){
167
                        ArrayList previousSelection;
168
                        public void hyperlinkUpdate(HyperlinkEvent e) {
169
                                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
170
                                        Rectangle2D bounds = null; 
171
                                        StringTokenizer st = new StringTokenizer(e.getDescription(),",");
172
                                        System.err.println("E.GETDESCRIPTION():" + e.getDescription());
173
                                        ArrayList features = new ArrayList();
174
                                     while (st.hasMoreTokens()) {
175
                                             int indexOfFeature = Integer.parseInt(st.nextToken());
176
                                             IFeature feature = (IFeature) routeTemp.getFeatureList().get(indexOfFeature);
177
                                             if(bounds == null)
178
                                                     bounds = feature.getGeometry().getBounds2D();
179
                                             else
180
                                                     bounds.add(feature.getGeometry().getBounds2D());
181
                                             features.add(feature);
182
                                     }
183
                                        if(bounds != null){
184
                                                bounds = expand(bounds);
185
                                                GraphicLayer graphicLayer = mapControlTemp.getMapContext().getGraphicsLayer();
186
                                                if(previousSelection != null){
187
                                                        for(int i = 0; i < previousSelection.size(); i++){
188
                                                                graphicLayer.removeGraphics((FGraphic) previousSelection.get(i));
189
                                                        }
190
                                                }
191
                                                previousSelection = new ArrayList();
192
                                                FSymbol lineSymbol = new FSymbol(FShape.LINE, Color.YELLOW);
193
                                                lineSymbol.setStroke(new BasicStroke(3.0f));
194
                                                int idSymbolLine = graphicLayer.addSymbol(lineSymbol);
195
                                                for(int i = 0; i < features.size(); i++ ){
196
                                                        IGeometry gAux = ((IFeature)features.get(i)).getGeometry();
197
                                                        FGraphic graphic = new FGraphic(gAux, idSymbolLine);
198
                                                        graphicLayer.addGraphic( graphic);
199
                                                        previousSelection.add(graphic);
200
                                                }
201
                                                mapControlTemp.drawGraphics();
202
                                                map.getMapContext().getViewPort().setExtent(bounds);
203
                                        }        
204
                              }
205
                        }});
206
                initialize();
207
                scrollPanel.setViewportView(htmlPanel);
208
                add(scrollPanel, BorderLayout.CENTER);
209
        }
210
        
211
        
212
        private Rectangle2D expand(Rectangle2D rect){
213
                double xmin = rect.getMinX();
214
                double ymin = rect.getMinY();
215
                double width = rect.getWidth();
216
                double height = rect.getHeight();
217
                
218
                xmin -= width ;
219
                ymin -= height ;
220
                double newWidth = width * 2;
221
                double newHeight = height * 2 ;
222
                return new Rectangle2D.Double(xmin, ymin, newWidth, newHeight);
223
        }
224
        
225
        
226
        public void setRoute(Route route){
227
                this.route = route;
228
                initialize();
229
        }
230
        
231
        private void initialize(){
232
                htmlText = "<head>";
233
                htmlText += "<style type='text/css'>";
234
                htmlText += "<!-- ";
235
                htmlText += "  .normal { ";
236
                htmlText += "        font-family: Arial, Helvetica, sans-serif;        font-size: 10px; font-style: normal; color: #0000FF;";
237
                htmlText += "} --> ";
238
                htmlText += "</style>";
239
                htmlText += "</head>";
240
                htmlText += "<body>";
241
                ArrayList features = route.getFeatureList();
242
                
243
                //Route is ordered from the the start to the end
244
                IFeature firstFeature = (IFeature) features.get(0);
245
                IFeature lastFeature = (IFeature) features.get(features.size() - 1);
246
                
247
                renderHeader(firstFeature, lastFeature);
248
                renderFirstStrech(firstFeature);
249
                
250
                IFeature previousFeature = firstFeature;
251
                for (int i = 1; i < features.size() ; i++) {
252
                        IFeature feature = (IFeature) features.get(i);
253
                        renderStrech(feature , previousFeature, i);
254
                        previousFeature = feature;
255
                }
256
                
257
                //TODO
258
                //Invertir el FIRST y el LAST
259
                //Borrar el graphics resaltado cuando se resalte otro
260
                renderLastStretch((IFeature)features.get(features.size() -1), previousFeature);
261
                htmlText += "</body>";
262
                System.out.println(htmlText);
263
                htmlPanel.setText(htmlText);
264
                
265
        }
266
        
267
        
268
        
269
        
270
        
271
        private void renderHeader(IFeature firstFeature, IFeature lastFeature){
272
                String startName = firstFeature.getAttribute(Route.TEXT_INDEX).toString();
273
                String stopName = lastFeature.getAttribute(Route.TEXT_INDEX).toString();
274
                htmlText += "<h1>";
275
                htmlText += "Informe de Ruta:" + startName + "-" + stopName ;//TODO INTERNACIONALIZAR ESTO
276
                htmlText += "</h1><br>";
277
                htmlText += "Salida desde: <b>";//TODO INTERNAC
278
                htmlText += startName;
279
                htmlText += "</b><br>";
280
                htmlText += "Llegada a:<b> ";//TODO INTERNAC
281
                htmlText += stopName + "</b><br>";
282
                
283
                //TODO METER LA LONGITUD TOTAL DEL TRAYECTO AQUI
284
                htmlText += "Longitud total del trayecto: <b>" + nf.format(getLengthOfRoute()) + "</b>";
285
                htmlText += LINE_SEPARATOR;
286
        }
287
        
288
        private double getLengthOfRoute() {
289
                double solution = 0d;
290
                ArrayList featureList = route.getFeatureList();
291
                for(int i = 0; i < featureList.size(); i++){
292
                        IFeature feature = (IFeature) featureList.get(i);
293
                        solution += ((DoubleValue)feature.getAttribute(Route.LENGTH_INDEX)).getValue();
294
                }
295
                return solution;
296
        }
297

    
298

    
299
        private void renderFirstStrech(IFeature feature){
300
                htmlText += "<table>";
301
                htmlText += "<tr>";
302
                htmlText += "<td>";
303
                htmlText += START_IMAGE;
304
                htmlText += "</td>";
305
                htmlText += "<td class='normal'>";
306
                htmlText += "1. Salir de:<b> ";//TODO INTERNAC
307
                htmlText += feature.getAttribute(Route.TEXT_INDEX);
308
                htmlText += "</b></td></tr>";
309
                htmlText += "<tr><td class='normal'><a href=\""+0+"\">Ver sobre el mapa</a><td></tr>";
310
                htmlText += "</table>";
311
                htmlText += LINE_SEPARATOR;
312
                
313
                double length = ((DoubleValue)feature.getAttribute(Route.LENGTH_INDEX)).getValue();
314
                double weight = ((DoubleValue)feature.getAttribute(Route.WEIGHT_INDEX)).getValue();
315
                acumuledLenght += length;
316
                acumuledWeight += weight;
317
                
318
                totalLenght += length;
319
                totalWeight += weight;
320
                
321
                tramesOfSameStreet.add(new Integer(0));
322
        }
323
        
324
        private void renderStrech(IFeature feature, IFeature previousFeature, int index){
325
                String street1 =  previousFeature.getAttribute(Route.TEXT_INDEX).toString();
326
                String street2 = feature.getAttribute(Route.TEXT_INDEX).toString();
327
                boolean changeStreet = ! street1.equalsIgnoreCase(street2);
328
                double length = ((DoubleValue)feature.getAttribute(Route.LENGTH_INDEX)).getValue();
329
                double weight = ((DoubleValue)feature.getAttribute(Route.WEIGHT_INDEX)).getValue();
330
                
331
                String textoTramo = null;
332
                String imageTurn = null;
333
                
334
                if(changeStreet){
335
                        numberOfStreets++;
336
                        String prefix = "Contin?e por <b>" + street1 + "</b> durante  "+
337
                        nf.format(acumuledLenght)+" y ";
338
                        int direction = TurnUtil.getDirection(previousFeature, feature);
339
                        
340
                        if(direction == TurnUtil.GO_STRAIGH_ON){
341
                                textoTramo = prefix + " prosiga por <b>"+ street2 + "</b>";
342
                                imageTurn = STRAIGHT_IMAGE;
343
                                
344
                        }else if(direction == TurnUtil.TURN_LEFT){
345
                                textoTramo = prefix += " gire a la <b>izquierda</b> por <b>" + street2 + "</b>";
346
                                imageTurn = LEFT_IMAGE;
347
                                
348
                        }else if(direction == TurnUtil.TURN_RIGHT){
349
                                textoTramo = prefix += " gire a la <b>derecha</b> por <b>" + street2 + "</b>";
350
                                imageTurn = RIGHT_IMAGE;        
351
                        }
352
                        htmlText += "<table>";
353
                        htmlText += "<tr>";
354
                        htmlText += "<td>";
355
                        htmlText += imageTurn;
356
                        htmlText += "</td>";
357
                        htmlText += "<td class='normal'>";
358
                        htmlText += numberOfStreets+" "+textoTramo;//TODO INTERNAC
359
                        htmlText += "</td></tr>";
360
                        htmlText += "<tr><td>Distancia acumulada:</td><td>"+nf.format(totalLenght)+"</td></tr>";
361
                        
362
                        if(!weightText.equalsIgnoreCase("Longitud:"))
363
                                htmlText += "<tr><td>Coste:</td><td>"+nf.format(totalWeight)+"</td></tr>";
364
                        
365
                        String features = "";
366
                        for(int i = 0; i < tramesOfSameStreet.size() -1; i++){
367
                                int featureIndex = ((Integer) tramesOfSameStreet.get(i)).intValue();
368
                                features += featureIndex + ",";
369
                        }
370
                        
371
                        features += ((Integer)tramesOfSameStreet.get(tramesOfSameStreet.size()-1)).intValue();
372
                        System.out.println("features = " + features);
373
                        htmlText += "<tr><td><a href=\""+features+"\">Ver sobre el mapa</a><td></tr>";
374
                        htmlText += "</table>";
375
                        htmlText += LINE_SEPARATOR;
376
                        
377
                        acumuledLenght = length;
378
                        acumuledWeight = weight;
379
                        tramesOfSameStreet.clear();
380
        
381
                        
382
                }else{
383
                        acumuledLenght += length;
384
                        acumuledWeight += weight;
385
                }
386
                
387
                tramesOfSameStreet.add(new Integer(index));
388
                totalLenght += length;
389
                totalWeight += weight;
390
                
391
        }
392
        
393
        
394
        
395
        private void renderLastStretch(IFeature feature, IFeature previousFeature){
396
                
397
//                double length = ((DoubleValue)feature.getAttribute(Route.LENGTH_INDEX)).getValue();
398
//                double weight = ((DoubleValue)feature.getAttribute(Route.WEIGHT_INDEX)).getValue();
399
//                
400
//                
401
//                totalLenght += length;
402
//                totalWeight += weight;
403
                
404
                htmlText += "<table>";
405
                htmlText += "<tr>";
406
                htmlText += "<td>";
407
                htmlText += STOP_IMAGE;
408
                htmlText += "</td>";
409
                htmlText += "<td>";
410
                htmlText += numberOfStreets+". Llegada: ";//TODO INTERNAC
411
                htmlText += feature.getAttribute(Route.TEXT_INDEX);
412
                htmlText += "</td></tr>";
413
                htmlText += "<tr><td>Longitud:</td><td>"+nf.format(totalLenght)+"</td></tr>";
414
                
415
                if(!weightText.equalsIgnoreCase("Longitud:"))
416
                        htmlText += "<tr><td>Coste:</td><td>"+totalWeight+"</td></tr>";
417
                htmlText += "<tr><td><a href=\""+(route.getFeatureList().size()-1)+"\">Ver sobre el mapa</a><td></tr>";
418
                htmlText += "</table>";
419
        }
420

    
421
        public WindowInfo getWindowInfo() {
422
                if (viewInfo == null) {
423
                        viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG | 
424
                                                                                WindowInfo.RESIZABLE | 
425
                                                                                WindowInfo.MAXIMIZABLE |
426
                                                                                WindowInfo.ICONIFIABLE | 
427
                                                                                WindowInfo.PALETTE);
428
                        viewInfo.setTitle("Informe de la ruta calculada");//Internacionalizar esto
429
                        viewInfo.setWidth(400);
430
                        viewInfo.setHeight(350);
431
                }
432
                return viewInfo;
433
        }
434
        
435
        
436
        
437
        
438
        
439
}
440