Statistics
| Revision:

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

History | View | Annotate | Download (30.2 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
 *
44
 * $Id: RouteControlPanel.java 8616 2006-11-08 16:48:20Z fjp $
45
 * $Log$
46
 * Revision 1.11  2006-11-08 16:48:19  fjp
47
 * *** empty log message ***
48
 *
49
 * Revision 1.10  2006/11/08 16:00:39  fjp
50
 * Por terminar el enlace flags-cuadro de di?logo
51
 *
52
 * Revision 1.9  2006/11/08 13:18:46  fjp
53
 * Por terminar el enlace flags-cuadro de di?logo
54
 *
55
 * Revision 1.8  2006/11/07 19:49:38  azabala
56
 * *** empty log message ***
57
 *
58
 * Revision 1.7  2006/11/06 13:13:53  azabala
59
 * *** empty log message ***
60
 *
61
 * Revision 1.6  2006/11/06 10:29:32  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.5  2006/11/03 19:39:29  azabala
65
 * *** empty log message ***
66
 *
67
 * Revision 1.4  2006/10/27 18:26:22  azabala
68
 * added implementation of load stages method
69
 *
70
 * Revision 1.3  2006/10/27 12:41:09  jaume
71
 * GUI
72
 *
73
 * Revision 1.2  2006/10/26 16:31:21  jaume
74
 * GUI
75
 *
76
 * Revision 1.1  2006/10/25 10:50:41  jaume
77
 * movement of classes and gui stuff
78
 *
79
 * Revision 1.4  2006/10/24 08:04:41  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.3  2006/10/23 16:00:20  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.2  2006/10/23 08:05:39  jaume
86
 * GUI
87
 *
88
 * Revision 1.1  2006/10/20 12:02:50  jaume
89
 * GUI
90
 *
91
 *
92
 */
93
package com.iver.cit.gvsig.graph.gui;
94

    
95
import java.awt.BorderLayout;
96
import java.awt.Color;
97
import java.awt.Component;
98
import java.awt.Dimension;
99
import java.awt.FlowLayout;
100
import java.awt.Font;
101
import java.awt.GridLayout;
102
import java.awt.event.ActionEvent;
103
import java.awt.event.ActionListener;
104
import java.util.ArrayList;
105
import java.util.Arrays;
106
import java.util.EventObject;
107
import java.util.List;
108

    
109
import javax.swing.ImageIcon;
110
import javax.swing.JCheckBox;
111
import javax.swing.JComboBox;
112
import javax.swing.JLabel;
113
import javax.swing.JPanel;
114
import javax.swing.JScrollPane;
115
import javax.swing.JTable;
116
import javax.swing.JTextField;
117
import javax.swing.event.CellEditorListener;
118
import javax.swing.table.DefaultTableCellRenderer;
119
import javax.swing.table.DefaultTableModel;
120
import javax.swing.table.TableCellEditor;
121
import javax.swing.table.TableCellRenderer;
122
import javax.swing.table.TableColumnModel;
123
import javax.swing.table.TableModel;
124

    
125
import org.cresques.cts.IProjection;
126
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
127
import org.gvsig.gui.beans.swing.JBlank;
128
import org.gvsig.gui.beans.swing.JButton;
129

    
130
import com.hardcode.gdbms.engine.values.Value;
131
import com.hardcode.gdbms.engine.values.ValueFactory;
132
import com.iver.andami.PluginServices;
133
import com.iver.andami.ui.mdiManager.IWindow;
134
import com.iver.andami.ui.mdiManager.WindowInfo;
135
import com.iver.cit.gvsig.fmap.DriverException;
136
import com.iver.cit.gvsig.fmap.MapContext;
137
import com.iver.cit.gvsig.fmap.MapControl;
138
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
139
import com.iver.cit.gvsig.fmap.core.FGeometry;
140
import com.iver.cit.gvsig.fmap.core.FPoint2D;
141
import com.iver.cit.gvsig.fmap.core.FShape;
142
import com.iver.cit.gvsig.fmap.core.IGeometry;
143
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
144
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
145
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
146
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
147
import com.iver.cit.gvsig.fmap.edition.EditionException;
148
import com.iver.cit.gvsig.fmap.layers.FLayer;
149
import com.iver.cit.gvsig.fmap.layers.FLayers;
150
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
151
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
152
import com.iver.cit.gvsig.fmap.layers.LayerEvent;
153
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
154
import com.iver.cit.gvsig.fmap.layers.LayerListenerAdapter;
155
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
156
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
157
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
158
import com.iver.cit.gvsig.graph.GraphException;
159
import com.iver.cit.gvsig.graph.NetworkUtils;
160
import com.iver.cit.gvsig.graph.core.GvFlag;
161
import com.iver.cit.gvsig.graph.core.IFlagListener;
162
import com.iver.cit.gvsig.graph.core.Network;
163
import com.iver.cit.gvsig.graph.solvers.FlagsMemoryDriver;
164
import com.iver.cit.gvsig.graph.solvers.Route;
165
import com.iver.cit.gvsig.graph.solvers.RouteMemoryDriver;
166
import com.iver.cit.gvsig.graph.tools.FlagListener;
167
import com.iver.cit.gvsig.project.documents.gui.FOpenDialog;
168
import com.iver.cit.gvsig.project.documents.view.gui.IView;
169
import com.iver.cit.gvsig.project.documents.view.gui.View;
170
import com.iver.cit.gvsig.util.GvSession;
171
import com.vividsolutions.jts.geom.Coordinate;
172
import com.vividsolutions.jts.geom.Geometry;
173
import com.vividsolutions.jts.geom.GeometryFactory;
174
import com.vividsolutions.jts.geom.MultiPoint;
175
import com.vividsolutions.jts.geom.Point;
176
import com.vividsolutions.jts.index.ItemVisitor;
177

    
178
public class RouteControlPanel extends JPanel implements IWindow,
179
                ActionListener, IFlagListener {
180
        WindowInfo wi;
181

    
182
        private JPanel westPanel = null;
183

    
184
        private JScrollPane scrlStages = null;
185

    
186
        private JTable tblStages = null;
187

    
188
        private GridBagLayoutPanel eastPanel = null;
189

    
190
        private DefaultTableModel tableModel = null; // @jve:decl-index=0:visual-constraint=""
191

    
192
        private JButton btnLoadStage = null;
193

    
194
        private JButton btnSaveStage = null;
195
        
196
        private Network network;
197

    
198
        private ArrayList routeFlags;
199

    
200
        private JLabel lblCost;
201

    
202
        private JLabel lblFlagAmout;
203

    
204
        private JButton btnPullDownStage;
205

    
206
        private JButton btnPushUpStage;
207

    
208
        private JButton btnRemoveStage;
209

    
210
        private JPanel southPanel;
211

    
212
        private final String[] colName = new String[] {
213
                        PluginServices.getText(this, "enable"),
214
                        PluginServices.getText(this, "stage"),
215
                        PluginServices.getText(this, "cost"), };
216

    
217
        private JButton btnGenerateRoute;
218

    
219
        private JButton btnSaveRoute;
220

    
221
        private MapControl mapCtrl;
222

    
223
        /**
224
         * This method initializes
225
         * 
226
         */
227
        public RouteControlPanel(Network network) {
228
                super();
229
                this.network = network;
230
//                GvFlag[] flags = network.getFlags();
231
//                List vflags = _getFlags();
232
//                for (int i = 0; i < flags.length; i++) {
233
//                        vflags.add(flags[i]);
234
//                }
235
                initialize();
236
        }
237
        
238
        /**
239
         * Method to remove all flags (use
240
         * */
241
        public void refresh(){
242
//                GvFlag[] flags = network.getFlags();
243
//                List vflags = _getFlags();
244
//                List tempFlags = Arrays.asList(flags);
245
//                vflags.addAll(tempFlags);
246
                lblFlagAmout.setText(String.valueOf(_getFlags()
247
                                .size()));
248
                updateTotalCost();
249
                getTblStages().repaint();
250
                
251
        }
252
        private void processLayer(FLayer layer) {
253
                if (layer.isActive()) {
254
                        if (layer instanceof FLyrVect) {
255
                                Network net = (Network) layer
256
                                                .getProperty("network");
257
                                if (net != null) {
258
                                        if(net == network)
259
                                                return;
260
                                        network = net;
261
                                        network.addFlagListener(this);
262
//                                        GvFlag[] newFlags = net.getFlags();
263
//                                        _getFlags().clear();
264
//                                        _getFlags().addAll(Arrays.asList(newFlags));
265
                                        refresh();
266
                                }// if
267
                        }// if
268
                }// if
269

    
270
        }
271
        
272
        public void setMapControl(MapControl mapCtrl) {
273
                if (mapCtrl != null) {
274
                        this.mapCtrl = mapCtrl;
275
                        LayerListenerAdapter listener = new LayerListenerAdapter() {
276
                                public void activationChanged(LayerEvent e) {
277
                                        processLayer(e.getSource());
278
                                }
279
                        };
280
                        mapCtrl.getMapContext().getLayers().addLayerListener(listener);
281
                        mapCtrl.getMapContext().getLayers().addLayerCollectionListener(
282
                                        listener);
283
                }
284
        }
285

    
286
        public RouteControlPanel() {
287
                super();
288
                initialize();
289
        }
290

    
291
        public void setCostUnits(String unitsName) {
292
                colName[2] = unitsName;
293
                getTblStages().repaint();
294
        }
295

    
296
        /**
297
         * This method initializes this
298
         * 
299
         */
300
        private void initialize() {
301
                BorderLayout borderLayout = new BorderLayout();
302
                borderLayout.setHgap(10);
303
                borderLayout.setVgap(10);
304
                JPanel cont = new JPanel(borderLayout);
305
                cont.setPreferredSize(new Dimension(400, 320));
306
                this.setSize(new Dimension(400, 320));
307
                cont.add(getWestPanel(), BorderLayout.WEST);
308
                cont.add(getEastPanel(), BorderLayout.CENTER);
309
                cont.add(getSouthPanel(), BorderLayout.SOUTH);
310
                this.add(cont);
311
                updateFlags();
312
        }
313

    
314
        private void updateFlags() {
315
                lblFlagAmout.setText(String.valueOf(_getFlags().size()));
316
                updateTotalCost();
317
                getTblStages().repaint();
318
        }
319

    
320
        private JPanel getSouthPanel() {
321
                if (southPanel == null) {
322
                        southPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
323
                        southPanel.add(getBtnRemoveStage());
324
                        southPanel.add(new JBlank(50, 20));
325
                        southPanel.add(getBtnPushUpStage());
326
                        southPanel.add(getBtnPullDownStage());
327
                        southPanel.add(new JLabel(PluginServices.getText(this,
328
                                        "flag_amount")));
329
                        lblFlagAmout = new JLabel();
330
                        lblFlagAmout.setFont(lblFlagAmout.getFont().deriveFont(Font.BOLD));
331
                        southPanel.add(lblFlagAmout);
332
                }
333
                return southPanel;
334
        }
335

    
336
        public Object getWindowModel() {
337
                return this.getClass();
338
        }
339

    
340
        public WindowInfo getWindowInfo() {
341
                if (wi == null) {
342
                        wi = new WindowInfo(WindowInfo.MODELESSDIALOG
343
                                        | WindowInfo.RESIZABLE | WindowInfo.MAXIMIZABLE
344
                                        | WindowInfo.ICONIFIABLE | WindowInfo.PALETTE);
345
                        wi.setWidth(450);
346
                        wi.setHeight(320);
347
                        wi.setTitle(PluginServices.getText(this, "route_control_panel")
348
                                        + "...");
349
                }
350
                return wi;
351
        }
352

    
353
        /**
354
         * This method initializes westPanel
355
         * 
356
         * @return javax.swing.JPanel
357
         */
358
        private JPanel getWestPanel() {
359
                if (westPanel == null) {
360
                        westPanel = new JPanel(new BorderLayout(5, 5));
361
                        lblCost = new JLabel();
362
                        lblCost.setFont(lblCost.getFont().deriveFont(Font.BOLD));
363
                        GridBagLayoutPanel aux = new GridBagLayoutPanel();
364
                        aux.addComponent(PluginServices.getText(this, "total_route_cost")
365
                                        + ":", lblCost);
366
                        aux.addComponent(getScrlStages());
367

    
368
                        westPanel.add(aux);
369
                }
370
                return westPanel;
371
        }
372

    
373
        private JButton getBtnPullDownStage() {
374
                if (btnPullDownStage == null) {
375
                        btnPullDownStage = new JButton(new ImageIcon(this.getClass()
376
                                        .getClassLoader().getResource("images/down-arrow.png")));
377
                        // btnPullDownStage.setName("btnPullDownStage");
378
                        btnPullDownStage.addActionListener(this);
379
                }
380
                return btnPullDownStage;
381
        }
382

    
383
        private JButton getBtnPushUpStage() {
384
                if (btnPushUpStage == null) {
385
                        btnPushUpStage = new JButton(new ImageIcon(this.getClass()
386
                                        .getClassLoader().getResource("images/up-arrow.png")));
387
                        // btnPushUpStage.setName("btnPushUpStage");
388
                        btnPushUpStage.addActionListener(this);
389
                }
390
                return btnPushUpStage;
391
        }
392

    
393
        private JButton getBtnRemoveStage() {
394
                if (btnRemoveStage == null) {
395
                        btnRemoveStage = new JButton(new ImageIcon(this.getClass()
396
                                        .getClassLoader().getResource("images/delete.png")));
397
                        // btnRemoveStage.setName("btnRemoveStage");
398
                        btnRemoveStage.addActionListener(this);
399
                }
400
                return btnRemoveStage;
401
        }
402

    
403
        /**
404
         * This method initializes scrlStages
405
         * 
406
         * @return javax.swing.JScrollPane
407
         */
408
        private JScrollPane getScrlStages() {
409
                if (scrlStages == null) {
410
                        scrlStages = new JScrollPane();
411
                        scrlStages.setViewportView(getTblStages());
412
                        scrlStages.setPreferredSize(new Dimension(270, 200));
413
                }
414
                return scrlStages;
415
        }
416

    
417
        /**
418
         * @deprecated
419
         * @param flag
420
         */
421
        public void addFlag(GvFlag flag) {
422
                _getFlags().add(flag);
423
                lblFlagAmout.setText(String.valueOf(_getFlags().size()));
424
                updateTotalCost();
425
                getTblStages().repaint();
426
        }
427

    
428
        private void updateTotalCost() {
429
                GvFlag[] flags = getFlags();
430
                double cost = 0;
431
                for (int i = 0; i < flags.length; i++) {
432
                        cost += flags[i].getCost();
433
                }
434
                lblCost.setText(cost + getCostUnitName());
435
        }
436

    
437
        private String getCostUnitName() {
438
                if (colName[2].equals(PluginServices.getText(this, "cost")))
439
                        return "";
440
                return colName[2];
441
        }
442

    
443
        public void removeFlag(GvFlag flag) {
444
                removeFlag(_getFlags().indexOf(flag));
445
        }
446

    
447
        public void removeFlag(int index) {
448
                _getFlags().remove(index);
449
                lblFlagAmout.setText(String.valueOf(_getFlags().size()));
450
                getTblStages().repaint();
451
        }
452

    
453
        /**
454
         * This method initializes tblStages
455
         * 
456
         * @return javax.swing.JTable
457
         */
458
        private JTable getTblStages() {
459
                if (tblStages == null) {
460
                        tblStages = new JTable();
461
                        tblStages
462
                                        .setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
463
                        tblStages.setModel(getTableModel());
464
                        TableCellRenderer tcr = new DefaultTableCellRenderer() {
465

    
466
                                public Component getTableCellRendererComponent(JTable table,
467
                                                Object value, boolean isSelected, boolean hasFocus,
468
                                                final int row, int column) {
469
                                        Object myValue = null;
470
                                        if (row >= getFlagCount())
471
                                                return null;
472
                                        if (column == 0) {
473
                                                JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER,
474
                                                                5, 0));
475

    
476
                                                final JCheckBox chk = new JCheckBox("", isSelected(row));
477

    
478
                                                p.setBackground(Color.WHITE);
479
                                                p.add(chk);
480
                                                return p;
481
                                        } else if (column == 1) {
482
                                                myValue = ((GvFlag) _getFlags().get(row)).toString();
483
                                        } else if (column == 2) {
484
                                                myValue = String
485
                                                                .valueOf(((GvFlag) _getFlags().get(row))
486
                                                                                .getCost());
487
                                        }
488
                                        return super.getTableCellRendererComponent(table, myValue,
489
                                                        isSelected, hasFocus, row, column);
490
                                }
491
                        };
492
                        TableCellEditor tce = new TableCellEditor() {
493

    
494
                                public Component getTableCellEditorComponent(JTable table,
495
                                                Object value, boolean isSelected, final int row,
496
                                                int column) {
497
                                        if (row > getFlagCount() - 1) {
498
                                                return null;
499
                                        }
500

    
501
                                        if (column == 0) {
502
                                                JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER,
503
                                                                5, 0));
504
                                                final JCheckBox chk = new JCheckBox("", isSelected(row));
505
                                                chk.addActionListener(new ActionListener() {
506
                                                        public void actionPerformed(
507
                                                                        java.awt.event.ActionEvent e) {
508
                                                                ((GvFlag) _getFlags().get(row)).setEnabled(chk
509
                                                                                .isSelected());
510
                                                        };
511
                                                });
512
                                                p.setBackground(Color.WHITE);
513
                                                p.add(chk);
514
                                                return p;
515
                                        } else if (column == 1) {
516
                                                final JTextField txt = new JTextField(
517
                                                                ((GvFlag) _getFlags().get(row)).toString());
518
                                                txt.addActionListener(new ActionListener() {
519
                                                        public void actionPerformed(ActionEvent e) {
520
                                                                GvFlag flag = (GvFlag) _getFlags().get(row);
521
                                                                flag.setDescription(txt.getText());
522
                                                        }
523
                                                });
524
                                                return txt;
525
                                        }
526
                                        return null;
527

    
528
                                }
529

    
530
                                public void cancelCellEditing() {
531
                                }
532

    
533
                                public boolean stopCellEditing() {
534
                                        return false;
535
                                }
536

    
537
                                public Object getCellEditorValue() {
538
                                        return null;
539
                                }
540

    
541
                                public boolean isCellEditable(EventObject anEvent) {
542
                                        return true;
543
                                }
544

    
545
                                public boolean shouldSelectCell(EventObject anEvent) {
546
                                        return false;
547
                                }
548

    
549
                                public void addCellEditorListener(CellEditorListener l) {
550
                                }
551

    
552
                                public void removeCellEditorListener(CellEditorListener l) {
553
                                }
554

    
555
                        };
