Revision 877 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/PostGISRasterWizard.java

View differences:

PostGISRasterWizard.java
20 20

  
21 21
import java.awt.BorderLayout;
22 22
import java.awt.Component;
23
import java.awt.Window;
23 24
import java.awt.event.ActionEvent;
24 25
import java.awt.event.ActionListener;
25 26
import java.sql.SQLException;
26 27
import java.util.ArrayList;
28
import java.util.Iterator;
29
import java.util.List;
30
import java.util.Map;
31
import java.util.Map.Entry;
27 32

  
28 33
import javax.swing.DefaultListModel;
29 34
import javax.swing.ImageIcon;
......
36 41
import javax.swing.event.ListSelectionEvent;
37 42
import javax.swing.event.ListSelectionListener;
38 43

  
39
import org.apache.log4j.Logger;
44
import org.apache.commons.collections.map.HashedMap;
45
import org.cresques.cts.IProjection;
40 46
import org.gvsig.andami.PluginServices;
47
import org.gvsig.andami.PluginsLocator;
48
import org.gvsig.andami.PluginsManager;
49
import org.gvsig.andami.messages.NotificationManager;
41 50
import org.gvsig.andami.ui.mdiManager.IWindow;
51
import org.gvsig.app.ApplicationLocator;
52
import org.gvsig.app.ApplicationManager;
42 53
import org.gvsig.app.gui.WizardPanel;
54
import org.gvsig.app.prepareAction.PrepareContext;
55
import org.gvsig.app.project.Project;
56
import org.gvsig.app.project.ProjectManager;
57
import org.gvsig.app.project.documents.table.TableDocument;
58
import org.gvsig.app.project.documents.table.TableManager;
43 59
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
60
import org.gvsig.fmap.dal.DALLocator;
61
import org.gvsig.fmap.dal.DataManager;
62
import org.gvsig.fmap.dal.feature.FeatureStore;
63
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorerParameters;
64
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
44 65
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
45 66
import org.gvsig.fmap.mapcontext.layers.FLayer;
46 67
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
47
import org.gvsig.geodb.vectorialdb.wizard.WizardVectorialDB;
68
import org.gvsig.geodb.ExtDB_Spatial;
69
import org.gvsig.geodb.vectorialdb.wizard.MyExplorer;
70
import org.gvsig.geodb.vectorialdb.wizard.UserSelectedFieldsPanel;
71
import org.gvsig.geodb.vectorialdb.wizard.UserTableSettingsPanel;
72
import org.gvsig.geodb.vectorialdb.wizard.VectorialDBConnectionParamsDialog;
73
import org.gvsig.tools.dynobject.DynObject;
48 74
import org.gvsig.utils.swing.JPasswordDlg;
75
import org.slf4j.Logger;
76
import org.slf4j.LoggerFactory;
49 77

  
50 78
/**
51
 * Wizard for adding a WKTRaster layer
79
 * Wizard for adding a PostGIS Raster layer
52 80
 *  
53 81
 * @author Nacho Brodin (nachobrodin@gmail.org)
54 82
 */
