Statistics
| Revision:

root / trunk / extensions / extGraph / src / org / gvsig / graph / gui / ServiceAreaControlPanel.java @ 30946

History | View | Annotate | Download (17 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 29800 2009-07-06 22:47:34Z fpenarrubia $
45
 * $Log$
46
 * Revision 1.26  2007-09-07 11:29:47  fjp
47
 * Casi compila. Falta arreglar lo de FArrowSymbol y retocar el graphiclist de FMap.
48
 *
49
 * Revision 1.25.2.5  2007/08/06 16:54:34  fjp
50
 * Versi?n en desarrollo con velocidades y un esbozo de ?reas de influencia
51
 *
52
 * Revision 1.25.2.4  2007/06/14 10:02:25  fjp
53
 * Pliego de redes a publicar SIN el cuadro de di?logo setVelocities (bueno, con , pero invisible)
54
 *
55
 * Revision 1.25.2.3  2007/05/24 11:33:36  fjp
56
 * Para que puedas a?adir los puntos que est?n cerca de la red. Los que no lo est?n, lanzan un error informando de qu? punto no est? cerca de la red.
57
 *
58
 * Revision 1.25.2.2  2007/05/15 07:08:21  fjp
59
 * Para calcular matrices de distancias
60
 *
61
 * Revision 1.25  2006/11/14 18:32:32  fjp
62
 * *** empty log message ***
63
 *
64
 * Revision 1.24  2006/11/14 16:12:01  fjp
65
 * *** empty log message ***
66
 *
67
 * Revision 1.23  2006/11/14 09:23:30  fjp
68
 * cargar paradas desde cualquier tema de puntos
69
 *
70
 * Revision 1.22  2006/11/10 13:57:04  fjp
71
 * *** empty log message ***
72
 *
73
 * Revision 1.21  2006/11/09 21:08:32  azabala
74
 * *** empty log message ***
75
 *
76
 * Revision 1.20  2006/11/09 12:51:12  jaume
77
 * *** empty log message ***
78
 *
79
 * Revision 1.19  2006/11/09 11:00:43  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.18  2006/11/09 10:59:53  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.17  2006/11/09 10:27:50  fjp
86
 * *** empty log message ***
87
 *
88
 * Revision 1.16  2006/11/09 10:24:11  fjp
89
 * *** empty log message ***
90
 *
91
 * Revision 1.15  2006/11/09 09:16:35  fjp
92
 * Ya va!!
93
 *
94
 * Revision 1.14  2006/11/08 20:14:52  azabala
95
 * *** empty log message ***
96
 *
97
 * Revision 1.13  2006/11/08 19:32:22  azabala
98
 * saveroute and saveflags modifications
99
 *
100
 * Revision 1.12  2006/11/08 18:16:28  fjp
101
 * *** empty log message ***
102
 *
103
 * Revision 1.11  2006/11/08 16:48:19  fjp
104
 * *** empty log message ***
105
 *
106
 * Revision 1.10  2006/11/08 16:00:39  fjp
107
 * Por terminar el enlace flags-cuadro de di?logo
108
 *
109
 * Revision 1.9  2006/11/08 13:18:46  fjp
110
 * Por terminar el enlace flags-cuadro de di?logo
111
 *
112
 * Revision 1.8  2006/11/07 19:49:38  azabala
113
 * *** empty log message ***
114
 *
115
 * Revision 1.7  2006/11/06 13:13:53  azabala
116
 * *** empty log message ***
117
 *
118
 * Revision 1.6  2006/11/06 10:29:32  jaume
119
 * *** empty log message ***
120
 *
121
 * Revision 1.5  2006/11/03 19:39:29  azabala
122
 * *** empty log message ***
123
 *
124
 * Revision 1.4  2006/10/27 18:26:22  azabala
125
 * added implementation of load stages method
126
 *
127
 * Revision 1.3  2006/10/27 12:41:09  jaume
128
 * GUI
129
 *
130
 * Revision 1.2  2006/10/26 16:31:21  jaume
131
 * GUI
132
 *
133
 * Revision 1.1  2006/10/25 10:50:41  jaume
134
 * movement of classes and gui stuff
135
 *
136
 * Revision 1.4  2006/10/24 08:04:41  jaume
137
 * *** empty log message ***
138
 *
139
 * Revision 1.3  2006/10/23 16:00:20  jaume
140
 * *** empty log message ***
141
 *
142
 * Revision 1.2  2006/10/23 08:05:39  jaume
143
 * GUI
144
 *
145
 * Revision 1.1  2006/10/20 12:02:50  jaume
146
 * GUI
147
 *
148
 *
149
 */
150
package org.gvsig.graph.gui;
151

    
152
import java.awt.BorderLayout;
153
import java.awt.Color;
154
import java.awt.Component;
155
import java.awt.Dimension;
156
import java.awt.FlowLayout;
157
import java.awt.Font;
158
import java.awt.GridLayout;
159
import java.awt.event.ActionEvent;
160
import java.awt.event.ActionListener;
161
import java.awt.geom.Point2D;
162
import java.util.Arrays;
163
import java.util.Random;
164

    
165
import javax.swing.JButton;
166
import javax.swing.JCheckBox;
167
import javax.swing.JLabel;
168
import javax.swing.JOptionPane;
169
import javax.swing.JPanel;
170
import javax.swing.JScrollPane;
171
import javax.swing.JTable;
172
import javax.swing.ListSelectionModel;
173
import javax.swing.event.ListSelectionEvent;
174
import javax.swing.event.ListSelectionListener;
175
import javax.swing.event.TableModelEvent;
176
import javax.swing.event.TableModelListener;
177
import javax.swing.table.AbstractTableModel;
178
import javax.swing.table.TableColumnModel;
179
import javax.swing.table.TableModel;
180

    
181
import org.apache.log4j.Logger;
182
import org.gvsig.exceptions.BaseException;
183
import org.gvsig.graph.core.GvFlag;
184
import org.gvsig.graph.core.Network;
185
import org.gvsig.graph.core.NetworkUtils;
186
import org.gvsig.graph.solvers.OneToManySolver;
187
import org.gvsig.graph.solvers.ServiceAreaExtractor2;
188
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
189

    
190
import com.hardcode.gdbms.engine.values.DoubleValue;
191
import com.hardcode.gdbms.engine.values.ValueFactory;
192
import com.iver.andami.PluginServices;
193
import com.iver.andami.ui.mdiManager.WindowInfo;
194
import com.iver.cit.gvsig.fmap.MapContext;
195
import com.iver.cit.gvsig.fmap.core.FShape;
196
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
197
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
198
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
199
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
200
import com.iver.cit.gvsig.fmap.rendering.IVectorialUniqueValueLegend;
201
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
202

    
203
public class ServiceAreaControlPanel extends RouteControlPanel {
204
        private static Logger logger = Logger.getLogger(ServiceAreaControlPanel.class.getName());
205

    
206
        WindowInfo wi;
207

    
208
        private MyTableModel tableModel2 = new MyTableModel();
209

    
210
        private JCheckBox chkCompactArea;
211
        private class MyTableModel extends AbstractTableModel {
212
                private String[] colName2;
213

    
214
                public int getColumnCount() {
215
                        return 3;
216
                }
217

    
218
                public int getRowCount() {
219
                        return _getFlags().size();
220
                }
221

    
222
                public Object getValueAt(int rowIndex, int columnIndex) {
223
                        GvFlag flag = (GvFlag) _getFlags().get(rowIndex);
224
                        switch (columnIndex) {
225
                        case 0:
226
                                return new Boolean(flag.isEnabled());
227
                        case 1:
228
                                return flag.getDescription();
229
                        case 2:
230
                                return flag.getProperties().get("service_area_costs");                                
231
                        }
232

    
233
                        return null;
234
                }
235

    
236
                public Class getColumnClass(int columnIndex) {
237
                        switch (columnIndex)
238
                        {
239
                        case 0:
240
                                return Boolean.class;
241
                        case 1:
242
                                return String.class;
243
                        case 2:
244
                                return String.class;
245
                        }
246
                        return super.getColumnClass(columnIndex);
247
                }
248

    
249
                public String getColumnName(int column) {
250
                        if (colName2 == null)
251
                                colName2 = new String[] {
252
                                        PluginServices.getText(this, "enable"),
253
                                        PluginServices.getText(this, "stage"),
254
                                        PluginServices.getText(this, "costs"),
255
                                        };
256
                        return colName2[column];
257
                }
258

    
259
                public boolean isCellEditable(int rowIndex, int columnIndex) {
260
                        return true;
261

    
262
                }
263

    
264
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
265
                        GvFlag flag = (GvFlag) _getFlags().get(rowIndex);
266
                        switch (columnIndex)
267
                        {
268
                        case 0:
269
                                Boolean bAux = (Boolean) aValue;
270
                                flag.setEnabled(bAux.booleanValue());
271
                                PluginServices.getMainFrame().enableControls();
272
                                return;
273
                        case 1:
274
                                String strAux = (String) aValue;
275
                                flag.setDescription(strAux);
276
                                return;
277
                        case 2: 
278
                                flag.getProperties().setProperty("service_area_costs", (String) aValue);
279
                                return;
280

    
281
                        }
282

    
283
                }
284

    
285
        }
286
        
287

    
288

    
289
        /**
290
         * This method initializes
291
         *
292
         */
293
        public ServiceAreaControlPanel(Network network) {
294
                super(network);
295
//                initialize();
296
        }
297

    
298

    
299
        @Override
300
        protected void initialize() {
301
                JButton jBtnCalculateServiceArea = new JButton(PluginServices.getText(null, "Calculate_Service_Areas"));
302
                jBtnCalculateServiceArea.addActionListener(new ActionListener() {
303

    
304
                        public void actionPerformed(ActionEvent e) {
305
                                try {
306
                                        calculateServiceAreas(network);
307
                                } catch (BaseException e1) {
308
                                        // TODO Auto-generated catch block
309
                                        e1.printStackTrace();
310
                                }
311
                        }
312
                        
313
                });
314
                panelButtonsEast.add(jBtnCalculateServiceArea);
315
                BorderLayout borderLayout = new BorderLayout();
316
                borderLayout.setHgap(10);
317
                borderLayout.setVgap(10);
318
                JPanel cont = new JPanel(borderLayout);
319
//                cont.setPreferredSize(new Dimension(490, 320));
320
                this.setPreferredSize(new Dimension(460, 280));
321
                cont.add(getWestPanel(), BorderLayout.CENTER);
322
                cont.add(getEastPanel(), BorderLayout.EAST);
323
                cont.add(getSouthPanel(), BorderLayout.SOUTH);
324
                this.setLayout(new FlowLayout(FlowLayout.LEADING, 10, 10));
325
                this.add(cont);
326
                updateFlags();
327
                getChkReturnToOrigin().setVisible(false);
328
                getChkTSP().setVisible(false);
329
                lblCost.setVisible(false);
330
                
331
        }
332
        
333
        /**
334
         * This method initializes eastPanel
335
         *
336
         * @return javax.swing.JPanel
337
         */
338
        protected JPanel getEastPanel() {
339
                if (eastPanel == null) {
340
                        GridLayout layout = new GridLayout();
341
                        layout.setColumns(1);
342
                        layout.setVgap(5);
343
                        
344
                        panelButtonsEast.add(getBtnLoadStage());
345
                        panelButtonsEast.add(getBtnSaveStage());
346
//                        panelButtonsEast.add(getBtnSaveRoute());
347
                        panelButtonsEast.add(getBtnCenterOnFlag());
348
                        
349
                        panelButtonsEast.add(getChkCompactArea());
350
                        
351
//                        panelButtonsEast.add(getBtnSetVelocities());
352
//                        panelButtonsEast.add(getChkTSP());
353
//                        panelButtonsEast.add(getChkReturnToOrigin());
354
                        panelButtonsEast.add(new JLabel(PluginServices.getText(this, "tolerance") + ":"));
355
                        panelButtonsEast.add(getTxtTolerance());
356
                        
357
                        layout.setRows(panelButtonsEast.getComponentCount());
358
                        panelButtonsEast.setLayout(layout);
359
                        eastPanel = new GridBagLayoutPanel();
360
                        eastPanel.addComponent(panelButtonsEast);
361
                }
362
                return eastPanel;
363
        }
364

    
365

    
366
        public JCheckBox getChkCompactArea() {
367
                if (chkCompactArea == null) {
368
                        chkCompactArea = new JCheckBox();
369
                        chkCompactArea.setText(PluginServices.getText(this,
370
                                        "compact_area"));
371
                }
372
                return chkCompactArea;
373

    
374
        }
375

    
376

    
377
        public void calculateServiceAreas(Network net) throws BaseException {
378
                OneToManySolver solver = new OneToManySolver();
379
                solver.setNetwork(net);
380
                GvFlag[] flags = net.getFlags();
381
                solver.putDestinationsOnNetwork(flags);
382

    
383
                ServiceAreaExtractor2 extractor = new ServiceAreaExtractor2(net);
384
                
385
                solver.addListener(extractor);
386
                for (int i = 0; i < flags.length; i++) {
387
                        String aux = (String) getTableModel().getValueAt(i, 2);
388
                        if (aux == null) {
389
                                JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), 
390
                                                PluginServices.getText(null, "please_introduce_some_costs") + ":" + 
391
                                                PluginServices.getText(null, "Line") + " " + (i+1));
392
                                return;
393
                        }
394
                        double[] costs = NetworkUtils.string2doubleArray(aux, ",");
395
                        Arrays.sort(costs);
396

    
397
                        solver.setSourceFlag(flags[i]);
398
                        long t1 = System.currentTimeMillis();                        
399
                        solver.setExploreAllNetwork(true);
400
                        for (int j=costs.length-1; j>= 0; j--) {
401
                                solver.setMaxCost(costs[j]*1.2);
402
                                extractor.setIdFlag(i);
403
                                double[] oneOnly = new double[1];
404
                                oneOnly[0] = costs[j];
405
                                extractor.setCosts(oneOnly);                        
406
                                extractor.setDoCompactArea(getChkCompactArea().isSelected());                        
407
                                solver.calculate();
408
                                extractor.writeServiceArea();
409
        //                        FLyrVect lyrPoints= extractor.getBorderPoints();
410
        //                        lyrPoints.setProjection(map.getProjection());
411
        //                        map.getLayers().addLayer(lyrPoints);
412
                                extractor.reset();
413
                        } // j
414
                        long t2 = System.currentTimeMillis();
415
                        System.out.println("Punto " + i + " de "
416
                                        + flags.length + ". " + (t2 - t1)
417
                                        + " msecs.");
418
                                                
419
                }
