Statistics
| Revision:

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

History | View | Annotate | Download (16.3 KB)

1 8209 jaume
/* 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$
45
* $Log$
46 8280 jaume
* Revision 1.4  2006-10-24 08:04:41  jaume
47 8258 jaume
* *** empty log message ***
48
*
49 8280 jaume
* Revision 1.3  2006/10/23 16:00:20  jaume
50
* *** empty log message ***
51
*
52 8258 jaume
* Revision 1.2  2006/10/23 08:05:39  jaume
53 8209 jaume
* GUI
54
*
55 8224 jaume
* Revision 1.1  2006/10/20 12:02:50  jaume
56
* GUI
57 8209 jaume
*
58 8224 jaume
*
59 8209 jaume
*/
60
package com.iver.cit.gvsig.graph.gui;
61
62
import java.awt.BorderLayout;
63
import java.awt.Color;
64
import java.awt.Component;
65
import java.awt.Dimension;
66
import java.awt.FlowLayout;
67
import java.awt.Font;
68
import java.awt.event.ActionEvent;
69
import java.awt.event.ActionListener;
70
import java.util.ArrayList;
71
import java.util.EventObject;
72
73 8224 jaume
import javax.swing.ImageIcon;
74 8209 jaume
import javax.swing.JCheckBox;
75
import javax.swing.JLabel;
76
import javax.swing.JPanel;
77
import javax.swing.JScrollPane;
78
import javax.swing.JTable;
79
import javax.swing.JTextField;
80
import javax.swing.event.CellEditorListener;
81
import javax.swing.table.DefaultTableCellRenderer;
82
import javax.swing.table.DefaultTableModel;
83
import javax.swing.table.TableCellEditor;
84
import javax.swing.table.TableCellRenderer;
85
import javax.swing.table.TableColumnModel;
86
import javax.swing.table.TableModel;
87
88
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
89 8224 jaume
import org.gvsig.gui.beans.swing.JBlank;
90 8209 jaume
import org.gvsig.gui.beans.swing.JButton;
91
92
import com.iver.andami.PluginServices;
93
import com.iver.andami.ui.mdiManager.IWindow;
94
import com.iver.andami.ui.mdiManager.WindowInfo;
95
import com.iver.cit.gvsig.graph.core.GvFlag;
96
97
98 8258 jaume
public class RouteControPanel extends JPanel implements IWindow, ActionListener {
99 8209 jaume
    WindowInfo wi;
100
    private JPanel westPanel = null;
101
    private JScrollPane scrlStages = null;
102
    private JTable tblStages = null;
103
    private GridBagLayoutPanel eastPanel = null;
104
    private DefaultTableModel tableModel = null;  //  @jve:decl-index=0:visual-constraint=""
105
    private JButton btnLoadStage = null;
106 8258 jaume
    private JButton btnSaveStage = null;
107 8209 jaume
    private ArrayList routeFlags;
108 8280 jaume
    private JLabel lblCost;
109 8224 jaume
        private JLabel lblFlagAmout;
110
        private JButton btnPullDownStage;
111
        private JButton btnPushUpStage;
112
        private JButton btnRemoveStage;
113
        private JPanel southPanel;
114 8280 jaume
        private final String[] colName = new String[] {
115 8224 jaume
            PluginServices.getText(this, "enable"),
116
            PluginServices.getText(this, "stage"),
117
            PluginServices.getText(this, "cost"),
118
    };
119 8209 jaume
    /**
120
     * This method initializes
121
     *
122
     */
123
    public RouteControPanel() {
124
        super();
125
        initialize();
126
    }
127
128 8224 jaume
    public void setCostUnits(String unitsName) {
129
            colName[2] = unitsName;
130
            getTblStages().repaint();
131
    }
132
133 8209 jaume
    /**
134
     * This method initializes this
135
     *
136
     */
137
    private void initialize() {
138
        BorderLayout borderLayout = new BorderLayout();
139
        borderLayout.setHgap(10);
140
        borderLayout.setVgap(10);
141
        JPanel cont = new JPanel(borderLayout);
142 8224 jaume
        cont.setPreferredSize(new Dimension(400,320));
143
        this.setSize(new Dimension(550,320));
144
        cont.add(getWestPanel(), BorderLayout.WEST);
145
        cont.add(getEastPanel(), BorderLayout.EAST);
146
        cont.add(getSouthPanel(), BorderLayout.SOUTH);
147 8209 jaume
        this.add(cont);
148
149
        addFlag(new GvFlag(0, 0, "FakeFlag1", 0));
150
        addFlag(new GvFlag(0, 0, "FakeFlag2", 0));
151
        addFlag(new GvFlag(0, 0, "FakeFlag3", 0));
152
        addFlag(new GvFlag(0, 0, "FakeFlag4", 0));
153
        addFlag(new GvFlag(0, 0, "FakeFlag5", 0));
154
155
    }
156
157 8224 jaume
    private JPanel getSouthPanel() {
158
            if (southPanel == null) {
159 8258 jaume
                    southPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
160 8224 jaume
                    southPanel.add(getBtnRemoveStage());
161
                    southPanel.add(new JBlank(50, 20));
162
                    southPanel.add(getBtnPushUpStage());
163
                    southPanel.add(getBtnPullDownStage());
164
                    southPanel.add(new JLabel(PluginServices.getText(this, "flag_amount")));
165
                    lblFlagAmout = new JLabel();
166
                    lblFlagAmout.setFont(lblFlagAmout.getFont().deriveFont(Font.BOLD));
167
                    southPanel.add(lblFlagAmout);
168
            }
169
            return southPanel;
170
    }
171
172
        public Object getWindowModel() {
173 8209 jaume
        return this.getClass();
174
    }
175
176
    public WindowInfo getWindowInfo() {
177
        if (wi==null) {
178
            wi = new WindowInfo(WindowInfo.RESIZABLE);
179
            wi.setWidth(450);
180 8224 jaume
            wi.setHeight(320);
181 8209 jaume
            wi.setTitle(PluginServices.getText(this, "create_network") + "...");
182
        }
183
        return wi;
184
    }
185
186
    /**
187
     * This method initializes westPanel
188
     *
189
     * @return javax.swing.JPanel
190
     */
191
    private JPanel getWestPanel() {
192
        if (westPanel == null) {
193
            westPanel = new JPanel(new BorderLayout(5, 5));
194
            lblCost = new JLabel();
195
            lblCost.setFont(lblCost.getFont().deriveFont(Font.BOLD));
196
            GridBagLayoutPanel aux = new GridBagLayoutPanel();
197
            aux.addComponent(PluginServices.getText(this, "total_route_cost")+":", lblCost);
198
            aux.addComponent(getScrlStages());
199 8224 jaume
200 8209 jaume
            westPanel.add(aux);
201
        }
202
        return westPanel;
203
    }
204
205 8224 jaume
    private JButton getBtnPullDownStage() {
206
                if (btnPullDownStage == null) {
207
                        btnPullDownStage = new JButton(new ImageIcon(this.getClass().getClassLoader().getResource("images/down-arrow.png")));
208
                        btnPullDownStage.setName("btnPullDownStage");
209 8258 jaume
                        btnPullDownStage.addActionListener(this);
210 8224 jaume
                }
211
                return btnPullDownStage;
212
        }
213
214
        private JButton getBtnPushUpStage() {
215
                if (btnPushUpStage == null) {
216
                        btnPushUpStage = new JButton(new ImageIcon(this.getClass().getClassLoader().getResource("images/up-arrow.png")));
217
                        btnPushUpStage.setName("btnPushUpStage");
218 8258 jaume
                        btnPushUpStage.addActionListener(this);
219 8224 jaume
                }
220
                return btnPushUpStage;
221
        }
222
223
        private JButton getBtnRemoveStage() {
224
                if (btnRemoveStage == null) {
225 8258 jaume
                        btnRemoveStage = new JButton(new ImageIcon(this.getClass().getClassLoader().getResource("images/delete.png")));
226 8224 jaume
                        btnRemoveStage.setName("btnRemoveStage");
227 8258 jaume
                        btnRemoveStage.addActionListener(this);
228 8224 jaume
                }
229
                return btnRemoveStage;
230
        }
231
232
        /**
233 8209 jaume
     * This method initializes scrlStages
234
     *
235
     * @return javax.swing.JScrollPane
236
     */
237
    private JScrollPane getScrlStages() {
238
        if (scrlStages == null) {
239
            scrlStages = new JScrollPane();
240
            scrlStages.setViewportView(getTblStages());
241 8224 jaume
            scrlStages.setPreferredSize(new Dimension(300, 200));
242 8209 jaume
        }
243
        return scrlStages;
244
    }
245
246
    public void addFlag(GvFlag flag) {
247 8258 jaume
        _getFlags().add(flag);
248
        lblFlagAmout.setText(String.valueOf(_getFlags().size()));
249 8224 jaume
        getTblStages().repaint();
250 8209 jaume
    }
251 8224 jaume
252
    public void removeFlag(GvFlag flag) {
253 8258 jaume
            removeFlag(_getFlags().indexOf(flag));
254 8224 jaume
    }
255
256
    public void removeFlag(int index) {
257 8258 jaume
            _getFlags().remove(index);
258
            lblFlagAmout.setText(String.valueOf(_getFlags().size()));
259 8224 jaume
            getTblStages().repaint();
260
    }
261 8209 jaume
    /**
262
     * This method initializes tblStages
263
     *
264
     * @return javax.swing.JTable
265
     */
266
    private JTable getTblStages() {
267
            if (tblStages == null) {
268
                    tblStages = new JTable();
269
                    tblStages.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
270
                    tblStages.setModel(getTableModel());
271
                    TableCellRenderer tcr = new DefaultTableCellRenderer() {
272
273
                            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, final int row, int column) {
274
                                    Object myValue = null;
275
                                    if (row >= getFlagCount())
276
                                            return null;
277
                                    if (column == 0) {
278
                                            JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER, 5,0));
279
280
                                            final JCheckBox chk = new JCheckBox("", isSelected(row));
281
282
                                            p.setBackground(Color.WHITE);
283
                                            p.add(chk);
284
                                            return p;
285
                                    } else if ( column == 1 ) {
286 8258 jaume
                                            myValue = ((GvFlag) _getFlags().get(row)).toString();
287 8209 jaume
                                    } else if (column == 2) {
288 8258 jaume
                                            myValue = String.valueOf(((GvFlag) _getFlags().get(row)).getCost());
289 8209 jaume
                                    }
290
                                    return super.getTableCellRendererComponent(table, myValue, isSelected, hasFocus, row, column);
291
                            }
292
                    };
