svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / MyFinishAction.java @ 4937
History | View | Annotate | Download (8.26 KB)
1 | 4380 | fjp | package com.iver.cit.gvsig.gui.cad; |
---|---|---|---|
2 | |||
3 | 4416 | fjp | import java.io.File; |
4 | 4740 | fjp | import java.sql.Connection; |
5 | import java.sql.DriverManager; |
||
6 | 4767 | fjp | import java.sql.Types; |
7 | 4416 | fjp | |
8 | 4380 | fjp | import jwizardcomponent.FinishAction; |
9 | import jwizardcomponent.JWizardComponents; |
||
10 | |||
11 | 4740 | fjp | import org.cresques.cts.IProjection; |
12 | import org.cresques.cts.ProjectionPool; |
||
13 | |||
14 | import com.hardcode.driverManager.Driver; |
||
15 | 4421 | fjp | import com.iver.cit.gvsig.CADExtension; |
16 | 4416 | fjp | import com.iver.cit.gvsig.fmap.MapControl; |
17 | 4740 | fjp | import com.iver.cit.gvsig.fmap.core.ICanReproject; |
18 | import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
||
19 | 4385 | fjp | import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
20 | 4416 | fjp | import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
21 | import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
||
22 | 4387 | fjp | import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
23 | 4740 | fjp | import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver; |
24 | import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGISWriter; |
||
25 | 4416 | fjp | import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
26 | 4937 | fjp | import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfWriter; |
27 | 4416 | fjp | import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter; |
28 | 4421 | fjp | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
29 | 4385 | fjp | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
30 | 4487 | fjp | import com.iver.cit.gvsig.gui.View; |
31 | 4385 | fjp | import com.iver.cit.gvsig.gui.cad.panels.ChooseGeometryType; |
32 | import com.iver.cit.gvsig.gui.cad.panels.JPanelFieldDefinition; |
||
33 | 4740 | fjp | import com.iver.cit.gvsig.gui.cad.panels.PostGISpanel; |
34 | 4937 | fjp | import com.iver.cit.gvsig.gui.cad.panels.FileBasedPanel; |
35 | 4740 | fjp | import com.iver.cit.gvsig.jdbc_spatial.gui.jdbcwizard.ConnectionSettings; |
36 | 4385 | fjp | |
37 | 4380 | fjp | public class MyFinishAction extends FinishAction |
38 | { |
||
39 | JWizardComponents myWizardComponents; |
||
40 | FinishAction oldAction; |
||
41 | 4416 | fjp | ITableDefinition lyrDef = null;
|
42 | 4487 | fjp | View view;
|
43 | 4416 | fjp | String actionComand;
|
44 | 4487 | fjp | public MyFinishAction(JWizardComponents wizardComponents, View view, String actionComand) { |
45 | 4380 | fjp | super(wizardComponents);
|
46 | oldAction = wizardComponents.getFinishAction(); |
||
47 | myWizardComponents = wizardComponents; |
||
48 | 4487 | fjp | this.view = view;
|
49 | 4416 | fjp | this.actionComand = actionComand;
|
50 | 4380 | fjp | // TODO Auto-generated constructor stub
|
51 | } |
||
52 | |||
53 | public void performAction() { |
||
54 | 4487 | fjp | FLyrVect lyr = null;
|
55 | MapControl mapCtrl = view.getMapControl(); |
||
56 | try {
|
||
57 | 4416 | fjp | // ChooseWriteDriver driverPanel = (ChooseWriteDriver) myWizardComponents.getWizardPanel(0);
|
58 | ChooseGeometryType geometryTypePanel = (ChooseGeometryType) myWizardComponents.getWizardPanel(0);
|
||
59 | JPanelFieldDefinition fieldDefinitionPanel = (JPanelFieldDefinition) myWizardComponents.getWizardPanel(1);
|
||
60 | |||
61 | 4385 | fjp | |
62 | 4416 | fjp | String layerName = geometryTypePanel.getLayerName();
|
63 | String selectedDriver = geometryTypePanel.getSelectedDriver();
|
||
64 | 4385 | fjp | int geometryType = geometryTypePanel.getSelectedGeometryType();
|
65 | FieldDescription[] fieldsDesc = fieldDefinitionPanel.getFieldsDescription();
|
||
66 | |||
67 | 4740 | fjp | Driver drv = LayerFactory.getDM().getDriver(selectedDriver);
|
68 | mapCtrl.getMapContext().beginAtomicEvent(); |
||
69 | 4416 | fjp | if (actionComand.equals("SHP")) |
70 | 4387 | fjp | { |
71 | 4937 | fjp | FileBasedPanel shpPanel = (FileBasedPanel) myWizardComponents.getWizardPanel(2);
|
72 | 4416 | fjp | File newFile = new File(shpPanel.getPath()); |
73 | SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
||
74 | lyrDef.setFieldsDesc(fieldsDesc); |
||
75 | lyrDef.setFile(newFile); |
||
76 | lyrDef.setName(layerName); |
||
77 | lyrDef.setShapeType(geometryType); |
||
78 | ShpWriter writer= (ShpWriter)LayerFactory.getWM().getWriter("Shape Writer");
|
||
79 | 4438 | fjp | writer.setFile(newFile); |
80 | writer.initialize(lyrDef); |
||
81 | 4416 | fjp | writer.preProcess(); |
82 | writer.postProcess(); |
||
83 | |||
84 | 4740 | fjp | |
85 | 4421 | fjp | lyr = (FLyrVect) LayerFactory.createLayer(layerName, |
86 | 4416 | fjp | (VectorialFileDriver) drv, newFile, mapCtrl.getProjection()); |
87 | |||
88 | 4387 | fjp | } |
89 | 4937 | fjp | else if (actionComand.equals("DXF")) |
90 | { |
||
91 | FileBasedPanel shpPanel = (FileBasedPanel) myWizardComponents.getWizardPanel(2);
|
||
92 | File newFile = new File(shpPanel.getPath()); |
||
93 | SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
||
94 | lyrDef.setFieldsDesc(fieldsDesc); |
||
95 | lyrDef.setFile(newFile); |
||
96 | lyrDef.setName(layerName); |
||
97 | lyrDef.setShapeType(geometryType); |
||
98 | DxfWriter writer= (DxfWriter)LayerFactory.getWM().getWriter("DXF Writer");
|
||
99 | writer.setFile(newFile); |
||
100 | writer.initialize(lyrDef); |
||
101 | writer.preProcess(); |
||
102 | writer.postProcess(); |
||
103 | |||
104 | |||
105 | lyr = (FLyrVect) LayerFactory.createLayer(layerName, |
||
106 | (VectorialFileDriver) drv, newFile, mapCtrl.getProjection()); |
||
107 | |||
108 | } |
||
109 | 4740 | fjp | else if (drv instanceof VectorialJDBCDriver) |
110 | 4387 | fjp | { |
111 | 4740 | fjp | VectorialJDBCDriver dbDriver = (VectorialJDBCDriver) drv; |
112 | PostGISpanel postgisPanel = (PostGISpanel) myWizardComponents.getWizardPanel(2);
|
||
113 | ConnectionSettings cs = postgisPanel.getConnSettings(); |
||
114 | 4416 | fjp | if (cs == null) |
115 | return;
|
||
116 | 4740 | fjp | Connection conex = DriverManager.getConnection(cs.getConnectionString(), |
117 | 4416 | fjp | cs.getUser(), cs.getPassw()); |
118 | |||
119 | 4740 | fjp | DBLayerDefinition dbLayerDef = new DBLayerDefinition();
|
120 | 4416 | fjp | dbLayerDef.setCatalogName(cs.getDb()); |
121 | 4740 | fjp | dbLayerDef.setTableName(layerName); |
122 | dbLayerDef.setShapeType(geometryType); |
||
123 | 4767 | fjp | dbLayerDef.setFieldsDesc(fieldsDesc); |
124 | 4416 | fjp | dbLayerDef.setFieldGeometry("the_geom");
|
125 | 4748 | fjp | dbLayerDef.setFieldID("gid");
|
126 | 4767 | fjp | |
127 | 4748 | fjp | dbLayerDef.setWhereClause("");
|
128 | 4740 | fjp | String strSRID = mapCtrl.getProjection().getAbrev()
|
129 | 4416 | fjp | .substring(5);
|
130 | 4740 | fjp | dbLayerDef.setSRID_EPSG(strSRID); |
131 | 4748 | fjp | dbLayerDef.setConnection(conex); |
132 | 4740 | fjp | |
133 | 4748 | fjp | PostGISWriter writer= new PostGISWriter(); //(PostGISWriter)LayerFactory.getWM().getWriter("PostGIS Writer"); |
134 | 4740 | fjp | writer.setWriteAll(true);
|
135 | writer.setCreateTable(true);
|
||
136 | writer.initialize(dbLayerDef); |
||
137 | |||
138 | 4767 | fjp | // Creamos la tabla.
|
139 | 4740 | fjp | writer.preProcess(); |
140 | writer.postProcess(); |
||
141 | |||
142 | if (dbDriver instanceof ICanReproject) |
||
143 | { |
||
144 | ((ICanReproject)dbDriver).setDestProjection(strSRID); |
||
145 | } |
||
146 | 4767 | fjp | |
147 | // Creamos el driver. OJO: Hay que a?adir el campo ID a la
|
||
148 | // definici?n de campos.
|
||
149 | |||
150 | boolean bFound = false; |
||
151 | for (int i=0; i < dbLayerDef.getFieldsDesc().length; i++) |
||
152 | { |
||
153 | FieldDescription f = dbLayerDef.getFieldsDesc()[i]; |
||
154 | if (f.getFieldName().equalsIgnoreCase(dbLayerDef.getFieldID()))
|
||
155 | { |
||
156 | bFound = true;
|
||
157 | break;
|
||
158 | } |
||
159 | } |
||
160 | // Si no est?, lo a?adimos
|
||
161 | if (!bFound)
|
||
162 | { |
||
163 | int numFieldsAnt = dbLayerDef.getFieldsDesc().length;
|
||
164 | FieldDescription[] newFields = new FieldDescription[dbLayerDef.getFieldsDesc().length + 1]; |
||
165 | for (int i=0; i < numFieldsAnt; i++) |
||
166 | { |
||
167 | newFields[i] = dbLayerDef.getFieldsDesc()[i]; |
||
168 | } |
||
169 | newFields[numFieldsAnt] = new FieldDescription();
|
||
170 | newFields[numFieldsAnt].setFieldDecimalCount(0);
|
||
171 | newFields[numFieldsAnt].setFieldType(Types.INTEGER);
|
||
172 | newFields[numFieldsAnt].setFieldLength(7);
|
||
173 | newFields[numFieldsAnt].setFieldName(dbLayerDef.getFieldID()); |
||
174 | dbLayerDef.setFieldsDesc(newFields); |
||
175 | |||
176 | } |
||
177 | |||
178 | 4740 | fjp | dbDriver.setData(conex, dbLayerDef); |
179 | IProjection proj = null;
|
||
180 | if (drv instanceof ICanReproject) |
||
181 | { |
||
182 | proj = ProjectionPool.get("EPSG:" + ((ICanReproject)dbDriver).getSourceProjection());
|
||
183 | } |
||
184 | 4387 | fjp | |
185 | 4740 | fjp | lyr = (FLyrVect) LayerFactory.createDBLayer(dbDriver, layerName, proj); |
186 | |||
187 | 4387 | fjp | } |
188 | else // Si no es ni lo uno ni lo otro, |
||
189 | { |
||
190 | |||
191 | 4416 | fjp | |
192 | 4387 | fjp | } |
193 | 4380 | fjp | } catch (Exception e) { |
194 | // TODO Auto-generated catch block
|
||
195 | e.printStackTrace(); |
||
196 | } |
||
197 | 4487 | fjp | lyr.setVisible(true);
|
198 | mapCtrl.getMapContext().getLayers().addLayer(lyr); |
||
199 | 4380 | fjp | |
200 | 4487 | fjp | mapCtrl.getMapContext().endAtomicEvent(); |
201 | lyr.addLayerListener(CADExtension.getEditionManager()); |
||
202 | lyr.setActive(true);
|
||
203 | lyr.setEditing(true);
|
||
204 | VectorialEditableAdapter vea = (VectorialEditableAdapter) lyr.getSource(); |
||
205 | // TODO: Provisional, para que al poner
|
||
206 | // un tema en edici?n el CADToolAdapter se entere
|
||
207 | CADExtension.getCADToolAdapter().setVectorialAdapter(vea); |
||
208 | vea.getCommandRecord().addCommandListener(mapCtrl); |
||
209 | view.showConsole(); |
||
210 | |||
211 | 4380 | fjp | // Para cerrar el cuadro de di?logo.
|
212 | oldAction.performAction(); |
||
213 | |||
214 | } |
||
215 | |||
216 | } |