svn-gvsig-desktop / tags / v2_0_0_Build_2059 / extensions / extGeoDB / src / org / gvsig / geodb / vectorialdb / wizard / WizardVectorialDB.java @ 39298
History | View | Annotate | Download (13.3 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.slf4j.Logger; |
52 |
import org.slf4j.LoggerFactory; |
53 |
|
54 |
import org.gvsig.andami.PluginServices; |
55 |
import org.gvsig.andami.messages.NotificationManager; |
56 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
57 |
import org.gvsig.app.ApplicationLocator; |
58 |
import org.gvsig.app.ApplicationManager; |
59 |
import org.gvsig.app.prepareAction.PrepareContext; |
60 |
import org.gvsig.app.prepareAction.PrepareContextView; |
61 |
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
62 |
import org.gvsig.fmap.dal.DALLocator; |
63 |
import org.gvsig.fmap.dal.DataManager; |
64 |
import org.gvsig.fmap.dal.DataStoreParameters; |
65 |
import org.gvsig.fmap.dal.exception.DataException; |
66 |
import org.gvsig.fmap.dal.exception.ReadException; |
67 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
68 |
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorer; |
69 |
import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
70 |
import org.gvsig.fmap.geom.primitive.Envelope; |
71 |
import org.gvsig.fmap.mapcontext.MapContext; |
72 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
73 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
74 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
75 |
import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
76 |
import org.gvsig.fmap.mapcontrol.MapControl; |
77 |
|
78 |
|
79 |
|
80 |
/**
|
81 |
* Driver-independent GeoDB wizard. Queries the drivers to fill GUI controls.
|
82 |
* Multi-table selection available.
|
83 |
*
|
84 |
* @author jldominguez
|
85 |
*
|
86 |
*/
|
87 |
public class WizardVectorialDB extends WizardDB { |
88 |
private static final long serialVersionUID = -5002685263220038989L; |
89 |
|
90 |
private static Logger logger = LoggerFactory |
91 |
.getLogger(WizardVectorialDB.class.getName()); |
92 |
|
93 |
private static final String GEODB_WIZARD_TAB_NAME = "GeoDB"; |
94 |
private DefaultViewPanel view = null; |
95 |
|
96 |
private PrepareContextView prepareContext;
|
97 |
|
98 |
/**
|
99 |
* This method initializes this
|
100 |
*
|
101 |
* @return void
|
102 |
*/
|
103 |
protected void initialize() { |
104 |
super.initialize();
|
105 |
setTabName(GEODB_WIZARD_TAB_NAME); |
106 |
setLayout(null);
|
107 |
setSize(512, 478); |
108 |
|
109 |
IWindow iw = PluginServices.getMDIManager().getActiveWindow(); |
110 |
|
111 |
if (iw == null) { |
112 |
return;
|
113 |
} |
114 |
|
115 |
if ((iw instanceof DefaultViewPanel)) { |
116 |
view = (DefaultViewPanel) iw; |
117 |
setMapCtrl(view.getMapControl()); |
118 |
} |
119 |
} |
120 |
|
121 |
|
122 |
|
123 |
protected List getTableList(DBServerExplorer explorer) throws DataException { |
124 |
return explorer.list(DBServerExplorer.MODE_GEOMETRY);
|
125 |
} |
126 |
|
127 |
|
128 |
protected TablesListItem createTabeListItem(DBServerExplorer dbExplorer,
|
129 |
DBStoreParameters param) { |
130 |
return new TablesListItemVectorial(dbExplorer, param, getMapCtrl(), |
131 |
this);
|
132 |
} |
133 |
|
134 |
public void setSettingsPanels(TablesListItem actTable) { |
135 |
super.setSettingsPanels(actTable);
|
136 |
} |
137 |
|
138 |
protected UserTableSettingsPanel createSettingsPanel(
|
139 |
TablesListItem actTable) { |
140 |
if (actTable == null) { |
141 |
return new UserTableSettingsVectorialPanel(null, null, "", |
142 |
getMapCtrl(), true, this, null, null); |
143 |
} |
144 |
String abrev = null; |
145 |
if (getMapCtrl() != null) { |
146 |
abrev = getMapCtrl().getViewPort().getProjection().getAbrev(); |
147 |
} |
148 |
|
149 |
return ((TablesListItemVectorial) actTable)
|
150 |
.getUserTableSettingsPanel(abrev); |
151 |
} |
152 |
|
153 |
|
154 |
public DataStoreParameters[] getParameters() { |
155 |
try {
|
156 |
TablesListItem[] selected = getSelectedTables();
|
157 |
int count = selected.length;
|
158 |
DBStoreParameters[] dbParameters = new DBStoreParameters[count]; |
159 |
String strEPSG = null; |
160 |
if (getMapCtrl() != null) { |
161 |
strEPSG = getMapCtrl().getViewPort().getProjection().getAbrev(); |
162 |
} |
163 |
|
164 |
for (int i = 0; i < count; i++) { |
165 |
TablesListItemVectorial item = (TablesListItemVectorial) selected[i]; |
166 |
|
167 |
dbParameters[i] = getParameterForTable(item); |
168 |
} |
169 |
|
170 |
return dbParameters;// layerArrayToGroup(all_layers, groupName); |
171 |
} catch (Exception e) { |
172 |
logger.error("While creating jdbc layer: " + e.getMessage(), e);
|
173 |
NotificationManager.addError("Error al cargar la capa: "
|
174 |
+ e.getMessage(), e); |
175 |
} |
176 |
|
177 |
return null; |
178 |
} |
179 |
|
180 |
public void execute() { |
181 |
MapControl mapControl = this.getMapCtrl();
|
182 |
IProjection proj = null;
|
183 |
TablesListItem[] tables = getSelectedTables();
|
184 |
|
185 |
|
186 |
List all_layers = new ArrayList(); |
187 |
String strEPSG = mapControl.getViewPort().getProjection().getAbrev();
|
188 |
LayerFactory layerFactory = LayerFactory.getInstance(); |
189 |
String groupName = null; |
190 |
Envelope env = null;
|
191 |
DBStoreParameters parameter; |
192 |
TablesListItem table; |
193 |
FeatureStore store, storeToAdd; |
194 |
|
195 |
DataManager man = DALLocator.getDataManager(); |
196 |
|
197 |
ApplicationManager appGvSIGMan = ApplicationLocator.getManager(); |
198 |
PrepareContext context = this.getPrepareDataStoreContext();
|
199 |
|
200 |
|
201 |
try {
|
202 |
|
203 |
FLayer layer; |
204 |
for (int i = 0; i < tables.length; i++) { |
205 |
table = tables[i]; |
206 |
UserTableSettingsVectorialPanel userTableSettingsPanel = |
207 |
(UserTableSettingsVectorialPanel) table |
208 |
.getUserTableSettingsPanel(); |
209 |
parameter = getParameterForTable(table); |
210 |
if (i == 0) { |
211 |
groupName = |
212 |
parameter.getDBName() + " (" + parameter.getHost()
|
213 |
+ ")";
|
214 |
} |
215 |
try {
|
216 |
parameter = |
217 |
(DBStoreParameters) appGvSIGMan |
218 |
.prepareOpenDataStoreParameters(parameter, context); |
219 |
|
220 |
} catch (Exception e2) { |
221 |
NotificationManager.addError(e2); |
222 |
continue;
|
223 |
} |
224 |
|
225 |
try {
|
226 |
store = (FeatureStore) man.createStore(parameter); |
227 |
} catch (Exception e) { |
228 |
NotificationManager.addError(e); |
229 |
return;
|
230 |
} |
231 |
|
232 |
try {
|
233 |
storeToAdd = |
234 |
(FeatureStore) appGvSIGMan.pepareOpenDataSource(store, |
235 |
context); |
236 |
} catch (Exception e) { |
237 |
NotificationManager.addError(e); |
238 |
store.dispose(); |
239 |
continue;
|
240 |
} |
241 |
|
242 |
try {
|
243 |
|
244 |
layer = |
245 |
layerFactory.createLayer( |
246 |
userTableSettingsPanel.getUserLayerName(), |
247 |
storeToAdd); |
248 |
all_layers.add(layer); |
249 |
if (env == null) { |
250 |
env = layer.getFullEnvelope(); |
251 |
} else {
|
252 |
env.add(layer.getFullEnvelope()); |
253 |
} |
254 |
storeToAdd.dispose(); |
255 |
} catch (Exception e) { |
256 |
storeToAdd.dispose(); |
257 |
NotificationManager.addError(e); |
258 |
} |
259 |
} |
260 |
|
261 |
MapContext mc = mapControl.getMapContext(); |
262 |
FLayers root = mapControl.getMapContext().getLayers(); |
263 |
if (all_layers.size() > 1) { |
264 |
FLayers group = new FLayers();// (mc,root); |
265 |
group.setMapContext(mc); |
266 |
group.setParentLayer(root); |
267 |
group.setName(groupName); |
268 |
|
269 |
Iterator iter = all_layers.iterator();
|
270 |
while (iter.hasNext()) {
|
271 |
group.addLayer((FLayer) iter.next()); |
272 |
} |
273 |
|
274 |
if ((group != null) && !(group.isOk())) { |
275 |
// if the layer is not okay (it has errors) process them
|
276 |
processErrorsOfLayer(group, mapControl); |
277 |
} |
278 |
|
279 |
if (group != null) { |
280 |
group.setVisible(true);
|
281 |
mapControl.getMapContext().beginAtomicEvent(); |
282 |
try {
|
283 |
// checkProjection(group, mapControl.getViewPort());
|
284 |
try {
|
285 |
mapControl.getMapContext().getLayers() |
286 |
.addLayer(group); |
287 |
group.dispose(); |
288 |
} catch (CancelationException e) {
|
289 |
// TODO Auto-generated catch block
|
290 |
e.printStackTrace(); |
291 |
} |
292 |
if (mapControl.getViewPort().getExtent() == null) { |
293 |
mapControl.getViewPort().setEnvelope(env); |
294 |
} |
295 |
} finally {
|
296 |
mapControl.getMapContext().endAtomicEvent(); |
297 |
} |
298 |
return;
|
299 |
} |
300 |
} else
|
301 |
if (all_layers.size() == 1) { |
302 |
layer = (FLayer) all_layers.get(0);
|
303 |
if (!(layer.isOk())) {
|
304 |
// if the layer is not okay (it has errors) process them
|
305 |
processErrorsOfLayer(layer, mapControl); |
306 |
} |
307 |
|
308 |
layer.setVisible(true);
|
309 |
mapControl.getMapContext().beginAtomicEvent(); |
310 |
// checkProjection(all_layers[0], mapControl.getViewPort());
|
311 |
try {
|
312 |
try {
|
313 |
mapControl.getMapContext().getLayers() |
314 |
.addLayer(layer); |
315 |
} catch (CancelationException e) {
|
316 |
return;
|
317 |
} |
318 |
|
319 |
if (mapControl.getViewPort().getExtent() == null) { |
320 |
try {
|
321 |
mapControl.getViewPort().setEnvelope( |
322 |
layer.getFullEnvelope()); |
323 |
} catch (ReadException e) {
|
324 |
NotificationManager.addError(e); |
325 |
return;
|
326 |
} |
327 |
} |
328 |
} finally {
|
329 |
|
330 |
mapControl.getMapContext().endAtomicEvent(); |
331 |
} |
332 |
return;
|
333 |
} |
334 |
} finally {
|
335 |
// Dispose all created layers. If they have been included into
|
336 |
// a FLayers object, they will have been binded there.
|
337 |
for (int i = 0; i < all_layers.size(); i++) { |
338 |
FLayer layer = (FLayer) all_layers.get(i); |
339 |
layer.dispose(); |
340 |
} |
341 |
} |
342 |
} |
343 |
|
344 |
protected PrepareContext getPrepareDataStoreContext() {
|
345 |
if (this.prepareContext == null) { |
346 |
this.prepareContext = new PrepareContextView() { |
347 |
public Window getOwnerWindow() { |
348 |
return null; |
349 |
} |
350 |
|
351 |
public MapControl getMapControl() {
|
352 |
return WizardVectorialDB.this
|
353 |
.getMapCtrl(); |
354 |
} |
355 |
|
356 |
public IProjection getViewProjection() {
|
357 |
MapControl mapControl = getMapControl(); |
358 |
if (mapControl!=null){ |
359 |
return mapControl.getProjection();
|
360 |
} |
361 |
return null; |
362 |
} |
363 |
|
364 |
}; |
365 |
} |
366 |
return this.prepareContext; |
367 |
} |
368 |
|
369 |
protected DBStoreParameters getParameterForTable(TablesListItem table) {
|
370 |
DBStoreParameters parameters = table.getParameters(); |
371 |
|
372 |
UserTableSettingsVectorialPanel userTableSettingsPanel = (UserTableSettingsVectorialPanel) table |
373 |
.getUserTableSettingsPanel(); |
374 |
|
375 |
Envelope _wa = userTableSettingsPanel.getWorkingArea(); |
376 |
|
377 |
String geomField = userTableSettingsPanel.getGeoFieldName();
|
378 |
String fidField = userTableSettingsPanel.getIdFieldName();
|
379 |
//IF is a multiple PK, remove the {} symbols
|
380 |
if (fidField.startsWith("{") && fidField.endsWith("}")) { |
381 |
fidField = fidField.substring(1, fidField.length()-1); |
382 |
} |
383 |
String[] pkFields = fidField.split(","); |
384 |
|
385 |
String[] fields = table.getUserSelectedFieldsPanel() |
386 |
.getUserSelectedFields(pkFields, geomField); |
387 |
|
388 |
if (userTableSettingsPanel.isSqlActive()) {
|
389 |
String whereClause = userTableSettingsPanel.getWhereClause();
|
390 |
parameters.setBaseFilter(whereClause); |
391 |
} else {
|
392 |
parameters.setBaseFilter("");
|
393 |
} |
394 |
|
395 |
parameters.setFields(fields); |
396 |
parameters.setDefaultGeometryField(geomField); |
397 |
if (_wa != null) { |
398 |
parameters.setWorkingArea(_wa); |
399 |
} |
400 |
|
401 |
IProjection proj_selected_by_user = userTableSettingsPanel.getProjection(); |
402 |
parameters.setCRS(proj_selected_by_user); |
403 |
|
404 |
return parameters;
|
405 |
|
406 |
} |
407 |
|
408 |
|
409 |
} |