556
                        TableColumnModel cm = tblStages.getColumnModel();
557
                        for (int i = 0; i < tblStages.getColumnCount(); i++) {
558
                                cm.getColumn(0).setCellEditor(tce);
559
                                cm.getColumn(0).setCellRenderer(tcr);
560
                        }
561

    
562
                        int tablePreferredWidth = (int) tblStages.getPreferredSize()
563
                                        .getWidth();
564
                        int colSize = tblStages.getFontMetrics(tblStages.getFont())
565
                                        .stringWidth(tblStages.getModel().getColumnName(0)) * 2;
566
                        cm.getColumn(0).setPreferredWidth((int) (colSize));
567
                        cm.getColumn(0).setMinWidth((int) (colSize));
568
                        cm.getColumn(0).setMaxWidth((int) (colSize));
569
                        tablePreferredWidth -= colSize;
570
                        cm.getColumn(1)
571
                                        .setPreferredWidth((int) (tablePreferredWidth * 0.7));
572
                        cm.getColumn(2)
573
                                        .setPreferredWidth((int) (tablePreferredWidth * 0.3));
574
                }
575
                return tblStages;
576
        }
577

    
578
        protected int getFlagCount() {
579
                return _getFlags().size();
580
        }
581

    
582
        private List _getFlags() {
583
//                if (routeFlags == null) {
584
//                        routeFlags = new ArrayList();
585
//                }
586
//                return routeFlags;
587
                        
588
                return network.getOriginaFlags();
589
        }
