Statistics
| Revision:

root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / gui / RouteControlPanel.java @ 8613

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

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

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

    
122
import org.cresques.cts.IProjection;
123
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
124
import org.gvsig.gui.beans.swing.JBlank;
125
import org.gvsig.gui.beans.swing.JButton;
126

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

    
174
public class RouteControlPanel extends JPanel implements IWindow,
175
                ActionListener, IFlagListener {
176
        WindowInfo wi;
177

    
178
        private JPanel westPanel = null;
179

    
180
        private JScrollPane scrlStages = null;
181

    
182
        private JTable tblStages = null;
183

    
184
        private GridBagLayoutPanel eastPanel = null;
185

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

    
188
        private JButton btnLoadStage = null;
189

    
190
        private JButton btnSaveStage = null;
191
        
192
        private Network network;
193

    
194
        private ArrayList routeFlags;
195

    
196
        private JLabel lblCost;
197

    
198
        private JLabel lblFlagAmout;
199

    
200
        private JButton btnPullDownStage;
201

    
202
        private JButton btnPushUpStage;
203

    
204
        private JButton btnRemoveStage;
205

    
206
        private JPanel southPanel;
207

    
208
        private final String[] colName = new String[] {
209
                        PluginServices.getText(this, "enable"),
210
                        PluginServices.getText(this, "stage"),
211
                        PluginServices.getText(this, "cost"), };
212

    
213
        private JButton btnGenerateRoute;
214

    
215
        private JButton btnSaveRoute;
216

    
217
        private MapControl mapCtrl;
218

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

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

    
282
        public RouteControlPanel() {
283
                super();
284
                initialize();
285
        }
286

    
287
        public void setCostUnits(String unitsName) {
288
                colName[2] = unitsName;
289
                getTblStages().repaint();
290
        }
291

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

    
310
        private void updateFlags() {
311
                lblFlagAmout.setText(String.valueOf(_getFlags().size()));
312
                updateTotalCost();
313
                getTblStages().repaint();
314
        }
315

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

    
332
        public Object getWindowModel() {
333
                return this.getClass();
334
        }
335

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

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

    
364
                        westPanel.add(aux);
365
                }
366
                return westPanel;
367
        }
368

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

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

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

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

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

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

    
433
        private String getCostUnitName() {
434
                if (colName[2].equals(PluginServices.getText(this, "cost")))
435
                        return "";
436
                return colName[2];
437
        }
438

    
439
        public void removeFlag(GvFlag flag) {
440
                removeFlag(_getFlags().indexOf(flag));
441
        }
442

    
443
        public void removeFlag(int index) {
444
                _getFlags().remove(index);
445
                lblFlagAmout.setText(String.valueOf(_getFlags().size()));
446
                getTblStages().repaint();
447
        }
448

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

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

    
472
                                                final JCheckBox chk = new JCheckBox("", isSelected(row));
473

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

    
490
                                public Component getTableCellEditorComponent(JTable table,
491
                                                Object value, boolean isSelected, final int row,
492
                                                int column) {
493
                                        if (row > getFlagCount() - 1) {
494
                                                return null;
495
                                        }
496

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

    
524
                                }
525

    
526
                                public void cancelCellEditing() {
527
                                }
528

    
529
                                public boolean stopCellEditing() {
530
                                        return false;
531
                                }
532

    
533
                                public Object getCellEditorValue() {
534
                                        return null;
535
                                }
536

    
537
                                public boolean isCellEditable(EventObject anEvent) {
538
                                        return true;
539
                                }
540

    
541
                                public boolean shouldSelectCell(EventObject anEvent) {
542
                                        return false;
543
                                }
544

    
545
                                public void addCellEditorListener(CellEditorListener l) {
546
                                }
547

    
548
                                public void removeCellEditorListener(CellEditorListener l) {
549
                                }
550

    
551
                        };
552
                        TableColumnModel cm = tblStages.getColumnModel();
553
                        for (int i = 0; i < tblStages.getColumnCount(); i++) {
554
                                cm.getColumn(0).setCellEditor(tce);
555
                                cm.getColumn(0).setCellRenderer(tcr);
556
                        }
557

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

    
574
        protected int getFlagCount() {
575
                return _getFlags().size();
576
        }
