svn-gvsig-desktop / tags / v1_0_2_Build_909 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / MyFinishAction.java @ 11150
History | View | Annotate | Download (10.8 KB)
1 | 4380 | fjp | package com.iver.cit.gvsig.gui.cad; |
---|---|---|---|
2 | |||
3 | 6157 | caballero | import java.awt.Component; |
4 | 4416 | fjp | import java.io.File; |
5 | 4740 | fjp | import java.sql.Connection; |
6 | import java.sql.DriverManager; |
||
7 | 4767 | fjp | import java.sql.Types; |
8 | 4416 | fjp | |
9 | 6157 | caballero | import javax.swing.JOptionPane; |
10 | |||
11 | 4380 | fjp | import jwizardcomponent.FinishAction; |
12 | import jwizardcomponent.JWizardComponents; |
||
13 | |||
14 | 4740 | fjp | import org.cresques.cts.IProjection; |
15 | |||
16 | import com.hardcode.driverManager.Driver; |
||
17 | 6157 | caballero | import com.iver.andami.PluginServices; |
18 | 5152 | fjp | import com.iver.andami.messages.NotificationManager; |
19 | 4421 | fjp | import com.iver.cit.gvsig.CADExtension; |
20 | 6022 | caballero | import com.iver.cit.gvsig.StartEditing; |
21 | 4416 | fjp | import com.iver.cit.gvsig.fmap.MapControl; |
22 | 5893 | fjp | import com.iver.cit.gvsig.fmap.core.FShape; |
23 | 4740 | fjp | import com.iver.cit.gvsig.fmap.core.ICanReproject; |
24 | 7703 | luisw2 | import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
25 | 4740 | fjp | import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
26 | 5152 | fjp | import com.iver.cit.gvsig.fmap.drivers.DXFLayerDefinition; |
27 | 5893 | fjp | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
28 | 4385 | fjp | import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
29 | 4416 | fjp | import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
30 | import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
||
31 | 4387 | fjp | import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
32 | 4740 | fjp | import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver; |
33 | import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGISWriter; |
||
34 | 5152 | fjp | import com.iver.cit.gvsig.fmap.edition.EditionException; |
35 | 4416 | fjp | import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
36 | 5893 | fjp | import com.iver.cit.gvsig.fmap.edition.rules.IRule; |
37 | import com.iver.cit.gvsig.fmap.edition.rules.RulePolygon; |
||
38 | 5152 | fjp | import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfFieldsMapping; |
39 | 4937 | fjp | import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfWriter; |
40 | 4416 | fjp | import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter; |
41 | 4421 | fjp | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
42 | 4385 | fjp | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
43 | import com.iver.cit.gvsig.gui.cad.panels.ChooseGeometryType; |
||
44 | 5152 | fjp | import com.iver.cit.gvsig.gui.cad.panels.FileBasedPanel; |
45 | 4385 | fjp | import com.iver.cit.gvsig.gui.cad.panels.JPanelFieldDefinition; |
46 | 4740 | fjp | import com.iver.cit.gvsig.gui.cad.panels.PostGISpanel; |
47 | import com.iver.cit.gvsig.jdbc_spatial.gui.jdbcwizard.ConnectionSettings; |
||
48 | 8765 | jjdelcerro | import com.iver.cit.gvsig.project.documents.view.gui.View; |
49 | 4385 | fjp | |
50 | 4380 | fjp | public class MyFinishAction extends FinishAction |
51 | { |
||
52 | JWizardComponents myWizardComponents; |
||
53 | FinishAction oldAction; |
||
54 | 4416 | fjp | ITableDefinition lyrDef = null;
|
55 | 4487 | fjp | View view;
|
56 | 4416 | fjp | String actionComand;
|
57 | 6022 | caballero | public MyFinishAction(JWizardComponents wizardComponents, View view, String actionComand) { |
58 | 4380 | fjp | super(wizardComponents);
|
59 | oldAction = wizardComponents.getFinishAction(); |
||
60 | myWizardComponents = wizardComponents; |
||
61 | 4487 | fjp | this.view = view;
|
62 | 4416 | fjp | this.actionComand = actionComand;
|
63 | 4380 | fjp | // TODO Auto-generated constructor stub
|
64 | } |
||
65 | |||
66 | public void performAction() { |
||
67 | 4487 | fjp | FLyrVect lyr = null;
|
68 | MapControl mapCtrl = view.getMapControl(); |
||
69 | 6022 | caballero | try {
|
70 | 4416 | fjp | // ChooseWriteDriver driverPanel = (ChooseWriteDriver) myWizardComponents.getWizardPanel(0);
|
71 | 4740 | fjp | mapCtrl.getMapContext().beginAtomicEvent(); |
72 | 4416 | fjp | if (actionComand.equals("SHP")) |
73 | 4387 | fjp | { |
74 | 6157 | caballero | FileBasedPanel shpPanel = (FileBasedPanel) myWizardComponents.getWizardPanel(2);
|
75 | 10937 | caballero | String path=shpPanel.getPath();
|
76 | if (!path.toLowerCase().endsWith(".shp")){ |
||
77 | path+=".shp";
|
||
78 | } |
||
79 | File newFile = new File(path); |
||
80 | 6157 | caballero | if( newFile.exists()){
|
81 | int resp = JOptionPane.showConfirmDialog( |
||
82 | (Component) PluginServices.getMainFrame(),PluginServices.getText(this,"fichero_ya_existe_seguro_desea_guardarlo"), |
||
83 | PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
||
84 | if (resp != JOptionPane.YES_OPTION) { |
||
85 | return;
|
||
86 | } |
||
87 | } |
||
88 | 5152 | fjp | ChooseGeometryType geometryTypePanel = (ChooseGeometryType) myWizardComponents.getWizardPanel(0);
|
89 | JPanelFieldDefinition fieldDefinitionPanel = (JPanelFieldDefinition) myWizardComponents.getWizardPanel(1);
|
||
90 | 6022 | caballero | |
91 | |||
92 | 5152 | fjp | String layerName = geometryTypePanel.getLayerName();
|
93 | String selectedDriver = geometryTypePanel.getSelectedDriver();
|
||
94 | int geometryType = geometryTypePanel.getSelectedGeometryType();
|
||
95 | FieldDescription[] fieldsDesc = fieldDefinitionPanel.getFieldsDescription();
|
||
96 | 6022 | caballero | |
97 | Driver drv = LayerFactory.getDM().getDriver(selectedDriver);
|
||
98 | |||
99 | 6157 | caballero | |
100 | |||
101 | 4416 | fjp | SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
102 | lyrDef.setFieldsDesc(fieldsDesc); |
||
103 | lyrDef.setFile(newFile); |
||
104 | lyrDef.setName(layerName); |
||
105 | lyrDef.setShapeType(geometryType); |
||
106 | ShpWriter writer= (ShpWriter)LayerFactory.getWM().getWriter("Shape Writer");
|
||
107 | 4438 | fjp | writer.setFile(newFile); |
108 | writer.initialize(lyrDef); |
||
109 | 4416 | fjp | writer.preProcess(); |
110 | writer.postProcess(); |
||
111 | 6022 | caballero | |
112 | |||
113 | 4421 | fjp | lyr = (FLyrVect) LayerFactory.createLayer(layerName, |
114 | 4416 | fjp | (VectorialFileDriver) drv, newFile, mapCtrl.getProjection()); |
115 | 6022 | caballero | |
116 | 4387 | fjp | } |
117 | 4937 | fjp | else if (actionComand.equals("DXF")) |
118 | { |
||
119 | 6473 | caballero | FileBasedPanel dxfPanel = (FileBasedPanel) myWizardComponents.getWizardPanel(0);
|
120 | 10937 | caballero | String path=dxfPanel.getPath();
|
121 | if (!path.toLowerCase().endsWith(".dxf")){ |
||
122 | path+=".dxf";
|
||
123 | } |
||
124 | File newFile = new File(path); |
||
125 | 6473 | caballero | if( newFile.exists()){
|
126 | int resp = JOptionPane.showConfirmDialog( |
||
127 | (Component) PluginServices.getMainFrame(),PluginServices.getText(this,"fichero_ya_existe_seguro_desea_guardarlo"), |
||
128 | PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
||
129 | if (resp != JOptionPane.YES_OPTION) { |
||
130 | return;
|
||
131 | } |
||
132 | } |
||
133 | 5152 | fjp | DXFLayerDefinition lyrDef = new DXFLayerDefinition();
|
134 | 4937 | fjp | lyrDef.setFile(newFile); |
135 | 5152 | fjp | String layerName = newFile.getName();
|
136 | 4937 | fjp | lyrDef.setName(layerName); |
137 | DxfWriter writer= (DxfWriter)LayerFactory.getWM().getWriter("DXF Writer");
|
||
138 | writer.setFile(newFile); |
||
139 | 5152 | fjp | DxfFieldsMapping fieldsMapping = new DxfFieldsMapping();
|
140 | fieldsMapping.setLayerField("Layer");
|
||
141 | fieldsMapping.setColorField("Color");
|
||
142 | fieldsMapping.setElevationField("Elevation");
|
||
143 | fieldsMapping.setThicknessField("Thickness");
|
||
144 | fieldsMapping.setTextField("Text");
|
||
145 | fieldsMapping.setHeightText("HeightText");
|
||
146 | fieldsMapping.setRotationText("RotationText");
|
||
147 | writer.setFieldMapping(fieldsMapping); |
||
148 | writer.setProjection(mapCtrl.getProjection()); |
||
149 | 4937 | fjp | writer.initialize(lyrDef); |
150 | writer.preProcess(); |
||
151 | writer.postProcess(); |
||
152 | 5152 | fjp | Driver drv = LayerFactory.getDM().getDriver("gvSIG DXF Memory Driver"); |
153 | 6022 | caballero | |
154 | 4937 | fjp | lyr = (FLyrVect) LayerFactory.createLayer(layerName, |
155 | (VectorialFileDriver) drv, newFile, mapCtrl.getProjection()); |
||
156 | 6022 | caballero | |
157 | } |
||
158 | 5152 | fjp | else if (actionComand.equals("POSTGIS")) |
159 | 4387 | fjp | { |
160 | 5152 | fjp | ChooseGeometryType geometryTypePanel = (ChooseGeometryType) myWizardComponents.getWizardPanel(0);
|
161 | JPanelFieldDefinition fieldDefinitionPanel = (JPanelFieldDefinition) myWizardComponents.getWizardPanel(1);
|
||
162 | 6022 | caballero | |
163 | |||
164 | 5152 | fjp | String layerName = geometryTypePanel.getLayerName();
|
165 | String selectedDriver = geometryTypePanel.getSelectedDriver();
|
||
166 | int geometryType = geometryTypePanel.getSelectedGeometryType();
|
||
167 | FieldDescription[] fieldsDesc = fieldDefinitionPanel.getFieldsDescription();
|
||
168 | 6022 | caballero | |
169 | Driver drv = LayerFactory.getDM().getDriver(selectedDriver);
|
||
170 | |||
171 | 4740 | fjp | VectorialJDBCDriver dbDriver = (VectorialJDBCDriver) drv; |
172 | PostGISpanel postgisPanel = (PostGISpanel) myWizardComponents.getWizardPanel(2);
|
||
173 | ConnectionSettings cs = postgisPanel.getConnSettings(); |
||
174 | 4416 | fjp | if (cs == null) |
175 | return;
|
||
176 | 4740 | fjp | Connection conex = DriverManager.getConnection(cs.getConnectionString(), |
177 | 4416 | fjp | cs.getUser(), cs.getPassw()); |
178 | |||
179 | 4740 | fjp | DBLayerDefinition dbLayerDef = new DBLayerDefinition();
|
180 | 4416 | fjp | dbLayerDef.setCatalogName(cs.getDb()); |
181 | 4740 | fjp | dbLayerDef.setTableName(layerName); |
182 | dbLayerDef.setShapeType(geometryType); |
||
183 | 4767 | fjp | dbLayerDef.setFieldsDesc(fieldsDesc); |
184 | 4416 | fjp | dbLayerDef.setFieldGeometry("the_geom");
|
185 | 4748 | fjp | dbLayerDef.setFieldID("gid");
|
186 | 6022 | caballero | |
187 | 4748 | fjp | dbLayerDef.setWhereClause("");
|
188 | 4740 | fjp | String strSRID = mapCtrl.getProjection().getAbrev()
|
189 | 4416 | fjp | .substring(5);
|
190 | 6022 | caballero | dbLayerDef.setSRID_EPSG(strSRID); |
191 | 4748 | fjp | dbLayerDef.setConnection(conex); |
192 | 4740 | fjp | |
193 | 4748 | fjp | PostGISWriter writer= new PostGISWriter(); //(PostGISWriter)LayerFactory.getWM().getWriter("PostGIS Writer"); |
194 | 4740 | fjp | writer.setWriteAll(true);
|
195 | writer.setCreateTable(true);
|
||
196 | writer.initialize(dbLayerDef); |
||
197 | |||
198 | 4767 | fjp | // Creamos la tabla.
|
199 | 4740 | fjp | writer.preProcess(); |
200 | writer.postProcess(); |
||
201 | 6022 | caballero | |
202 | 4740 | fjp | if (dbDriver instanceof ICanReproject) |
203 | 6022 | caballero | { |
204 | 4740 | fjp | ((ICanReproject)dbDriver).setDestProjection(strSRID); |
205 | } |
||
206 | 6022 | caballero | |
207 | // Creamos el driver. OJO: Hay que a?adir el campo ID a la
|
||
208 | 4767 | fjp | // definici?n de campos.
|
209 | 6022 | caballero | |
210 | 4767 | fjp | boolean bFound = false; |
211 | for (int i=0; i < dbLayerDef.getFieldsDesc().length; i++) |
||
212 | { |
||
213 | FieldDescription f = dbLayerDef.getFieldsDesc()[i]; |
||
214 | if (f.getFieldName().equalsIgnoreCase(dbLayerDef.getFieldID()))
|
||
215 | { |
||
216 | bFound = true;
|
||
217 | break;
|
||
218 | } |
||
219 | } |
||
220 | // Si no est?, lo a?adimos
|
||
221 | if (!bFound)
|
||
222 | { |
||
223 | int numFieldsAnt = dbLayerDef.getFieldsDesc().length;
|
||
224 | FieldDescription[] newFields = new FieldDescription[dbLayerDef.getFieldsDesc().length + 1]; |
||
225 | for (int i=0; i < numFieldsAnt; i++) |
||
226 | { |
||
227 | newFields[i] = dbLayerDef.getFieldsDesc()[i]; |
||
228 | } |
||
229 | newFields[numFieldsAnt] = new FieldDescription();
|
||
230 | newFields[numFieldsAnt].setFieldDecimalCount(0);
|
||
231 | newFields[numFieldsAnt].setFieldType(Types.INTEGER);
|
||
232 | newFields[numFieldsAnt].setFieldLength(7);
|
||
233 | newFields[numFieldsAnt].setFieldName(dbLayerDef.getFieldID()); |
||
234 | dbLayerDef.setFieldsDesc(newFields); |
||
235 | 6022 | caballero | |
236 | 4767 | fjp | } |
237 | |||
238 | 4740 | fjp | dbDriver.setData(conex, dbLayerDef); |
239 | 6022 | caballero | IProjection proj = null;
|
240 | 4740 | fjp | if (drv instanceof ICanReproject) |
241 | 6022 | caballero | { |
242 | 7703 | luisw2 | proj = CRSFactory.getCRS("EPSG:" + ((ICanReproject)dbDriver).getSourceProjection());
|
243 | 4740 | fjp | } |
244 | 6022 | caballero | |
245 | 4740 | fjp | lyr = (FLyrVect) LayerFactory.createDBLayer(dbDriver, layerName, proj); |
246 | 6022 | caballero | |
247 | 4387 | fjp | } |
248 | 6022 | caballero | else // Si no es ni lo uno ni lo otro, |
249 | 4387 | fjp | { |
250 | 6022 | caballero | |
251 | |||
252 | 4387 | fjp | } |
253 | 4380 | fjp | } catch (Exception e) { |
254 | // TODO Auto-generated catch block
|
||
255 | e.printStackTrace(); |
||
256 | } |
||
257 | 4487 | fjp | lyr.setVisible(true);
|
258 | 6022 | caballero | |
259 | 4487 | fjp | mapCtrl.getMapContext().getLayers().addLayer(lyr); |
260 | 6022 | caballero | |
261 | 4487 | fjp | mapCtrl.getMapContext().endAtomicEvent(); |
262 | lyr.addLayerListener(CADExtension.getEditionManager()); |
||
263 | lyr.setActive(true);
|
||
264 | 6473 | caballero | |
265 | 5152 | fjp | try {
|
266 | lyr.setEditing(true);
|
||
267 | 5893 | fjp | VectorialEditableAdapter vea = (VectorialEditableAdapter) lyr.getSource(); |
268 | vea.getRules().clear(); |
||
269 | 6002 | fjp | // TODO: ESTO ES PROVISIONAL, DESCOMENTAR LUEGO
|
270 | 6013 | fjp | if (vea.getShapeType() == FShape.POLYGON)
|
271 | { |
||
272 | IRule rulePol = new RulePolygon();
|
||
273 | vea.getRules().add(rulePol); |
||
274 | } |
||
275 | 6022 | caballero | StartEditing.startCommandsApplicable(view,lyr); |
276 | 5893 | fjp | vea.getCommandRecord().addCommandListener(mapCtrl); |
277 | view.showConsole(); |
||
278 | 6022 | caballero | |
279 | 5893 | fjp | // Para cerrar el cuadro de di?logo.
|
280 | oldAction.performAction(); |
||
281 | 5152 | fjp | } catch (EditionException e) {
|
282 | e.printStackTrace(); |
||
283 | NotificationManager.addError(e); |
||
284 | 6013 | fjp | } catch (DriverIOException e) {
|
285 | 5893 | fjp | e.printStackTrace(); |
286 | 6013 | fjp | NotificationManager.addError(e); |
287 | 5893 | fjp | |
288 | 5152 | fjp | } |
289 | 5893 | fjp | |
290 | 6022 | caballero | |
291 | 4380 | fjp | } |
292 | 6022 | caballero | |
293 | 4380 | fjp | } |