420
                extractor.closeFiles();
421
                MapContext map = mapCtrl.getMapContext();
422
                FLyrVect lyrPol = extractor.getPolygonLayer();
423
                lyrPol.setProjection(map.getProjection());
424
                IVectorialUniqueValueLegend defaultLegend = LegendFactory.createVectorialUniqueValueLegend(FShape.POLYGON);
425
                defaultLegend.setClassifyingFieldNames(new String[] {"COST"} );
426
                ISymbol myDefaultSymbol = SymbologyFactory.
427
                        createDefaultSymbolByShapeType(FShape.POLYGON);
428

    
429
                defaultLegend.setDefaultSymbol(myDefaultSymbol);
430

    
431
                DoubleValue clave;
432
                IFillSymbol theSymbol = null;
433
                Random rnd = new Random(System.currentTimeMillis());
434
                int iField = lyrPol.getRecordset().getFieldIndexByName("COST");
435

    
436
                for (int j = 0; j < lyrPol.getRecordset().getRowCount(); j++) {
437
                        clave = (DoubleValue) lyrPol.getRecordset().getFieldValue(j, iField);
438
                        if (defaultLegend.getSymbolByValue(clave) == null) {
439
                                theSymbol =        (IFillSymbol) SymbologyFactory.
440
                                        createDefaultSymbolByShapeType(FShape.POLYGON);
441
                                theSymbol.setDescription(clave.toString());
442
                                Color newColor = new Color(rnd.nextFloat(), 
443
                                                rnd.nextFloat(),
444
                                                rnd.nextFloat(), 0.7f);
445
                                theSymbol.setFillColor(newColor);
446

    
447
                                defaultLegend.addSymbol(clave, theSymbol);
448
                        }
449

    
450
                } // for