590

    
591
        protected boolean isSelected(int row) {
592
                return ((GvFlag) _getFlags().get(row)).isEnabled();
593
        }
594

    
595
        /**
596
         * This method initializes eastPanel
597
         * 
598
         * @return javax.swing.JPanel
599
         */
600
        private JPanel getEastPanel() {
601
                if (eastPanel == null) {
602
                        GridLayout layout = new GridLayout();
603
                        layout.setColumns(1);
604
                        layout.setVgap(5);
605
                        JPanel aux = new JPanel();
606
                        aux.add(getBtnLoadStage());
607
                        aux.add(getBtnSaveStage());
608
                        aux.add(getBtnSaveRoute());
609
                        aux.add(getBtnGenerateReport());
610
                        layout.setRows(aux.getComponentCount());
611
                        aux.setLayout(layout);
612
                        eastPanel = new GridBagLayoutPanel();
613
                        eastPanel.addComponent(aux);
614
                }
615
                return eastPanel;
616
        }
617

    
618
        private JButton getBtnGenerateReport() {
619
                if (btnGenerateRoute == null) {
620
                        btnGenerateRoute = new JButton();
621
                        btnGenerateRoute.setText(PluginServices.getText(this,
622
                                        "generate_report"));
623
                        btnGenerateRoute.addActionListener(this);
624
                }
625
                return btnGenerateRoute;
626
        }
