Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / gui / solvers / ClosestFacilityDialog.java @ 31984

History | View | Annotate | Download (32 KB)

1
package org.gvsig.graph.gui.solvers;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Color;
5
import java.awt.GridLayout;
6
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionListener;
8
import java.awt.event.MouseEvent;
9
import java.awt.event.MouseListener;
10
import java.io.File;
11
import java.io.FileNotFoundException;
12
import java.util.ArrayList;
13
import java.util.Hashtable;
14
import java.util.prefs.Preferences;
15

    
16
import javax.swing.ButtonGroup;
17
import javax.swing.JButton;
18
import javax.swing.JCheckBox;
19
import javax.swing.JComboBox;
20
import javax.swing.JFileChooser;
21
import javax.swing.JLabel;
22
import javax.swing.JOptionPane;
23
import javax.swing.JPanel;
24
import javax.swing.JRadioButton;
25
import javax.swing.JScrollPane;
26
import javax.swing.JTable;
27
import javax.swing.JTextField;
28
import javax.swing.border.TitledBorder;
29
import javax.swing.table.AbstractTableModel;
30
import javax.swing.table.TableColumnModel;
31

    
32
import org.gvsig.graph.ClosestFacilityController;
33
import org.gvsig.graph.IClosestFacilityListener;
34
import org.gvsig.graph.core.GraphException;
35
import org.gvsig.graph.core.GvFlag;
36
import org.gvsig.graph.core.Network;
37
import org.gvsig.graph.solvers.Route;
38

    
39
import com.hardcode.driverManager.DriverLoadException;
40
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException;
41
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
42
import com.hardcode.gdbms.engine.data.NoSuchTableException;
43
import com.iver.andami.PluginServices;
44
import com.iver.andami.ui.mdiManager.IWindow;
45
import com.iver.andami.ui.mdiManager.WindowInfo;
46
import com.iver.cit.gvsig.exceptions.visitors.ProcessWriterVisitorException;
47
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
48
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException;
49
import com.iver.cit.gvsig.fmap.layers.FLayer;
50
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
51
import com.iver.cit.gvsig.project.documents.view.gui.View;
52
import com.iver.utiles.GenericFileFilter;
53

    
54
public class ClosestFacilityDialog extends JPanel implements IWindow, ActionListener, MouseListener, IClosestFacilityListener, Runnable{
55
        
56
        /**
57
         * 
58
         */
59
        private static final long serialVersionUID = 8072170250049258985L;
60
        
61
        private JPanel panelFacilitiesWest;
62
        private JPanel panelFacilitiesCenter;
63
        private JPanel panelFacilitiesEast;
64
        private JPanel panelFacilities;
65
        private TitledBorder titledBorderFacilities;
66
        private JLabel labelFacilitiesName;
67
        private JComboBox comboFacilities;
68
        private JCheckBox checkFacilitiesSelection;
69
        private JLabel labelFacilitiesNumber;
70
        private JComboBox comboFacilitiesNumber; //Contendra tantos numeros como puntos tenga la capa de proveedores
71
        private JLabel labelFacilitiesMaxLimit;
72
        private JTextField textFieldFacilitiesMaxLimit;
73
        private JLabel labelFacilitiesMaxLimitUnits;
74
        
75
        private JPanel panelEvents;
76
        private TitledBorder titledBorderEvents;
77
        private JPanel panelEventsRadioButtons;
78
        private ButtonGroup buttonGroupEventsRouteToFrom;
79
        private JRadioButton radioButtonEventsTo;
80
        private JRadioButton radioButtonEventsFrom;
81
        private JPanel panelEventsTable;
82
        private JScrollPane scrollEventsTable;
83
        private JTable tableEvents;
84
        private JPanel panelEventsTableButtons;
85
        private JButton buttonEventsRemove;
86
        private JButton buttonEventsLoad;
87
        private JButton buttonEventsSave;
88
        
89
        private JPanel panelSolution;
90
        private TitledBorder titledBorderSolution;
91
        private JScrollPane scrollSolutionTable;
92
        private JTable tableSolution;
93
        private JPanel panelSolutionButtons;
94
        private JButton buttonSolutionProperties;
95
        private JButton buttonSolutionSolve;
96
        private JButton buttonSolutionInstructions;
97
        private JButton buttonSolutionZoomRoute;
98
        private JButton buttonSolutionDrawRoute;
99
        
100
        private WindowInfo wi;
101
        
102
        private Hashtable properties;
103
        
104
        private EventsDataModel events;
105
        private FacilitiesDataModel facilities;
106
        
107
        private ClosestFacilityController cfc;
108
        
109
        private GvFlag selectedEvent;
110
        
111
        private PluginServices ps=PluginServices.getPluginServices(this);
112
        
113
        public ClosestFacilityDialog(ClosestFacilityController cfc){
114
                
115
                //FACILITIES
116
                this.panelFacilities=new JPanel(new GridLayout(1, 3, 5, 5));
117
                
118
                this.panelFacilitiesWest=new JPanel(new GridLayout(3, 1, 5, 7));
119
                this.panelFacilitiesCenter=new JPanel(new GridLayout(3, 1, 5, 7));
120
                this.panelFacilitiesEast=new JPanel(new GridLayout(3, 1, 5, 7));
121
                
122
                this.titledBorderFacilities=new TitledBorder(ps.getText("facilities"));
123
                this.panelFacilities.setBorder(this.titledBorderFacilities);
124
                this.labelFacilitiesName=new JLabel(ps.getText("facilities")+":", JLabel.RIGHT);
125
                this.comboFacilities=new JComboBox();
126
                this.comboFacilities.addActionListener(this);
127
                this.checkFacilitiesSelection=new JCheckBox(ps.getText("only_use_the_selected_points"));
128
                this.checkFacilitiesSelection.addActionListener(this);
129
                
130
                this.labelFacilitiesNumber=new JLabel(ps.getText("number_of_facilities_to_search"), JLabel.RIGHT);
131
                this.comboFacilitiesNumber=new JComboBox();
132
                this.comboFacilitiesNumber.addActionListener(this);
133

    
134
                this.labelFacilitiesMaxLimit=new JLabel(ps.getText("max_cost_limit"), JLabel.RIGHT);
135
                this.textFieldFacilitiesMaxLimit=new JTextField();
136
                this.labelFacilitiesMaxLimitUnits=new JLabel(ps.getText("cost_facility_units"), JLabel.LEFT);
137
                
138
                this.panelFacilitiesWest.add(this.labelFacilitiesName);
139
                this.panelFacilitiesWest.add(this.labelFacilitiesNumber);
140
                this.panelFacilitiesWest.add(this.labelFacilitiesMaxLimit);
141
                
142
                this.panelFacilitiesCenter.add(this.comboFacilities);
143
                this.panelFacilitiesCenter.add(this.comboFacilitiesNumber);
144
                this.panelFacilitiesCenter.add(this.textFieldFacilitiesMaxLimit);
145
                
146
                this.panelFacilitiesEast.add(this.checkFacilitiesSelection);
147
                this.panelFacilitiesEast.add(new JPanel());
148
                this.panelFacilitiesEast.add(this.labelFacilitiesMaxLimitUnits);
149
                
150
                this.panelFacilities.add(this.panelFacilitiesWest);
151
                this.panelFacilities.add(this.panelFacilitiesCenter);
152
                this.panelFacilities.add(this.panelFacilitiesEast);
153
                
154
                //EVENTS
155
                
156
                this.panelEvents=new JPanel(new BorderLayout(5, 5));
157
                this.titledBorderEvents=new TitledBorder(ps.getText("events"));
158
                this.panelEvents.setBorder(this.titledBorderEvents);
159
                
160
                this.panelEventsRadioButtons=new JPanel(new GridLayout(2, 1, 3, 3));
161
                this.buttonGroupEventsRouteToFrom=new ButtonGroup();
162
                this.radioButtonEventsTo=new JRadioButton(ps.getText("route_to_the_event"));
163
                this.radioButtonEventsFrom=new JRadioButton(ps.getText("route_from_the_event"));
164
                this.buttonGroupEventsRouteToFrom.add(this.radioButtonEventsTo);
165
                this.buttonGroupEventsRouteToFrom.add(this.radioButtonEventsFrom);
166
                this.panelEventsRadioButtons.add(this.radioButtonEventsTo);
167
                this.panelEventsRadioButtons.add(this.radioButtonEventsFrom);
168
                
169
                this.panelEventsTable=new JPanel(new BorderLayout(5, 5));
170
                this.scrollEventsTable=new JScrollPane();
171
                this.scrollEventsTable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
172
                this.scrollEventsTable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
173
                this.tableEvents=new JTable();
174
                this.tableEvents.setShowGrid(true);
175
                this.tableEvents.setRowSelectionAllowed(true);
176
                this.tableEvents.addMouseListener(this);
177
                this.scrollEventsTable.getViewport().add(this.tableEvents);
178
                
179
                this.panelEventsTableButtons=new JPanel(new GridLayout(1, 3, 3, 7));
180
                this.buttonEventsRemove=new JButton(ps.getText("remove_event"));
181
                this.buttonEventsRemove.addActionListener(this);
182
                this.buttonEventsLoad=new JButton(ps.getText("load_events"));
183
                this.buttonEventsLoad.addActionListener(this);
184
                this.buttonEventsSave=new JButton(ps.getText("save_events"));
185
                this.buttonEventsSave.addActionListener(this);
186
                this.panelEventsTableButtons.add(this.buttonEventsRemove);
187
                this.panelEventsTableButtons.add(this.buttonEventsLoad);
188
                this.panelEventsTableButtons.add(this.buttonEventsSave);
189
                this.panelEventsTable.add(this.scrollEventsTable, BorderLayout.CENTER);
190
                this.panelEventsTable.add(this.panelEventsTableButtons, BorderLayout.SOUTH);
191
                
192
                this.panelEvents.add(this.panelEventsRadioButtons, BorderLayout.WEST);
193
                this.panelEvents.add(this.panelEventsTable, BorderLayout.CENTER);
194
                
195
                //SOLUTION
196
                
197
                this.panelSolution=new JPanel(new BorderLayout(5, 5));
198
                this.titledBorderSolution=new TitledBorder(ps.getText("closest_facility_solution"));
199
                this.panelSolution.setBorder(this.titledBorderSolution);
200
                
201
                this.scrollSolutionTable=new JScrollPane();
202
                this.scrollSolutionTable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
203
                this.scrollSolutionTable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
204
                this.tableSolution=new JTable();
205
                this.tableSolution.setShowGrid(true);
206
                this.tableSolution.setRowSelectionAllowed(true);
207
                this.tableSolution.addMouseListener(this);
208
                this.scrollSolutionTable.getViewport().add(this.tableSolution);
209
                
210
                //this.panelSolutionButtons=new JPanel(new GridLayout(1, 5, 3, 7));
211
                this.panelSolutionButtons=new JPanel(new GridLayout(1, 4, 3, 7));//De momento 4 porque se ha eliminado el boton de propiedades
212
                //this.buttonSolutionProperties=new JButton("Propiedades");
213
                //this.buttonSolutionProperties.addActionListener(this);
214
                this.buttonSolutionInstructions=new JButton(ps.getText("closest_facility_instructions"));
215
                this.buttonSolutionInstructions.addActionListener(this);
216
                this.buttonSolutionDrawRoute=new JButton(ps.getText("draw_route"));
217
                this.buttonSolutionDrawRoute.addActionListener(this);
218
                this.buttonSolutionZoomRoute=new JButton(ps.getText("zoom_route"));
219
                this.buttonSolutionZoomRoute.addActionListener(this);
220
                this.buttonSolutionSolve=new JButton(ps.getText("closest_facility_solve"));
221
                this.buttonSolutionSolve.addActionListener(this);
222
                //this.panelSolutionButtons.add(this.buttonSolutionProperties);
223
                this.panelSolutionButtons.add(this.buttonSolutionInstructions);
224
                this.panelSolutionButtons.add(this.buttonSolutionDrawRoute);
225
                this.panelSolutionButtons.add(this.buttonSolutionZoomRoute);
226
                this.panelSolutionButtons.add(this.buttonSolutionSolve);
227
                
228
                this.panelSolution.add(this.scrollSolutionTable, BorderLayout.CENTER);
229
                this.panelSolution.add(this.panelSolutionButtons, BorderLayout.SOUTH);
230
                
231
                //GENERAL
232
                
233
                this.setLayout(new GridLayout(3, 1, 10, 10));
234
                this.add(this.panelFacilities);
235
                this.add(this.panelEvents);
236
                this.add(this.panelSolution);
237
                
238
                this.radioButtonEventsFrom.setSelected(true);
239
                
240
                this.properties=new Hashtable();
241
                this.properties.put("ROUND_COST", Integer.valueOf(5));
242
                
243
                this.cfc=cfc;
244
                this.facilities=new FacilitiesDataModel(this.cfc, this.tableSolution);
245
                this.cfc.addClosestFacilityListener(this);
246
                this.events=new EventsDataModel(this.cfc, this.tableEvents);
247
                this.selectedEvent=null;
248
        }
249

    
250
        public void addFacilitiesLayer(FLyrVect facilitiesLayer){
251
                this.addFacilitiesLayerToCombo(facilitiesLayer);
252
        }
253
        
254
        public void removeFacilitiesLayer(FLyrVect facilitiesLayer){
255
                this.comboFacilities.removeItem(facilitiesLayer.getName());
256
                this.comboFacilities.updateUI();
257
        }
258
        
259
        private boolean addFacilitiesLayerToCombo(FLyrVect facilitiesLayer){
260
                this.comboFacilities.addItem(facilitiesLayer.getName());
261
                this.comboFacilities.updateUI();
262
                return true;
263
        }
264

    
265
        private void removeFacilitiesLayers(){
266
                this.comboFacilities.removeAllItems();
267
                this.comboFacilities.updateUI();
268
        }
269
        
270
        private void fillMaxFacilitiesCombo(int maxFacilities) {
271
                int realMaxFacilities=(maxFacilities<this.getToolMaxFacilitiesNumber())? maxFacilities : this.getToolMaxFacilitiesNumber();
272
                this.comboFacilitiesNumber.removeAllItems();
273
                for (int i = 1; i <= realMaxFacilities; i++) {
274
                        this.comboFacilitiesNumber.addItem(Integer.valueOf(i));
275
                }
276
        }
277

    
278
        public boolean onlySelectedFacilities(){
279
                return this.checkFacilitiesSelection.isSelected();
280
        }
281
        
282
        public void setMaxFacilitiesNumber(int maxFacilitiesNumber){
283
                this.cfc.setMaxFacilitiesNumber(maxFacilitiesNumber);
284
        }
285
        
286
        public int getMaxFacilitiesNumber(){
287
                return this.cfc.getMaxFacilitiesNumber();
288
        }
289
        
290
        public void setToolMaxFacilitiesNumber(int toolMaxFacilitiesNumber){
291
                this.cfc.setToolMaxFacilitiesNumber(toolMaxFacilitiesNumber);
292
        }
293
        
294
        public int getToolMaxFacilitiesNumber(){
295
                return this.cfc.getToolMaxFacilitiesNumber();
296
        }
297
        
298
        public int getSelectedMaxFacilitiesNumber(){
299
                return ((Integer)this.comboFacilitiesNumber.getSelectedItem()).intValue();
300
        }
301
        
302
        public double getFacilitiesMaxLimit() throws NumberFormatException{
303
                 return Double.parseDouble(this.textFieldFacilitiesMaxLimit.getText().trim());
304
        }
305
        
306
        public boolean isToEventSelected(){
307
                return this.radioButtonEventsTo.isSelected();
308
        }
309
        
310
        public boolean isFromEventSelected(){
311
                return this.radioButtonEventsFrom.isSelected();
312
        }
313
        
314
        public GvFlag getSelectedEvent(){
315
                if(this.tableEvents.getSelectedRowCount()>0 && this.tableEvents.getRowCount()>0){
316
                        Object o=this.events.getValueAt(this.tableEvents.getSelectedRow(), 0);
317
                        if(o!=null)
318
                                return this.cfc.getEvent(((Integer)o).intValue());
319
                }
320
                return null;
321
        }
322
        
323
        public WindowInfo getWindowInfo() {
324
                if(this.wi==null){
325
                        this.wi=new WindowInfo(WindowInfo.PALETTE | WindowInfo.RESIZABLE);
326
                        this.wi.setTitle(ps.getText("closest_facility"));
327
                        this.wi.setWidth(510);
328
                        this.wi.setHeight(400);
329
                }
330
                
331
                return wi;
332
        }
333
        
334
        public Object getWindowProfile(){
335
                return WindowInfo.TOOL_PROFILE;
336
        }
337
        
338
        public void actionPerformed(ActionEvent e) {
339
                Object source=e.getSource();
340
                
341
                if(source.getClass()==JButton.class){
342
                        if(source==this.buttonEventsLoad){
343
                                try{
344
                                        String path="";
345
                                        Preferences prefs = Preferences.userRoot().node("gvsig.foldering");
346
                                        path = prefs.get("DataFolder", null);
347
                                        JFileChooser fileChooser=new JFileChooser(path);
348
                                        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
349
                                        fileChooser.addChoosableFileFilter(new GenericFileFilter("shp", ps.getText("shp_files")));                                
350
                                        if(fileChooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
351
                                                
352
                                                this.cfc.loadEventsFromFile(fileChooser.getSelectedFile());
353
                                        }
354
                                }
355
                                catch(ReadDriverException except){
356
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
357
                                }
358
                                catch (FileNotFoundException except) {
359
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
360
                                }
361
                                catch (NullPointerException except) {
362
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
363
                                }
364
                                catch (GraphException except) {
365
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
366
                                }
367
                                catch (DriverLoadException except) {
368
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
369
                                }
370
                                catch (NoSuchTableException except) {
371
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
372
                                }
373
                                catch(Exception except){
374
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
375
                                }
376
                        }
377
                        else if(source==this.buttonEventsRemove){
378
                                if(this.tableEvents.getSelectedRowCount()>0 && this.tableEvents.getRowCount()>0){
379
                                        GvFlag flag=this.getSelectedEvent();
380
                                        if(flag!=null){
381
                                                this.cfc.removeEvent(flag);
382
                                        }
383
                                }
384
                        }
385
                        else if(source==this.buttonEventsSave){
386
                                if(this.tableEvents.getRowCount()>0){
387
                                        String path="";
388
                                        Preferences prefs = Preferences.userRoot().node("gvsig.foldering");
389
                                        path = prefs.get("DataFolder", null);
390
                                        JFileChooser fileChooser=new JFileChooser(path);
391
                                        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
392
                                        fileChooser.addChoosableFileFilter(new GenericFileFilter("shp", ps.getText("shp_files")));                                
393
                                        if(fileChooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){
394
                                                File shpFile=fileChooser.getSelectedFile();
395
                                                if (!shpFile.getPath().toLowerCase().endsWith(".shp"))
396
                                                        shpFile = new File(shpFile.getPath() + ".shp");
397
                                                if(shpFile!=null){
398
                                                        try{
399
                                                                if(shpFile.exists()){
400
                                                                        if(JOptionPane.showConfirmDialog(this, ps.getText("overwrite_selected_file_confirmation"), ps.getText("confirmation_overwrite"), JOptionPane.INFORMATION_MESSAGE)==JOptionPane.YES_OPTION){
401
                                                                                this.cfc.createSHPEvents(shpFile);
402
                                                                        }
403
                                                                }
404
                                                                else{
405
                                                                        this.cfc.createSHPEvents(shpFile);
406
                                                                }
407
                                                        } catch (StartWriterVisitorException e1) {
408
                                                                // TODO Auto-generated catch block
409
                                                                e1.printStackTrace();
410
                                                        } catch (StopWriterVisitorException e1) {
411
                                                                e1.printStackTrace();
412
                                                        } catch (InitializeWriterException e1) {
413
                                                                e1.printStackTrace();
414
                                                        } catch (ProcessWriterVisitorException e1) {
415
                                                                // TODO Auto-generated catch block
416
                                                                e1.printStackTrace();
417
                                                        }
418
                                                }
419
                                        }
420
                                }
421
                                else{
422
                                        JOptionPane.showMessageDialog(this, ps.getText("no_events_network"), ps.getText("warning_message"), JOptionPane.INFORMATION_MESSAGE);
423
                                }
424
                        }
425
                        else if(source==this.buttonSolutionInstructions){                
426
                                if(this.tableSolution.getRowCount()>0 && this.tableSolution.getSelectedRowCount()>0){
427
                                        try {
428
                                                Route route=this.cfc.getSolvedRoute(this.tableSolution.getSelectedRow());
429
                                                if(route!=null){
430
                                                        this.cfc.showRouteReport(route);
431
                                                }
432
                                        } catch (GraphException except) {
433
                                                JOptionPane.showMessageDialog(this, except.getMessage(), ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
434
                                        }
435
                                }
436
                                else{
437
                                        JOptionPane.showMessageDialog(this, ps.getText("select_route_show_instructions"), ps.getText("closest_facility_instructions"), JOptionPane.WARNING_MESSAGE        );
438
                                }
439
                        }
440
                        else if(source==this.buttonSolutionDrawRoute){
441
                                if(this.tableSolution.getRowCount()>0 && this.tableSolution.getSelectedRowCount()>0){
442
                                        try {
443
                                                Route route=this.cfc.getSolvedRoute(this.tableSolution.getSelectedRow());
444
                                                if(route!=null){
445
                                                        GvFlag facility=this.cfc.getSolvedFacility(this.tableSolution.getSelectedRow());
446
                                                        
447
                                                        this.cfc.drawRouteOnGraphics(route);
448
                                                        if(this.cfc.getSolvedEvent()!=null) this.cfc.flashFlag(this.cfc.getSolvedEvent(), Color.RED, 5);
449
                                                        if(facility!=null) this.cfc.flashFlag(facility, Color.BLUE, 5);
450
                                                }
451
                                        } catch (GraphException except) {
452
                                                JOptionPane.showMessageDialog(this, except.getMessage(), ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
453
                                        }
454
                                }
455
                                else{
456
                                        JOptionPane.showMessageDialog(this, ps.getText("select_route_to_draw"), ps.getText("select_route_show_instructions"), JOptionPane.WARNING_MESSAGE        );
457
                                }
458
                        }
459
                        else if(source==this.buttonSolutionZoomRoute){
460
                                if(this.tableSolution.getRowCount()>0 && this.tableSolution.getSelectedRowCount()>0){
461
                                        try {
462
                                                Route route=this.cfc.getSolvedRoute(this.tableSolution.getSelectedRow());
463
                                                if(route!=null){
464
                                                        GvFlag facility=this.cfc.getSolvedFacility(this.tableSolution.getSelectedRow());
465
                                                        GvFlag[] flags={this.cfc.getSolvedEvent(), facility};
466
                                                        
467
                                                        this.cfc.drawRouteOnGraphics(route);
468
                                                        this.cfc.centerGraphicsOnFlags(flags);
469
                                                        if(this.cfc.getSolvedEvent()!=null) this.cfc.flashFlag(this.cfc.getSolvedEvent(), Color.RED, 5);
470
                                                        if(facility!=null) this.cfc.flashFlag(facility, Color.BLUE, 5);
471
                                                }
472
                                        } catch (GraphException except) {
473
                                                JOptionPane.showMessageDialog(this, except.getMessage(), ps.getText("warning_message"), JOptionPane.ERROR_MESSAGE);
474
                                        }
475
                                }
476
                                else{
477
                                        JOptionPane.showMessageDialog(this, ps.getText("select_route_to_zoom"), ps.getText("warning_message"), JOptionPane.WARNING_MESSAGE        );
478
                                }
479
                        }
480
                        else if(source==this.buttonSolutionProperties){
481
                                ClosestFacilityProperties cfp=new ClosestFacilityProperties(this.properties);
482
                                PluginServices.getMDIManager().addWindow(cfp);
483
                        }
484
                        else if(source==this.buttonSolutionSolve){
485
                                FLyrVect layerFacilities=this.getSelectedFacilitiesLayer();
486
                                double costFacilitiesLimit=-1;
487
                                try{
488
                                        costFacilitiesLimit=this.getFacilitiesMaxLimit();
489
                                }
490
                                catch(NumberFormatException except){
491
                                        costFacilitiesLimit=-1;
492
                                }
493

    
494
                                GvFlag sourceFlag=this.getSelectedEvent();
495
                                
496
                                if(layerFacilities!=null){
497
                                        if(this.cfc.getMaxFacilitiesNumber()>0){
498
                                                if(sourceFlag!=null){                                                                                                                                
499
                                                        try {
500
                                                                solve(sourceFlag, layerFacilities, this.isFromEventSelected(), this.onlySelectedFacilities(), costFacilitiesLimit);
501
                                                        }
502
                                                        catch (ReadDriverException except) {
503
                                                                JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
504
                                                        }
505
                                                        catch (GraphException except) {
506
                                                                JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
507
                                                        }
508
                                                }
509
                                                else{
510
                                                        JOptionPane.showMessageDialog(this, ps.getText("no_event_selected"), ps.getText("warning_message"), JOptionPane.ERROR_MESSAGE);
511
                                                }
512
                                        }
513
                                        else{
514
                                                JOptionPane.showMessageDialog(this, ps.getText("max_facilities_number_higher_zero"), ps.getText("warning_message"), JOptionPane.ERROR_MESSAGE);
515
                                        }
516
                                }
517
                                else{
518
                                        JOptionPane.showMessageDialog(this, ps.getText("no_facilities_layer_selected"), ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
519
                                }
520
                        }
521
                }
522
                else if(source==this.comboFacilities){
523
                        Thread thread=new Thread(this);
524
                        thread.start();
525
                }
526
                else if(source==this.comboFacilitiesNumber){
527
                        try{                
528
                                if(this.comboFacilitiesNumber.getSelectedIndex()!=-1)
529
                                        this.setMaxFacilitiesNumber(((Integer)this.comboFacilitiesNumber.getSelectedItem()).intValue());
530
                        }
531
                        catch(ClassCastException except){
532
                                
533
                        }
534
                }
535
                else if(source==this.checkFacilitiesSelection){
536
                        Thread thread=new Thread(this);
537
                        thread.start();
538
                }
539
        }
540
        
541
        public void run(){
542
                Color foregroundButtonSolve=this.buttonSolutionSolve.getForeground();
543
                String tooltipButtonSolve=this.buttonSolutionSolve.getToolTipText();
544
                try{
545
                        this.buttonSolutionSolve.setToolTipText(ps.getText("loading_facilities"));
546
                        this.buttonSolutionSolve.setEnabled(false);
547
                        this.buttonSolutionSolve.setForeground(Color.gray);
548
                        FLyrVect selectedFacilitiesLayer=this.getSelectedFacilitiesLayer();
549
                        this.properties.put("LINES_LAYER_NAME", selectedFacilitiesLayer.getName());
550
                        String[] fieldNames=selectedFacilitiesLayer.getRecordset().getFieldNames();
551
                        ArrayList fieldNamesArray=new ArrayList();
552
                        for (int i = 0; i < fieldNames.length; i++) {
553
                                fieldNamesArray.add(fieldNames[i]);
554
                        }
555
                        this.properties.put("LAYER_FIELDS", fieldNamesArray);
556
                        
557
                        if(selectedFacilitiesLayer!=null){
558
                                this.loadFacilitiesFromLayer(selectedFacilitiesLayer);
559
                        }
560
                        
561
                        this.comboFacilitiesNumber.setSelectedIndex(this.comboFacilitiesNumber.getItemCount()-1);
562
                }
563
                catch(ReadDriverException except){
564
                }
565
                catch (GraphException except) {
566
                }
567
                finally{
568
                        this.buttonSolutionSolve.setEnabled(true);
569
                        this.buttonSolutionSolve.setForeground(foregroundButtonSolve);
570
                        this.buttonSolutionSolve.setToolTipText(tooltipButtonSolve);
571
                }
572
        }
573
        
574
        private void loadFacilitiesFromLayer(FLyrVect layerFacilities) throws ReadDriverException, GraphException{
575
                this.cfc.removeAllFacilities();
576
                int rejectedFacilities=this.cfc.loadFacilitiesFromLayer(layerFacilities, this.checkFacilitiesSelection.isSelected());
577
                //this.cfc.flashFacilitiesOnGraphics();
578
                if(rejectedFacilities>0)
579
                        if(rejectedFacilities==1)
580
                                JOptionPane.showMessageDialog(this, rejectedFacilities+" "+ps.getText("rejected_facility_out_of_network"), ps.getText("warning_message"), JOptionPane.WARNING_MESSAGE);
581
                        else
582
                                JOptionPane.showMessageDialog(this, rejectedFacilities+" "+ps.getText("rejected_facilities_out_of_network"), ps.getText("warning_message"), JOptionPane.WARNING_MESSAGE);
583
                int maxFacilities=this.cfc.getFacilitiesCount();
584
                this.fillMaxFacilitiesCombo(maxFacilities);
585
                this.setMaxFacilitiesNumber(maxFacilities);
586
        }
587
        
588
        //Metodos interfaz MouseListener
589
        public void mouseClicked(MouseEvent e) {
590
                if(e.getSource().equals(this.tableEvents)){
591
                        if(this.tableEvents.getSelectedRow()>-1){
592
                                if(this.tableEvents.getRowCount()>0){
593
                                        if(this.selectedEvent!=null){
594
                                                this.cfc.removeEventFromGraphics(this.selectedEvent);
595
                                        }
596
                                        this.selectedEvent=this.cfc.getEvent(((Integer)this.tableEvents.getValueAt(this.tableEvents.getSelectedRow(), 0)).intValue());
597
                                        if(this.selectedEvent!=null){
598
                                                this.cfc.addEventToGraphics(this.selectedEvent);
599
                                                //this.cfc.centerGraphicsOnFlag(this.selectedEvent);
600
                                                this.cfc.flashFlag(this.selectedEvent, Color.RED, 5);
601
                                        }
602
                                }
603
                        }
604
                        else{
605
                                if(this.selectedEvent!=null){
606
                                        this.cfc.removeEventFromGraphics(this.selectedEvent);
607
                                }
608
                        }
609
                }
610
                else if(e.getSource().equals(this.tableSolution)){
611
                        if(this.tableSolution.getSelectedRow()>-1){
612
                                if(this.tableEvents.getRowCount()>0){
613
                                        GvFlag facility=this.cfc.getSolvedFacility(this.tableSolution.getSelectedRow());
614
                                        if(facility!=null){
615
                                                this.cfc.clearFlashes();
616
                                                this.cfc.flashFlag(facility, Color.BLUE, 5);
617
                                        }
618
                                }
619
                        }
620
                }
621
        }
622

    
623
        public void mouseEntered(MouseEvent e) {
624

    
625
        }
626

    
627
        public void mouseExited(MouseEvent e) {
628

    
629
        }
630

    
631
        public void mousePressed(MouseEvent e) {
632

    
633
        }
634

    
635
        public void mouseReleased(MouseEvent e) {
636

    
637
        }
638
        //Fin metodos interfaz MouseListener
639

    
640
        private FLyrVect getSelectedFacilitiesLayer() {
641
                String layerName=String.valueOf(this.comboFacilities.getSelectedItem());
642
                FLyrVect lyrVect=null;
643
                if(layerName!=null || layerName.equalsIgnoreCase("null")){
644
                        IWindow wi=PluginServices.getMDIManager().getActiveWindow();
645
                        if(wi instanceof View){
646
                                FLayer layer=((View)wi).getMapControl().getMapContext().getLayers().getLayer(layerName);
647
                                if(layer instanceof FLyrVect){
648
                                        lyrVect=(FLyrVect)layer;
649
                                }
650
                        }
651
                }
652
                
653
                return lyrVect;
654
        }
655

    
656
        public void addedSolvedFacility(GvFlag solvedFacility) {
657
                //this.tableSolution.revalidate();
658
                this.tableSolution.updateUI();
659
        }
660

    
661
        public void allEventsRemoved() {
662
                this.tableEvents.revalidate();
663
        }
664

    
665
        public void allFacilitiesRemoved() {
666
                
667
        }
668

    
669
        public void eventAdded(GvFlag event) {
670
                this.tableEvents.revalidate();
671
        }
672

    
673
        public void eventModified(GvFlag oldEvent, GvFlag modifiedEvent) {
674
                this.tableEvents.revalidate();
675
        }
676

    
677
        public void eventRemoved(GvFlag event) {
678
                this.tableEvents.revalidate();
679
        }
680

    
681
        public void facilityAdded(GvFlag facility) {
682
                
683
        }
684

    
685
        public void facilityModified(GvFlag oldFacility, GvFlag modifiedFacility) {
686
                
687
        }
688

    
689
        public void facilityRemoved(GvFlag facility) {
690
                
691
        }
692

    
693
        public void removedSolvedFacilities() {
694
                //this.tableSolution.revalidate();
695
                this.tableSolution.updateUI();
696
        }
697
        
698
        private void solve(GvFlag sourceFlag, FLyrVect layerFacilities, boolean fromEvent, boolean onlySelectedFacilities, double costFacilitiesLimit) throws ReadDriverException, GraphException {
699
                Network network=this.cfc.getNetwork();
700

    
701
                this.cfc.setOnlySelectedFacilities(this.onlySelectedFacilities());
702
                this.cfc.setLayerFacilities(layerFacilities);
703
                this.cfc.setSourceEvent(sourceFlag);
704
                if(fromEvent) this.cfc.setFromEvent();
705
                else this.cfc.setToEvent();
706
                this.cfc.setFacilitiesMaxLimit(costFacilitiesLimit);
707
                this.cfc.solve();
708
        }
709
        
710
        class EventsDataModel extends AbstractTableModel{
711
                
712
                /**
713
                 * 
714
                 */
715
                private static final long serialVersionUID = -1064456858672232579L;
716
                
717
                private PluginServices ps=PluginServices.getPluginServices(this);
718
                
719
                private ClosestFacilityController cfc;
720
                private JTable eventsTable;
721

    
722
                public EventsDataModel(ClosestFacilityController cfc, JTable table){
723
                        this.cfc=cfc;
724
                        this.eventsTable=table;
725
                        this.eventsTable.setModel(this);
726
                        
727
                        this.eventsTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
728
                        TableColumnModel cm = this.eventsTable.getColumnModel();
729

    
730
                        int tablePreferredWidth = (int) this.eventsTable.getPreferredSize()
731
                                        .getWidth();
732
                        int colSize = this.eventsTable.getFontMetrics(this.eventsTable.getFont())
733
                                        .stringWidth(this.eventsTable.getModel().getColumnName(0)) * 4;                        
734
                        cm.getColumn(0).setPreferredWidth((int) (colSize));
735
                        cm.getColumn(0).setMinWidth((int) (colSize));
736
                        cm.getColumn(0).setMaxWidth((int) (colSize));
737
                        
738
                        tablePreferredWidth -= colSize;
739
                        //cm.getColumn(1).setPreferredWidth((int) (tablePreferredWidth * 0.8));
740
                }
741

    
742
                public int getColumnCount() {
743
                        return 2;
744
                }
745

    
746
                public int getRowCount() {
747
                        return this.cfc.getEventCount();
748
                }
749
                
750
                public Class getColumnClass(int columnIndex) {
751
                        switch (columnIndex){
752
                                case 0:
753
                                        return Integer.class;
754
                                case 1:
755
                                        return String.class;
756
                                default:
757
                                        return String.class;
758
                        }
759
                }
760

    
761
                public Object getValueAt(int rowIndex, int columnIndex) {
762
                        try{
763
                                //GvFlag flag = this.cfc.getEvent(((Integer)this.eventsTable.getValueAt(this.eventsTable.getSelectedRow(), 0)).intValue());
764
                                //System.out.println("Events:getValueAt:rowIndex="+rowIndex+", colIndex="+columnIndex+", idFlag="+flag.getIdFlag());
765
                                switch(columnIndex){
766
                                        case 0:
767
                                                return Integer.valueOf(this.cfc.getEventByIndex(rowIndex).getIdFlag());
768
                                        case 1:
769
                                                return this.cfc.getEventByIndex(rowIndex).getDescription();
770
                                        default:
771
                                                return null;
772
                                }
773
                        }
774
                        catch(IndexOutOfBoundsException except){
775
                                return null;
776
                        }
777
                }
778

    
779
                public String getColumnName(int column) {
780
                        switch(column){
781
                                case 0:
782
                                        return "Id";
783
                                case 1:
784
                                        return ps.getText("table_events_column_description");
785
                                default:
786
                                        return String.valueOf((char)(65+column)); //ASCII
787
                        }
788
                }
789

    
790
                public boolean isCellEditable(int rowIndex, int columnIndex) {
791
                        switch(columnIndex){
792
                                case 0:
793
                                        return false;
794
                                case 1:
795
                                        return true;
796
                                default:
797
                                        return false;
798
                        }
799
                }
800

    
801
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
802
                        switch(columnIndex){
803
                        case 0:
804
                                break;
805
                        
806
                        case 1:
807
                                this.cfc.getEvent(Integer.parseInt(String.valueOf(this.getValueAt(rowIndex, 0)))).setDescription(String.valueOf(aValue));
808
                                break;
809
                        }
810
                }
811
        }
812
        
813
        class FacilitiesDataModel extends AbstractTableModel{
814
                
815
                /**
816
                 * 
817
                 */
818
                private static final long serialVersionUID = -6329862517799391098L;
819
                
820
                private PluginServices ps=PluginServices.getPluginServices(this);
821
                
822
                private ClosestFacilityController cfc;
823
                
824
                private JTable solutionTable;
825

    
826
                public FacilitiesDataModel(ClosestFacilityController cfc, JTable table){
827
                        this.cfc=cfc;
828
                        this.solutionTable=table;
829
                        this.solutionTable.setModel(this);
830
                        
831
                        this.solutionTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
832
                        TableColumnModel cm = this.solutionTable.getColumnModel();
833

    
834
                        int tablePreferredWidth = (int) this.solutionTable.getPreferredSize()
835
                                        .getWidth();
836
                        int colSize = Double.valueOf(this.solutionTable.getFontMetrics(this.solutionTable.getFont()).stringWidth(this.solutionTable.getModel().getColumnName(0)) * 1.5).intValue();
837
                        cm.getColumn(0).setPreferredWidth((int) (colSize));
838
                        cm.getColumn(0).setMinWidth((int) (colSize));
839
                        cm.getColumn(0).setMaxWidth((int) (colSize));
840
                        tablePreferredWidth -= colSize;
841
                        cm.getColumn(1)
842
                                        .setPreferredWidth((int) (tablePreferredWidth * 0.9));
843
                        cm.getColumn(2)
844
                                        .setPreferredWidth((int) (tablePreferredWidth * 0.1));
845
                }
846

    
847
                public int getColumnCount() {
848
                        return 3;
849
                }
850

    
851
                public int getRowCount() {
852
                        return this.cfc.getSolvedFacilitiesCount();
853
                }
854

    
855
                public Class getColumnClass(int columnIndex) {
856
                        switch (columnIndex){
857
                                case 0:
858
                                        return Integer.class;
859
                                case 1:
860
                                        return String.class;
861
                                case 2:
862
                                        return Double.class;
863
                                default:
864
                                        return String.class;
865
                        }
866
                }
867

    
868
                public Object getValueAt(int rowIndex, int columnIndex) {
869
                        // TODO Auto-generated method stub
870
                        try{                                
871
                                switch(columnIndex){
872
                                        case 0:
873
                                                //GvFlag flag1 = this.cfc.getSolvedFacility(rowIndex);
874
                                                //return Integer.valueOf(flag1.getIdFlag());
875
                                                return rowIndex+1;
876
                                        case 1:
877
                                                GvFlag flag2 = this.cfc.getSolvedFacility(rowIndex);
878
                                                return flag2.getDescription();
879
                                        case 2:
880
                                                //Route route=this.cfc.getSolvedRoute(rowIndex);
881
                                                GvFlag flag=this.cfc.getSolvedFacility(rowIndex);
882
                                                return Double.valueOf(flag.getCost());
883
                                        default:
884
                                                return null;
885
                                }
886
                        }
887
                        catch(IndexOutOfBoundsException except){
888
                                return null;
889
                        }
890
                }
891

    
892
                public String getColumnName(int column) {
893
                        // TODO Auto-generated method stub
894
                        switch(column){
895
                                case 0:
896
                                        return ps.getText("table_solution_column_facilities_position");
897
                                case 1:
898
                                        return ps.getText("table_solution_column_description");
899
                                case 2:
900
                                        return ps.getText("table_solution_column_cost");
901
                                default:
902
                                        return String.valueOf((char)(65+column)); //ASCII
903
                        }
904
                }
905

    
906
                @Override
907
                public boolean isCellEditable(int rowIndex, int columnIndex) {
908
                        return false;
909
                }
910

    
911
                @Override
912
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
913
                        //Ninguna celda es editable                        
914
                }
915
        }
916
        
917
}