svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / SaveAs.java @ 4937
History | View | Annotate | Download (9.85 KB)
1 | 4850 | fjp | package com.iver.cit.gvsig; |
---|---|---|---|
2 | |||
3 | import java.awt.Component; |
||
4 | import java.io.File; |
||
5 | import java.sql.Connection; |
||
6 | import java.sql.DriverManager; |
||
7 | import java.sql.SQLException; |
||
8 | |||
9 | import javax.swing.JFileChooser; |
||
10 | import javax.swing.JOptionPane; |
||
11 | |||
12 | import com.hardcode.driverManager.DriverLoadException; |
||
13 | 4877 | fjp | import com.hardcode.gdbms.engine.values.Value; |
14 | 4850 | fjp | import com.iver.andami.PluginServices; |
15 | import com.iver.andami.messages.NotificationManager; |
||
16 | import com.iver.andami.plugins.Extension; |
||
17 | import com.iver.cit.gvsig.fmap.DriverException; |
||
18 | import com.iver.cit.gvsig.fmap.FMap; |
||
19 | 4877 | fjp | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
20 | 4850 | fjp | import com.iver.cit.gvsig.fmap.core.IFeature; |
21 | 4877 | fjp | import com.iver.cit.gvsig.fmap.core.IGeometry; |
22 | 4850 | fjp | import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
23 | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
||
24 | import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
||
25 | import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
||
26 | import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGISWriter; |
||
27 | import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
||
28 | import com.iver.cit.gvsig.fmap.edition.EditionException; |
||
29 | import com.iver.cit.gvsig.fmap.edition.IWriter; |
||
30 | 4937 | fjp | import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfFieldsMapping; |
31 | import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfWriter; |
||
32 | 4850 | fjp | import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter; |
33 | import com.iver.cit.gvsig.fmap.layers.FBitSet; |
||
34 | import com.iver.cit.gvsig.fmap.layers.FLayer; |
||
35 | import com.iver.cit.gvsig.fmap.layers.FLayers; |
||
36 | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
||
37 | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
||
38 | import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
||
39 | import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
||
40 | import com.iver.cit.gvsig.gui.View; |
||
41 | import com.iver.cit.gvsig.jdbc_spatial.DlgConnection; |
||
42 | import com.iver.cit.gvsig.jdbc_spatial.gui.jdbcwizard.ConnectionSettings; |
||
43 | import com.iver.cit.gvsig.project.ProjectView; |
||
44 | import com.iver.utiles.SimpleFileFilter; |
||
45 | |||
46 | public class SaveAs implements Extension { |
||
47 | |||
48 | /**
|
||
49 | * @see com.iver.andami.plugins.Extension#inicializar()
|
||
50 | */
|
||
51 | public void inicializar() { |
||
52 | } |
||
53 | |||
54 | /**
|
||
55 | * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
|
||
56 | */
|
||
57 | public void execute(String actionCommand) { |
||
58 | com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager() |
||
59 | .getActiveView(); |
||
60 | |||
61 | if (f instanceof View) { |
||
62 | View vista = (View) f; |
||
63 | ProjectView model = vista.getModel(); |
||
64 | FMap mapa = model.getMapContext(); |
||
65 | FLayers layers = mapa.getLayers(); |
||
66 | FLayer[] actives = layers.getActives();
|
||
67 | try {
|
||
68 | // NOTA: SI HAY UNA SELECCI?N, SOLO SE SALVAN LOS SELECCIONADOS
|
||
69 | for (int i = 0; i < actives.length; i++) { |
||
70 | if (actives[i] instanceof FLyrVect) { |
||
71 | FLyrVect lv = (FLyrVect) actives[i]; |
||
72 | |||
73 | if (actionCommand.equals("SHP")) { |
||
74 | |||
75 | saveToShp(lv); |
||
76 | } |
||
77 | if (actionCommand.equals("DXF")) { |
||
78 | saveToDxf(lv); |
||
79 | } |
||
80 | if (actionCommand.equals("POSTGIS")) { |
||
81 | saveToPostGIS(lv); |
||
82 | } |
||
83 | } // actives[i]
|
||
84 | } // for
|
||
85 | } catch (EditionException e) {
|
||
86 | e.printStackTrace(); |
||
87 | NotificationManager.addError(e.getMessage(), e); |
||
88 | } |
||
89 | |||
90 | } |
||
91 | } |
||
92 | |||
93 | public void saveToPostGIS(FLyrVect layer) throws EditionException { |
||
94 | try {
|
||
95 | String tableName = JOptionPane.showInputDialog(PluginServices |
||
96 | .getText(this, "intro_tablename")); |
||
97 | if (tableName == null) |
||
98 | return;
|
||
99 | DlgConnection dlg = new DlgConnection();
|
||
100 | dlg.setModal(true);
|
||
101 | dlg.setVisible(true);
|
||
102 | ConnectionSettings cs = dlg.getConnSettings(); |
||
103 | if (cs == null) |
||
104 | return;
|
||
105 | Connection conex = DriverManager.getConnection(cs |
||
106 | .getConnectionString(), cs.getUser(), cs.getPassw()); |
||
107 | |||
108 | DBLayerDefinition dbLayerDef = new DBLayerDefinition();
|
||
109 | dbLayerDef.setCatalogName(cs.getDb()); |
||
110 | dbLayerDef.setTableName(tableName); |
||
111 | dbLayerDef.setShapeType(layer.getShapeType()); |
||
112 | 4937 | fjp | SelectableDataSource sds = layer.getRecordset(); |
113 | FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
||
114 | 4850 | fjp | dbLayerDef.setFieldsDesc(fieldsDescrip); |
115 | dbLayerDef.setFieldGeometry("the_geom");
|
||
116 | dbLayerDef.setFieldID("gid");
|
||
117 | |||
118 | dbLayerDef.setWhereClause("");
|
||
119 | String strSRID = layer.getProjection().getAbrev().substring(5); |
||
120 | dbLayerDef.setSRID_EPSG(strSRID); |
||
121 | dbLayerDef.setConnection(conex); |
||
122 | |||
123 | PostGISWriter writer = new PostGISWriter(); // (PostGISWriter)LayerFactory.getWM().getWriter("PostGIS |
||
124 | 4937 | fjp | // Writer");
|
125 | 4850 | fjp | writer.setWriteAll(true);
|
126 | writer.setCreateTable(true);
|
||
127 | writer.initialize(dbLayerDef); |
||
128 | writeFeatures(layer, writer); |
||
129 | |||
130 | } catch (DriverException e) {
|
||
131 | e.printStackTrace(); |
||
132 | throw new EditionException(e); |
||
133 | } catch (DriverLoadException e) {
|
||
134 | throw new EditionException(e); |
||
135 | } catch (SQLException e) { |
||
136 | throw new EditionException(e); |
||
137 | } catch (DriverIOException e) {
|
||
138 | throw new EditionException(e); |
||
139 | 4875 | fjp | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
140 | e.printStackTrace(); |
||
141 | throw new EditionException(e); |
||
142 | 4850 | fjp | } |
143 | |||
144 | } |
||
145 | |||
146 | /**
|
||
147 | 4937 | fjp | * @param layer
|
148 | * FLyrVect to obtain features. If selection, only selected
|
||
149 | * features will be precessed.
|
||
150 | * @param writer
|
||
151 | * (Must be already initialized)
|
||
152 | 4850 | fjp | * @throws EditionException
|
153 | * @throws DriverException
|
||
154 | * @throws DriverIOException
|
||
155 | 4937 | fjp | * @throws com.hardcode.gdbms.engine.data.driver.DriverException
|
156 | 4850 | fjp | */
|
157 | 4937 | fjp | public void writeFeatures(FLyrVect layer, IWriter writer) |
158 | throws EditionException, DriverException, DriverIOException,
|
||
159 | com.hardcode.gdbms.engine.data.driver.DriverException { |
||
160 | 4850 | fjp | ReadableVectorial va = layer.getSource(); |
161 | 4877 | fjp | SelectableDataSource sds = layer.getRecordset(); |
162 | 4937 | fjp | |
163 | 4850 | fjp | // Creamos la tabla.
|
164 | writer.preProcess(); |
||
165 | |||
166 | int rowCount;
|
||
167 | 4937 | fjp | FBitSet bitSet = layer.getRecordset().getSelection(); |
168 | if (bitSet.cardinality() == 0) { |
||
169 | 4850 | fjp | rowCount = va.getShapeCount(); |
170 | for (int i = 0; i < rowCount; i++) { |
||
171 | 4877 | fjp | IGeometry geom = va.getShape(i); |
172 | 4850 | fjp | |
173 | 4877 | fjp | if (geom != null) { |
174 | Value[] values = sds.getRow(i);
|
||
175 | 4937 | fjp | IFeature feat = new DefaultFeature(geom, values, "" + i); |
176 | 4850 | fjp | DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
177 | DefaultRowEdited.STATUS_ADDED, i); |
||
178 | writer.process(edRow); |
||
179 | } |
||
180 | } |
||
181 | 4937 | fjp | } else {
|
182 | for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet |
||
183 | .nextSetBit(i + 1)) {
|
||
184 | 4877 | fjp | IGeometry geom = va.getShape(i); |
185 | 4850 | fjp | |
186 | 4877 | fjp | if (geom != null) { |
187 | Value[] values = sds.getRow(i);
|
||
188 | 4937 | fjp | IFeature feat = new DefaultFeature(geom, values, "" + i); |
189 | 4850 | fjp | DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
190 | DefaultRowEdited.STATUS_ADDED, i); |
||
191 | 4877 | fjp | |
192 | 4850 | fjp | writer.process(edRow); |
193 | } |
||
194 | } |
||
195 | 4937 | fjp | |
196 | 4850 | fjp | } |
197 | |||
198 | writer.postProcess(); |
||
199 | } |
||
200 | |||
201 | 4937 | fjp | public void saveToDxf(FLyrVect layer) throws EditionException { |
202 | try {
|
||
203 | JFileChooser jfc = new JFileChooser(); |
||
204 | SimpleFileFilter filterShp = new SimpleFileFilter("dxf", |
||
205 | PluginServices.getText(this, "dxf_files")); |
||
206 | jfc.setFileFilter(filterShp); |
||
207 | if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) |
||
208 | { |
||
209 | File newFile = jfc.getSelectedFile();
|
||
210 | String path = newFile.getAbsolutePath();
|
||
211 | if (!(path.toLowerCase().endsWith(".dxf"))) { |
||
212 | path = path + ".dxf";
|
||
213 | } |
||
214 | newFile = new File(path); |
||
215 | 4850 | fjp | |
216 | 4937 | fjp | DxfWriter writer = (DxfWriter) LayerFactory.getWM().getWriter( |
217 | "DXF Writer");
|
||
218 | SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
||
219 | SelectableDataSource sds = layer.getRecordset(); |
||
220 | FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
||
221 | lyrDef.setFieldsDesc(fieldsDescrip); |
||
222 | lyrDef.setFile(newFile); |
||
223 | lyrDef.setName(newFile.getName()); |
||
224 | lyrDef.setShapeType(layer.getShapeType()); |
||
225 | writer.setFile(newFile); |
||
226 | writer.initialize(lyrDef); |
||
227 | writer.setProjection(layer.getProjection()); |
||
228 | DxfFieldsMapping fieldsMapping = new DxfFieldsMapping();
|
||
229 | // TODO: Recuperar aqu? los campos del cuadro de di?logo.
|
||
230 | writer.setFieldMapping(fieldsMapping); |
||
231 | |||
232 | writeFeatures(layer, writer); |
||
233 | } |
||
234 | |||
235 | } catch (DriverIOException e) {
|
||
236 | e.printStackTrace(); |
||
237 | throw new EditionException(e); |
||
238 | } catch (DriverException e) {
|
||
239 | e.printStackTrace(); |
||
240 | throw new EditionException(e); |
||
241 | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
||
242 | e.printStackTrace(); |
||
243 | throw new EditionException(e); |
||
244 | } |
||
245 | |||
246 | 4850 | fjp | } |
247 | |||
248 | public void saveToShp(FLyrVect layer) throws EditionException { |
||
249 | try {
|
||
250 | JFileChooser jfc = new JFileChooser(); |
||
251 | SimpleFileFilter filterShp = new SimpleFileFilter("shp", |
||
252 | PluginServices.getText(this, "shp_files")); |
||
253 | jfc.setFileFilter(filterShp); |
||
254 | if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
||
255 | File newFile = jfc.getSelectedFile();
|
||
256 | String path = newFile.getAbsolutePath();
|
||
257 | if (!(path.toLowerCase().endsWith(".shp"))) { |
||
258 | path = path + ".shp";
|
||
259 | } |
||
260 | newFile = new File(path); |
||
261 | |||
262 | ShpWriter writer = (ShpWriter) LayerFactory.getWM().getWriter( |
||
263 | "Shape Writer");
|
||
264 | 4937 | fjp | SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
265 | SelectableDataSource sds = layer.getRecordset(); |
||
266 | FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
||
267 | lyrDef.setFieldsDesc(fieldsDescrip); |
||
268 | lyrDef.setFile(newFile); |
||
269 | lyrDef.setName(newFile.getName()); |
||
270 | lyrDef.setShapeType(layer.getShapeType()); |
||
271 | writer.setFile(newFile); |
||
272 | writer.initialize(lyrDef); |
||
273 | |||
274 | 4850 | fjp | writeFeatures(layer, writer); |
275 | |||
276 | } |
||
277 | } catch (DriverIOException e) {
|
||
278 | e.printStackTrace(); |
||
279 | throw new EditionException(e); |
||
280 | } catch (DriverException e) {
|
||
281 | e.printStackTrace(); |
||
282 | throw new EditionException(e); |
||
283 | 4875 | fjp | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
284 | e.printStackTrace(); |
||
285 | throw new EditionException(e); |
||
286 | 4850 | fjp | } |
287 | |||
288 | } |
||
289 | |||
290 | /**
|
||
291 | * @see com.iver.andami.plugins.Extension#isEnabled()
|
||
292 | */
|
||
293 | public boolean isEnabled() { |
||
294 | View f = (View) PluginServices.getMDIManager().getActiveView(); |
||
295 | |||
296 | if (f == null) { |
||
297 | return false; |
||
298 | } else
|
||
299 | return true; |
||
300 | } |
||
301 | |||
302 | /**
|
||
303 | * @see com.iver.andami.plugins.Extension#isVisible()
|
||
304 | */
|
||
305 | public boolean isVisible() { |
||
306 | com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager() |
||
307 | .getActiveView(); |
||
308 | |||
309 | if (f == null) { |
||
310 | return false; |
||
311 | } |
||
312 | |||
313 | if (f.getClass() == View.class) { |
||
314 | return true; |
||
315 | } else {
|
||
316 | return false; |
||
317 | } |
||
318 | |||
319 | } |
||
320 | } |