Statistics
| Revision:

root / trunk / extensions / extGraph / src / org / gvsig / graph / gui / solvers / ClosestFacilityDialog.java @ 24989

History | View | Annotate | Download (31.8 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);
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
                                                this.cfc.loadEventsFromFile(fileChooser.getSelectedFile());
352
                                        }
353
                                }
354
                                catch(ReadDriverException except){
355
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
356
                                }
357
                                catch (FileNotFoundException except) {
358
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
359
                                }
360
                                catch (NullPointerException except) {
361
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
362
                                }
363
                                catch (GraphException except) {
364
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
365
                                }
366
                                catch (DriverLoadException except) {
367
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
368
                                }
369
                                catch (NoSuchTableException except) {
370
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
371
                                }
372
                                catch(Exception except){
373
                                        JOptionPane.showMessageDialog(this, except.getMessage() , ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
374
                                }
375
                        }
376
                        else if(source==this.buttonEventsRemove){
377
                                if(this.tableEvents.getSelectedRowCount()>0 && this.tableEvents.getRowCount()>0){
378
                                        GvFlag flag=this.getSelectedEvent();
379
                                        if(flag!=null){
380
                                                this.cfc.removeEvent(flag);
381
                                        }
382
                                }
383
                        }
384
                        else if(source==this.buttonEventsSave){
385
                                if(this.tableEvents.getRowCount()>0){
386
                                        String path="";
387
                                        Preferences prefs = Preferences.userRoot().node("gvsig.foldering");
388
                                        path = prefs.get("DataFolder", null);
389
                                        JFileChooser fileChooser=new JFileChooser(path);
390
                                        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
391
                                        fileChooser.addChoosableFileFilter(new GenericFileFilter("shp", ps.getText("shp_files")));                                
392
                                        if(fileChooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){
393
                                                File shpFile=fileChooser.getSelectedFile();
394
                                                if(shpFile!=null){
395
                                                        try{
396
                                                                if(shpFile.exists()){
397
                                                                        if(JOptionPane.showConfirmDialog(this, ps.getText("overwrite_selected_file_confirmation"), ps.getText("confirmation_overwrite"), JOptionPane.INFORMATION_MESSAGE)==JOptionPane.YES_OPTION){
398
                                                                                this.cfc.createSHPEvents(shpFile);
399
                                                                        }
400
                                                                }
401
                                                                else{
402
                                                                        this.cfc.createSHPEvents(shpFile);
403
                                                                }
404
                                                        } catch (StartWriterVisitorException e1) {
405
                                                                // TODO Auto-generated catch block
406
                                                                e1.printStackTrace();
407
                                                        } catch (StopWriterVisitorException e1) {
408
                                                                e1.printStackTrace();
409
                                                        } catch (InitializeWriterException e1) {
410
                                                                e1.printStackTrace();
411
                                                        } catch (ProcessWriterVisitorException e1) {
412
                                                                // TODO Auto-generated catch block
413
                                                                e1.printStackTrace();
414
                                                        }
415
                                                }
416
                                        }
417
                                }
418
                                else{
419
                                        JOptionPane.showMessageDialog(this, ps.getText("no_events_network"), ps.getText("warning_message"), JOptionPane.INFORMATION_MESSAGE);
420
                                }
421
                        }
