Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / gui / ServiceAreaControlPanel.java @ 30840

History | View | Annotate | Download (16.8 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
                        double[] costs = NetworkUtils.string2doubleArray(aux, ",");
389
                        Arrays.sort(costs);
390

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

    
423
                defaultLegend.setDefaultSymbol(myDefaultSymbol);
424

    
425
                DoubleValue clave;
426
                IFillSymbol theSymbol = null;
427
                Random rnd = new Random(System.currentTimeMillis());
428
                int iField = lyrPol.getRecordset().getFieldIndexByName("COST");
429

    
430
                for (int j = 0; j < lyrPol.getRecordset().getRowCount(); j++) {
431
                        clave = (DoubleValue) lyrPol.getRecordset().getFieldValue(j, iField);
432
                        if (defaultLegend.getSymbolByValue(clave) == null) {
433
                                theSymbol =        (IFillSymbol) SymbologyFactory.
434
                                        createDefaultSymbolByShapeType(FShape.POLYGON);
435
                                theSymbol.setDescription(clave.toString());
436
                                Color newColor = new Color(rnd.nextFloat(), 
437
                                                rnd.nextFloat(),
438
                                                rnd.nextFloat(), 0.7f);
439
                                theSymbol.setFillColor(newColor);
440

    
441
                                defaultLegend.addSymbol(clave, theSymbol);
442
                        }
443

    
444
                } // for
445
                lyrPol.setLegend(defaultLegend);
446
                
447
                FLyrVect lyrLine = extractor.getLineLayer();
448
                lyrLine.setProjection(map.getProjection());
449

    
450
                solver.removeDestinationsFromNetwork(net.getFlags());
451
                map.beginAtomicEvent();
452
                map.getLayers().addLayer(lyrPol);
453
                map.getLayers().addLayer(lyrLine);
454
                
455
                map.endAtomicEvent();
456
                
457
        }
458

    
459
        public Object getWindowModel() {
460
                return this.getClass();
461
        }
462

    
463
        public WindowInfo getWindowInfo() {
464
                if (wi == null) {
465
                        wi = new WindowInfo(WindowInfo.MODELESSDIALOG
466
                                    | WindowInfo.MAXIMIZABLE
467
                                        | WindowInfo.ICONIFIABLE | WindowInfo.PALETTE);
468
                        wi.setWidth(600);
469
                        wi.setHeight((int) this.getPreferredSize().getHeight());
470
                        wi.setTitle(PluginServices.getText(this, "service_area_control_panel"));
471
                }
472
                return wi;
473
        }
474

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

    
489
                        westPanel.add(aux);
490
                }
491
                return westPanel;
492
        }
493

    
494

    
495
        protected JScrollPane getScrlStages() {
496
                if (scrlStages == null) {
497
                        scrlStages = new JScrollPane();
498
                        scrlStages.setViewportView(getTblStages());
499
                        scrlStages.setPreferredSize(new Dimension(400, 200));
500
                }
501
                return scrlStages;
502
        }
503

    
504

    
505
        /**
506
         * This method initializes tblStages
507
         *
508
         * @return javax.swing.JTable
509
         */
510
        protected JTable getTblStages() {
511
                if (tblStages == null) {
512
                        tblStages = new JTable();
513
                        tblStages
514
                                        .setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
515
                        tblStages.setModel(getTableModel());
516
                        TableColumnModel cm = tblStages.getColumnModel();
517

    
518
                        int tablePreferredWidth = 400;
519
                        int colSize = (int) (1.2 * tblStages.getFontMetrics(tblStages.getFont())
520
                                        .stringWidth(tblStages.getModel().getColumnName(0)));
521
                        cm.getColumn(0).setPreferredWidth((int) (colSize));
522
                        cm.getColumn(0).setMinWidth((int) (colSize));
523
                        cm.getColumn(0).setMaxWidth((int) (colSize));
524
                        tablePreferredWidth -= colSize;
525
                        cm.getColumn(1)
526
                                        .setPreferredWidth((int) (tablePreferredWidth * 0.5));
527
                        cm.getColumn(2)
528
                                        .setPreferredWidth((int) (tablePreferredWidth * 0.5));
529

    
530
                        // Ask to be notified of selection changes.
531
                        ListSelectionModel rowSM = tblStages.getSelectionModel();
532
                        rowSM.addListSelectionListener(new ListSelectionListener() {
533
                            public void valueChanged(ListSelectionEvent e) {
534
                                //Ignore extra messages.
535
                                if (e.getValueIsAdjusting())
536
                                        return;
537

    
538
                                ListSelectionModel lsm =
539
                                    (ListSelectionModel)e.getSource();
540
                                getBtnCenterOnFlag().setEnabled(!lsm.isSelectionEmpty());
541
                                        int[] selected = tblStages.getSelectedRows();
542
                                        if (selected.length == 0)
543
                                                return;
544
                                        GvFlag flag = (GvFlag) _getFlags().get(selected[0]);
545

    
546
                                Point2D p = flag.getOriginalPoint();
547
                                mapCtrl.repaint(); // borramos el de antes
548
                                NetworkUtils.flashPoint(mapCtrl, p.getX(), p.getY());
549
                            }
550
                        });
551

    
552
                        tblStages.getModel().addTableModelListener(new TableModelListener() {
553

    
554
                                public void tableChanged(TableModelEvent e) {
555
                                        System.out.println("Table model changed");
556
//                                        getBtnCenterOnFlag().setEnabled(false);
557
                                }
558

    
559
                        });
560

    
561

    
562
                }
563
                return tblStages;
564
        }
565

    
566

    
567
        @Override
568
        protected TableModel getTableModel() {
569
                if (tableModel2 == null)
570
                        tableModel2 = new MyTableModel();
571
                return tableModel2;
572
        }
573

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