55 83
public class PostGISRasterWizard extends WizardPanel implements ActionListener, ListSelectionListener {
56
	private static final long serialVersionUID = 1L;
57 84

  
58
	private static Logger logger = Logger.getLogger(WizardVectorialDB.class.getName());
59
	protected IConnection conex = null;
60
	private ConnectionWithParams selectedDataSource = null;
61
	private JPanel namePanel = null;
62
	private JPanel tablesPanel = null;
63
	private JScrollPane tablesScrollPane = null;
64
	private AvailableTablesCheckBoxList tablesList = null;
65
	private JComboBox datasourceComboBox = null;
66
	private JButton dbButton = null;
67
	private AbstractViewPanel view = null;
85
    private static Logger               logger             = LoggerFactory.getLogger(PostGISRasterWizard.class.getName());
86
    private static final String         TAB_NAME           = "PostGIS Raster";
87
	private JPanel                      namePanel          = null;
88
	private JPanel                      tablesPanel        = null;
89
	private JScrollPane                 tablesScrollPane   = null;
90
	private AvailableTablesCheckBoxList tablesList         = null;
91
	private JComboBox                   datasourceComboBox = null;
92
	private JButton                     dbButton           = null;
93
	private AbstractViewPanel           view               = null;
94
	private PrepareContext              prepareDSContext   = null;
95
	private UserSelectedFieldsPanel     emptyFieldsPanel   = null;
68 96

  
69 97
	public PostGISRasterWizard() {
70 98
		super();
......
77 105
	 * @return void
78 106
	 */
79 107
	private void initialize() {
80
		setTabName(PluginServices.getText(this, "WKT Raster"));
108
		setTabName(PluginServices.getText(this, TAB_NAME));
81 109
		setLayout(null);
82 110
		setSize(512, 478);
83 111

  
......
94 122
		view = (AbstractViewPanel) iw;
95 123
		setMapCtrl(view.getMapControl());
96 124
		add(getNamePanel(), null);
97
		loadVectorialDBDatasourcesCombo();
125
		loadVectorialDBDatasourcesCombo(null);
98 126

  
99 127
		add(getTablesPanel(), null);
100 128
	}
101 129

  
102
	protected void loadVectorialDBDatasourcesCombo() {
103
		getDatasourceComboBox().removeAllItems();
130
	/**
131
	 * Loads the ComboBox for listing connections
132
	 * @param sel
133
	 */
134
    private void loadVectorialDBDatasourcesCombo(MyExplorer sel) {
135
        
136
        PluginsManager manager = PluginsLocator.getManager();
137
        DynObject values = manager.getPlugin(ExtDB_Spatial.class).getPluginProperties();
138
        Map connections = (Map) values.getDynValue("db_connections");
139
        if (connections != null){
140
            Iterator it = connections.entrySet().iterator();
141
            getDatasourceComboBox().removeAllItems();
142
            getDatasourceComboBox().addItem("");
143
            while (it.hasNext()){
144
                Map.Entry entry = (Entry) it.next();
145
                MyExplorer myExplorer = new MyExplorer();
146
                myExplorer.setDbExplorerParameters((DBServerExplorerParameters) entry.getValue());
147
                myExplorer.setName((String) entry.getKey());
148
                getDatasourceComboBox().addItem(myExplorer);
149
                if(sel!=null && sel.getName().equalsIgnoreCase(myExplorer.getName())){
150
                    getDatasourceComboBox().setSelectedItem(myExplorer);
151
                }
152
            }
153
        } else {
154
            connections = new HashedMap();
155
            values.setDynValue("db_connections", connections);
156
        }
157
	}
104 158

  
105
		getDatasourceComboBox().addItem(new ConnectionWithParams());
106 159

  
107
		ConnectionWithParams[] conn = SingleVectorialDBConnectionManager.instance()
108
		.getAllConnections();
160
	public void initWizard() {
161
	}
109 162

  
110
		if (conn == null) {
111
			return;
112
		}
163
	public void execute() {
164
		executeWizard();
165
	}
166
	
167
	public Object executeWizard() {
168
		TablesListItem[] tables = getSelectedTables();
113 169

  
114
		for (int i = 0; i < conn.length; i++) {
115
			getDatasourceComboBox().addItem(conn[i]);
170
		DataManager man = DALLocator.getDataManager();
171
		FeatureStore store;
172
		
173
		String docName;
174
		TableDocument document;
175
		Project project = ProjectManager.getInstance().getCurrentProject();
176

  
177
		ApplicationManager appGvSIGMan = ApplicationLocator.getManager();
178
		PrepareContext context = this.getPrepareDataStoreContext();
179
		DBStoreParameters storeParams;
180
        List<TableDocument> tabledocs =
181
            new ArrayList<TableDocument>(tables.length);
182
		for (TablesListItem table : tables) {
183
			storeParams = getParameterForTable(table);
184

  
185
			try {
186
				storeParams = (DBStoreParameters) appGvSIGMan
187
						.prepareOpenDataStoreParameters(storeParams, context);
188
			} catch (Exception e2) {
189
				NotificationManager.addError(e2);
190
				continue;
191
			}
192

  
193
			UserTableSettingsPanel userTableSettingsPanel = table
194
					.getUserTableSettingsPanel();
195

  
196
			docName = userTableSettingsPanel.getUserLayerName();
197
			try {
198
				store = (FeatureStore) man.openStore(storeParams.getDataStoreName(), storeParams);
199
			} catch (Exception e) {
200
				NotificationManager.addError(e);
201
                return null;
202
			}
203

  
204
			try {
205
				appGvSIGMan.pepareOpenDataSource(
206
						store, context);
207
			} catch (Exception e) {
208
				NotificationManager.addError(e);
209
				store.dispose();
210
                return null;
211
			}
212

  
213
			document = (TableDocument) ProjectManager.getInstance().createDocument(TableManager.TYPENAME, docName);
214
			document.setStore(store);
215
            // project.add(document);
216
            tabledocs.add(document);
116 217
		}
218
        return tabledocs;
117 219
	}
118

  
119
	/**
120
	 * Utility method to instantiate a IVectorialDataBaseDriver from its name.
121
	 *
122
	 * @param drvname driver's name
123
	 * @return a IVectorialDataBaseDriver
124
	 *
125
	 * @throws DriverLoadException
126
	 */
127
	public IVectorialDatabaseDriver getDriverFromName(String drvname)
128
	throws DriverLoadException {
129
		IVectorialDatabaseDriver resp =
130
			(IVectorialDatabaseDriver) LayerFactory.getDM().getDriver(drvname);
131
		return resp;
220
	
221
	protected PrepareContext getPrepareDataStoreContext() {
222
		if (this.prepareDSContext == null) {
223
			this.prepareDSContext = new PrepareContext() {
224
				public Window getOwnerWindow() {
225
					return null;
226
				}
227
				public IProjection getViewProjection() {
228
					return null;
229
				}
230
			};
231
		}
232
		return this.prepareDSContext;
132 233
	}
234
	
235
	protected DBStoreParameters getParameterForTable(TablesListItem table) {
236
		DBStoreParameters parameters = table.getParameters();
133 237

  
134
	public void initWizard() {
135
	}
238
		UserTableSettingsPanel userTableSettingsPanel = table.getUserTableSettingsPanel();
136 239

  
137
	public void execute() {
138
		SingleVectorialDBConnectionExtension.saveAllToPersistence();
240
		String fidField = userTableSettingsPanel.getIdFieldName();
241
		if (!(fidField.startsWith("{") && fidField.endsWith("}"))) {
242
			parameters.setPkFields(new String[] { fidField });
243
			fidField = null;
244
		}
245
		String[] fields = table.getUserSelectedFieldsPanel()
246
				.getUserSelectedFields(fidField, null);
247

  
248
		if (userTableSettingsPanel.isSqlActive()) {
249
			String whereClause = userTableSettingsPanel
250
					.getWhereClause();
251
			parameters.setBaseFilter(whereClause);
252
		} else {
253
			parameters.setBaseFilter("");
254
		}
255
		
256
		parameters.setFields(fields);
257
		return parameters;
139 258
	}
140 259

  
141 260
	/**
......
182 301
	 * Gets the list of selected tables
183 302
	 * @return
184 303
	 */
304
    @SuppressWarnings("unchecked")
185 305
	protected TablesListItem[] getSelectedTables() {
186 306
		int count = tablesList.getModel().getSize();
187
		ArrayList<TablesListItem> resp = new ArrayList<TablesListItem>();
307
		ArrayList resp = new ArrayList();
188 308

  
189 309
		for (int i = 0; i < count; i++) {
190 310
			TablesListItem item = (TablesListItem) tablesList.getModel()
191
			.getElementAt(i);
311
					.getElementAt(i);
192 312

  
193 313
			if (item.isSelected()) {
194 314
				resp.add(item);
......
296 416
	}
297 417

  
298 418
	public void actionPerformed(ActionEvent arg0) {
419
		if (datasourceComboBox.getItemCount() == 0) {
420
			setEmptyPanels();
421
		}
299 422
		Object src = arg0.getSource();
300 423

  
301
		if (src == getDatasourceComboBox()) {
302
			Object sel_obj = getDatasourceComboBox().getSelectedItem();
424
		if (src == datasourceComboBox) {
425
            Object selected = datasourceComboBox.getSelectedItem();
426
            if (selected instanceof MyExplorer) {
427
                MyExplorer sel_obj = (MyExplorer) selected;
303 428

  
304
			if (sel_obj == null) {
305
				return;
306
			}
429
                if (sel_obj == null) {
430
                    return;
431
                }
432
                getDatasourceComboBox().repaint();
433
                dbExplorerParameters = sel_obj.getDbSeverExplorerParameters();
434
            }
307 435

  
308
			if (!(sel_obj instanceof ConnectionWithParams)) {
309
				return;
310
			}
436
		} else if (src == dbButton) {
437
			MyExplorer sel = addNewConnection();
311 438

  
312
			selectedDataSource = (ConnectionWithParams) sel_obj;
439
			if (sel != null) {
440
				dbExplorerParameters = sel.getDbSeverExplorerParameters();
441
				loadVectorialDBDatasourcesCombo(sel);
442
				getDatasourceComboBox().setSelectedItem(sel);
313 443

  
314
			if (selectedDataSource.isNull()) {
315
				updateTableList(selectedDataSource);
316
				//setEmptyPanels();
317
				return;
318 444
			}
445
		}
446
		
447
		updateTableList(dbExplorerParameters);
448
	}
449
	
450
	private MyExplorer addNewConnection() {
451
		MyExplorer myExplorer = new MyExplorer();
452
		DBServerExplorerParameters resp = null;
319 453

  
320
			if (!selectedDataSource.isConnected()) {
321
				if (!tryToConnect(selectedDataSource)) {
322
					datasourceComboBox.setSelectedIndex(0);
323
					return;
324
				}
454
		VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
455
		newco.showDialog();
456

  
457
		if (newco.isOkPressed()) {
458
			try {
459
				resp = newco.getParameters();
460
			} catch (Exception e) {
461
				showConnectionErrorMessage(e.getMessage());
462
				return null;
325 463
			}
326

  
327
			getDatasourceComboBox().repaint();
328
			updateTableList(selectedDataSource);
329

  
330
			// setEmptyPanels();
331
			return;
464
			PluginsManager manager = PluginsLocator.getManager();
465
            DynObject values = manager.getPlugin(ExtDB_Spatial.class).getPluginProperties();
466
            Map connections = (Map) values.getDynValue("db_connections");
467
            if(connections == null){
468
                connections = new HashedMap();
469
                values.setDynValue("db_connections", connections);
470
            }
471
            connections.put(newco.getConnectionName(), resp);
472
            
473
//			SingleVectorialDBConnectionExtension.saveAllToPersistence();
474
			myExplorer.setDbExplorerParameters(resp);
475
			myExplorer.setName(newco.getConnectionName());
476
			return myExplorer;
477
		} else {
478
			return null;
332 479
		}
480
	}
481
	
482
	protected void setEmptyPanels() {
483
		removeFieldPanels();
484
		add(emptyFieldsPanel);
485
		fieldsPanel = emptyFieldsPanel;
333 486

  
334
		if (src == getJdbcButton()) {
335
			ConnectionWithParams sel = addNewConnection();
336

  
337
			if (sel != null) {
338
				loadVectorialDBDatasourcesCombo();
339
				getDatasourceComboBox().setSelectedItem(sel);
487
		repaint();
488
	}
489
	
490
	private void removeFieldPanels() {
491
		for (int i = 0; i < getComponentCount(); i++) {
492
			if (getComponent(i) instanceof UserSelectedFieldsPanel) {
493
				remove(i);
340 494
			}
341 495
		}
342 496
	}

Also available in: Unified diff