293
                    TableCellEditor tce = new TableCellEditor() {
294
295
                            public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, final int row, int column) {
296
                                    if (row > getFlagCount() - 1) {
297
                                            return null;
298
                                    }
299
300
                                    if (column == 0) {
301
                                            JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER, 5,0));
302
                                            final JCheckBox chk = new JCheckBox("", isSelected(row));
303
                                            chk.addActionListener(new ActionListener() {
304
                                                    public void actionPerformed(java.awt.event.ActionEvent e) {
305 8258 jaume
                                                            ((GvFlag)_getFlags().get(row)).setEnabled(chk.isSelected());
306 8209 jaume
                                                    };
307
                                            });
308
                                            p.setBackground(Color.WHITE);
309
                                            p.add(chk);
310
                                            return p;
311
                                    } else if (column == 1) {
312 8258 jaume
                                            final JTextField txt = new JTextField(((GvFlag) _getFlags().get(row)).toString());
313 8209 jaume
                                            txt.addActionListener(new ActionListener() {
314
                                                    public void actionPerformed(ActionEvent e) {
315 8258 jaume
                                                            GvFlag flag = (GvFlag) _getFlags().get(row);
316 8209 jaume
                                                            flag.setDescription(txt.getText());
317
                                                    }
318
                                            });
319
                                            return txt;
320
                                    }
