svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extGeoDB / src / org / gvsig / geodb / vectorialdb / wizard / WizardVectorialDB.java @ 34114
History | View | Annotate | Download (10.4 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* Prodevelop Integraci?n de Tecnolog?as SL
|
34 |
* Conde Salvatierra de ?lava , 34-10
|
35 |
* 46004 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963 510 612
|
39 |
* +34 963 510 968
|
40 |
* gis@prodevelop.es
|
41 |
* http://www.prodevelop.es
|
42 |
*/
|
43 |
package org.gvsig.geodb.vectorialdb.wizard; |
44 |
|
45 |
import java.awt.Window; |
46 |
import java.util.ArrayList; |
47 |
import java.util.Iterator; |
48 |
import java.util.List; |
49 |
|
50 |
import org.cresques.cts.IProjection; |
51 |
import org.gvsig.andami.PluginServices; |
52 |
import org.gvsig.andami.messages.NotificationManager; |
53 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
54 |
import org.gvsig.app.ApplicationLocator; |
55 |
import org.gvsig.app.ApplicationManager; |
56 |
import org.gvsig.app.prepareAction.PrepareContext; |
57 |
import org.gvsig.app.prepareAction.PrepareContextView; |
58 |
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
59 |
import org.gvsig.fmap.dal.DALLocator; |
60 |
import org.gvsig.fmap.dal.DataManager; |
61 |
import org.gvsig.fmap.dal.DataStoreParameters; |
62 |
import org.gvsig.fmap.dal.exception.ReadException; |
63 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
64 |
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorer; |
65 |
import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
66 |
import org.gvsig.fmap.geom.primitive.Envelope; |
67 |
import org.gvsig.fmap.mapcontext.MapContext; |
68 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
69 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
70 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
71 |
import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
72 |
import org.gvsig.fmap.mapcontrol.MapControl; |
73 |
import org.slf4j.Logger; |
74 |
import org.slf4j.LoggerFactory; |
75 |
|
76 |
|
77 |
|
78 |
/**
|
79 |
* Driver-independent GeoDB wizard. Queries the drivers to fill GUI controls.
|
80 |
* Multi-table selection available.
|
81 |
*
|
82 |
* @author jldominguez
|
83 |
*
|
84 |
*/
|
85 |
public class WizardVectorialDB extends WizardDB { |
86 |
/**
|
87 |
*
|
88 |
*/
|
89 |
private static final long serialVersionUID = -5002685263220038989L; |
90 |
|
91 |
private static Logger logger = LoggerFactory |
92 |
.getLogger(WizardVectorialDB.class.getName()); |
93 |
|
94 |
private static final String GEODB_WIZARD_TAB_NAME = "GeoDB"; |
95 |
private DefaultViewPanel view = null; |
96 |
|
97 |
private PrepareContextView prepareContext;
|
98 |
|
99 |
/**
|
100 |
* This method initializes this
|
101 |
*
|
102 |
* @return void
|
103 |
*/
|
104 |
protected void initialize() { |
105 |
super.initialize();
|
106 |
setTabName(GEODB_WIZARD_TAB_NAME); |
107 |
setLayout(null);
|
108 |
setSize(512, 478); |
109 |
|
110 |
IWindow iw = PluginServices.getMDIManager().getActiveWindow(); |
111 |
|
112 |
if (iw == null) { |
113 |
return;
|
114 |
} |
115 |
|
116 |
if ((iw instanceof DefaultViewPanel)) { |
117 |
view = (DefaultViewPanel) iw; |
118 |
setMapCtrl(view.getMapControl()); |
119 |
} |
120 |
} |
121 |
|
122 |
|
123 |
protected TablesListItem createTabeListItem(DBServerExplorer dbExplorer,
|
124 |
DBStoreParameters param) { |
125 |
return new TablesListItemVectorial(dbExplorer, param, getMapCtrl(), |
126 |
this);
|
127 |
} |
128 |
|
129 |
public void setSettingsPanels(TablesListItem actTable) { |
130 |
super.setSettingsPanels(actTable);
|
131 |
} |
132 |
|
133 |
protected UserTableSettingsPanel createSettingsPanel(
|
134 |
TablesListItem actTable) { |
135 |
if (actTable == null) { |
136 |
return new UserTableSettingsVectorialPanel(null, null, "", |
137 |
getMapCtrl(), true, this, null, null); |
138 |
} |
139 |
String abrev = null; |
140 |
if (getMapCtrl() != null) { |
141 |
abrev = getMapCtrl().getViewPort().getProjection().getAbrev(); |
142 |
} |
143 |
|
144 |
return ((TablesListItemVectorial) actTable)
|
145 |
.getUserTableSettingsPanel(abrev); |
146 |
} |
147 |
|
148 |
|
149 |
public DataStoreParameters[] getParameters() { |
150 |
try {
|
151 |
TablesListItem[] selected = getSelectedTables();
|
152 |
int count = selected.length;
|
153 |
DBStoreParameters[] dbParameters = new DBStoreParameters[count]; |
154 |
String strEPSG = null; |
155 |
if (getMapCtrl() != null) { |
156 |
strEPSG = getMapCtrl().getViewPort().getProjection().getAbrev(); |
157 |
} |
158 |
|
159 |
for (int i = 0; i < count; i++) { |
160 |
TablesListItemVectorial item = (TablesListItemVectorial) selected[i]; |
161 |
|
162 |
dbParameters[i] = getParameterForTable(item); |
163 |
} |
164 |
|
165 |
return dbParameters;// layerArrayToGroup(all_layers, groupName); |
166 |
} catch (Exception e) { |
167 |
logger.error("While creating jdbc layer: " + e.getMessage(), e);
|
168 |
NotificationManager.addError("Error al cargar la capa: "
|
169 |
+ e.getMessage(), e); |
170 |
} |
171 |
|
172 |
return null; |
173 |
} |
174 |
|
175 |
public void execute() { |
176 |
MapControl mapControl = this.getMapCtrl();
|
177 |
IProjection proj = null;
|
178 |
TablesListItem[] tables = getSelectedTables();
|
179 |
|
180 |
|
181 |
List all_layers = new ArrayList(); |
182 |
String strEPSG = mapControl.getViewPort().getProjection().getAbrev();
|
183 |
LayerFactory layerFactory = LayerFactory.getInstance(); |
184 |
String groupName = null; |
185 |
Envelope env = null;
|
186 |
DBStoreParameters parameter; |
187 |
TablesListItem table; |
188 |
FeatureStore store, storeToAdd; |
189 |
|
190 |
DataManager man = DALLocator.getDataManager(); |
191 |
|
192 |
ApplicationManager appGvSIGMan = ApplicationLocator.getManager(); |
193 |
PrepareContext context = this.getPrepareDataStoreContext();
|
194 |
|
195 |
|
196 |
FLayer layer; |
197 |
for (int i = 0; i < tables.length; i++) { |
198 |
table = tables[i]; |
199 |
UserTableSettingsVectorialPanel userTableSettingsPanel = (UserTableSettingsVectorialPanel) table |
200 |
.getUserTableSettingsPanel(); |
201 |
parameter = getParameterForTable(table); |
202 |
if (i == 0) { |
203 |
groupName = parameter.getDBName() + " (" + parameter.getHost()
|
204 |
+ ")";
|
205 |
} |
206 |
try {
|
207 |
parameter = (DBStoreParameters) appGvSIGMan |
208 |
.prepareOpenDataStoreParameters(parameter, context); |
209 |
|
210 |
} catch (Exception e2) { |
211 |
NotificationManager.addError(e2); |
212 |
continue;
|
213 |
} |
214 |
|
215 |
try {
|
216 |
store = (FeatureStore) man.createStore(parameter); |
217 |
} catch (Exception e) { |
218 |
NotificationManager.addError(e); |
219 |
return;
|
220 |
} |
221 |
|
222 |
try {
|
223 |
storeToAdd = (FeatureStore) appGvSIGMan.pepareOpenDataSource( |
224 |
store, context); |
225 |
} catch (Exception e) { |
226 |
NotificationManager.addError(e); |
227 |
store.dispose(); |
228 |
continue;
|
229 |
} |
230 |
|
231 |
try {
|
232 |
|
233 |
layer = layerFactory.createLayer(userTableSettingsPanel |
234 |
.getUserLayerName(), storeToAdd); |
235 |
all_layers.add(layer); |
236 |
if (env == null) { |
237 |
env = layer.getFullEnvelope(); |
238 |
} else {
|
239 |
env.add(layer.getFullEnvelope()); |
240 |
} |
241 |
} catch (Exception e) { |
242 |
storeToAdd.dispose(); |
243 |
NotificationManager.addError(e); |
244 |
} |
245 |
} |
246 |
|
247 |
MapContext mc = mapControl.getMapContext(); |
248 |
FLayers root = mapControl.getMapContext().getLayers(); |
249 |
if (all_layers.size() > 1) { |
250 |
FLayers group = new FLayers();// (mc,root); |
251 |
group.setMapContext(mc); |
252 |
group.setParentLayer(root); |
253 |
group.setName(groupName); |
254 |
|
255 |
Iterator iter = all_layers.iterator();
|
256 |
while (iter.hasNext()) {
|
257 |
group.addLayer((FLayer) iter.next()); |
258 |
} |
259 |
|
260 |
if ((group != null) && !(group.isOk())) { |
261 |
// if the layer is not okay (it has errors) process them
|
262 |
processErrorsOfLayer(group, mapControl); |
263 |
} |
264 |
|
265 |
if (group != null) { |
266 |
group.setVisible(true);
|
267 |
mapControl.getMapContext().beginAtomicEvent(); |
268 |
try {
|
269 |
// checkProjection(group, mapControl.getViewPort());
|
270 |
try {
|
271 |
mapControl.getMapContext().getLayers().addLayer(group); |
272 |
} catch (CancelationException e) {
|
273 |
// TODO Auto-generated catch block
|
274 |
e.printStackTrace(); |
275 |
} |
276 |
if (mapControl.getViewPort().getExtent() == null) { |
277 |
mapControl.getViewPort().setEnvelope(env); |
278 |
} |
279 |
} finally {
|
280 |
mapControl.getMapContext().endAtomicEvent(); |
281 |
} |
282 |
return;
|
283 |
} |
284 |
} else if (all_layers.size() == 1) { |
285 |
layer = (FLayer) all_layers.get(0);
|
286 |
if (!(layer.isOk())) {
|
287 |
// if the layer is not okay (it has errors) process them
|
288 |
processErrorsOfLayer(layer, mapControl); |
289 |
} |
290 |
|
291 |
layer.setVisible(true);
|
292 |
mapControl.getMapContext().beginAtomicEvent(); |
293 |
// checkProjection(all_layers[0], mapControl.getViewPort());
|
294 |
try {
|
295 |
try {
|
296 |
mapControl.getMapContext().getLayers().addLayer(layer); |
297 |
} catch (CancelationException e) {
|
298 |
return;
|
299 |
} |
300 |
|
301 |
if (mapControl.getViewPort().getExtent() == null) { |
302 |
try {
|
303 |
mapControl.getViewPort().setEnvelope( |
304 |
layer.getFullEnvelope()); |
305 |
} catch (ReadException e) {
|
306 |
NotificationManager.addError(e); |
307 |
return;
|
308 |
} |
309 |
} |
310 |
} finally {
|
311 |
|
312 |
mapControl.getMapContext().endAtomicEvent(); |
313 |
} |
314 |
return;
|
315 |
} |
316 |
} |
317 |
|
318 |
protected PrepareContext getPrepareDataStoreContext() {
|
319 |
if (this.prepareContext == null) { |
320 |
this.prepareContext = new PrepareContextView() { |
321 |
public Window getOwnerWindow() { |
322 |
return null; |
323 |
} |
324 |
|
325 |
public MapControl getMapControl() {
|
326 |
return WizardVectorialDB.this
|
327 |
.getMapCtrl(); |
328 |
} |
329 |
|
330 |
}; |
331 |
} |
332 |
return this.prepareContext; |
333 |
} |
334 |
|
335 |
protected DBStoreParameters getParameterForTable(TablesListItem table) {
|
336 |
DBStoreParameters parameters = table.getParameters(); |
337 |
|
338 |
String strEPSG = null; |
339 |
if (getMapCtrl() != null) { |
340 |
strEPSG = getMapCtrl().getViewPort().getProjection().getAbrev(); |
341 |
} |
342 |
|
343 |
UserTableSettingsVectorialPanel userTableSettingsPanel = (UserTableSettingsVectorialPanel) table |
344 |
.getUserTableSettingsPanel(); |
345 |
|
346 |
Envelope _wa = userTableSettingsPanel.getWorkingArea(); |
347 |
|
348 |
String geomField = userTableSettingsPanel.getGeoFieldName();
|
349 |
String fidField = userTableSettingsPanel.getIdFieldName();
|
350 |
if (!(fidField.startsWith("{") && fidField.endsWith("}"))) { |
351 |
parameters.setPkFields(new String[] { fidField }); |
352 |
fidField = null;
|
353 |
} |
354 |
String[] fields = table.getUserSelectedFieldsPanel() |
355 |
.getUserSelectedFields(fidField, geomField); |
356 |
|
357 |
if (userTableSettingsPanel.isSqlActive()) {
|
358 |
String whereClause = userTableSettingsPanel.getWhereClause();
|
359 |
parameters.setBaseFilter(whereClause); |
360 |
} else {
|
361 |
parameters.setBaseFilter("");
|
362 |
} |
363 |
|
364 |
parameters.setFields(fields); |
365 |
parameters.setDefaultGeometryField(geomField); |
366 |
if (_wa != null) { |
367 |
parameters.setWorkingArea(_wa); |
368 |
} |
369 |
|
370 |
parameters.setCRS(strEPSG); |
371 |
|
372 |
return parameters;
|
373 |
|
374 |
} |
375 |
|
376 |
|
377 |
} |