root / trunk / extensions / extSDE / src / com / iver / cit / gvsig / sde / gui / sdewizard2 / WizardSDE.java @ 11193
History | View | Annotate | Download (24.9 KB)
1 | 11193 | caballero | /* 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 com.iver.cit.gvsig.sde.gui.sdewizard2; |
||
44 | |||
45 | import java.awt.BorderLayout; |
||
46 | import java.awt.event.ActionEvent; |
||
47 | import java.awt.event.ActionListener; |
||
48 | import java.awt.geom.Rectangle2D; |
||
49 | import java.sql.SQLException; |
||
50 | import java.util.ArrayList; |
||
51 | import java.util.TreeMap; |
||
52 | import java.util.Vector; |
||
53 | |||
54 | import javax.swing.DefaultListModel; |
||
55 | import javax.swing.ImageIcon; |
||
56 | import javax.swing.JComboBox; |
||
57 | import javax.swing.JOptionPane; |
||
58 | import javax.swing.JPanel; |
||
59 | import javax.swing.JScrollPane; |
||
60 | import javax.swing.ListSelectionModel; |
||
61 | import javax.swing.event.ListSelectionEvent; |
||
62 | import javax.swing.event.ListSelectionListener; |
||
63 | |||
64 | import org.apache.log4j.Logger; |
||
65 | import org.cresques.cts.IProjection; |
||
66 | import org.gvsig.gui.beans.swing.JButton; |
||
67 | |||
68 | import com.esri.sde.sdk.client.SeConnection; |
||
69 | import com.esri.sde.sdk.client.SeException; |
||
70 | import com.esri.sde.sdk.client.SeLayer; |
||
71 | import com.hardcode.driverManager.DriverLoadException; |
||
72 | import com.iver.andami.PluginServices; |
||
73 | import com.iver.andami.messages.NotificationManager; |
||
74 | import com.iver.andami.ui.mdiManager.IWindow; |
||
75 | import com.iver.cit.gvsig.fmap.MapContext; |
||
76 | import com.iver.cit.gvsig.fmap.core.ICanReproject; |
||
77 | import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
||
78 | import com.iver.cit.gvsig.fmap.drivers.sde.ArcSDELayerDefinition; |
||
79 | import com.iver.cit.gvsig.fmap.drivers.sde.VectorialSDEDriver; |
||
80 | import com.iver.cit.gvsig.fmap.drivers.sde.VectorialSDELayerDefinition; |
||
81 | import com.iver.cit.gvsig.fmap.layers.FLayer; |
||
82 | import com.iver.cit.gvsig.fmap.layers.FLayers; |
||
83 | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
||
84 | import com.iver.cit.gvsig.gui.WizardPanel; |
||
85 | import com.iver.cit.gvsig.project.documents.view.gui.View; |
||
86 | import com.iver.utiles.swing.JPasswordDlg; |
||
87 | |||
88 | |||
89 | /**
|
||
90 | * Driver-independent GeoBD wizard. Queries the drivers to fill GUI controls.
|
||
91 | * Multi-table selection available.
|
||
92 | *
|
||
93 | * @author jldominguez
|
||
94 | *
|
||
95 | */
|
||
96 | public class WizardSDE extends WizardPanel implements ActionListener, |
||
97 | ListSelectionListener {
|
||
98 | private static Logger logger = Logger.getLogger(WizardSDE.class.getName()); |
||
99 | |||
100 | private SeConnection conex = null; |
||
101 | private ConnectionWithParamsSDE selectedDataSource = null; |
||
102 | private JPanel namePanel = null; |
||
103 | private JPanel tablesPanel = null; |
||
104 | private JScrollPane tablesScrollPane = null; |
||
105 | private AvailableTablesCheckBoxList tablesList = null; |
||
106 | private JComboBox datasourceComboBox = null; |
||
107 | private UserTableSettingsPanel settingsPanel = null; |
||
108 | private UserSelectedFieldsPanel fieldsPanel = null; |
||
109 | private UserTableSettingsPanel emptySettingsPanel = null; |
||
110 | private UserSelectedFieldsPanel emptyFieldsPanel = null; |
||
111 | private JButton jdbcButton = null; |
||
112 | private View view = null; |
||
113 | |||
114 | public WizardSDE() {
|
||
115 | super();
|
||
116 | initialize(); |
||
117 | } |
||
118 | |||
119 | /**
|
||
120 | * This method initializes this
|
||
121 | *
|
||
122 | * @return void
|
||
123 | */
|
||
124 | private void initialize() { |
||
125 | setTabName("SDE");
|
||
126 | setLayout(null);
|
||
127 | setSize(512, 478); |
||
128 | |||
129 | IWindow iw = PluginServices.getMDIManager().getActiveWindow(); |
||
130 | |||
131 | if (iw == null) { |
||
132 | return;
|
||
133 | } |
||
134 | |||
135 | if (!(iw instanceof View)) { |
||
136 | return;
|
||
137 | } |
||
138 | |||
139 | view = (View) iw;
|
||
140 | setMapCtrl(view.getMapControl()); |
||
141 | |||
142 | emptySettingsPanel = new UserTableSettingsPanel(null, null, "", |
||
143 | getMapCtrl(), true, this); |
||
144 | emptyFieldsPanel = new UserSelectedFieldsPanel(null, null, true, this); |
||
145 | |||
146 | add(getNamePanel(), null);
|
||
147 | loadJdbcDatasourcesCombo(); |
||
148 | |||
149 | add(getTablesPanel(), null);
|
||
150 | } |
||
151 | |||
152 | private void loadJdbcDatasourcesCombo() { |
||
153 | getDatasourceComboBox().removeAllItems(); |
||
154 | |||
155 | getDatasourceComboBox().addItem(new ConnectionWithParamsSDE());
|
||
156 | |||
157 | ConnectionWithParamsSDE[] conn = SingleSDEConnectionManager.instance()
|
||
158 | .getAllConnections(); |
||
159 | |||
160 | if (conn == null) { |
||
161 | return;
|
||
162 | } |
||
163 | |||
164 | for (int i = 0; i < conn.length; i++) { |
||
165 | getDatasourceComboBox().addItem(conn[i]); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | private String[] getDriverNames() { |
||
170 | Class[] classes = new Class[] { VectorialSDEDriver.class }; |
||
171 | |||
172 | ArrayList ret = new ArrayList(); |
||
173 | String[] driverNames = LayerFactory.getDM().getDriverNames(); |
||
174 | |||
175 | for (int i = 0; i < driverNames.length; i++) { |
||
176 | boolean is = false; |
||
177 | |||
178 | for (int j = 0; j < classes.length; j++) { |
||
179 | if (LayerFactory.getDM().isA(driverNames[i], classes[j])) {
|
||
180 | ret.add(driverNames[i]); |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | |||
185 | return (String[]) ret.toArray(new String[0]); |
||
186 | } |
||
187 | |||
188 | /**
|
||
189 | * Utility method to instantiate a VectorialJDBCDriver from its name.
|
||
190 | *
|
||
191 | * @param drvname driver's name
|
||
192 | * @return a VectorialJDBCDriver
|
||
193 | *
|
||
194 | * @throws DriverLoadException
|
||
195 | */
|
||
196 | public VectorialSDEDriver getDriverFromName(String drvname) |
||
197 | throws DriverLoadException {
|
||
198 | VectorialSDEDriver resp = (VectorialSDEDriver) LayerFactory.getDM() |
||
199 | .getDriver(drvname); |
||
200 | |||
201 | return resp;
|
||
202 | } |
||
203 | |||
204 | public void initWizard() { |
||
205 | } |
||
206 | |||
207 | public void execute() { |
||
208 | } |
||
209 | /**
|
||
210 | * Return FLayers if user performs multi selection.
|
||
211 | */
|
||
212 | public FLayer getLayer() {
|
||
213 | try {
|
||
214 | IProjection proj = null;
|
||
215 | TablesListItem[] selected = getSelectedTables();
|
||
216 | int count = selected.length;
|
||
217 | String groupName = selectedDataSource.getDb() + " (" + |
||
218 | conex.getSdeDbaName() + ")";
|
||
219 | |||
220 | FLayer[] all_layers = new FLayer[count]; |
||
221 | String strEPSG = getMapCtrl().getViewPort().getProjection()
|
||
222 | .getAbrev().substring(5);
|
||
223 | |||
224 | for (int i = 0; i < count; i++) { |
||
225 | TablesListItem item = selected[i]; |
||
226 | |||
227 | VectorialSDEDriver driver = null;
|
||
228 | driver = (VectorialSDEDriver) getDriverFromName(selectedDataSource.getDrvName()); |
||
229 | |||
230 | Rectangle2D _wa = item.getUserTableSettingsPanel()
|
||
231 | .getWorkingArea(); |
||
232 | |||
233 | if (_wa != null) { |
||
234 | driver.setWorkingArea(_wa); |
||
235 | } |
||
236 | |||
237 | String layerName = item.getUserTableSettingsPanel()
|
||
238 | .getUserLayerName(); |
||
239 | String tableName = item.getTableName();
|
||
240 | String fidField = item.getUserTableSettingsPanel()
|
||
241 | .getIdFieldName(); |
||
242 | String geomField = item.getUserTableSettingsPanel()
|
||
243 | .getGeoFieldName(); |
||
244 | String[] fields = item.getUserSelectedFieldsPanel() |
||
245 | .getUserSelectedFields(fidField, geomField); |
||
246 | |||
247 | // fields = driver.manageGeometryField(fields, geomField);
|
||
248 | |||
249 | // driver.manageGeometryField(geomField);
|
||
250 | VectorialSDELayerDefinition lyrDef = new ArcSDELayerDefinition();
|
||
251 | lyrDef.setHost(selectedDataSource.getHost()); |
||
252 | lyrDef.setPort(selectedDataSource.getPort()); |
||
253 | lyrDef.setSchema(selectedDataSource.getDb()); |
||
254 | lyrDef.setUser(selectedDataSource.getUser()); |
||
255 | lyrDef.setPassword(selectedDataSource.getPw()); |
||
256 | lyrDef.setName(layerName); |
||
257 | lyrDef.setTableName(tableName); |
||
258 | // lyrDef.setWhereClause(whereClause);
|
||
259 | // lyrDef.setFieldGeometry(arcsde_wizard.getGeomField());
|
||
260 | // lyrDef.setFieldID(arcsde_wizard.getFID());
|
||
261 | // lyrDef.setFieldNames(arcsde_wizard.getFields());
|
||
262 | // lyrDef.setSRID_EPSG(strEPSG);
|
||
263 | lyrDef.setConnectionName(selectedDataSource.getName()); |
||
264 | |||
265 | if (item.getUserTableSettingsPanel().isSqlActive()) {
|
||
266 | String whereClause = item.getUserTableSettingsPanel()
|
||
267 | .getWhereClause(); |
||
268 | lyrDef.setWhereClause(whereClause); |
||
269 | } |
||
270 | else {
|
||
271 | lyrDef.setWhereClause("");
|
||
272 | } |
||
273 | |||
274 | lyrDef.setFieldGeometry(geomField); |
||
275 | String[] fieldsAux=null; |
||
276 | if ((fields != null) && (fields.length != 0)) { |
||
277 | if (!fields[0].equals(lyrDef.getFieldGeometry())) { |
||
278 | fieldsAux = new String[fields.length + 1]; |
||
279 | fieldsAux[0] = lyrDef.getFieldGeometry();
|
||
280 | |||
281 | for (int k = 1; k < fields.length+1; k++) { |
||
282 | fieldsAux[k] = fields[k - 1];
|
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | fields=fieldsAux; |
||
287 | // else {
|
||
288 | // SeSqlConstruct sqlConstruct = new SeSqlConstruct(tableName);
|
||
289 | // sqlTotal = sqlConstruct.getWhere();
|
||
290 | //
|
||
291 | // SeTable table1 = new SeTable(conn, tableName);
|
||
292 | //
|
||
293 | // /*
|
||
294 | // * Get the table's column definition.
|
||
295 | // */
|
||
296 | // SeColumnDefinition[] tableDef = table1.describe();
|
||
297 | // this.fields = new String[tableDef.length];
|
||
298 | //
|
||
299 | // /*
|
||
300 | // * Store the names of all the table's columns in the
|
||
301 | // * String array cols. This array specifies the columns
|
||
302 | // * to be retrieved from the database.
|
||
303 | // */
|
||
304 | // int idField = 1;
|
||
305 | //
|
||
306 | // for (int i = 0; i < tableDef.length; i++) {
|
||
307 | // if (tableDef[i].getType() == SeColumnDefinition.TYPE_SHAPE) {
|
||
308 | // this.fields[0] = tableDef[i].getName();
|
||
309 | // } else {
|
||
310 | // this.fields[idField] = tableDef[i].getName();
|
||
311 | // idField++;
|
||
312 | // }
|
||
313 | // }
|
||
314 | //
|
||
315 | // }
|
||
316 | |||
317 | lyrDef.setFieldNames(fields); |
||
318 | |||
319 | lyrDef.setFieldID(fidField); |
||
320 | |||
321 | if (_wa != null) { |
||
322 | lyrDef.setWorkingArea(_wa); |
||
323 | } |
||
324 | |||
325 | lyrDef.setSRID_EPSG(strEPSG); |
||
326 | |||
327 | if (driver instanceof ICanReproject) { |
||
328 | ((ICanReproject) driver).setDestProjection(strEPSG); |
||
329 | } |
||
330 | |||
331 | driver.setData(null, lyrDef);
|
||
332 | |||
333 | if (driver instanceof ICanReproject) { |
||
334 | proj = CRSFactory.getCRS("EPSG:" +
|
||
335 | ((ICanReproject) driver).getSourceProjection()); |
||
336 | } |
||
337 | |||
338 | // all_layers[i] = SDELayerFactory.getLayerSDE(driver, layerName,
|
||
339 | // proj);
|
||
340 | all_layers[i] = LayerFactory.createDBLayer(driver, layerName, |
||
341 | proj); |
||
342 | } |
||
343 | |||
344 | return layerArrayToGroup(all_layers, groupName);
|
||
345 | } |
||
346 | catch (Exception e) { |
||
347 | logger.error("While creating jdbc layer: " + e.getMessage(), e);
|
||
348 | NotificationManager.addError("Error al cargar la capa: " +
|
||
349 | e.getMessage(), e); |
||
350 | } |
||
351 | |||
352 | return null; |
||
353 | } |
||
354 | |||
355 | private FLayer layerArrayToGroup(FLayer[] all_layers, String name) { |
||
356 | if (all_layers.length == 1) { |
||
357 | return all_layers[0]; |
||
358 | } |
||
359 | |||
360 | MapContext mc = view.getMapControl().getMapContext(); |
||
361 | FLayers root = view.getMapControl().getMapContext().getLayers(); |
||
362 | |||
363 | FLayers group = new FLayers(mc, root);
|
||
364 | group.setName(name); |
||
365 | |||
366 | for (int i = 0; i < all_layers.length; i++) { |
||
367 | group.addLayer(all_layers[i]); |
||
368 | } |
||
369 | |||
370 | return group;
|
||
371 | } |
||
372 | |||
373 | private TablesListItem[] getSelectedTables() { |
||
374 | int count = tablesList.getModel().getSize();
|
||
375 | ArrayList resp = new ArrayList(); |
||
376 | |||
377 | for (int i = 0; i < count; i++) { |
||
378 | TablesListItem item = (TablesListItem) tablesList.getModel() |
||
379 | .getElementAt(i); |
||
380 | |||
381 | if (item.isSelected()) {
|
||
382 | resp.add(item); |
||
383 | } |
||
384 | } |
||
385 | |||
386 | return (TablesListItem[]) resp.toArray(new TablesListItem[0]); |
||
387 | } |
||
388 | |||
389 | /**
|
||
390 | * This method initializes namePanel
|
||
391 | *
|
||
392 | * @return javax.swing.JPanel
|
||
393 | */
|
||
394 | private JPanel getNamePanel() { |
||
395 | if (namePanel == null) { |
||
396 | namePanel = new JPanel(); |
||
397 | namePanel.setLayout(null);
|
||
398 | namePanel.setBounds(new java.awt.Rectangle(5, 5, 501, 51)); |
||
399 | namePanel.setBorder(javax.swing.BorderFactory.createTitledBorder( |
||
400 | null, PluginServices.getText(this, "choose_connection"), |
||
401 | javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, |
||
402 | javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
||
403 | namePanel.add(getDatasourceComboBox(), null);
|
||
404 | namePanel.add(getJdbcButton(), null);
|
||
405 | } |
||
406 | |||
407 | return namePanel;
|
||
408 | } |
||
409 | |||
410 | /**
|
||
411 | * This method initializes tablesPanel
|
||
412 | *
|
||
413 | * @return javax.swing.JPanel
|
||
414 | */
|
||
415 | private JPanel getTablesPanel() { |
||
416 | if (tablesPanel == null) { |
||
417 | tablesPanel = new JPanel(); |
||
418 | tablesPanel.setLayout(new BorderLayout()); |
||
419 | tablesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder( |
||
420 | null, PluginServices.getText(this, "choose_table"), |
||
421 | javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, |
||
422 | javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
||
423 | tablesPanel.setBounds(new java.awt.Rectangle(5, 55, 246, 191)); |
||
424 | tablesPanel.add(getTablesScrollPane(), java.awt.BorderLayout.CENTER); |
||
425 | } |
||
426 | |||
427 | return tablesPanel;
|
||
428 | } |
||
429 | |||
430 | /**
|
||
431 | * This method initializes settingsPanel
|
||
432 | *
|
||
433 | * @return javax.swing.JPanel
|
||
434 | */
|
||
435 | |||
436 | /**
|
||
437 | * This method initializes tablesScrollPane
|
||
438 | *
|
||
439 | * @return javax.swing.JScrollPane
|
||
440 | */
|
||
441 | private JScrollPane getTablesScrollPane() { |
||
442 | if (tablesScrollPane == null) { |
||
443 | tablesScrollPane = new JScrollPane(); |
||
444 | tablesScrollPane.setViewportView(getTablesList()); |
||
445 | } |
||
446 | |||
447 | return tablesScrollPane;
|
||
448 | } |
||
449 | |||
450 | /**
|
||
451 | * This method initializes tablesList
|
||
452 | *
|
||
453 | * @return javax.swing.JList
|
||
454 | */
|
||
455 | private AvailableTablesCheckBoxList getTablesList() {
|
||
456 | if (tablesList == null) { |
||
457 | tablesList = new AvailableTablesCheckBoxList(this); |
||
458 | tablesList.addListSelectionListener(this);
|
||
459 | tablesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||
460 | } |
||
461 | |||
462 | return tablesList;
|
||
463 | } |
||
464 | |||
465 | /**
|
||
466 | * This method initializes layerNameTextField
|
||
467 | *
|
||
468 | * @return javax.swing.JTextField
|
||
469 | */
|
||
470 | |||
471 | /**
|
||
472 | * This method initializes jComboBox
|
||
473 | *
|
||
474 | * @return javax.swing.JComboBox
|
||
475 | */
|
||
476 | private JComboBox getDatasourceComboBox() { |
||
477 | if (datasourceComboBox == null) { |
||
478 | datasourceComboBox = new JComboBox(); |
||
479 | datasourceComboBox.setBounds(new java.awt.Rectangle(10, 20, 446, 21)); |
||
480 | datasourceComboBox.addActionListener(this);
|
||
481 | } |
||
482 | |||
483 | return datasourceComboBox;
|
||
484 | } |
||
485 | |||
486 | public void actionPerformed(ActionEvent arg0) { |
||
487 | Object src = arg0.getSource();
|
||
488 | |||
489 | if (src == datasourceComboBox) {
|
||
490 | Object sel_obj = datasourceComboBox.getSelectedItem();
|
||
491 | |||
492 | if (sel_obj == null) { |
||
493 | return;
|
||
494 | } |
||
495 | |||
496 | if (!(sel_obj instanceof ConnectionWithParamsSDE)) { |
||
497 | return;
|
||
498 | } |
||
499 | |||
500 | selectedDataSource = (ConnectionWithParamsSDE) sel_obj; |
||
501 | |||
502 | if (selectedDataSource.isNull()) {
|
||
503 | updateTableList(selectedDataSource); |
||
504 | setEmptyPanels(); |
||
505 | |||
506 | return;
|
||
507 | } |
||
508 | |||
509 | if (!selectedDataSource.isConnected()) {
|
||
510 | if (!tryToConnect(selectedDataSource)) {
|
||
511 | datasourceComboBox.setSelectedIndex(0);
|
||
512 | |||
513 | return;
|
||
514 | } |
||
515 | } |
||
516 | |||
517 | getDatasourceComboBox().repaint(); |
||
518 | updateTableList(selectedDataSource); |
||
519 | |||
520 | // setEmptyPanels();
|
||
521 | return;
|
||
522 | } |
||
523 | |||
524 | if (src == jdbcButton) {
|
||
525 | ConnectionWithParamsSDE sel = addNewConnection(); |
||
526 | |||
527 | if (sel != null) { |
||
528 | loadJdbcDatasourcesCombo(); |
||
529 | getDatasourceComboBox().setSelectedItem(sel); |
||
530 | } |
||
531 | } |
||
532 | } |
||
533 | |||
534 | private boolean tryToConnect(ConnectionWithParamsSDE _cwp) { |
||
535 | JPasswordDlg dlg = new JPasswordDlg();
|
||
536 | String strMessage = PluginServices.getText(this, "conectar_jdbc"); |
||
537 | String strPassword = PluginServices.getText(this, "password"); |
||
538 | dlg.setMessage(strMessage + " [" + _cwp.getDrvName() + ", " + |
||
539 | _cwp.getHost() + ", " + _cwp.getPort() + ", " + _cwp.getDb() + |
||
540 | ", " + _cwp.getUser() + "]. " + strPassword + "?"); |
||
541 | |||
542 | dlg.show(); |
||
543 | |||
544 | String clave = dlg.getPassword();
|
||
545 | |||
546 | if (clave == null) { |
||
547 | return false; |
||
548 | } |
||
549 | |||
550 | try {
|
||
551 | _cwp.connect(clave); |
||
552 | } |
||
553 | catch (SQLException e) { |
||
554 | showConnectionErrorMessage(e.getMessage()); |
||
555 | |||
556 | return false; |
||
557 | } |
||
558 | |||
559 | return true; |
||
560 | } |
||
561 | |||
562 | private void updateTableList(ConnectionWithParamsSDE src) { |
||
563 | if (src.isNull()) {
|
||
564 | getTablesList().setModel(new DefaultListModel()); |
||
565 | getTablesScrollPane().setViewportView(tablesList); |
||
566 | tablesScrollPane.updateUI(); |
||
567 | |||
568 | return;
|
||
569 | } |
||
570 | |||
571 | conex = src.getConnection(); |
||
572 | |||
573 | String drvName = src.getDrvName();
|
||
574 | String dbName = ""; |
||
575 | |||
576 | VectorialSDEDriver drv = null;
|
||
577 | |||
578 | try {
|
||
579 | dbName = src.getDb();//getConnection().getDatabaseName();
|
||
580 | drv = (VectorialSDEDriver) LayerFactory.getDM().getDriver(drvName); |
||
581 | } catch (Exception e) { |
||
582 | logger.error("While getting driver instance: " + e.getMessage(), e);
|
||
583 | } |
||
584 | |||
585 | if (!(drv instanceof VectorialSDEDriver)) { |
||
586 | logger.error( |
||
587 | "Unexpected driver type (not a ArcSdeDriver driver)");
|
||
588 | |||
589 | return;
|
||
590 | } |
||
591 | |||
592 | String[] tablnames = null; |
||
593 | |||
594 | try {
|
||
595 | tablnames = getTableNames(conex, dbName); |
||
596 | } catch (SQLException e) { |
||
597 | logger.error("While getting table names: " + e.getMessage(), e);
|
||
598 | |||
599 | return;
|
||
600 | } |
||
601 | |||
602 | DefaultListModel lmodel = new DefaultListModel(); |
||
603 | |||
604 | for (int i = 0; i < tablnames.length; i++) { |
||
605 | lmodel.addElement(new TablesListItem(tablnames[i], drv, conex,
|
||
606 | getMapCtrl(), this));
|
||
607 | } |
||
608 | |||
609 | getTablesList().setModel(lmodel); |
||
610 | getTablesScrollPane().setViewportView(tablesList); |
||
611 | tablesScrollPane.updateUI(); |
||
612 | } |
||
613 | |||
614 | public void valueChanged(ListSelectionEvent arg0) { |
||
615 | Object src = arg0.getSource();
|
||
616 | |||
617 | if (src == tablesList) {
|
||
618 | TablesListItem selected = (TablesListItem) tablesList.getSelectedValue(); |
||
619 | |||
620 | try {
|
||
621 | setSettingsPanels(selected); |
||
622 | } |
||
623 | catch (SQLException e) { |
||
624 | showConnectionErrorMessage(e.getMessage()); |
||
625 | tablesList.clearSelection(); |
||
626 | setEmptyPanels(); |
||
627 | } |
||
628 | |||
629 | checkFinishable(); |
||
630 | } |
||
631 | } |
||
632 | |||
633 | private boolean validFormSettings() { |
||
634 | int count = tablesList.getModel().getSize();
|
||
635 | |||
636 | boolean at_least_one = false; |
||
637 | boolean resp = true; |
||
638 | |||
639 | for (int i = 0; i < count; i++) { |
||
640 | TablesListItem item = (TablesListItem) tablesList.getModel() |
||
641 | .getElementAt(i); |
||
642 | |||
643 | if (item.isSelected()) {
|
||
644 | at_least_one = true;
|
||
645 | } |
||
646 | |||
647 | if (item.disturbsWizardValidity()) {
|
||
648 | resp = false;
|
||
649 | } |
||
650 | } |
||
651 | |||
652 | return (at_least_one && resp);
|
||
653 | } |
||
654 | |||
655 | public void checkFinishable() { |
||
656 | boolean finishable = validFormSettings();
|
||
657 | callStateChanged(finishable); |
||
658 | } |
||
659 | |||
660 | /**
|
||
661 | * This method initializes jdbcButton
|
||
662 | *
|
||
663 | * @return javax.swing.JButton
|
||
664 | */
|
||
665 | private JButton getJdbcButton() { |
||
666 | if (jdbcButton == null) { |
||
667 | jdbcButton = new JButton(); |
||
668 | jdbcButton.addActionListener(this);
|
||
669 | jdbcButton.setToolTipText(PluginServices.getText(this,
|
||
670 | "add_connection"));
|
||
671 | jdbcButton.setBounds(new java.awt.Rectangle(465, 20, 26, 21)); |
||
672 | |||
673 | String _file = createResourceUrl("images/jdbc.png").getFile(); |
||
674 | jdbcButton.setIcon(new ImageIcon(_file)); |
||
675 | } |
||
676 | |||
677 | return jdbcButton;
|
||
678 | } |
||
679 | |||
680 | private ConnectionWithParamsSDE addNewConnection() {
|
||
681 | ConnectionWithParamsSDE resp = null;
|
||
682 | |||
683 | SDEConnectionParamsDialog newco = new SDEConnectionParamsDialog();
|
||
684 | newco.showDialog(); |
||
685 | |||
686 | if (newco.isOkPressed()) {
|
||
687 | String _drvname = newco.getConnectionDriverName();
|
||
688 | String _host = newco.getConnectionServerUrl();
|
||
689 | String _port = newco.getConnectionPort();
|
||
690 | String _dbname = newco.getConnectionDBName();
|
||
691 | String _user = newco.getConnectionUser();
|
||
692 | String _pw = newco.getConnectionPassword();
|
||
693 | String _conn_usr_name = newco.getConnectionName();
|
||
694 | |||
695 | boolean hasToBeCon = newco.hasToBeConnected();
|
||
696 | |||
697 | try {
|
||
698 | resp = SingleSDEConnectionManager.instance() |
||
699 | .getConnection(_drvname, |
||
700 | _user, _pw, _conn_usr_name, _host, _port, _dbname, |
||
701 | hasToBeCon); |
||
702 | } |
||
703 | catch (SQLException e) { |
||
704 | showConnectionErrorMessage(e.getMessage()); |
||
705 | |||
706 | return null; |
||
707 | } |
||
708 | |||
709 | return resp;
|
||
710 | } |
||
711 | else {
|
||
712 | return null; |
||
713 | } |
||
714 | } |
||
715 | |||
716 | private void showConnectionErrorMessage(String _msg) { |
||
717 | String msg = (_msg.length() > 300) ? "" : (": " + _msg); |
||
718 | String title = PluginServices.getText(this, "connection_error"); |
||
719 | JOptionPane.showMessageDialog(this, title + msg, title, |
||
720 | JOptionPane.ERROR_MESSAGE);
|
||
721 | } |
||
722 | |||
723 | private java.net.URL createResourceUrl(String path) { |
||
724 | return getClass().getClassLoader().getResource(path);
|
||
725 | } |
||
726 | |||
727 | public void setSettingsPanels(TablesListItem actTable) |
||
728 | throws SQLException { |
||
729 | if (actTable == null) { |
||
730 | setEmptyPanels(); |
||
731 | |||
732 | return;
|
||
733 | } |
||
734 | |||
735 | settingsPanel = actTable.getUserTableSettingsPanel(); |
||
736 | fieldsPanel = actTable.getUserSelectedFieldsPanel(); |
||
737 | |||
738 | removeFieldPanels(); |
||
739 | add(fieldsPanel); |
||
740 | fieldsPanel.repaint(); |
||
741 | |||
742 | removeSettingsPanels(); |
||
743 | add(settingsPanel); |
||
744 | settingsPanel.repaint(); |
||
745 | |||
746 | repaint(); |
||
747 | } |
||
748 | |||
749 | private void setEmptyPanels() { |
||
750 | removeFieldPanels(); |
||
751 | add(emptyFieldsPanel); |
||
752 | removeSettingsPanels(); |
||
753 | add(emptySettingsPanel); |
||
754 | |||
755 | settingsPanel = emptySettingsPanel; |
||
756 | fieldsPanel = emptyFieldsPanel; |
||
757 | |||
758 | repaint(); |
||
759 | } |
||
760 | |||
761 | private void removeFieldPanels() { |
||
762 | for (int i = 0; i < getComponentCount(); i++) { |
||
763 | if (getComponent(i) instanceof UserSelectedFieldsPanel) { |
||
764 | remove(i); |
||
765 | } |
||
766 | } |
||
767 | } |
||
768 | |||
769 | private void removeSettingsPanels() { |
||
770 | for (int i = 0; i < getComponentCount(); i++) { |
||
771 | if (getComponent(i) instanceof UserTableSettingsPanel) { |
||
772 | remove(i); |
||
773 | } |
||
774 | } |
||
775 | } |
||
776 | |||
777 | |||
778 | public String[] getTableNames(Object conex, String dbName) throws SQLException { |
||
779 | Vector theLayers=null; |
||
780 | try {
|
||
781 | theLayers = ((SeConnection)conex).getLayers(); |
||
782 | } catch (SeException e) {
|
||
783 | throw new SQLException(e.getMessage()); |
||
784 | } |
||
785 | TreeMap ret = new TreeMap(); |
||
786 | for (int i = 0; i < theLayers.size(); i++) { |
||
787 | SeLayer layer = (SeLayer) theLayers.elementAt(i); |
||
788 | ret.put(layer.getTableName(), layer.getTableName()); |
||
789 | } |
||
790 | return (String[]) ret.keySet().toArray(new String[0]); |
||
791 | } |
||
792 | |||
793 | |||
794 | |||
795 | } // @jve:decl-index=0:visual-constraint="10,10" |