321
                                    return null;
322
323
                            }
324
                            public void cancelCellEditing() {}
325
326
                            public boolean stopCellEditing() {
327 8258 jaume
                                    return false;
328 8209 jaume
                            }
329
330
                            public Object getCellEditorValue() {
331
                                    return null;
332
                            }
333
334
                            public boolean isCellEditable(EventObject anEvent) {
335
                                    return true;
336
                            }
337
338
                            public boolean shouldSelectCell(EventObject anEvent) {
339
                                    return false;
340
                            }
341
342
                            public void addCellEditorListener(CellEditorListener l) {}
343
344
                            public void removeCellEditorListener(CellEditorListener l) {}
345
346
                    };
347
                    TableColumnModel cm = tblStages.getColumnModel();
348
                    for (int i = 0; i < tblStages.getColumnCount(); i++) {
349
                            cm.getColumn(0).setCellEditor(tce);
350
                            cm.getColumn(0).setCellRenderer(tcr);
351
                    }
352
353
                    int tablePreferredWidth = (int) tblStages.getPreferredSize().getWidth();
354
                    int colSize = tblStages.
355
                            getFontMetrics(tblStages.getFont()).
356
                            stringWidth(tblStages.getModel().getColumnName(0))*2;
357
                    cm.getColumn(0).setPreferredWidth((int) (colSize));
