Statistics
| Revision:

root / trunk / extensions / extGraph / src / org / gvsig / graph / gui / RouteReportPanel.java @ 31786

History | View | Annotate | Download (17.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 31786 2009-12-15 23:09:12Z 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.Component;
100
import java.awt.Dimension;
101
import java.awt.Insets;
102
import java.awt.event.ActionEvent;
103
import java.awt.event.ActionListener;
104
import java.awt.geom.Rectangle2D;
105
import java.awt.print.PrinterException;
106
import java.io.BufferedWriter;
107
import java.io.File;
108
import java.io.FileOutputStream;
109
import java.io.FileWriter;
110
import java.io.OutputStream;
111
import java.io.OutputStreamWriter;
112
import java.text.NumberFormat;
113
import java.util.ArrayList;
114
import java.util.StringTokenizer;
115

    
116
import javax.swing.JEditorPane;
117
import javax.swing.JOptionPane;
118
import javax.swing.JPanel;
119
import javax.swing.JScrollPane;
120
import javax.swing.border.EmptyBorder;
121
import javax.swing.event.HyperlinkEvent;
122
import javax.swing.event.HyperlinkListener;
123
import javax.swing.filechooser.FileFilter;
124
import javax.swing.text.Element;
125
import javax.swing.text.View;
126
import javax.swing.text.ViewFactory;
127

    
128
import org.gvsig.graph.core.TurnUtil;
129
import org.gvsig.graph.solvers.Route;
130
import org.gvsig.gui.beans.swing.JButton;
131
import org.gvsig.gui.beans.swing.JFileChooser;
132

    
133
import com.hardcode.gdbms.engine.values.DoubleValue;
134
import com.iver.andami.PluginServices;
135
import com.iver.andami.ui.mdiManager.IWindow;
136
import com.iver.andami.ui.mdiManager.WindowInfo;
137
import com.iver.cit.gvsig.fmap.MapControl;
138
import com.iver.cit.gvsig.fmap.core.IFeature;
139
import com.iver.cit.gvsig.fmap.core.IGeometry;
140
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
141
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
142
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
143
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
144
import com.lowagie.text.Document;
145
import com.lowagie.text.PageSize;
146
import com.lowagie.text.Rectangle;
147
import com.lowagie.text.pdf.PdfContentByte;
148
import com.lowagie.text.pdf.PdfWriter;
149

    
150
public class RouteReportPanel extends JPanel implements IWindow {
151

    
152
        private final String START_IMAGE = "<img src=\"images/drapeau_depart.gif\">";
153
        private final String STOP_IMAGE = "<img src=\"images/drapeau_arrivee.gif\">";
154
        private final String LEFT_IMAGE = "<img src=\"images/turn-left.png\">";
155
        private final String RIGHT_IMAGE = "<img src=\"images/turn-right.png\">";
156
        private final String STRAIGHT_IMAGE = "<img src=\"images/gtk-go-up.png\">";
157

    
158
        private final String LINE_SEPARATOR = "<hr width =\"70%\">";
159

    
160
        private Route route;
161

    
162
        private JScrollPane scrollPanel;
163
        private JEditorPane htmlPanel;
164
        private WindowInfo viewInfo;
165

    
166
        private StringBuilder htmlText;
167
        private MapControl mapControl;
168

    
169
        // Para poder poner duraci?n, etc.
170
        private String weightText = "Longitud:";
171

    
172
        // Distancias y costes acumulados (entre dos cambios de calles)
173
        private double acumuledLenght = 0d;
174
        private double acumuledWeight = 0d;
175

    
176
        private double totalLenght = 0d;
177
        private double totalWeight = 0d;
178

    
179
        private int numberOfStreets = 1;// partimos de 1 porque consideramos la
180
                                                                        // salida
181
        private ArrayList tramesOfSameStreet = new ArrayList();
182

    
183
        NumberFormat nf = NumberFormat.getInstance();
184

    
185
        public RouteReportPanel(Route route, MapControl mapControl) {
186
                super();
187
                setLayout(new BorderLayout());
188
                this.route = route;
189
                this.mapControl = mapControl;
190
                scrollPanel = new JScrollPane();
191
                htmlPanel = new JEditorPane();
192
                htmlPanel.setEditable(false);
193
                HTMLEditorKit kit = new HTMLEditorKit();
194
                // {
195
                // public ViewFactory getViewFactory()
196
                // {
197
                // return new HTMLFactory()
198
                // {
199
                // public View create(Element elem)
200
                // {
201
                // View view = super.create(elem);
202
                //
203
                // if (view instanceof ImageView)
204
                // {
205
                // ((javax.swing.text.html.ImageView) view).setLoadsSynchronously(true);
206
                // }
207
                // return view;
208
                // }
209
                // };
210
                // }
211
                // };
212
                htmlPanel.setEditorKit(kit);
213

    
214
                nf.setMaximumFractionDigits(2);
215

    
216
                final MapControl map = mapControl;
217
                final Route routeTemp = route;
218
                final MapControl mapControlTemp = mapControl;
219
                htmlPanel.addHyperlinkListener(new HyperlinkListener() {
220
                        ArrayList previousSelection;
221

    
222
                        public void hyperlinkUpdate(HyperlinkEvent e) {
223
                                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
224
                                        Rectangle2D bounds = null;
225
                                        StringTokenizer st = new StringTokenizer(
226
                                                        e.getDescription(), ",");
227
                                        System.err.println("E.GETDESCRIPTION():"
228
                                                        + e.getDescription());
229
                                        ArrayList features = new ArrayList();
230
                                        while (st.hasMoreTokens()) {
231
                                                int indexOfFeature = Integer.parseInt(st.nextToken());
232
                                                IFeature feature = (IFeature) routeTemp
233
                                                                .getFeatureList().get(indexOfFeature);
234
                                                if (bounds == null)
235
                                                        bounds = feature.getGeometry().getBounds2D();
236
                                                else
237
                                                        bounds.add(feature.getGeometry().getBounds2D());
238
                                                features.add(feature);
239
                                        }
240
                                        if (bounds != null) {
241
                                                bounds = expand(bounds);
242
                                                GraphicLayer graphicLayer = mapControlTemp
243
                                                                .getMapContext().getGraphicsLayer();
244
                                                if (previousSelection != null) {
245
                                                        for (int i = 0; i < previousSelection.size(); i++) {
246
                                                                graphicLayer
247
                                                                                .removeGraphic((FGraphic) previousSelection
248
                                                                                                .get(i));
249
                                                        }
250
                                                }
251
                                                previousSelection = new ArrayList();
252

    
253
                                                ILineSymbol lineSymbol = SymbologyFactory
254
                                                                .createDefaultLineSymbol();
255
                                                lineSymbol.setLineColor(Color.YELLOW);
256
                                                lineSymbol.setLineWidth(3.0f);
257
                                                int idSymbolLine = graphicLayer.addSymbol(lineSymbol);
258
                                                for (int i = 0; i < features.size(); i++) {
259
                                                        IGeometry gAux = ((IFeature) features.get(i))
260
                                                                        .getGeometry();
261
                                                        FGraphic graphic = new FGraphic(gAux, idSymbolLine);
262
                                                        graphic.setTag("ROUTE");
263
                                                        graphicLayer.addGraphic(graphic);
264
                                                        previousSelection.add(graphic);
265
                                                }
266
                                                mapControlTemp.drawGraphics();
267
                                                map.getMapContext().getViewPort().setExtent(bounds);
268
                                        }
269
                                }
270
                        }
271
                });
272
                initialize();
273
                scrollPanel.setViewportView(htmlPanel);
274
                add(scrollPanel, BorderLayout.CENTER);
275
                JButton btn = new JButton(_T("Print"));
276
                btn.addActionListener(new ActionListener() {
277

    
278
                        public void actionPerformed(ActionEvent e) {
279
                                printReport();
280
                        }
281

    
282
                });
283
                add(btn, BorderLayout.SOUTH);
284
        }
285

    
286
        /**
287
         * Prints report (html)
288
         */
289
        protected void printReport() {
290
                try {
291
                        htmlPanel.print();
292
                } catch (PrinterException e) {
293
                        e.printStackTrace();
294
                        JOptionPane.showMessageDialog((Component) PluginServices.getMDIManager().getActiveWindow(),
295
                                        e.getLocalizedMessage());
296
                }
297
        }
298

    
299
        private Rectangle2D expand(Rectangle2D rect) {
300
                double xmin = rect.getMinX();
301
                double ymin = rect.getMinY();
302
                double width = rect.getWidth();
303
                double height = rect.getHeight();
304

    
305
                xmin -= width;
306
                ymin -= height;
307
                double newWidth = width * 2;
308
                double newHeight = height * 2;
309
                return new Rectangle2D.Double(xmin, ymin, newWidth, newHeight);
310
        }
311

    
312
        public void setRoute(Route route) {
313
                this.route = route;
314
                initialize();
315
        }
316

    
317
        private void initialize() {
318
                htmlText = new StringBuilder("<head>");
319
                htmlText.append("<style type='text/css'>");
320
                htmlText.append("<!-- ");
321
                htmlText.append("  .normal { ");
322
                htmlText
323
                                .append("        font-family: Arial, Helvetica, sans-serif;        font-size: 10px; font-style: normal; color: #333333;");
324
                htmlText.append("}");
325
                htmlText.append("  a { ");
326
                htmlText
327
                                .append("        font-family: Arial, Helvetica, sans-serif;        font-size: 10px; font-style: italic; font-weight: bold;");
328
                htmlText.append("}");
329
                htmlText.append("  h1 { ");
330
                htmlText
331
                                .append("        font-family: Arial, Helvetica, sans-serif;        font-size: 14px;");
332
                htmlText.append("}");
333
                htmlText.append("  .distancia { ");
334
                htmlText
335
                                .append("        font-family: Arial, Helvetica, sans-serif;        font-size: 10px; color: #666666;");
336
                htmlText.append("}");
337
                htmlText.append("  .resumen { ");
338
                htmlText
339
                                .append("        font-family: Arial, Helvetica, sans-serif;        font-size: 12px; color: #333333;");
340
                htmlText.append("}");
341
                htmlText.append("  .left { ");
342
                htmlText.append("        font-weight: bold; color: #990000;");
343
                htmlText.append("}");
344
                htmlText.append("  .right { ");
345
                htmlText.append("        font-weight: bold; color: #0033FF;");
346
                htmlText.append("}");
347

    
348
                htmlText.append(" -->");
349
                htmlText.append("</style>");
350
                htmlText.append("</head>");
351
                htmlText.append("<body>");
352
                ArrayList features = route.getFeatureList();
353

    
354
                // Route is ordered from the the start to the end
355
                IFeature firstFeature = (IFeature) features.get(0);
356
                IFeature lastFeature = (IFeature) features.get(features.size() - 1);
357

    
358
                renderHeader(firstFeature, lastFeature);
359
                renderFirstStrech(firstFeature);
360

    
361
                IFeature previousFeature = firstFeature;
362
                for (int i = 1; i < features.size(); i++) {
363
                        IFeature feature = (IFeature) features.get(i);
364
                        renderStrech(feature, previousFeature, i);
365
                        previousFeature = feature;
366
                }
367

    
368
                // TODO
369
                // Invertir el FIRST y el LAST
370
                // Borrar el graphics resaltado cuando se resalte otro
371
                renderLastStretch((IFeature) features.get(features.size() - 1),
372
                                previousFeature);
373
                htmlText.append("</body>");
374
                // System.out.println(htmlText);
375
                htmlPanel.setText(htmlText.toString());
376

    
377
        }
378

    
379
        private void renderHeader(IFeature firstFeature, IFeature lastFeature) {
380
                String startName = firstFeature.getAttribute(Route.TEXT_INDEX)
381
                                .toString();
382
                String stopName = lastFeature.getAttribute(Route.TEXT_INDEX).toString();
383
                htmlText.append("<h1>");
384
                htmlText.append(_T("Route_report") + ":" + startName + "-" + stopName);
385
                htmlText.append("</h1><br>");
386
                htmlText.append("<span class='resumen'>" + _T("Start_from") + ": <b>");
387
                htmlText.append(startName);
388
                htmlText.append("</b><br>");
389
                htmlText.append(_T("Arrival_to") + ":<b> ");
390
                htmlText.append(stopName + "</b><br>");
391

    
392
                htmlText.append(_T("Total_length") + ": <b>"
393
                                + nf.format(getLengthOfRoute()) + "</b></span>");
394
                htmlText.append(LINE_SEPARATOR);
395
        }
396

    
397
        private double getLengthOfRoute() {
398
                double solution = 0d;
399
                ArrayList featureList = route.getFeatureList();
400
                for (int i = 0; i < featureList.size(); i++) {
401
                        IFeature feature = (IFeature) featureList.get(i);
402
                        solution += ((DoubleValue) feature.getAttribute(Route.LENGTH_INDEX))
403
                                        .getValue();
404
                }
405
                return solution;
406
        }
407

    
408
        private void renderFirstStrech(IFeature feature) {
409
                htmlText.append("<table>");
410
                htmlText.append("<tr>");
411
                htmlText.append("<td width='40'>");
412
                htmlText.append(START_IMAGE);
413
                htmlText.append("</td>");
414
                htmlText.append("<td class='normal'>");
415
                htmlText.append("1. " + _T("Start_from") + ":<b> ");
416
                htmlText.append(feature.getAttribute(Route.TEXT_INDEX));
417
                htmlText.append("</b></td></tr>");
418
                htmlText.append("<tr>");
419
                htmlText.append("<td width='40'></td><td><a href=\"" + 0 + "\">"
420
                                + _T("Show_in_map") + "</a><td></tr>");
421
                htmlText.append("</table>");
422
                htmlText.append(LINE_SEPARATOR);
423

    
424
                double length = ((DoubleValue) feature.getAttribute(Route.LENGTH_INDEX))
425
                                .getValue();
426
                double weight = ((DoubleValue) feature.getAttribute(Route.WEIGHT_INDEX))
427
                                .getValue();
428
                acumuledLenght += length;
429
                acumuledWeight += weight;
430

    
431
                totalLenght += length;
432
                totalWeight += weight;
433

    
434
                tramesOfSameStreet.add(new Integer(0));
435
        }
436

    
437
        private String _T(String str) {
438
                return PluginServices.getText(this, str);
439
        }
440

    
441
        private void renderStrech(IFeature feature, IFeature previousFeature,
442
                        int index) {
443
                String street1 = previousFeature.getAttribute(Route.TEXT_INDEX)
444
                                .toString();
445
                String street2 = feature.getAttribute(Route.TEXT_INDEX).toString();
446
                boolean changeStreet = !street1.equalsIgnoreCase(street2);
447
                double length = ((DoubleValue) feature.getAttribute(Route.LENGTH_INDEX))
448
                                .getValue();
449
                double weight = ((DoubleValue) feature.getAttribute(Route.WEIGHT_INDEX))
450
                                .getValue();
451

    
452
                String textoTramo = null;
453
                String imageTurn = null;
454

    
455
                if (changeStreet) {
456
                        numberOfStreets++;
457
                        String prefix = _T("follow") + " <b>" + street1 + "</b> "
458
                                        + _T("during") + " " + nf.format(acumuledLenght) + " "
459
                                        + _T("and");
460
                        int direction = TurnUtil.getDirection(previousFeature, feature);
461

    
462
                        if (direction == TurnUtil.GO_STRAIGH_ON) {
463
                                textoTramo = prefix + " " + _T("continue_by") + " <b> "
464
                                                + street2 + "</b>";
465
                                imageTurn = STRAIGHT_IMAGE;
466

    
467
                        } else if (direction == TurnUtil.TURN_LEFT) {
468
                                textoTramo = prefix + " " + _T("turn") + " "
469
                                                + "<span class='left'><b> " + _T("left")
470
                                                + "</b></span> " + _T("by_turn") + " <b>" + street2
471
                                                + "</b> ";
472
                                imageTurn = LEFT_IMAGE;
473

    
474
                        } else if (direction == TurnUtil.TURN_RIGHT) {
475
                                textoTramo = prefix + " " + _T("turn")
476
                                                + " <span class='right'><b> " + _T("right")
477
                                                + " </b></span> " + _T("by_turn") + " <b>" + street2
478
                                                + "</b>";
479
                                imageTurn = RIGHT_IMAGE;
480
                        }
481
                        htmlText.append("<table>");
482
                        htmlText.append("<tr>");
483
                        htmlText.append("<td width='40'>");
484
                        htmlText.append(imageTurn);
485
                        htmlText.append("</td>");
486
                        htmlText.append("<td class='normal'>");
487
                        htmlText.append(numberOfStreets + " " + textoTramo);// TODO INTERNAC
488
                        htmlText.append("</td></tr>");
489
                        htmlText.append("<tr>");
490
                        htmlText.append("<td with='40'></td><td class='distancia'>"
491
                                        + _T("Accumulated_distance") + ":" + nf.format(totalLenght)
492
                                        + "</td></tr>");
493

    
494
                        if (!weightText.equalsIgnoreCase("Longitud:")) {
495
                                htmlText.append("<tr>");
496
                                htmlText.append("<td with='40'></td><td class='distancia'>"
497
                                                + _T("cost") + ":" + nf.format(totalWeight)
498
                                                + "</td></tr>");
499
                        }
500

    
501
                        String features = "";
502
                        for (int i = 0; i < tramesOfSameStreet.size() - 1; i++) {
503
                                int featureIndex = ((Integer) tramesOfSameStreet.get(i))
504
                                                .intValue();
505
                                features += featureIndex + ",";
506
                        }
507

    
508
                        features += ((Integer) tramesOfSameStreet.get(tramesOfSameStreet
509
                                        .size() - 1)).intValue();
510
                        // System.out.println("features = " + features);
511
                        htmlText.append("<tr><td with='40'></td><td><a href=\"" + features
512
                                        + "\">" + _T("Show_in_map") + "</a><td></tr>");
513
                        htmlText.append("</table>");
514
                        htmlText.append(LINE_SEPARATOR);
515

    
516
                        acumuledLenght = length;
517
                        acumuledWeight = weight;
518
                        tramesOfSameStreet.clear();
519

    
520
                } else {
521
                        acumuledLenght += length;
522
                        acumuledWeight += weight;
523
                }
524

    
525
                tramesOfSameStreet.add(new Integer(index));
526
                totalLenght += length;
527
                totalWeight += weight;
528

    
529
        }
530

    
531
        private void renderLastStretch(IFeature feature, IFeature previousFeature) {
532

    
533
                // double length =
534
                // ((DoubleValue)feature.getAttribute(Route.LENGTH_INDEX)).getValue();
535
                // double weight =
536
                // ((DoubleValue)feature.getAttribute(Route.WEIGHT_INDEX)).getValue();
537
                //                
538
                //                
539
                // totalLenght += length;
540
                // totalWeight += weight;
541

    
542
                htmlText.append("<table>");
543
                htmlText.append("<tr>");
544
                htmlText.append("<td width='40'>");
545
                htmlText.append(STOP_IMAGE);
546
                htmlText.append("</td>");
547
                htmlText.append("<td class='resumen'>");
548
                htmlText.append(numberOfStreets + ". " + _T("Arrival_to") + ": ");
549
                htmlText.append(feature.getAttribute(Route.TEXT_INDEX));
550
                htmlText.append("</td></tr>");
551
                htmlText.append("<tr><td with='40'></td><td class='resumen'>"
552
                                + _T("Accumulated_distance") + ":" + nf.format(totalLenght)
553
                                + "</td></tr>");
554

    
555
                if (!weightText.equalsIgnoreCase("Longitud:"))
556
                        htmlText.append("<tr><td with='40'></td><td class='resumen'>Coste:"
557
                                        + totalWeight + "</td></tr>");
558
                htmlText.append("<tr><td with='40'></td><td><a href=\""
559
                                + (route.getFeatureList().size() - 1) + "\">"
560
                                + _T("Show_in_map") + "</a><td></tr>");
561
                htmlText.append("</table>");
562
        }
563

    
564
        public WindowInfo getWindowInfo() {
565
                if (viewInfo == null) {
566
                        viewInfo = new WindowInfo(WindowInfo.MODELESSDIALOG
567
                                        | WindowInfo.RESIZABLE | WindowInfo.MAXIMIZABLE
568
                                        | WindowInfo.ICONIFIABLE | WindowInfo.PALETTE);
569
                        viewInfo.setTitle(_T("route_report_title"));// Internacionalizar
570
                                                                                                                // esto
571
                        viewInfo.setWidth(400);
572
                        viewInfo.setHeight(350);
573
                }
574
                return viewInfo;
575
        }
576

    
577
        public Object getWindowProfile() {
578
                return WindowInfo.TOOL_PROFILE;
579
        }
580

    
581
}