627

    
628
        private JButton getBtnSaveRoute() {
629
                if (btnSaveRoute == null) {
630
                        btnSaveRoute = new JButton();
631
                        btnSaveRoute.setText(PluginServices.getText(this, "save_route"));
632
                        btnSaveRoute.addActionListener(this);
633
                }
634
                return btnSaveRoute;
635
        }
636

    
637
        /**
638
         * This method initializes defaultTableModel
639
         * 
640
         * @return javax.swing.table.DefaultTableModel
641
         */
642
        private TableModel getTableModel() {
643
                if (tableModel == null) {
644
                        tableModel = new DefaultTableModel() {
645

    
646
                                public String getColumnName(int column) {
647
                                        return colName[column];
648
                                }
649

    
650
                                public Object getValueAt(int row, int column) {
651
                                        if (row > getFlagCount() - 1)
652
                                                return null;
653

    
654
                                        GvFlag flag = (GvFlag) _getFlags().get(row);
655
                                        // azo: I dont know why it returns null in the middle of
656
                                        // addStage operation (synchronized vector doesnt solve it)
657
                                        if (flag == null)
658
                                                return null;
659

    
660
                                        switch (column) {
661
                                        case 0:
662
                                                return new Boolean(flag.isEnabled());
663
                                        case 1:
664
                                                return flag;
665
                                        case 2:
666
                                                return new Double(flag.getCost());
667
                                        }
668
                                        return null;
669
                                }
670

    
671
                                public void setValueAt(Object aValue, int row, int column) {
672
                                        if (row > getFlagCount() - 1)
673
                                                return;
674
                                        switch (column) {
675
                                        case 0:
676
                                                ((GvFlag) _getFlags().get(row))
677
                                                                .setEnabled(((Boolean) aValue).booleanValue());
678
                                                break;
679
                                        case 1:
680
                                                ((GvFlag) _getFlags().get(row))
681
                                                                .setDescription((String) aValue);
682
                                                break;
683
                                        /*
684
                                         * case 2: getCosts().set(row, new Double((String) aValue));
685
                                         * break;
686
                                         */
687
                                        }
688

    
689
                                }
690
                        };
691
                        tableModel.setColumnCount(3);
692
                        int i = getFlagCount();
693
                        if (i < 10)
694
                                i = 10;
695

    
696
                        tableModel.setRowCount(i);
697
                }
698
                return tableModel;
699
        }
