Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / gui / RouteReportPanel.java @ 30840

History | View | Annotate | Download (15.8 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 30840 2009-09-18 16:06:37Z fpenarrubia $
47
* $Log$
48
* Revision 1.15  2007-09-07 11:29:47  fjp
49
* Casi compila. Falta arreglar lo de FArrowSymbol y retocar el graphiclist de FMap.
50
*
51
* Revision 1.14  2006/11/20 08:44:55  fjp
52
* borrar tramos amarillos, seleccionar solo campos num?ricos y situar las barreras encima del tramo invalidado
53
*
54
* Revision 1.13  2006/11/14 16:12:01  fjp
55
* *** empty log message ***
56
*
57
* Revision 1.12  2006/11/08 16:48:20  fjp
58
* *** empty log message ***
59
*
60
* Revision 1.11  2006/11/06 17:19:02  fjp
61
* Depurando el aspecto
62
*
63
* Revision 1.10  2006/11/06 13:21:38  fjp
64
* detalles
65
*
66
* Revision 1.9  2006/10/27 10:17:27  fjp
67
* Correcto. Falta ponerlo bonito.
68
*
69
* Revision 1.8  2006/10/26 17:47:14  fjp
70
* previo a formato
71
*
72
* Revision 1.7  2006/10/26 11:42:42  fjp
73
* Ya pita, ya.
74
*
75
* Revision 1.6  2006/10/25 15:51:20  fjp
76
* por terminar lo de los giros
77
*
78
* Revision 1.5  2006/10/25 14:47:34  fjp
79
* *** empty log message ***
80
*
81
* Revision 1.4  2006/10/24 18:42:05  azabala
82
* *** empty log message ***
83
*
84
* Revision 1.3  2006/10/23 18:51:42  azabala
85
* *** empty log message ***
86
*
87
* Revision 1.2  2006/10/20 19:54:01  azabala
88
* *** empty log message ***
89
*
90
* Revision 1.1  2006/10/19 19:09:43  azabala
91
* *** empty log message ***
92
*
93
*
94
*/
95
package org.gvsig.graph.gui;
96

    
97
import java.awt.BorderLayout;
98
import java.awt.Color;
99
import java.awt.geom.Rectangle2D;
100
import java.text.NumberFormat;
101
import java.util.ArrayList;
102
import java.util.StringTokenizer;
103

    
104
import javax.swing.JEditorPane;
105
import javax.swing.JPanel;
106
import javax.swing.JScrollPane;
107
import javax.swing.event.HyperlinkEvent;
108
import javax.swing.event.HyperlinkListener;
109

    
110
import org.gvsig.graph.core.TurnUtil;
111
import org.gvsig.graph.solvers.Route;
112

    
113
import com.hardcode.gdbms.engine.values.DoubleValue;
114
import com.iver.andami.PluginServices;
115
import com.iver.andami.ui.mdiManager.IWindow;
116
import com.iver.andami.ui.mdiManager.WindowInfo;
117
import com.iver.cit.gvsig.fmap.MapControl;
118
import com.iver.cit.gvsig.fmap.core.IFeature;
119
import com.iver.cit.gvsig.fmap.core.IGeometry;
120
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
121
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
122
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
123
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
124

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

    
332

    
333
        private void renderFirstStrech(IFeature feature){
334
                htmlText.append("<table>");
335
                htmlText.append("<tr>");
336
                htmlText.append("<td width='40'>");
337
                htmlText.append(START_IMAGE);
338
                htmlText.append("</td>");
339
                htmlText.append("<td class='normal'>");
340
                htmlText.append("1. " + _T("Start_from") + ":<b> ");
341
                htmlText.append(feature.getAttribute(Route.TEXT_INDEX));
342
                htmlText.append("</b></td></tr>");
343
                htmlText.append("<tr>"); 
344
                htmlText.append("<td width='40'></td><td><a href=\""+0+"\">" + _T("Show_in_map") + "</a><td></tr>");
345
                htmlText.append("</table>");
346
                htmlText.append(LINE_SEPARATOR);
347
                
348
                double length = ((DoubleValue)feature.getAttribute(Route.LENGTH_INDEX)).getValue();
349
                double weight = ((DoubleValue)feature.getAttribute(Route.WEIGHT_INDEX)).getValue();
350
                acumuledLenght += length;
351
                acumuledWeight += weight;
352
                
353
                totalLenght += length;
354
                totalWeight += weight;
355
                
356
                tramesOfSameStreet.add(new Integer(0));
357
        }
358
        
359
        private String _T(String str) {
360
                return PluginServices.getText(this, str);
361
        }
362
        
363
        private void renderStrech(IFeature feature, IFeature previousFeature, int index){
364
                String street1 =  previousFeature.getAttribute(Route.TEXT_INDEX).toString();
365
                String street2 = feature.getAttribute(Route.TEXT_INDEX).toString();
366
                boolean changeStreet = ! street1.equalsIgnoreCase(street2);
367
                double length = ((DoubleValue)feature.getAttribute(Route.LENGTH_INDEX)).getValue();
368
                double weight = ((DoubleValue)feature.getAttribute(Route.WEIGHT_INDEX)).getValue();
369
                
370
                String textoTramo = null;
371
                String imageTurn = null;
372
                
373
                if(changeStreet){
374
                        numberOfStreets++;
375
                        String prefix = _T("follow") + " <b>" + street1 + "</b> " + _T("during") + " " +
376
                        nf.format(acumuledLenght)+ " " + _T("and");
377
                        int direction = TurnUtil.getDirection(previousFeature, feature);
378
                        
379
                        if(direction == TurnUtil.GO_STRAIGH_ON){
380
                                textoTramo = prefix + " " + _T("continue_by") + " <b> "+ street2 + "</b>";
381
                                imageTurn = STRAIGHT_IMAGE;
382
                                
383
                        }else if(direction == TurnUtil.TURN_LEFT){
384
                                textoTramo = prefix + " " + _T("turn") + " " + "<span class='left'><b> " + _T("left") +
385
                                "</b></span> " + _T("by") + " <b>" + street2 + "</b> ";
386
                                imageTurn = LEFT_IMAGE;
387
                                
388
                        }else if(direction == TurnUtil.TURN_RIGHT){
389
                                textoTramo = prefix + " " + _T("turn") + " <span class='right'><b> " + _T("right") + 
390
                                        " </b></span> " + _T("by") + " <b>" + street2 + "</b>";
391
                                imageTurn = RIGHT_IMAGE;        
392
                        }
393
                        htmlText.append("<table>");
394
                        htmlText.append("<tr>");
395
                        htmlText.append("<td width='40'>");
396
                        htmlText.append(imageTurn);
397
                        htmlText.append("</td>");
398
                        htmlText.append("<td class='normal'>");
399
                        htmlText.append(numberOfStreets+" "+textoTramo);//TODO INTERNAC
400
                        htmlText.append("</td></tr>");
401
                        htmlText.append("<tr>");
402
                        htmlText.append("<td with='40'></td><td class='distancia'>" + _T("Accumulated_distance") + ":" +nf.format(totalLenght)+"</td></tr>");
403
                        
404
                        if(!weightText.equalsIgnoreCase("Longitud:"))
405
                        {
406
                                htmlText.append("<tr>");
407
                                htmlText.append("<td with='40'></td><td class='distancia'>" + _T("cost") + ":" +nf.format(totalWeight)+"</td></tr>");
408
                        }
409
                        
410
                        String features = "";
411
                        for(int i = 0; i < tramesOfSameStreet.size() -1; i++){
412
                                int featureIndex = ((Integer) tramesOfSameStreet.get(i)).intValue();
413
                                features += featureIndex + ",";
414
                        }
415
                        
416
                        features += ((Integer)tramesOfSameStreet.get(tramesOfSameStreet.size()-1)).intValue();
417
//                        System.out.println("features = " + features);
418
                        htmlText.append("<tr><td with='40'></td><td><a href=\""+features+"\">Ver sobre el mapa</a><td></tr>");
419
                        htmlText.append("</table>");
420
                        htmlText.append(LINE_SEPARATOR);
421
                        
422
                        acumuledLenght = length;
423
                        acumuledWeight = weight;
424
                        tramesOfSameStreet.clear();
425
        
426
                        
427
                }else{
428
                        acumuledLenght += length;
429
                        acumuledWeight += weight;
430
                }
431
                
432
                tramesOfSameStreet.add(new Integer(index));
433
                totalLenght += length;
434
                totalWeight += weight;
435
                
436
        }
437
        
438
        
439
        
440
        private void renderLastStretch(IFeature feature, IFeature previousFeature){
441
                
442
//                double length = ((DoubleValue)feature.getAttribute(Route.LENGTH_INDEX)).getValue();
443
//                double weight = ((DoubleValue)feature.getAttribute(Route.WEIGHT_INDEX)).getValue();
444
//                
445
//                
446
//                totalLenght += length;
447
//                totalWeight += weight;
448
                
449
                htmlText.append("<table>");
450
                htmlText.append("<tr>");
451
                htmlText.append("<td width='40'>");
452
                htmlText.append(STOP_IMAGE);
453
                htmlText.append("</td>");
454
                htmlText.append("<td class='resumen'>");
455
                htmlText.append(numberOfStreets+". " + _T("Llegada") + ":" );
456
                htmlText.append(feature.getAttribute(Route.TEXT_INDEX));
457
                htmlText.append("</td></tr>");
458
                htmlText.append("<tr><td with='40'></td><td class='resumen'>" + _T("Longitud") + ":" +nf.format(totalLenght)+"</td></tr>");
459
                
460
                if(!weightText.equalsIgnoreCase("Longitud:"))
461
                        htmlText.append("<tr><td with='40'></td><td class='resumen'>Coste:"+totalWeight+"</td></tr>");
462
                htmlText.append("<tr><td with='40'></td><td><a href=\""+(route.getFeatureList().size()-1)+"\">Ver sobre el mapa</a><td></tr>");
463
                htmlText.append("</table>");
464
        }
465

    
466
        public WindowInfo getWindowInfo() {
467
                if (viewInfo == null) {
468
                        viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG | 
469
                                                                                WindowInfo.RESIZABLE | 
470
                                                                                WindowInfo.MAXIMIZABLE |
471
                                                                                WindowInfo.ICONIFIABLE | 
472
                                                                                WindowInfo.PALETTE);
473
                        viewInfo.setTitle("Informe de la ruta calculada");//Internacionalizar esto
474
                        viewInfo.setWidth(400);
475
                        viewInfo.setHeight(350);
476
                }
477
                return viewInfo;
478
        }
479
        
480
        public Object getWindowProfile() {
481
                return WindowInfo.TOOL_PROFILE;
482
        }
483
        
484
        
485
        
486
        
487
        
488
}
489