gvsig-raster / org.gvsig.raster.postgis / trunk / org.gvsig.raster.postgis / org.gvsig.raster.postgis.app / org.gvsig.raster.postgis.app.postgisrasterclient / src / main / java / org / gvsig / raster / postgis / app / postgisrasterclient / gui / AddPostGISRasterLayerWizard.java @ 938
History | View | Annotate | Download (15.2 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.postgis.app.postgisrasterclient.gui; |
23 |
|
24 |
import java.awt.event.ActionEvent; |
25 |
import java.awt.event.ActionListener; |
26 |
import java.util.Iterator; |
27 |
import java.util.List; |
28 |
import java.util.Map; |
29 |
import java.util.Map.Entry; |
30 |
|
31 |
import javax.swing.JComboBox; |
32 |
import javax.swing.JOptionPane; |
33 |
import javax.swing.JPanel; |
34 |
|
35 |
import org.apache.commons.collections.map.HashedMap; |
36 |
import org.gvsig.andami.PluginServices; |
37 |
import org.gvsig.andami.PluginsLocator; |
38 |
import org.gvsig.andami.PluginsManager; |
39 |
import org.gvsig.andami.messages.NotificationManager; |
40 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
41 |
import org.gvsig.app.gui.WizardPanel; |
42 |
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel; |
43 |
import org.gvsig.fmap.dal.DALLocator; |
44 |
import org.gvsig.fmap.dal.DataManager; |
45 |
import org.gvsig.fmap.dal.DataStore; |
46 |
import org.gvsig.fmap.dal.DataStoreParameters; |
47 |
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters; |
48 |
import org.gvsig.fmap.dal.exception.DataException; |
49 |
import org.gvsig.fmap.dal.exception.InitializeException; |
50 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
51 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
52 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
53 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
54 |
import org.gvsig.fmap.dal.feature.FeatureType; |
55 |
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorer; |
56 |
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorerParameters; |
57 |
import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
58 |
import org.gvsig.fmap.mapcontext.MapContext; |
59 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
60 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
61 |
import org.gvsig.geodb.ExtDB_Spatial; |
62 |
import org.gvsig.geodb.vectorialdb.wizard.MyExplorer; |
63 |
import org.gvsig.geodb.vectorialdb.wizard.VectorialDBConnectionParamsDialog; |
64 |
import org.gvsig.raster.fmap.layers.DefaultFLyrRaster; |
65 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
66 |
import org.gvsig.raster.postgis.io.PostGISRasterCoreException; |
67 |
import org.gvsig.raster.postgis.io.PostGISRasterServerExplorer; |
68 |
import org.gvsig.raster.postgis.io.PostGISRasterServerExplorerParameters; |
69 |
import org.gvsig.raster.postgis.swing.AddLayerUI; |
70 |
import org.gvsig.raster.postgis.swing.PostGISRasterSwingLocator; |
71 |
import org.gvsig.raster.util.RasterNotLoadException; |
72 |
import org.gvsig.tools.dynobject.DynObject; |
73 |
import org.slf4j.Logger; |
74 |
import org.slf4j.LoggerFactory; |
75 |
|
76 |
/**
|
77 |
* Wizard panel to add a new PostGIS raster layer
|
78 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
79 |
*/
|
80 |
public class AddPostGISRasterLayerWizard extends WizardPanel implements ActionListener { |
81 |
private static final long serialVersionUID = 1L; |
82 |
private static final String TAB_NAME = "PostGIS Raster"; |
83 |
private static Logger logger = LoggerFactory.getLogger(AddPostGISRasterLayerWizard.class.getName()); |
84 |
private AbstractViewPanel view = null; |
85 |
private AddLayerUI apiUI = null; |
86 |
private DBServerExplorerParameters dbExplorerParameters = null; |
87 |
|
88 |
public AddPostGISRasterLayerWizard() {
|
89 |
super();
|
90 |
initialize(); |
91 |
} |
92 |
|
93 |
/**
|
94 |
* This method initializes this
|
95 |
*
|
96 |
* @return void
|
97 |
*/
|
98 |
private void initialize() { |
99 |
setTabName(PluginServices.getText(this, TAB_NAME));
|
100 |
setLayout(null);
|
101 |
|
102 |
IWindow iw = PluginServices.getMDIManager().getActiveWindow(); |
103 |
|
104 |
if (iw == null) { |
105 |
return;
|
106 |
} |
107 |
|
108 |
if (!(iw instanceof AbstractViewPanel)) { |
109 |
return;
|
110 |
} |
111 |
|
112 |
apiUI = PostGISRasterSwingLocator.getSwingManager().createAddLayerUI(); |
113 |
apiUI.addListenerToBDSelectionCombo(this);
|
114 |
apiUI.addListenerToNewDBButton(this);
|
115 |
|
116 |
view = (AbstractViewPanel) iw; |
117 |
setMapCtrl(view.getMapControl()); |
118 |
|
119 |
JPanel mainPanel = (JPanel)apiUI.getComponent(); |
120 |
mainPanel.setBounds(new java.awt.Rectangle(0, 0, 501, 350)); |
121 |
add(mainPanel, null);
|
122 |
loadVectorialDBDatasourcesCombo(null);
|
123 |
} |
124 |
|
125 |
/**
|
126 |
* Loads the ComboBox for listing connections
|
127 |
* @param sel
|
128 |
*/
|
129 |
@SuppressWarnings("unchecked") |
130 |
private void loadVectorialDBDatasourcesCombo(MyExplorer sel) { |
131 |
JComboBox combo = apiUI.getDBCombo();
|
132 |
PluginsManager manager = PluginsLocator.getManager(); |
133 |
DynObject values = manager.getPlugin(ExtDB_Spatial.class).getPluginProperties(); |
134 |
Map connections = (Map) values.getDynValue("db_connections"); |
135 |
if (connections != null){ |
136 |
Iterator it = connections.entrySet().iterator();
|
137 |
combo.removeAllItems(); |
138 |
combo.addItem("");
|
139 |
while (it.hasNext()){
|
140 |
Map.Entry entry = (Entry) it.next();
|
141 |
MyExplorer myExplorer = new MyExplorer();
|
142 |
myExplorer.setDbExplorerParameters((DBServerExplorerParameters) entry.getValue()); |
143 |
myExplorer.setName((String) entry.getKey());
|
144 |
combo.addItem(myExplorer); |
145 |
if(sel!=null && sel.getName().equalsIgnoreCase(myExplorer.getName())){ |
146 |
combo.setSelectedItem(myExplorer); |
147 |
} |
148 |
} |
149 |
} else {
|
150 |
connections = new HashedMap();
|
151 |
values.setDynValue("db_connections", connections);
|
152 |
} |
153 |
} |
154 |
|
155 |
/**
|
156 |
* Return FLayers if user performs multi selection.
|
157 |
*/
|
158 |
public FLayer getLayer() {
|
159 |
|
160 |
String host = dbExplorerParameters.getHost();
|
161 |
if(host.compareTo("localhost") == 0) |
162 |
host = "127.0.0.1";
|
163 |
Integer port = dbExplorerParameters.getPort();
|
164 |
String dbname = dbExplorerParameters.getDBName();
|
165 |
String user = dbExplorerParameters.getUser();
|
166 |
String password = dbExplorerParameters.getPassword();
|
167 |
String table = null; |
168 |
String schema = null; |
169 |
|
170 |
List<Object> tables = apiUI.getSelectedTables(); |
171 |
TableListItem item = (TableListItem)tables.get(0);
|
172 |
schema = item.getSchema(); |
173 |
table = item.getTableName(); |
174 |
|
175 |
String connString = null; |
176 |
if(schema == null) |
177 |
connString = "PG:host='" + host + "' port='" + port + "' dbname='" + dbname + "' user='" + user + "' password='" + password + "' table='" + table + "'"; |
178 |
else
|
179 |
connString = "PG:host='" + host + "' port='" + port + "' dbname='" + dbname + "' user='" + user + "' password='" + password + "' schema='" + schema + "' table='" + table + "'"; |
180 |
|
181 |
//connString += " column='rast' where='rid = 1041'";
|
182 |
try {
|
183 |
return createLayer(getFormatedRasterFileName(connString), connString, item);
|
184 |
} catch (LoadLayerException e) {
|
185 |
logger.error("Error creating layer: " + e.getMessage(), e);
|
186 |
showConnectionErrorMessage(PluginServices.getText(this, "error_creating_layer")); |
187 |
return null; |
188 |
} |
189 |
} |
190 |
|
191 |
/**
|
192 |
* Builds a new raster layer
|
193 |
* @param fileName
|
194 |
* @return
|
195 |
* @throws RasterNotLoadException
|
196 |
* @throws LoadLayerException
|
197 |
*/
|
198 |
@SuppressWarnings("deprecation") |
199 |
public FLyrRaster createLayer(String layerName, String conn, TableListItem item) throws LoadLayerException { |
200 |
DataManager dataManager = DALLocator.getDataManager(); |
201 |
DataStore dataStore = null;
|
202 |
try {
|
203 |
PostGISRasterServerExplorerParameters explorerParams = (PostGISRasterServerExplorerParameters) dataManager.createServerExplorerParameters(PostGISRasterServerExplorer.NAME); |
204 |
explorerParams.setHost(conn); |
205 |
PostGISRasterServerExplorer explorer = (PostGISRasterServerExplorer) dataManager.createServerExplorer(explorerParams); |
206 |
RasterDataParameters storeParameters = (RasterDataParameters)explorer.getStoreParameters(); |
207 |
storeParameters.setURI(conn); |
208 |
storeParameters.setDynValue("DBPARAMS", item.getDBStoreParameters());
|
209 |
storeParameters.setDynValue("DBEXPLORER", item.getExplorer());
|
210 |
dataStore = dataManager.createStore(storeParameters); |
211 |
} catch (ValidateDataParametersException e) {
|
212 |
throw new LoadLayerException("Error validating parameters", e); |
213 |
} catch (InitializeException e) {
|
214 |
throw new LoadLayerException("Error initializating layer.", e); |
215 |
} catch (ProviderNotRegisteredException e) {
|
216 |
throw new LoadLayerException("Provider not registered.", e); |
217 |
} catch (PostGISRasterCoreException e) {
|
218 |
throw new LoadLayerException("", e); |
219 |
} |
220 |
|
221 |
DefaultFLyrRaster lyr = new DefaultFLyrRaster();
|
222 |
lyr.setName(layerName); |
223 |
lyr.setDataStore(dataStore); |
224 |
return lyr;
|
225 |
} |
226 |
|
227 |
/**
|
228 |
* Gets the name of a raster file
|
229 |
*/
|
230 |
private String getFormatedRasterFileName(String name) { |
231 |
String newName = ""; |
232 |
String schema = null; |
233 |
String table = null; |
234 |
int index = name.indexOf(" schema='") + 8; |
235 |
if(index != -1) { |
236 |
try {
|
237 |
schema = name.substring(index + 1, name.indexOf("'", index + 1)); |
238 |
newName += schema + ".";
|
239 |
} catch (StringIndexOutOfBoundsException e) { |
240 |
} |
241 |
} |
242 |
index = name.indexOf(" table='") + 7; |
243 |
if(index != -1) { |
244 |
try {
|
245 |
table = name.substring(index + 1, name.indexOf("'", index + 1)); |
246 |
newName += table; |
247 |
} catch (StringIndexOutOfBoundsException e) { |
248 |
} |
249 |
} |
250 |
return newName;
|
251 |
} |
252 |
|
253 |
@Override
|
254 |
public void close() { |
255 |
|
256 |
} |
257 |
|
258 |
@Override
|
259 |
public void execute() { |
260 |
FLayer lyr = getLayer(); |
261 |
MapContext mapContext = null;
|
262 |
IWindow[] w = PluginServices.getMDIManager().getAllWindows();
|
263 |
for (int i = 0; i < w.length; i++) { |
264 |
if (w[i] instanceof org.gvsig.app.project.documents.view.gui.AbstractViewPanel) { |
265 |
mapContext = ((org.gvsig.app.project.documents.view.gui.AbstractViewPanel)w[i]).getMapControl().getMapContext(); |
266 |
} |
267 |
} |
268 |
|
269 |
mapContext.beginAtomicEvent(); |
270 |
lyr.setVisible(true);
|
271 |
mapContext.getLayers().addLayer(lyr); |
272 |
mapContext.callLegendChanged(); |
273 |
mapContext.endAtomicEvent(); |
274 |
mapContext.invalidate(); |
275 |
} |
276 |
|
277 |
@Override
|
278 |
public DataStoreParameters[] getParameters() { |
279 |
return null; |
280 |
} |
281 |
|
282 |
@Override
|
283 |
public void initWizard() { |
284 |
|
285 |
} |
286 |
|
287 |
/**
|
288 |
* Updates the JList component with the list of tables of
|
289 |
* the selected database.
|
290 |
*
|
291 |
* @param dbSeverExplorerParameters2
|
292 |
*/
|
293 |
@SuppressWarnings("unchecked") |
294 |
private void updateTableList( |
295 |
DBServerExplorerParameters dbSeverExplorerParameters2) { |
296 |
if (dbSeverExplorerParameters2 == null) { |
297 |
return;
|
298 |
} |
299 |
DataManager dm = DALLocator.getDataManager(); |
300 |
DBServerExplorer dbExplorer = null;
|
301 |
try {
|
302 |
dbExplorer = (DBServerExplorer) dm.openServerExplorer(dbSeverExplorerParameters2.getExplorerName(), dbSeverExplorerParameters2); |
303 |
|
304 |
List parameters = dbExplorer.list();
|
305 |
|
306 |
Iterator iter = parameters.iterator();
|
307 |
DBStoreParameters param = null;
|
308 |
apiUI.clearList(); |
309 |
while (iter.hasNext()) {
|
310 |
param = (DBStoreParameters) iter.next(); |
311 |
|
312 |
//Criterios para a?adir la tabla a la lista
|
313 |
try {
|
314 |
if(!param.getTable().startsWith("o_") && isRasterTable(param)) { |
315 |
apiUI.addElementToTable(new TableListItem(dbExplorer, param));
|
316 |
} |
317 |
} catch (PostGISRasterCoreException e) {
|
318 |
logger.error("While getting table names: " + e.getMessage(), e);
|
319 |
} |
320 |
} |
321 |
|
322 |
} catch (Exception e) { |
323 |
logger.error("While getting table names: " + e.getMessage(), e);
|
324 |
showConnectionErrorMessage(PluginServices.getText(this, "error_getting_tables")); |
325 |
return;
|
326 |
} |
327 |
} |
328 |
|
329 |
/**
|
330 |
* Checks is the table has the structure to contain a raster
|
331 |
* @param param
|
332 |
* @return
|
333 |
* @throws PostGISRasterCoreException
|
334 |
*/
|
335 |
@SuppressWarnings({ "deprecation" }) |
336 |
private boolean isRasterTable(DBStoreParameters param) throws PostGISRasterCoreException { |
337 |
DataManager dm = DALLocator.getDataManager(); |
338 |
FeatureStore store; |
339 |
try {
|
340 |
store = (FeatureStore)dm.createStore(param); |
341 |
|
342 |
FeatureType featureType = store.getDefaultFeatureType(); |
343 |
if(featureType.size() == 2) { |
344 |
if(existsAttr(featureType, "rid") && existsAttr(featureType, "rast")) |
345 |
return true; |
346 |
} |
347 |
store.dispose(); |
348 |
} catch (ValidateDataParametersException e) {
|
349 |
throw new PostGISRasterCoreException(e); |
350 |
} catch (InitializeException e) {
|
351 |
throw new PostGISRasterCoreException(e); |
352 |
} catch (ProviderNotRegisteredException e) {
|
353 |
throw new PostGISRasterCoreException(e); |
354 |
} catch (DataException e) {
|
355 |
throw new PostGISRasterCoreException(e); |
356 |
} |
357 |
return false; |
358 |
} |
359 |
|
360 |
@SuppressWarnings("unchecked") |
361 |
private boolean existsAttr(FeatureType featureType, String attr) { |
362 |
Iterator it = featureType.iterator();
|
363 |
while( it.hasNext() ) {
|
364 |
FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor)it.next(); |
365 |
if(attribute.getName().compareTo(attr) == 0) { |
366 |
return true; |
367 |
} |
368 |
} |
369 |
return false; |
370 |
} |
371 |
|
372 |
/*
|
373 |
* (non-Javadoc)
|
374 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
|
375 |
*/
|
376 |
public void actionPerformed(ActionEvent e) { |
377 |
if (apiUI.isAddDBEventObject(e.getSource())) {
|
378 |
MyExplorer sel = addNewConnection(); |
379 |
|
380 |
if (sel != null) { |
381 |
dbExplorerParameters = sel.getDbSeverExplorerParameters(); |
382 |
loadVectorialDBDatasourcesCombo(sel); |
383 |
apiUI.getDBCombo().setSelectedItem(sel); |
384 |
} |
385 |
} |
386 |
|
387 |
if(e.getSource() == apiUI.getDBCombo()) {
|
388 |
JComboBox dbCombo = apiUI.getDBCombo();
|
389 |
if(dbCombo.getItemCount() == 0) { |
390 |
apiUI.clearList(); |
391 |
} else {
|
392 |
Object selected = dbCombo.getSelectedItem();
|
393 |
if(selected == null) |
394 |
return;
|
395 |
if (selected instanceof MyExplorer) { |
396 |
MyExplorer sel_obj = (MyExplorer) selected; |
397 |
dbCombo.repaint(); |
398 |
dbExplorerParameters = sel_obj.getDbSeverExplorerParameters(); |
399 |
updateTableList(dbExplorerParameters); |
400 |
} |
401 |
} |
402 |
} |
403 |
} |
404 |
|
405 |
/**
|
406 |
* Adds a new connection using the generic dialog. This function
|
407 |
* is used when the button add new connection is pressed by the user
|
408 |
* @return
|
409 |
*/
|
410 |
@SuppressWarnings("unchecked") |
411 |
private MyExplorer addNewConnection() {
|
412 |
MyExplorer myExplorer = new MyExplorer();
|
413 |
DBServerExplorerParameters resp = null;
|
414 |
|
415 |
VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
|
416 |
newco.showDialog(); |
417 |
|
418 |
if (newco.isOkPressed()) {
|
419 |
try {
|
420 |
resp = newco.getParameters(); |
421 |
} catch (Exception e) { |
422 |
showConnectionErrorMessage(e.getMessage()); |
423 |
return null; |
424 |
} |
425 |
PluginsManager manager = PluginsLocator.getManager(); |
426 |
DynObject values = manager.getPlugin(ExtDB_Spatial.class).getPluginProperties(); |
427 |
Map connections = (Map) values.getDynValue("db_connections"); |
428 |
if(connections == null){ |
429 |
connections = new HashedMap();
|
430 |
values.setDynValue("db_connections", connections);
|
431 |
} |
432 |
connections.put(newco.getConnectionName(), resp); |
433 |
|
434 |
myExplorer.setDbExplorerParameters(resp); |
435 |
myExplorer.setName(newco.getConnectionName()); |
436 |
return myExplorer;
|
437 |
} else {
|
438 |
return null; |
439 |
} |
440 |
} |
441 |
|
442 |
/**
|
443 |
* Shows an error
|
444 |
* @param _msg
|
445 |
*/
|
446 |
private void showConnectionErrorMessage(String _msg) { |
447 |
String msg = (_msg.length() > 300) ? "" : (": " + _msg); |
448 |
String title = PluginServices.getText(this, "connection_error"); |
449 |
JOptionPane.showMessageDialog(apiUI.getComponent(), title + msg, title,
|
450 |
JOptionPane.ERROR_MESSAGE);
|
451 |
} |
452 |
|
453 |
} |