358
                    cm.getColumn(0).setMinWidth((int) (colSize));
359
                    cm.getColumn(0).setMaxWidth((int) (colSize));
360
                    tablePreferredWidth -= colSize;
361
                    cm.getColumn(1).setPreferredWidth((int) (tablePreferredWidth * 0.7));
362
                    cm.getColumn(2).setPreferredWidth((int) (tablePreferredWidth * 0.3));
363
            }
364
            return tblStages;
365
    }
366
367
    protected int getFlagCount() {
368 8258 jaume
        return _getFlags().size();
369 8209 jaume
    }
370
371 8258 jaume
372
    private ArrayList _getFlags() {
373 8209 jaume
        if (routeFlags == null) {
374
            routeFlags = new ArrayList();
375
        }
376
        return routeFlags;
377
    }
378
379
    protected boolean isSelected(int row) {
380 8258 jaume
        return ((GvFlag) _getFlags().get(row)).isEnabled();
381 8209 jaume
    }
382
383
    /**
384
     * This method initializes eastPanel
385
     *
386
     * @return javax.swing.JPanel
387
     */
388
    private JPanel getEastPanel() {
389
        if (eastPanel == null) {
390
            eastPanel = new GridBagLayoutPanel();
391
            eastPanel.addComponent(getBtnLoadStage());
392 8258 jaume
            eastPanel.add(getBtnSaveStage());
393 8209 jaume
        }
394
        return eastPanel;
395
    }
396
397
    /**
398
     * This method initializes defaultTableModel
399
     *
400
     * @return javax.swing.table.DefaultTableModel
401
     */
402
    private TableModel getTableModel() {
403
        if (tableModel == null) {
404
            tableModel = new DefaultTableModel() {
405
406
407
                public String getColumnName(int column) {
408
                    return colName[column];
409
                }
410
411
                public Object getValueAt(int row, int column) {
412
                    if (row > getFlagCount()-1)
413
                        return null;
414
                    switch (column) {
415
                    case 0:
416 8258 jaume
                            return new Boolean(((GvFlag) _getFlags().get(row)).isEnabled());
417 8209 jaume
                    case 1:
418 8258 jaume
                        return _getFlags().get(row);
419 8209 jaume
                    case 2:
420 8258 jaume
                            return new Double(((GvFlag) _getFlags().get(row)).getCost());
421 8209 jaume
                    }
422 8258 jaume
                    return null;
423 8209 jaume
                }
424
425
                public void setValueAt(Object aValue, int row, int column) {
426
                    if (row > getFlagCount()-1)
427
                        return;
428
                    switch (column) {
429
                    case 0:
430 8258 jaume
                            ((GvFlag) _getFlags().get(row)).setEnabled(((Boolean) aValue).booleanValue());
431 8209 jaume
                        break;
432
                    case 1:
433 8258 jaume
                        ((GvFlag) _getFlags().get(row)).setDescription((String) aValue);
434 8209 jaume
                        break;
435
                    /*case 2:
436
                        getCosts().set(row, new Double((String) aValue));
437
                        break;*/
438
                    }
439
440
                }
441
            };
442
            tableModel.setColumnCount(3);
443
            int i = getFlagCount();
444
            if (i<10)
445
                i = 10;
446
447
            tableModel.setRowCount(i);
448
        }
449
        return tableModel;
450
    }
