Statistics
| Revision:

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

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

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

    
89
import javax.swing.JEditorPane;
90
import javax.swing.JPanel;
91
import javax.swing.JScrollPane;
92
import javax.swing.event.HyperlinkEvent;
93
import javax.swing.event.HyperlinkListener;
94

    
95

    
96

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

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

    
295

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

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