451
                lyrPol.setLegend(defaultLegend);
452
                
453
                FLyrVect lyrLine = extractor.getLineLayer();
454
                lyrLine.setProjection(map.getProjection());
455

    
456
                solver.removeDestinationsFromNetwork(net.getFlags());
457
                map.beginAtomicEvent();
458
                map.getLayers().addLayer(lyrPol);
459
                map.getLayers().addLayer(lyrLine);
460
                
461
                map.endAtomicEvent();
462
                
463
        }
464

    
465
        public Object getWindowModel() {
466
                return this.getClass();
467
        }
468

    
469
        public WindowInfo getWindowInfo() {
470
                if (wi == null) {
471
                        wi = new WindowInfo(WindowInfo.MODELESSDIALOG
472
                                    | WindowInfo.MAXIMIZABLE
473
                                        | WindowInfo.ICONIFIABLE | WindowInfo.PALETTE);
474
                        wi.setWidth(600);
475
                        wi.setHeight((int) this.getPreferredSize().getHeight());
476
                        wi.setTitle(PluginServices.getText(this, "service_area_control_panel"));
477
                }
478
                return wi;
479
        }
480

    
481
        public Object getWindowProfile(){
482
                return WindowInfo.TOOL_PROFILE;
483
        }
484
        
485
        protected JPanel getWestPanel() {
486
                if (westPanel == null) {
487
                        westPanel = new JPanel(new BorderLayout(5, 5));
488
                        lblCost = new JLabel();
489
                        lblCost.setFont(lblCost.getFont().deriveFont(Font.BOLD));
490
                        GridBagLayoutPanel aux = new GridBagLayoutPanel();
491
//                        aux.addComponent(PluginServices.getText(this, "total_route_cost")
492
//                                        + ":", lblCost);
493
                        aux.addComponent(getScrlStages());
494

    
495
                        westPanel.add(aux);
496
                }
497
                return westPanel;
498
        }