700

    
701
        /**
702
         * This method initializes btnLoadStage
703
         * 
704
         * @return javax.swing.JButton
705
         */
706
        private JButton getBtnLoadStage() {
707
                if (btnLoadStage == null) {
708
                        btnLoadStage = new JButton();
709
                        btnLoadStage.setText(PluginServices.getText(this, "load_stages"));
710
                        btnLoadStage.addActionListener(this);
711
                }
712
                return btnLoadStage;
713
        }
714

    
715
        /**
716
         * This method initializes btnSaveStages
717
         * 
718
         * @return javax.swing.JButton
719
         */
720
        private JButton getBtnSaveStage() {
721
                if (btnSaveStage == null) {
722
                        btnSaveStage = new JButton();
723
                        btnSaveStage.setText(PluginServices.getText(this, "save_stages"));
724
                        btnSaveStage.addActionListener(this);
725
                }
726
                return btnSaveStage;
727
        }
728

    
729
        class FormatSelectionPanel extends JPanel implements IWindow {
730

    
731
                JComboBox formats;
732
                WindowInfo wi = null;
733

    
734
                public FormatSelectionPanel() {
735
                        super(new BorderLayout());
736
                        String[] formatos = { "SHP", "DXF", "POSTGIS", "GML" };
737
                        formats = new JComboBox(formatos);
738
                        GridBagLayoutPanel contentPanel = 
739
                                new GridBagLayoutPanel();
740
                        contentPanel.addComponent(new JLabel(PluginServices.getText(null,
741
                        "Seleccione_un_formato_para_guardar_la_ruta")));
742
                        contentPanel.addComponent(formats);
743
                        JButton okButton = new JButton(PluginServices.getText(null,
744
                                        "Aceptar"));
745
                        okButton.addActionListener(new ActionListener() {
746
                                public void actionPerformed(ActionEvent arg0) {
747
                                        close();
748
                                }
749
                        });
750
                        contentPanel.addComponent(okButton);
751
                        
752
                        add(contentPanel);
753
                }
754

    
755
                void close() {
756
                        PluginServices.getMDIManager().closeWindow(this);
757
                }
758

    
759
                public String getSelectedFormat() {
760
                        return (String) formats.getSelectedItem();
761
                }
762

    
763
                public WindowInfo getWindowInfo() {
764
                        if (wi == null) {
765
                                wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.PALETTE);
766
                                wi.setTitle(PluginServices
767
                                                .getText(null, "Seleccion_de_formato"));// Internacionalizar
768
                                                                                                                                // esto
769
                                wi.setWidth(300);
770
                                wi.setHeight(65);
771
                        }
772
                        return wi;
773
                }