577

    
578
        private List _getFlags() {
579
//                if (routeFlags == null) {
580
//                        routeFlags = new ArrayList();
581
//                }
582
//                return routeFlags;
583
                        
584
                return network.getOriginaFlags();
585
        }
586

    
587
        protected boolean isSelected(int row) {
588
                return ((GvFlag) _getFlags().get(row)).isEnabled();
589
        }
590

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

    
614
        private JButton getBtnGenerateReport() {
615
                if (btnGenerateRoute == null) {
616
                        btnGenerateRoute = new JButton();
617
                        btnGenerateRoute.setText(PluginServices.getText(this,
618
                                        "generate_report"));
619
                        btnGenerateRoute.addActionListener(this);
620
                }
621
                return btnGenerateRoute;
622
        }
623

    
624
        private JButton getBtnSaveRoute() {
625
                if (btnSaveRoute == null) {
626
                        btnSaveRoute = new JButton();
627
                        btnSaveRoute.setText(PluginServices.getText(this, "save_route"));
628
                        btnSaveRoute.addActionListener(this);
629
                }
630
                return btnSaveRoute;
631
        }
632

    
633
        /**
634
         * This method initializes defaultTableModel
635
         * 
636
         * @return javax.swing.table.DefaultTableModel
637
         */
638
        private TableModel getTableModel() {
639
                if (tableModel == null) {
640
                        tableModel = new DefaultTableModel() {
641

    
642
                                public String getColumnName(int column) {
643
                                        return colName[column];
644
                                }
645

    
646
                                public Object getValueAt(int row, int column) {
647
                                        if (row > getFlagCount() - 1)
648
                                                return null;
649

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

    
656
                                        switch (column) {
657
                                        case 0:
658
                                                return new Boolean(flag.isEnabled());
659
                                        case 1:
660
                                                return flag;
661
                                        case 2:
662
                                                return new Double(flag.getCost());
663
                                        }
664
                                        return null;
665
                                }
666

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

    
685
                                }
686
                        };
687
                        tableModel.setColumnCount(3);
688
                        int i = getFlagCount();
689
                        if (i < 10)
690
                                i = 10;
691

    
692
                        tableModel.setRowCount(i);
693
                }
694
                return tableModel;
695
        }
696

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

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

    
725
        class FormatSelectionPanel extends JPanel implements IWindow {
726

    
727
                JComboBox formats;
728
                WindowInfo wi = null;
729

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

    
751
                void close() {
752
                        PluginServices.getMDIManager().closeWindow(this);
753
                }
754

    
755
                public String getSelectedFormat() {
756
                        return (String) formats.getSelectedItem();
757
                }
758

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

    
771
        }
772

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

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

    
808
                } else if (c.equals(getBtnRemoveStage())) {
809
                        removeStage();
810
                } else if (c.equals(getBtnSaveRoute())) {
811
                        saveRoute();
812
                } else if (c.equals(getBtnGenerateReport())) {
813

    
814
                }
815
                repaint();
816
        }
817

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

    
859
                FormatSelectionPanel selectionPanel = new FormatSelectionPanel();
860
                PluginServices.getMDIManager().addWindow(selectionPanel);
861

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

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

    
896
                FormatSelectionPanel selectionPanel = new FormatSelectionPanel();
897
                PluginServices.getMDIManager().addWindow(selectionPanel);
898

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

    
921
        /**
922
         * Converts a point layer loaded in the active view's toc in a collection of
923
         * flags of the active network
924
         * 
925
         */
