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