774

    
775
        }
776

    
777
        public void actionPerformed(ActionEvent e) {
778
                Component c = (Component) e.getSource();
779
                if (c.equals(getBtnLoadStage())) {
780
                        loadStages();
781
                } else if (c.equals(getBtnSaveStage())) {
782
                        saveStage();
783
                } else if (c.equals(getBtnPushUpStage())) {
784
                        int[] selected = tblStages.getSelectedRows();
785
                        if (selected.length == 0 || selected[0] == 0)
786
                                return;
787
                        tblStages.clearSelection();
788
                        for (int i = 0; i < selected.length; i++) {
789
                                Object aux = _getFlags().get(selected[i] - 1);
790
                                _getFlags().set(selected[i] - 1, _getFlags().get(selected[i]));
791
                                _getFlags().set(selected[i], aux);
792
                                selected[i]--;
793
                                tblStages.addRowSelectionInterval(selected[i], selected[i]);
794
                        }
795
                } else if (c.equals(getBtnPullDownStage())) {
796
                        // pull down
797
                        int[] selected = tblStages.getSelectedRows();
798
                        if (selected.length == 0
799
                                        || selected[selected.length - 1] >= _getFlags().size() - 1)
800
                                return;
801

    
802
                        // move rows
803
                        tblStages.clearSelection();
804
                        for (int i = selected.length - 1; i >= 0; i--) {
805
                                Object aux = _getFlags().get(selected[i] + 1);
806
                                _getFlags().set(selected[i] + 1, _getFlags().get(selected[i]));
807
                                _getFlags().set(selected[i], aux);
808
                                selected[i]++;
809
                                tblStages.addRowSelectionInterval(selected[i], selected[i]);
810
                        }
811

    
812
                } else if (c.equals(getBtnRemoveStage())) {
813
                        removeStage();
814
                } else if (c.equals(getBtnSaveRoute())) {
815
                        saveRoute();
816
                } else if (c.equals(getBtnGenerateReport())) {
817

    
818
                }
819
                repaint();
820
        }
