root / tags / v1_0_2_Build_911 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / MyFinishAction.java @ 11388
History | View | Annotate | Download (10.8 KB)
1 |
package com.iver.cit.gvsig.gui.cad; |
---|---|
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.Types; |
8 |
|
9 |
import javax.swing.JOptionPane; |
10 |
|
11 |
import jwizardcomponent.FinishAction; |
12 |
import jwizardcomponent.JWizardComponents; |
13 |
|
14 |
import org.cresques.cts.IProjection; |
15 |
|
16 |
import com.hardcode.driverManager.Driver; |
17 |
import com.iver.andami.PluginServices; |
18 |
import com.iver.andami.messages.NotificationManager; |
19 |
import com.iver.cit.gvsig.CADExtension; |
20 |
import com.iver.cit.gvsig.StartEditing; |
21 |
import com.iver.cit.gvsig.fmap.MapControl; |
22 |
import com.iver.cit.gvsig.fmap.core.FShape; |
23 |
import com.iver.cit.gvsig.fmap.core.ICanReproject; |
24 |
import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
25 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
26 |
import com.iver.cit.gvsig.fmap.drivers.DXFLayerDefinition; |
27 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
28 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
29 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
30 |
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
31 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
32 |
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver; |
33 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGISWriter; |
34 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
35 |
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
36 |
import com.iver.cit.gvsig.fmap.edition.rules.IRule; |
37 |
import com.iver.cit.gvsig.fmap.edition.rules.RulePolygon; |
38 |
import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfFieldsMapping; |
39 |
import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfWriter; |
40 |
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter; |
41 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
42 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
43 |
import com.iver.cit.gvsig.gui.cad.panels.ChooseGeometryType; |
44 |
import com.iver.cit.gvsig.gui.cad.panels.FileBasedPanel; |
45 |
import com.iver.cit.gvsig.gui.cad.panels.JPanelFieldDefinition; |
46 |
import com.iver.cit.gvsig.gui.cad.panels.PostGISpanel; |
47 |
import com.iver.cit.gvsig.jdbc_spatial.gui.jdbcwizard.ConnectionSettings; |
48 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
49 |
|
50 |
public class MyFinishAction extends FinishAction |
51 |
{ |
52 |
JWizardComponents myWizardComponents; |
53 |
FinishAction oldAction; |
54 |
ITableDefinition lyrDef = null;
|
55 |
View view;
|
56 |
String actionComand;
|
57 |
public MyFinishAction(JWizardComponents wizardComponents, View view, String actionComand) { |
58 |
super(wizardComponents);
|
59 |
oldAction = wizardComponents.getFinishAction(); |
60 |
myWizardComponents = wizardComponents; |
61 |
this.view = view;
|
62 |
this.actionComand = actionComand;
|
63 |
// TODO Auto-generated constructor stub
|
64 |
} |
65 |
|
66 |
public void performAction() { |
67 |
FLyrVect lyr = null;
|
68 |
MapControl mapCtrl = view.getMapControl(); |
69 |
try {
|
70 |
// ChooseWriteDriver driverPanel = (ChooseWriteDriver) myWizardComponents.getWizardPanel(0);
|
71 |
mapCtrl.getMapContext().beginAtomicEvent(); |
72 |
if (actionComand.equals("SHP")) |
73 |
{ |
74 |
FileBasedPanel shpPanel = (FileBasedPanel) myWizardComponents.getWizardPanel(2);
|
75 |
String path=shpPanel.getPath();
|
76 |
if (!path.toLowerCase().endsWith(".shp")){ |
77 |
path+=".shp";
|
78 |
} |
79 |
File newFile = new File(path); |
80 |
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 |
ChooseGeometryType geometryTypePanel = (ChooseGeometryType) myWizardComponents.getWizardPanel(0);
|
89 |
JPanelFieldDefinition fieldDefinitionPanel = (JPanelFieldDefinition) myWizardComponents.getWizardPanel(1);
|
90 |
|
91 |
|
92 |
String layerName = geometryTypePanel.getLayerName();
|
93 |
String selectedDriver = geometryTypePanel.getSelectedDriver();
|
94 |
int geometryType = geometryTypePanel.getSelectedGeometryType();
|
95 |
FieldDescription[] fieldsDesc = fieldDefinitionPanel.getFieldsDescription();
|
96 |
|
97 |
Driver drv = LayerFactory.getDM().getDriver(selectedDriver);
|
98 |
|
99 |
|
100 |
|
101 |
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 |
writer.setFile(newFile); |
108 |
writer.initialize(lyrDef); |
109 |
writer.preProcess(); |
110 |
writer.postProcess(); |
111 |
|
112 |
|
113 |
lyr = (FLyrVect) LayerFactory.createLayer(layerName, |
114 |
(VectorialFileDriver) drv, newFile, mapCtrl.getProjection()); |
115 |
|
116 |
} |
117 |
else if (actionComand.equals("DXF")) |
118 |
{ |
119 |
FileBasedPanel dxfPanel = (FileBasedPanel) myWizardComponents.getWizardPanel(0);
|
120 |
String path=dxfPanel.getPath();
|
121 |
if (!path.toLowerCase().endsWith(".dxf")){ |
122 |
path+=".dxf";
|
123 |
} |
124 |
File newFile = new File(path); |
125 |
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 |
DXFLayerDefinition lyrDef = new DXFLayerDefinition();
|
134 |
lyrDef.setFile(newFile); |
135 |
String layerName = newFile.getName();
|
136 |
lyrDef.setName(layerName); |
137 |
DxfWriter writer= (DxfWriter)LayerFactory.getWM().getWriter("DXF Writer");
|
138 |
writer.setFile(newFile); |
139 |
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 |
writer.initialize(lyrDef); |
150 |
writer.preProcess(); |
151 |
writer.postProcess(); |
152 |
Driver drv = LayerFactory.getDM().getDriver("gvSIG DXF Memory Driver"); |
153 |
|
154 |
lyr = (FLyrVect) LayerFactory.createLayer(layerName, |
155 |
(VectorialFileDriver) drv, newFile, mapCtrl.getProjection()); |
156 |
|
157 |
} |
158 |
else if (actionComand.equals("POSTGIS")) |
159 |
{ |
160 |
ChooseGeometryType geometryTypePanel = (ChooseGeometryType) myWizardComponents.getWizardPanel(0);
|
161 |
JPanelFieldDefinition fieldDefinitionPanel = (JPanelFieldDefinition) myWizardComponents.getWizardPanel(1);
|
162 |
|
163 |
|
164 |
String layerName = geometryTypePanel.getLayerName();
|
165 |
String selectedDriver = geometryTypePanel.getSelectedDriver();
|
166 |
int geometryType = geometryTypePanel.getSelectedGeometryType();
|
167 |
FieldDescription[] fieldsDesc = fieldDefinitionPanel.getFieldsDescription();
|
168 |
|
169 |
Driver drv = LayerFactory.getDM().getDriver(selectedDriver);
|
170 |
|
171 |
VectorialJDBCDriver dbDriver = (VectorialJDBCDriver) drv; |
172 |
PostGISpanel postgisPanel = (PostGISpanel) myWizardComponents.getWizardPanel(2);
|
173 |
ConnectionSettings cs = postgisPanel.getConnSettings(); |
174 |
if (cs == null) |
175 |
return;
|
176 |
Connection conex = DriverManager.getConnection(cs.getConnectionString(), |
177 |
cs.getUser(), cs.getPassw()); |
178 |
|
179 |
DBLayerDefinition dbLayerDef = new DBLayerDefinition();
|
180 |
dbLayerDef.setCatalogName(cs.getDb()); |
181 |
dbLayerDef.setTableName(layerName); |
182 |
dbLayerDef.setShapeType(geometryType); |
183 |
dbLayerDef.setFieldsDesc(fieldsDesc); |
184 |
dbLayerDef.setFieldGeometry("the_geom");
|
185 |
dbLayerDef.setFieldID("gid");
|
186 |
|
187 |
dbLayerDef.setWhereClause("");
|
188 |
String strSRID = mapCtrl.getProjection().getAbrev()
|
189 |
.substring(5);
|
190 |
dbLayerDef.setSRID_EPSG(strSRID); |
191 |
dbLayerDef.setConnection(conex); |
192 |
|
193 |
PostGISWriter writer= new PostGISWriter(); //(PostGISWriter)LayerFactory.getWM().getWriter("PostGIS Writer"); |
194 |
writer.setWriteAll(true);
|
195 |
writer.setCreateTable(true);
|
196 |
writer.initialize(dbLayerDef); |
197 |
|
198 |
// Creamos la tabla.
|
199 |
writer.preProcess(); |
200 |
writer.postProcess(); |
201 |
|
202 |
if (dbDriver instanceof ICanReproject) |
203 |
{ |
204 |
((ICanReproject)dbDriver).setDestProjection(strSRID); |
205 |
} |
206 |
|
207 |
// Creamos el driver. OJO: Hay que a?adir el campo ID a la
|
208 |
// definici?n de campos.
|
209 |
|
210 |
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 |
|
236 |
} |
237 |
|
238 |
dbDriver.setData(conex, dbLayerDef); |
239 |
IProjection proj = null;
|
240 |
if (drv instanceof ICanReproject) |
241 |
{ |
242 |
proj = CRSFactory.getCRS("EPSG:" + ((ICanReproject)dbDriver).getSourceProjection());
|
243 |
} |
244 |
|
245 |
lyr = (FLyrVect) LayerFactory.createDBLayer(dbDriver, layerName, proj); |
246 |
|
247 |
} |
248 |
else // Si no es ni lo uno ni lo otro, |
249 |
{ |
250 |
|
251 |
|
252 |
} |
253 |
} catch (Exception e) { |
254 |
// TODO Auto-generated catch block
|
255 |
e.printStackTrace(); |
256 |
} |
257 |
lyr.setVisible(true);
|
258 |
|
259 |
mapCtrl.getMapContext().getLayers().addLayer(lyr); |
260 |
|
261 |
mapCtrl.getMapContext().endAtomicEvent(); |
262 |
lyr.addLayerListener(CADExtension.getEditionManager()); |
263 |
lyr.setActive(true);
|
264 |
|
265 |
try {
|
266 |
lyr.setEditing(true);
|
267 |
VectorialEditableAdapter vea = (VectorialEditableAdapter) lyr.getSource(); |
268 |
vea.getRules().clear(); |
269 |
// TODO: ESTO ES PROVISIONAL, DESCOMENTAR LUEGO
|
270 |
if (vea.getShapeType() == FShape.POLYGON)
|
271 |
{ |
272 |
IRule rulePol = new RulePolygon();
|
273 |
vea.getRules().add(rulePol); |
274 |
} |
275 |
StartEditing.startCommandsApplicable(view,lyr); |
276 |
vea.getCommandRecord().addCommandListener(mapCtrl); |
277 |
view.showConsole(); |
278 |
|
279 |
// Para cerrar el cuadro de di?logo.
|
280 |
oldAction.performAction(); |
281 |
} catch (EditionException e) {
|
282 |
e.printStackTrace(); |
283 |
NotificationManager.addError(e); |
284 |
} catch (DriverIOException e) {
|
285 |
e.printStackTrace(); |
286 |
NotificationManager.addError(e); |
287 |
|
288 |
} |
289 |
|
290 |
|
291 |
} |
292 |
|
293 |
} |
294 |
|