499

    
500

    
501
        protected JScrollPane getScrlStages() {
502
                if (scrlStages == null) {
503
                        scrlStages = new JScrollPane();
504
                        scrlStages.setViewportView(getTblStages());
505
                        scrlStages.setPreferredSize(new Dimension(400, 200));
506
                }
507
                return scrlStages;
508
        }
509

    
510

    
511
        /**
512
         * This method initializes tblStages
513
         *
514
         * @return javax.swing.JTable
515
         */
516
        protected JTable getTblStages() {
517
                if (tblStages == null) {
518
                        tblStages = new JTable();
519
                        tblStages
520
                                        .setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
521
                        tblStages.setModel(getTableModel());
522
                        TableColumnModel cm = tblStages.getColumnModel();
523

    
524
                        int tablePreferredWidth = 400;
525
                        int colSize = (int) (1.2 * tblStages.getFontMetrics(tblStages.getFont())
526
                                        .stringWidth(tblStages.getModel().getColumnName(0)));
527
                        cm.getColumn(0).setPreferredWidth((int) (colSize));
528
                        cm.getColumn(0).setMinWidth((int) (colSize));
529
                        cm.getColumn(0).setMaxWidth((int) (colSize));
530
                        tablePreferredWidth -= colSize;
531
                        cm.getColumn(1)
532
                                        .setPreferredWidth((int) (tablePreferredWidth * 0.5));
533
                        cm.getColumn(2)
534
                                        .setPreferredWidth((int) (tablePreferredWidth * 0.5));
535

    
536
                        // Ask to be notified of selection changes.
537
                        ListSelectionModel rowSM = tblStages.getSelectionModel();
538
                        rowSM.addListSelectionListener(new ListSelectionListener() {
539
                            public void valueChanged(ListSelectionEvent e) {
540
                                //Ignore extra messages.
541
                                if (e.getValueIsAdjusting())
542
                                        return;
543

    
544
                                ListSelectionModel lsm =
545
                                    (ListSelectionModel)e.getSource();
546
                                getBtnCenterOnFlag().setEnabled(!lsm.isSelectionEmpty());
547
                                        int[] selected = tblStages.getSelectedRows();
548
                                        if (selected.length == 0)
549
                                                return;
550
                                        GvFlag flag = (GvFlag) _getFlags().get(selected[0]);
551

    
552
                                Point2D p = flag.getOriginalPoint();
553
                                mapCtrl.repaint(); // borramos el de antes
554
                                NetworkUtils.flashPoint(mapCtrl, p.getX(), p.getY());
555
                            }
556
                        });
557

    
558
                        tblStages.getModel().addTableModelListener(new TableModelListener() {
559

    
560
                                public void tableChanged(TableModelEvent e) {
561
                                        System.out.println("Table model changed");
562
//                                        getBtnCenterOnFlag().setEnabled(false);
563
                                }
564

    
565
                        });
566

    
567

    
568
                }
569
                return tblStages;
570
        }
571

    
572

    
573
        @Override
574
        protected TableModel getTableModel() {
575
                if (tableModel2 == null)
576
                        tableModel2 = new MyTableModel();
577
                return tableModel2;
578
        }
579

    
580
} // @jve:decl-index=0:visual-constraint="17,9"