821

    
822
        private void saveStage(){
823
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
824
                if(! (window instanceof IView))
825
                        return;
826
                
827
                IView view = (View) window;
828
                MapControl mapControl = view.getMapControl();
829
                MapContext map = mapControl.getMapContext();
830
                FLayers layers = map.getLayers();
831
                Network net = null;
832
                SingleLayerIterator it = new SingleLayerIterator(layers);
833
                while (it.hasNext() && net == null) {
834
                        FLayer aux = it.next();
835
                        if (!aux.isActive())
836
                                continue;
837
                        net = (Network) aux.getProperty("network");
838
                }
839
                if (net == null)
840
                        return;
841
                List features = new ArrayList();
842
                GvFlag[] flags = net.getFlags();
843
                for(int i = 0; i < flags.length; i++){
844
                        GvFlag flag = flags[i];
845
                        FPoint2D point = new FPoint2D(flag.getOriginalPoint());
846
                        Value[] values = new Value[6];
847
                        values[GvFlag.ID_FLAG_INDEX] = ValueFactory.createValue(flag.getIdFlag());
848
                        values[GvFlag.ID_ARC_INDEX] = ValueFactory.createValue(flag.getIdArc());
849
                        values[GvFlag.DESCRIPTION_INDEX] = ValueFactory.createValue(flag.getDescription());
850
                        values[GvFlag.DIREC_INDEX] = ValueFactory.createValue(flag.getDirec());
851
                        values[GvFlag.PCT_INDEX] = ValueFactory.createValue(flag.getPct());
852
                        values[GvFlag.COST_INDEX] = ValueFactory.createValue(flag.getCost());
853
                        IGeometry geo = ShapeFactory.createPoint2D(point);
854
                        DefaultFeature feature = new DefaultFeature(geo, values, new Integer(flag.getIdFlag()).toString() );
855
                        features.add(feature);
856
                }
857
                
858
                FlagsMemoryDriver driver = new FlagsMemoryDriver(features);
859
                IProjection projection = FOpenDialog.getLastProjection();
860
                FLyrVect routeLayer = (FLyrVect) LayerFactory.createLayer("Route",
861
                                driver, projection);
862

    
863
                FormatSelectionPanel selectionPanel = new FormatSelectionPanel();
864
                PluginServices.getMDIManager().addWindow(selectionPanel);
865

    
866
                String format = selectionPanel.getSelectedFormat();
867
                com.iver.cit.gvsig.ExportTo export = new com.iver.cit.gvsig.ExportTo();
868
                MapContext context = mapControl.getMapContext();
869
                try {
870
                        if (format.equalsIgnoreCase("SHP")) {
871
                                export.saveToShp(context, routeLayer);
872
                        } else if (format.equalsIgnoreCase("DXF")) {
873
                                export.saveToDxf(context, routeLayer);
874
                        } else if (format.equalsIgnoreCase("GML")) {
875
                                export.saveToGml(context, routeLayer);
876
                        } else if (format.equalsIgnoreCase("POSTGIS")) {
877
                                export.saveToPostGIS(context, routeLayer);
878
                        }
879
                } catch (EditionException e1) {
880
                        // TODO Auto-generated catch block
881
                        e1.printStackTrace();
882
                } catch (DriverIOException e1) {
883
                        // TODO Auto-generated catch block
884
                        e1.printStackTrace();
885
                }
886

    
887
        }
888
        private void saveRoute() {
889
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
890
                if(! (window instanceof IView))
891
                        return;
892
                IView view = (View) window;
893
                MapControl mapControl = view.getMapControl();
894
                Route route = (Route) GvSession.getInstance().get(mapControl, "Route");
895
                RouteMemoryDriver driver = new RouteMemoryDriver(route.getFeatureList());
896
                IProjection projection = FOpenDialog.getLastProjection();
897
                FLyrVect routeLayer = (FLyrVect) LayerFactory.createLayer("Route",
898
                                driver, projection);
899

    
900
                FormatSelectionPanel selectionPanel = new FormatSelectionPanel();
901
                PluginServices.getMDIManager().addWindow(selectionPanel);
902

    
903
                String format = selectionPanel.getSelectedFormat();
904
                com.iver.cit.gvsig.ExportTo export = new com.iver.cit.gvsig.ExportTo();
905
                MapContext context = mapControl.getMapContext();
906
                try {
907
                        if (format.equalsIgnoreCase("SHP")) {
908
                                export.saveToShp(context, routeLayer);
909
                        } else if (format.equalsIgnoreCase("DXF")) {
910
                                export.saveToDxf(context, routeLayer);
911
                        } else if (format.equalsIgnoreCase("GML")) {
912
                                export.saveToGml(context, routeLayer);
913
                        } else if (format.equalsIgnoreCase("POSTGIS")) {
914
                                export.saveToPostGIS(context, routeLayer);
915
                        }
916
                } catch (EditionException e1) {
917
                        // TODO Auto-generated catch block
918
                        e1.printStackTrace();
919
                } catch (DriverIOException e1) {
920
                        // TODO Auto-generated catch block
921
                        e1.printStackTrace();
922
                }
923
        }
924

    
925
        /**
926
         * Converts a point layer loaded in the active view's toc in a collection of
927
         * flags of the active network
928
         * 
929
         */