422
                        else if(source==this.buttonSolutionInstructions){                
423
                                if(this.tableSolution.getRowCount()>0 && this.tableSolution.getSelectedRowCount()>0){
424
                                        try {
425
                                                Route route=this.cfc.getSolvedRoute(this.tableSolution.getSelectedRow());
426
                                                if(route!=null){
427
                                                        this.cfc.showRouteReport(route);
428
                                                }
429
                                        } catch (GraphException except) {
430
                                                JOptionPane.showMessageDialog(this, except.getMessage(), ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
431
                                        }
432
                                }
433
                                else{
434
                                        JOptionPane.showMessageDialog(this, ps.getText("select_route_show_instructions"), ps.getText("closest_facility_instructions"), JOptionPane.WARNING_MESSAGE        );
435
                                }
436
                        }
437
                        else if(source==this.buttonSolutionDrawRoute){
438
                                if(this.tableSolution.getRowCount()>0 && this.tableSolution.getSelectedRowCount()>0){
439
                                        try {
440
                                                Route route=this.cfc.getSolvedRoute(this.tableSolution.getSelectedRow());
441
                                                if(route!=null){
442
                                                        GvFlag facility=this.cfc.getSolvedFacility(this.tableSolution.getSelectedRow());
443
                                                        
444
                                                        this.cfc.drawRouteOnGraphics(route);
445
                                                        if(this.cfc.getSolvedEvent()!=null) this.cfc.flashFlag(this.cfc.getSolvedEvent(), Color.RED, 5);
446
                                                        if(facility!=null) this.cfc.flashFlag(facility, Color.BLUE, 5);
447
                                                }
448
                                        } catch (GraphException except) {
449
                                                JOptionPane.showMessageDialog(this, except.getMessage(), ps.getText("error_message"), JOptionPane.ERROR_MESSAGE);
450
                                        }
451
                                }
452
                                else{
453
                                        JOptionPane.showMessageDialog(this, ps.getText("select_route_to_draw"), ps.getText("select_route_show_instructions"), JOptionPane.WARNING_MESSAGE        );
454
                                }
455
                        }
456
                        else if(source==this.buttonSolutionZoomRoute){
457
                                if(this.tableSolution.getRowCount()>0 && this.tableSolution.getSelectedRowCount()>0){
458
                                        try {
459
                                                Route route=this.cfc.getSolvedRoute(this.tableSolution.getSelectedRow());
460
                                                if(route!=null){
461
                                                        GvFlag facility=this.cfc.getSolvedFacility(this.tableSolution.getSelectedRow());
462
                                                        GvFlag[] flags={this.cfc.getSolvedEvent(), facility};
463
                                                        
464
                                                        this.cfc.drawRouteOnGraphics(route);
465
                                                        this.cfc.centerGraphicsOnFlags(flags);
466
                                                        if(this.cfc.getSolvedEvent()!=null) this.cfc.flashFlag(this.cfc.getSolvedEvent(), Color.RED, 5);
467
                                                        if(facility!=null) this.cfc.flashFlag(facility, Color.BLUE, 5);
468
                                                }
469
                                        } catch (GraphException except) {
470
                                                JOptionPane.showMessageDialog(this, except.getMessage(), ps.getText("warning_message"), JOptionPane.ERROR_MESSAGE);
471
                                        }
472
                                }
473
                                else{
474
                                        JOptionPane.showMessageDialog(this, ps.getText("select_route_to_zoom"), ps.getText("warning_message"), JOptionPane.WARNING_MESSAGE        );
475
                                }
476
                        }
477
                        else if(source==this.buttonSolutionProperties){
478
                                ClosestFacilityProperties cfp=new ClosestFacilityProperties(this.properties);
479
                                PluginServices.getMDIManager().addWindow(cfp);
480
                        }
481
                        else if(source==this.buttonSolutionSolve){
482
                                FLyrVect layerFacilities=this.getSelectedFacilitiesLayer();
483
                                double costFacilitiesLimit=-1;
484
                                try{
485
                                        costFacilitiesLimit=this.getFacilitiesMaxLimit();
486
                                }
487
                                catch(NumberFormatException except){
488
                                        costFacilitiesLimit=-1;
489
                                }
490

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

    
620
        public void mouseEntered(MouseEvent e) {
621

    
622
        }
623

    
624
        public void mouseExited(MouseEvent e) {
625

    
626
        }
627

    
628
        public void mousePressed(MouseEvent e) {
629

    
630
        }
631

    
632
        public void mouseReleased(MouseEvent e) {
633

    
634
        }
635
        //Fin metodos interfaz MouseListener
636

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

    
653
        public void addedSolvedFacility(GvFlag solvedFacility) {
654
                //this.tableSolution.revalidate();
655
                this.tableSolution.updateUI();
656
        }
657

    
658
        public void allEventsRemoved() {
659
                this.tableEvents.revalidate();
660
        }
661

    
662
        public void allFacilitiesRemoved() {
663
                
664
        }
665

    
666
        public void eventAdded(GvFlag event) {
667
                this.tableEvents.revalidate();
668
        }
669

    
670
        public void eventModified(GvFlag oldEvent, GvFlag modifiedEvent) {
671
                this.tableEvents.revalidate();
672
        }
673

    
674
        public void eventRemoved(GvFlag event) {
675
                this.tableEvents.revalidate();
676
        }
677

    
678
        public void facilityAdded(GvFlag facility) {
679
                
680
        }
681

    
682
        public void facilityModified(GvFlag oldFacility, GvFlag modifiedFacility) {
683
                
684
        }
685

    
686
        public void facilityRemoved(GvFlag facility) {
687
                
688
        }
689

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

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

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

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

    
739
                public int getColumnCount() {
740
                        return 2;
741
                }
742

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

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

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

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

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

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

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

    
844
                public int getColumnCount() {
845
                        return 3;
846
                }
847

    
848
                public int getRowCount() {
849
                        return this.cfc.getSolvedFacilitiesCount();
850
                }
851

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

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

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

    
903
                @Override
904
                public boolean isCellEditable(int rowIndex, int columnIndex) {
905
                        return false;
906
                }
907

    
908
                @Override
909
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
910
                        //Ninguna celda es editable                        
911
                }
912
        }
913
}