926
        private void loadStages() {
927

    
928
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
929
                if(! (window instanceof IView))
930
                        return;
931
                IView view = (IView) window;
932
                MapControl mapControl = view.getMapControl();
933
                MapContext map = mapControl.getMapContext();
934
                FLayers layers = map.getLayers();
935

    
936
                String title = PluginServices.getText(this,
937
                                "Seleccionar_capa_con_puntos_de_parada");
938
                String introductoryText = PluginServices.getText(this,
939
                                "Seleccione_una_capa_de_puntos_para_crear_paradas");
940

    
941
                LayerSelectionPanel selectionPanel = new LayerSelectionPanel(layers,
942
                                title, introductoryText, FLyrVect.class, FShape.POINT);
943
                PluginServices.getMDIManager().addWindow(selectionPanel);
944

    
945
                if (!selectionPanel.wasFinishPressed())
946
                        return;
947
                else {
948
                        FLayer layer = selectionPanel.getLayer();
949
                        if (layer == null)
950
                                return;
951
                        if (!(layer instanceof FLyrVect))
952
                                return;
953
                        FLyrVect vectLyr = (FLyrVect) layer;
954

    
955
                        try {
956
                                if (vectLyr.getShapeType() != FShape.POINT)
957
                                        return;
958

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

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

    
976
                                _getFlags().clear();
977
                                double realTol = mapControl.getViewPort().toMapDistance(
978
                                                FlagListener.pixelTolerance);
979
                                GvFlag flag;
980
                                ReadableVectorial reader = vectLyr.getSource();
981
                                int numShapes = reader.getShapeCount();
982

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

    
992
                                for (int i = 0; i < numShapes; i++) {
993
                                        Geometry geo = reader.getShape(i).toJTSGeometry();
994
                                        if (!((geo instanceof Point) || (geo instanceof MultiPoint)))
995
                                                continue;
996

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

    
1006
                                                if (flag == null) {
1007
                                                        System.out.println("no esta en la red");
1008

    
1009
                                                        // TODO A?adir a un list, y mostrar todos al final
1010

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

    
1021
                                        }
1022
                                        mapControl.drawGraphics();
1023
                                        PluginServices.getMainFrame().enableControls();
1024
                                }
1025

    
1026
                        } catch (DriverException e1) {
1027
                                return;
1028
                        } catch (DriverIOException e2) {
1029
                                return;
1030
                        } catch (GraphException e3) {
1031
                                return;
1032
                        }
1033
                }
1034
        }
1035

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

    
1044
                Network net = null;
1045
                MapControl mapControl = null;
1046
                MapContext map = null;
1047
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices
1048
                                .getMDIManager().getActiveWindow();
1049
                if (f == null) {
1050
                        return;
1051
                }
1052

    
1053
                if (f instanceof View) {
1054
                        View v = (View) f;
1055
                        mapControl = v.getMapControl();
1056
                        map = mapControl.getMapContext();
1057
                        SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
1058
                        while (it.hasNext()) {
1059
                                FLayer aux = it.next();
1060
                                if (!aux.isActive())
1061
                                        continue;
1062
                                net = (Network) aux.getProperty("network");
1063
                                if (net != null) {
1064
                                        break;
1065
                                }
1066
                        }// while
1067
                }// if
1068

    
1069
                for (int i = selected.length - 1; i >= 0; i--) {
1070
                        try {
1071
                                removeFlag(selected[i]);
1072

    
1073
                                // TODO: Duda ?quitamos el flag de la red,
1074
                                // y del graphics del mapcontrol?
1075
                                // Para probar, vamos a hacerlo as?
1076
                                if (net != null) {
1077
                                        final GvFlag flag = (GvFlag) _getFlags().get(selected[i]);
1078
                                        net.removeFlag(flag);
1079

    
1080
                                        // TODO Queda por eliminar el graphics de la capa
1081
                                        // GraphicLayer.
1082
                                        final GraphicLayer grLyr = map.getGraphicsLayer();
1083
                                        grLyr.process(new ItemVisitor() {
1084
                                                public void visitItem(Object arg0) {
1085
                                                        FGraphic graphics = (FGraphic) arg0;
1086
                                                        Geometry geo = graphics.getGeom().toJTSGeometry();
1087
                                                        Coordinate coord = new Coordinate(flag
1088
                                                                        .getOriginalPoint().getX(), flag
1089
                                                                        .getOriginalPoint().getY());
1090
                                                        Geometry point = new GeometryFactory()
1091
                                                                        .createPoint(coord);
1092
                                                        if (point.equals(geo))
1093
                                                                grLyr.removeGraphics(graphics);
1094
                                                }
1095
                                        }, null);
1096
                                        mapControl.drawGraphics();
1097
                                }
1098
                        } catch (IndexOutOfBoundsException iobEx) {
1099
                        }
1100

    
1101
                }// for
1102
                tblStages.clearSelection();
1103
        }
1104

    
1105
        public GvFlag[] getFlags() {
1106
                return (GvFlag[]) _getFlags().toArray(new GvFlag[0]);
1107
        }
1108

    
1109
        public void flagsChanged(int reason) {
1110
                refresh();
1111
                
1112
        }
1113
} // @jve:decl-index=0:visual-constraint="17,9"