Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / SaveTurnCostsExtension.java @ 39203

History | View | Annotate | Download (8.9 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.graph;
42

    
43
import java.awt.Component;
44
import java.io.File;
45
import java.nio.charset.Charset;
46
import java.sql.Types;
47

    
48
import javax.swing.JComponent;
49
import javax.swing.JOptionPane;
50
import javax.swing.filechooser.FileFilter;
51

    
52
import org.gvsig.graph.core.GvTurn;
53
import org.gvsig.graph.core.Network;
54
import org.gvsig.graph.gui.RouteControlPanel;
55
import org.gvsig.gui.beans.swing.JFileChooser;
56

    
57
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException;
58
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
59
import com.hardcode.gdbms.engine.values.Value;
60
import com.hardcode.gdbms.engine.values.ValueFactory;
61
import com.iver.andami.PluginServices;
62
import com.iver.andami.plugins.Extension;
63
import com.iver.andami.ui.mdiManager.IWindow;
64
import com.iver.cit.gvsig.exceptions.visitors.ProcessWriterVisitorException;
65
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
66
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException;
67
import com.iver.cit.gvsig.fmap.MapContext;
68
import com.iver.cit.gvsig.fmap.MapControl;
69
import com.iver.cit.gvsig.fmap.core.DefaultRow;
70
import com.iver.cit.gvsig.fmap.core.FShape;
71
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
72
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
73
import com.iver.cit.gvsig.fmap.drivers.TableDefinition;
74
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
75
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
76
import com.iver.cit.gvsig.fmap.edition.writers.dbf.DbfWriter;
77
import com.iver.cit.gvsig.fmap.layers.FLayer;
78
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
79
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
80
import com.iver.cit.gvsig.project.documents.view.IProjectView;
81
import com.iver.cit.gvsig.project.documents.view.gui.IView;
82
import com.iver.cit.gvsig.project.documents.view.gui.View;
83

    
84
public class SaveTurnCostsExtension extends Extension {
85
        /**
86
         * Component to control flags and routes
87
         * */
88
        private RouteControlPanel controlPanel;
89
        
90
//        private String fieldType;
91
//        private String fieldDist;
92
//        private String fieldSense;
93

    
94
        public void initialize() {
95

    
96
        }
97

    
98
        public void execute(String actionCommand) {
99
                IView view = (View) PluginServices.getMDIManager().getActiveWindow();
100
                MapControl mapControl = view.getMapControl();
101
                MapContext map = mapControl.getMapContext();
102
                SingleLayerIterator lyrIterator = new SingleLayerIterator(map
103
                                .getLayers());
104
                while (lyrIterator.hasNext()) {
105
                        FLayer lyr = lyrIterator.next();
106
                        if ((lyr.isActive()) && (lyr instanceof FLyrVect))
107
                        {
108
                                FLyrVect lyrVect = (FLyrVect) lyr;
109
                                Network net = (Network) lyr.getProperty("network");
110

    
111
                                if ( net != null)
112
                                {
113
                                        try {
114
                                                save_turncosts(lyrVect, mapControl);
115
                                        } catch (ReadDriverException e) {
116
                                                // TODO Auto-generated catch block
117
                                                e.printStackTrace();
118
                                        }
119
                                }
120
                        }
121
                }
122

    
123

    
124
        }
125

    
126
        private void save_turncosts(FLyrVect lyrVect, MapControl mapControl) throws ReadDriverException {
127
                Network net = (Network) lyrVect.getProperty("network");
128

    
129
                if ( net != null)
130
                {
131
                        String curDir = System.getProperty("user.dir");
132

    
133
                        JFileChooser fileChooser = new JFileChooser("dbf files", new File(curDir));
134
                        fileChooser.setFileFilter(new FileFilter() {
135

    
136
                                @Override
137
                                public boolean accept(File f) {
138
                                        if (f.isDirectory())
139
                                                return true;
140
                                        String path = f.getPath().toLowerCase();
141
                                        if (path.endsWith(".dbf"))
142
                                                return true;
143
                                        return false;
144
                                }
145

    
146
                                @Override
147
                                public String getDescription() {
148
                                        return ".dbf files";
149
                                }
150
                                
151
                        });
152
                        int res = fileChooser.showSaveDialog((Component) PluginServices.getMainFrame());
153
                        if (res==JFileChooser.APPROVE_OPTION) {
154
                                File dbfFile =fileChooser.getSelectedFile();
155
                                if (!dbfFile.getPath().toLowerCase().endsWith(".dbf"))
156
                                        dbfFile = new File(dbfFile.getPath() + ".dbf");
157

    
158
                                FieldDescription[] fields = new FieldDescription[3];
159
                                FieldDescription fieldFromId = new FieldDescription();
160
                                fieldFromId.setFieldName("from");
161
                                fieldFromId.setFieldType(Types.INTEGER);
162

    
163
                                FieldDescription fieldToId = new FieldDescription();
164
                                fieldToId.setFieldName("to");
165
                                fieldToId.setFieldType(Types.INTEGER);
166

    
167
                                FieldDescription fieldTurnCost = new FieldDescription();
168
                                fieldTurnCost.setFieldName("turncost");
169
                                fieldTurnCost.setFieldType(Types.DOUBLE);
170
                                fieldTurnCost.setFieldDecimalCount(2);
171
                                
172
                                fields[0] = fieldFromId;
173
                                fields[1] = fieldToId;
174
                                fields[2] = fieldTurnCost;
175
                                                                
176
                                DbfWriter dbfWriter = new DbfWriter();
177
                                dbfWriter.setFile(dbfFile);
178
                                
179
                                // We create a table definition for turncosts table.
180
                                ITableDefinition tableDef = new TableDefinition();
181
                                tableDef.setFieldsDesc(fields);
182
                                tableDef.setName("turcosts");
183

    
184

    
185
                                try {
186
                                        dbfWriter.initialize(tableDef);
187
                                        dbfWriter.setCharset(Charset.defaultCharset());
188
                                        
189
                                        dbfWriter.preProcess();
190
                                        
191
                                        for (int i=0; i < net.getTurnCosts().size(); i++) {
192
                                                GvTurn turn =  net.getTurnCosts().get(i);
193
                                                
194
                                                Value[] values = new Value[fields.length];
195
                                                values[0] = ValueFactory.createValue(turn.getIdArcFrom());
196
                                                values[1] = ValueFactory.createValue(turn.getIdArcTo());
197
                                                values[2] = ValueFactory.createValue(turn.getCost());
198
                                                DefaultRow myRow = new DefaultRow(values, i + "");
199
                                                IRowEdited editedRow = new DefaultRowEdited(myRow,
200
                                                                DefaultRowEdited.STATUS_ADDED, i);
201
        
202
                                                dbfWriter.process(editedRow);
203
        
204
                                        }
205
                                        dbfWriter.postProcess();
206
                                } catch (InitializeWriterException e) {
207
                                        // TODO Auto-generated catch block
208
                                        e.printStackTrace();
209
                                } catch (StartWriterVisitorException e) {
210
                                        // TODO Auto-generated catch block
211
                                        e.printStackTrace();
212
                                } catch (ProcessWriterVisitorException e) {
213
                                        // TODO Auto-generated catch block
214
                                        e.printStackTrace();
215
                                } catch (StopWriterVisitorException e) {
216
                                        // TODO Auto-generated catch block
217
                                        e.printStackTrace();
218
                                }
219
                                
220
                        }
221
                }
222
                else
223
                {
224
                        JOptionPane.showMessageDialog((JComponent) PluginServices.getMDIManager().getActiveWindow(),
225
                                                PluginServices.getText(this, "la_capa_no_tiene_red_asociada"));
226
                }
227
        }
228

    
229
        public boolean isEnabled() {
230
                IWindow f = PluginServices.getMDIManager()
231
                 .getActiveWindow();
232
                if (f == null) {
233
                    return false;
234
                }
235
                if (f instanceof View) {
236
                    View v = (View) f;
237
                        MapContext map = v.getMapControl().getMapContext();
238
                        SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
239
                        while (it.hasNext())
240
                        {
241
                                FLayer aux = it.next();
242
                                if (!aux.isAvailable())
243
                                        continue;
244

    
245
                                if (!aux.isActive())
246
                                        continue;
247
                                Network net = (Network) aux.getProperty("network");
248

    
249
                                if ( net != null)
250
                                {
251
                                        if (net.getTurnCosts().size() > 0)
252
                                                return true;
253
                                }
254
                        }
255
                        return false;
256

    
257
                }
258
                return false;
259

    
260
        }
261

    
262
        public boolean isVisible() {
263
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
264

    
265
                if (f == null) {
266
                        return false;
267
                }
268

    
269
                if (f instanceof View) {
270
                        View vista = (View) f;
271
                        IProjectView model = vista.getModel();
272
                        MapContext mapa = model.getMapContext();
273
                        FLayer[] activeLayers = mapa.getLayers().getActives();
274
                        if (activeLayers.length > 0)
275
                                if (activeLayers[0] instanceof FLyrVect){
276
                                        FLyrVect lyrVect = (FLyrVect) activeLayers[0];
277
                                        if (!lyrVect.isAvailable())
278
                                                return false;
279
                                        int shapeType ;
280
                                        try {
281
                                                shapeType = lyrVect.getShapeType();
282
//                                                if (shapeType == FShape.LINE)
283
                                                if ((shapeType & FShape.LINE) == FShape.LINE)
284
                                                        return true;
285
                                        } catch (ReadDriverException e) {
286
                                                // TODO Auto-generated catch block
287
                                                e.printStackTrace();
288
                                        }
289
                                }        
290
                }
291
                return false;
292

    
293
        }
294

    
295
}