930
        private void loadStages() {
931

    
932
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
933
                if(! (window instanceof IView))
934
                        return;
935
                IView view = (IView) window;
936
                MapControl mapControl = view.getMapControl();
937
                MapContext map = mapControl.getMapContext();
938
                FLayers layers = map.getLayers();
939

    
940
                String title = PluginServices.getText(this,
941
                                "Seleccionar_capa_con_puntos_de_parada");
942
                String introductoryText = PluginServices.getText(this,
943
                                "Seleccione_una_capa_de_puntos_para_crear_paradas");
944

    
945
                LayerSelectionPanel selectionPanel = new LayerSelectionPanel(layers,
946
                                title, introductoryText, FLyrVect.class, FShape.POINT);
947
                PluginServices.getMDIManager().addWindow(selectionPanel);
948

    
949
                if (!selectionPanel.wasFinishPressed())
950
                        return;
951
                else {
952
                        FLayer layer = selectionPanel.getLayer();
953
                        if (layer == null)
954
                                return;
955
                        if (!(layer instanceof FLyrVect))
956
                                return;
957
                        FLyrVect vectLyr = (FLyrVect) layer;
958

    
959
                        try {
960
                                if (vectLyr.getShapeType() != FShape.POINT)
961
                                        return;
962

    
963
                                // TODO
964
                                // Hay que refinar bastante, para que el usuario eliga
965
                                // con que red y con qu? capa lineal quiere trabajar
966
                                // tal y como est?, si hubiese varias se trabajaria
967
                                // con la primera que devuelva el iterador
968

    
969
                                Network net = null;
970
                                SingleLayerIterator it = new SingleLayerIterator(layers);
971
                                while (it.hasNext() && net == null) {
972
                                        FLayer aux = it.next();
973
                                        if (!aux.isActive())
974
                                                continue;
975
                                        net = (Network) aux.getProperty("network");
976
                                }
977
                                if (net == null)
978
                                        return;
979

    
980
                                _getFlags().clear();
981
                                double realTol = mapControl.getViewPort().toMapDistance(
982
                                                FlagListener.pixelTolerance);
983
                                GvFlag flag;
984
                                ReadableVectorial reader = vectLyr.getSource();
985
                                int numShapes = reader.getShapeCount();
986

    
987
                                GraphicLayer graphicLayer = map.getGraphicsLayer();
988
                                FSymbol simFlag = new FSymbol(FShape.POINT, Color.BLUE);
989
                                simFlag.setSize(24);
990
                                simFlag.setStyle(FConstant.SYMBOL_STYLE_MARKER_IMAGEN);
991
                                ImageIcon icon = new ImageIcon(this.getClass().getClassLoader()
992
                                                .getResource("images/pushpin.png"));
993
                                simFlag.setIcon(icon.getImage());
994
                                int idSymbolFlag = graphicLayer.addSymbol(simFlag);
995

    
996
                                for (int i = 0; i < numShapes; i++) {
997
                                        Geometry geo = reader.getShape(i).toJTSGeometry();
998
                                        if (!((geo instanceof Point) || (geo instanceof MultiPoint)))
999
                                                continue;
1000

    
1001
                                        /*
1002
                                         * TODO Aqui deberiamos comprobar si el registro tiene
1003
                                         * alguno de los campos de un Flag. A saber: ->cost
1004
                                         * ->description
1005
                                         */
1006
                                        Coordinate[] coords = geo.getCoordinates();
1007
                                        for (int j = 0; j < coords.length; j++) {
1008
                                                flag = net.addFlag(coords[j].x, coords[j].y, realTol);
1009

    
1010
                                                if (flag == null) {
1011
                                                        System.out.println("no esta en la red");
1012

    
1013
                                                        // TODO A?adir a un list, y mostrar todos al final
1014

    
1015
                                                        // JOptionPane.showMessageDialog(null, "No est?
1016
                                                        // sobre la red");
1017
                                                        continue;
1018
                                                }
1019
                                                addFlag(flag);
1020
                                                IGeometry gAux = ShapeFactory.createPoint2D(
1021
                                                                coords[j].x, coords[j].y);
1022
                                                FGraphic graphic = new FGraphic(gAux, idSymbolFlag);
1023
                                                graphicLayer.addGraphic(graphic);
1024

    
1025
                                        }
1026
                                        mapControl.drawGraphics();
1027
                                        PluginServices.getMainFrame().enableControls();
1028
                                }
1029

    
1030
                        } catch (DriverException e1) {
1031
                                return;
1032
                        } catch (DriverIOException e2) {
1033
                                return;
1034
                        } catch (GraphException e3) {
1035
                                return;
1036
                        }
1037
                }
1038
        }
1039

    
1040
        /**
1041
         * Removes the selected flags in the component's table from: -the graphic
1042
         * layer of the active view -the active network -the own table
1043
         * 
1044
         */
1045
        private void removeStage() {
1046
                int[] selected = tblStages.getSelectedRows();
1047

    
1048
                for (int i = selected.length - 1; i >= 0; i--) {
1049
                        try {
1050
                                // removeFlag(selected[i]);
1051
                                if (network != null) {
1052
                                        final GvFlag flag = (GvFlag) _getFlags().get(selected[i]);
1053
                                        NetworkUtils.clearFlagFromGraphics(mapCtrl, flag);
1054
                                        network.removeFlag(flag);
1055
                                        mapCtrl.repaint();
1056
                                }
1057
                        } catch (IndexOutOfBoundsException iobEx) {
1058
                        }
1059

    
1060
                }// for
1061
                tblStages.clearSelection();
1062
                mapCtrl.drawMap(false);
1063
        }
1064

    
1065
        public GvFlag[] getFlags() {
1066
                return (GvFlag[]) _getFlags().toArray(new GvFlag[0]);
1067
        }
1068

    
1069
        public void flagsChanged(int reason) {
1070
                refresh();
1071
                mapCtrl.drawGraphics();
1072
                invalidate();
1073
                
1074
        }
1075
} // @jve:decl-index=0:visual-constraint="17,9"