451
452
    /**
453
     * This method initializes btnLoadStage
454
     *
455
     * @return javax.swing.JButton
456
     */
457
    private JButton getBtnLoadStage() {
458
        if (btnLoadStage == null) {
459
            btnLoadStage = new JButton();
460
            btnLoadStage.setText(PluginServices.getText(this, "load_stages"));
461 8258 jaume
            btnLoadStage.addActionListener(this);
462 8209 jaume
        }
463
        return btnLoadStage;
464
    }
465
466
    /**
467
     * This method initializes btnSaveStages
468
     *
469
     * @return javax.swing.JButton
470
     */
471 8258 jaume
    private JButton getBtnSaveStage() {
472
        if (btnSaveStage == null) {
473
            btnSaveStage = new JButton();
474
            btnSaveStage.setText(PluginServices.getText(this, "save_stages"));
475
            btnSaveStage.addActionListener(this);
476 8209 jaume
        }
477 8258 jaume
        return btnSaveStage;
478 8209 jaume
    }
479
480 8258 jaume
        public void actionPerformed(ActionEvent e) {
481
                String name = ((Component) e.getSource()).getName();
482
                if (name.equals(getBtnLoadStage().getName())) {
483
                        // load
484
485
                } else if (name.equals(getBtnSaveStage().getName())) {
486
                        // save
487
488
                } else if (name.equals(getBtnPushUpStage().getName())) {
489
                        // push up
490
                        int[] selected = tblStages.getSelectedRows();
491
                        if (selected.length == 0 || selected[0] == 0)
492
                                return;
493
494
                        tblStages.clearSelection();
495
496
                        for (int i = 0; i < selected.length; i++) {
497
                                Object aux = _getFlags().get(selected[i]-1);
498
                                _getFlags().set(selected[i]-1, _getFlags().get(selected[i]));
499
                                _getFlags().set(selected[i], aux);
500
                                selected[i]--;
501
                                tblStages.addRowSelectionInterval(selected[i], selected[i]);
502
                        }
503 8280 jaume
504 8258 jaume
                } else if (name.equals(getBtnPullDownStage().getName())) {
505
                        // pull down
506
                        int[] selected = tblStages.getSelectedRows();
507
                        if (selected.length == 0 || selected[selected.length-1] >= _getFlags().size()-1)
508
                                return;
509
510
                        // move rows
511
                        tblStages.clearSelection();
512
                        for (int i = selected.length-1; i >=0 ; i--) {
513
                                Object aux = _getFlags().get(selected[i]+1);
514
                                _getFlags().set(selected[i]+1, _getFlags().get(selected[i]));
515
                                _getFlags().set(selected[i], aux);
516
                                selected[i]++;
517
                                tblStages.addRowSelectionInterval(selected[i], selected[i]);
518
                        }
519
520 8280 jaume
521 8258 jaume
                } else if (name.equals(getBtnRemoveStage().getName())) {
522
                        // remove
523
                        int[] selected = tblStages.getSelectedRows();
524
                        for (int i = selected.length-1; i >=0 ; i--) {
525
                                try {
526
                                        removeFlag(selected[i]);
527
                                } catch (IndexOutOfBoundsException iobEx) {}
528
529
                        }
530
                        tblStages.clearSelection();
531
532
                }
533
                repaint();
534
        }
535
536
        public GvFlag[] getFlags() {
537
                return (GvFlag[]) _getFlags().toArray(new GvFlag[0]);
538
        }
539
540 8209 jaume
}  //  @jve:decl-index=0:visual-constraint="10,10"