svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / vectorialdb / wizard / WizardDB.java @ 43759
History | View | Annotate | Download (23.7 KB)
1 | 40557 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
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 3
|
||
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 | 41495 | jjdelcerro | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 40557 | jjdelcerro | * 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 | 41495 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40557 | jjdelcerro | *
|
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | package org.gvsig.geodb.vectorialdb.wizard; |
25 | |||
26 | import java.awt.BorderLayout; |
||
27 | import java.awt.Window; |
||
28 | import java.awt.event.ActionEvent; |
||
29 | import java.awt.event.ActionListener; |
||
30 | import java.util.ArrayList; |
||
31 | 43406 | jjdelcerro | import java.util.Comparator; |
32 | 40435 | jjdelcerro | import java.util.Iterator; |
33 | import java.util.List; |
||
34 | import java.util.Map; |
||
35 | import java.util.Map.Entry; |
||
36 | |||
37 | import javax.swing.DefaultListModel; |
||
38 | import javax.swing.JComboBox; |
||
39 | import javax.swing.JOptionPane; |
||
40 | import javax.swing.JPanel; |
||
41 | import javax.swing.JScrollPane; |
||
42 | import javax.swing.ListSelectionModel; |
||
43 | 43128 | jjdelcerro | import javax.swing.SwingUtilities; |
44 | 41258 | jjdelcerro | import javax.swing.event.AncestorEvent; |
45 | import javax.swing.event.AncestorListener; |
||
46 | 40435 | jjdelcerro | import javax.swing.event.ListSelectionEvent; |
47 | import javax.swing.event.ListSelectionListener; |
||
48 | |||
49 | import org.apache.commons.collections.map.HashedMap; |
||
50 | 43032 | jjdelcerro | import org.apache.commons.lang3.text.WordUtils; |
51 | 40435 | jjdelcerro | import org.cresques.cts.IProjection; |
52 | import org.slf4j.Logger; |
||
53 | import org.slf4j.LoggerFactory; |
||
54 | |||
55 | import org.gvsig.andami.IconThemeHelper; |
||
56 | import org.gvsig.andami.PluginServices; |
||
57 | import org.gvsig.andami.PluginsLocator; |
||
58 | import org.gvsig.andami.PluginsManager; |
||
59 | import org.gvsig.andami.messages.NotificationManager; |
||
60 | import org.gvsig.app.ApplicationLocator; |
||
61 | import org.gvsig.app.ApplicationManager; |
||
62 | import org.gvsig.app.gui.WizardPanel; |
||
63 | import org.gvsig.app.prepareAction.PrepareContext; |
||
64 | import org.gvsig.app.project.Project; |
||
65 | import org.gvsig.app.project.ProjectManager; |
||
66 | import org.gvsig.app.project.documents.table.TableDocument; |
||
67 | import org.gvsig.app.project.documents.table.TableManager; |
||
68 | import org.gvsig.fmap.dal.DALLocator; |
||
69 | import org.gvsig.fmap.dal.DataManager; |
||
70 | 41495 | jjdelcerro | import org.gvsig.fmap.dal.DataServerExplorerPool; |
71 | import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
||
72 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.DataStoreParameters; |
73 | import org.gvsig.fmap.dal.exception.DataException; |
||
74 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
75 | import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorer; |
||
76 | import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorerParameters; |
||
77 | import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
||
78 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
79 | import org.gvsig.fmap.mapcontrol.MapControl; |
||
80 | import org.gvsig.geodb.ExtDB_Spatial; |
||
81 | import org.gvsig.gui.beans.swing.JButton; |
||
82 | import org.gvsig.i18n.Messages; |
||
83 | import org.gvsig.tools.dynobject.DynObject; |
||
84 | |||
85 | public class WizardDB extends WizardPanel implements ActionListener, |
||
86 | 41495 | jjdelcerro | ListSelectionListener {
|
87 | 40435 | jjdelcerro | |
88 | 41495 | jjdelcerro | /**
|
89 | *
|
||
90 | 40435 | jjdelcerro | */
|
91 | private static final long serialVersionUID = -7045762275505941695L; |
||
92 | private static final String WIZARD_TAB_NAME = "DB"; |
||
93 | 41495 | jjdelcerro | private static Logger logger = LoggerFactory.getLogger(WizardDB.class |
94 | .getName()); |
||
95 | 40435 | jjdelcerro | |
96 | private JPanel namePanel = null; |
||
97 | 41495 | jjdelcerro | private JPanel tablesPanel = null; |
98 | private JScrollPane tablesScrollPane = null; |
||
99 | private AvailableTablesCheckBoxList tablesList = null; |
||
100 | private JComboBox datasourceComboBox = null; |
||
101 | private UserSelectedFieldsPanel fieldsPanel = null; |
||
102 | private UserSelectedFieldsPanel emptyFieldsPanel = null; |
||
103 | private JButton dbButton = null; |
||
104 | private DBServerExplorerParameters dbExplorerParameters;
|
||
105 | private DBServerExplorer dbExplorer = null; |
||
106 | 40435 | jjdelcerro | |
107 | private UserTableSettingsPanel settingsPanel = null; |
||
108 | 41495 | jjdelcerro | protected UserTableSettingsPanel emptySettingsPanel = null; |
109 | private PrepareContext prepareDSContext;
|
||
110 | 40435 | jjdelcerro | |
111 | public WizardDB() {
|
||
112 | 41495 | jjdelcerro | super();
|
113 | initialize(); |
||
114 | this.addAncestorListener(new AncestorListener() { |
||
115 | public void ancestorAdded(AncestorEvent ae) { |
||
116 | } |
||
117 | 40435 | jjdelcerro | |
118 | 41495 | jjdelcerro | public void ancestorRemoved(AncestorEvent ae) { |
119 | if ( dbExplorer != null ) { |
||
120 | dbExplorer.dispose(); |
||
121 | dbExplorer = null;
|
||
122 | } |
||
123 | } |
||
124 | |||
125 | public void ancestorMoved(AncestorEvent ae) { |
||
126 | } |
||
127 | }); |
||
128 | } |
||
129 | |||
130 | 41258 | jjdelcerro | public DBServerExplorer getServerExplorer() {
|
131 | return this.dbExplorer; |
||
132 | } |
||
133 | 40435 | jjdelcerro | |
134 | 41495 | jjdelcerro | protected void initialize() { |
135 | setTabName(WIZARD_TAB_NAME); |
||
136 | setLayout(null);
|
||
137 | setSize(512, 478); |
||
138 | 40435 | jjdelcerro | |
139 | 41495 | jjdelcerro | emptyFieldsPanel = new UserSelectedFieldsPanel(null, true, this); |
140 | add(emptyFieldsPanel); |
||
141 | 40435 | jjdelcerro | |
142 | add(getNamePanel(), null);
|
||
143 | 41495 | jjdelcerro | loadVectorialDBDatasourcesCombo(null);
|
144 | 40435 | jjdelcerro | |
145 | 41495 | jjdelcerro | add(getTablesPanel(), null);
|
146 | 40435 | jjdelcerro | |
147 | emptySettingsPanel = createSettingsPanel(null);
|
||
148 | add(emptySettingsPanel); |
||
149 | |||
150 | 41495 | jjdelcerro | } |
151 | 40435 | jjdelcerro | |
152 | @SuppressWarnings("rawtypes") |
||
153 | private void loadVectorialDBDatasourcesCombo(MyExplorer sel) { |
||
154 | 41495 | jjdelcerro | DataManager dataManager = DALLocator.getDataManager(); |
155 | DataServerExplorerPool explorersPool = dataManager.getDataServerExplorerPool(); |
||
156 | Iterator it = explorersPool.iterator();
|
||
157 | getDatasourceComboBox().removeAllItems(); |
||
158 | getDatasourceComboBox().addItem("");
|
||
159 | while ( it.hasNext() ) {
|
||
160 | DataServerExplorerPoolEntry entry = (DataServerExplorerPoolEntry) it.next(); |
||
161 | 43759 | jjdelcerro | if( entry.getExplorerParameters() instanceof DBServerExplorerParameters ) { |
162 | MyExplorer myExplorer = new MyExplorer();
|
||
163 | myExplorer.setDbExplorerParameters((DBServerExplorerParameters) entry.getExplorerParameters()); |
||
164 | myExplorer.setName(entry.getName()); |
||
165 | getDatasourceComboBox().addItem(myExplorer); |
||
166 | if ( sel != null && sel.getName().equalsIgnoreCase(myExplorer.getName()) ) { |
||
167 | getDatasourceComboBox().setSelectedItem(myExplorer); |
||
168 | } |
||
169 | 40435 | jjdelcerro | } |
170 | } |
||
171 | |||
172 | 41495 | jjdelcerro | } |
173 | 40435 | jjdelcerro | |
174 | 41495 | jjdelcerro | public void initWizard() { |
175 | } |
||
176 | |||
177 | 40435 | jjdelcerro | @Override
|
178 | public void execute() { |
||
179 | executeWizard(); |
||
180 | } |
||
181 | |||
182 | @Override
|
||
183 | public Object executeWizard() { |
||
184 | 41495 | jjdelcerro | TablesListItem[] tables = getSelectedTables();
|
185 | 40435 | jjdelcerro | |
186 | 41495 | jjdelcerro | DataManager man = DALLocator.getDataManager(); |
187 | FeatureStore store; |
||
188 | 40435 | jjdelcerro | |
189 | 41495 | jjdelcerro | String docName;
|
190 | TableDocument document; |
||
191 | Project project = ProjectManager.getInstance().getCurrentProject(); |
||
192 | 40435 | jjdelcerro | |
193 | 41495 | jjdelcerro | ApplicationManager appGvSIGMan = ApplicationLocator.getManager(); |
194 | PrepareContext context = this.getPrepareDataStoreContext();
|
||
195 | DBStoreParameters storeParams; |
||
196 | List<TableDocument> tabledocs
|
||
197 | = new ArrayList<TableDocument>(tables.length); |
||
198 | for ( TablesListItem table : tables ) {
|
||
199 | storeParams = getParameterForTable(table); |
||
200 | 40435 | jjdelcerro | |
201 | 41495 | jjdelcerro | try {
|
202 | storeParams = (DBStoreParameters) appGvSIGMan |
||
203 | .prepareOpenDataStoreParameters(storeParams, context); |
||
204 | } catch (Exception e2) { |
||
205 | NotificationManager.addError(e2); |
||
206 | continue;
|
||
207 | } |
||
208 | 40435 | jjdelcerro | |
209 | 41495 | jjdelcerro | UserTableSettingsPanel userTableSettingsPanel = table |
210 | .getUserTableSettingsPanel(); |
||
211 | |||
212 | docName = userTableSettingsPanel.getUserLayerName(); |
||
213 | try {
|
||
214 | store = (FeatureStore) man.openStore(storeParams.getDataStoreName(), storeParams); |
||
215 | } catch (Exception e) { |
||
216 | NotificationManager.addError(e); |
||
217 | 40435 | jjdelcerro | return null; |
218 | 41495 | jjdelcerro | } |
219 | 40435 | jjdelcerro | |
220 | 41495 | jjdelcerro | try {
|
221 | appGvSIGMan.pepareOpenDataSource( |
||
222 | store, context); |
||
223 | } catch (Exception e) { |
||
224 | NotificationManager.addError(e); |
||
225 | store.dispose(); |
||
226 | 40435 | jjdelcerro | return null; |
227 | 41495 | jjdelcerro | } |
228 | 40435 | jjdelcerro | |
229 | 41495 | jjdelcerro | document = (TableDocument) ProjectManager.getInstance().createDocument(TableManager.TYPENAME, docName); |
230 | document.setStore(store); |
||
231 | 40435 | jjdelcerro | // project.add(document);
|
232 | tabledocs.add(document); |
||
233 | 41495 | jjdelcerro | } |
234 | 40435 | jjdelcerro | return tabledocs;
|
235 | 41495 | jjdelcerro | } |
236 | 40435 | jjdelcerro | |
237 | 41495 | jjdelcerro | protected DBStoreParameters getParameterForTable(TablesListItem table) {
|
238 | DBStoreParameters parameters = table.getParameters(); |
||
239 | 40435 | jjdelcerro | |
240 | 41495 | jjdelcerro | UserTableSettingsPanel userTableSettingsPanel = table |
241 | .getUserTableSettingsPanel(); |
||
242 | 40435 | jjdelcerro | |
243 | 41495 | jjdelcerro | String fidField = userTableSettingsPanel.getIdFieldName();
|
244 | //IF is a multiple PK, remove the {} symbols
|
||
245 | if ( fidField.startsWith("{") && fidField.endsWith("}") ) { |
||
246 | fidField = fidField.substring(1, fidField.length() - 1); |
||
247 | } |
||
248 | String[] pkFields = fidField.split(","); |
||
249 | 40435 | jjdelcerro | |
250 | 41495 | jjdelcerro | String[] fields = table.getUserSelectedFieldsPanel() |
251 | .getUserSelectedFields(pkFields, null);
|
||
252 | 40435 | jjdelcerro | |
253 | 41495 | jjdelcerro | if ( userTableSettingsPanel.isSqlActive() ) {
|
254 | String whereClause = userTableSettingsPanel
|
||
255 | .getWhereClause(); |
||
256 | parameters.setBaseFilter(whereClause); |
||
257 | } else {
|
||
258 | parameters.setBaseFilter("");
|
||
259 | } |
||
260 | 40435 | jjdelcerro | |
261 | 41495 | jjdelcerro | parameters.setFields(fields); |
262 | 40435 | jjdelcerro | |
263 | 41495 | jjdelcerro | return parameters;
|
264 | 40435 | jjdelcerro | |
265 | 41495 | jjdelcerro | } |
266 | 40435 | jjdelcerro | |
267 | 41495 | jjdelcerro | @SuppressWarnings({"rawtypes", "unchecked"}) |
268 | 40435 | jjdelcerro | protected TablesListItem[] getSelectedTables() { |
269 | 41495 | jjdelcerro | int count = tablesList.getModel().getSize();
|
270 | ArrayList resp = new ArrayList(); |
||
271 | 40435 | jjdelcerro | |
272 | 41495 | jjdelcerro | for ( int i = 0; i < count; i++ ) { |
273 | TablesListItem item = (TablesListItem) tablesList.getModel() |
||
274 | .getElementAt(i); |
||
275 | 40435 | jjdelcerro | |
276 | 41495 | jjdelcerro | if ( item.isSelected() ) {
|
277 | resp.add(item); |
||
278 | } |
||
279 | } |
||
280 | 40435 | jjdelcerro | |
281 | 41495 | jjdelcerro | return (TablesListItem[]) resp.toArray(new TablesListItem[0]); |
282 | } |
||
283 | 40435 | jjdelcerro | |
284 | 41495 | jjdelcerro | /**
|
285 | * This method initializes namePanel
|
||
286 | *
|
||
287 | * @return javax.swing.JPanel
|
||
288 | */
|
||
289 | private JPanel getNamePanel() { |
||
290 | if ( namePanel == null ) { |
||
291 | namePanel = new JPanel(); |
||
292 | namePanel.setLayout(null);
|
||
293 | namePanel.setBounds(new java.awt.Rectangle(5, 5, 501, 51)); |
||
294 | namePanel.setBorder(javax.swing.BorderFactory.createTitledBorder( |
||
295 | null, PluginServices.getText(this, "choose_connection"), |
||
296 | javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, |
||
297 | javax.swing.border.TitledBorder.DEFAULT_POSITION, null,
|
||
298 | null));
|
||
299 | namePanel.add(getDatasourceComboBox(), null);
|
||
300 | namePanel.add(getJdbcButton(), null);
|
||
301 | } |
||
302 | 40435 | jjdelcerro | |
303 | 41495 | jjdelcerro | return namePanel;
|
304 | } |
||
305 | 40435 | jjdelcerro | |
306 | 41495 | jjdelcerro | /**
|
307 | * This method initializes tablesPanel
|
||
308 | *
|
||
309 | * @return javax.swing.JPanel
|
||
310 | */
|
||
311 | private JPanel getTablesPanel() { |
||
312 | if ( tablesPanel == null ) { |
||
313 | tablesPanel = new JPanel(); |
||
314 | tablesPanel.setLayout(new BorderLayout()); |
||
315 | tablesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder( |
||
316 | null, PluginServices.getText(this, "choose_table"), |
||
317 | javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, |
||
318 | javax.swing.border.TitledBorder.DEFAULT_POSITION, null,
|
||
319 | null));
|
||
320 | tablesPanel.setBounds(new java.awt.Rectangle(5, 55, 246, 166)); |
||
321 | tablesPanel |
||
322 | .add(getTablesScrollPane(), java.awt.BorderLayout.CENTER); |
||
323 | } |
||
324 | 40435 | jjdelcerro | |
325 | 41495 | jjdelcerro | return tablesPanel;
|
326 | } |
||
327 | 40435 | jjdelcerro | |
328 | 41495 | jjdelcerro | /**
|
329 | * This method initializes settingsPanel
|
||
330 | *
|
||
331 | * @return javax.swing.JPanel
|
||
332 | */
|
||
333 | /**
|
||
334 | * This method initializes tablesScrollPane
|
||
335 | *
|
||
336 | * @return javax.swing.JScrollPane
|
||
337 | */
|
||
338 | private JScrollPane getTablesScrollPane() { |
||
339 | if ( tablesScrollPane == null ) { |
||
340 | tablesScrollPane = new JScrollPane(); |
||
341 | tablesScrollPane.setViewportView(getTablesList()); |
||
342 | } |
||
343 | 40435 | jjdelcerro | |
344 | 41495 | jjdelcerro | return tablesScrollPane;
|
345 | } |
||
346 | 40435 | jjdelcerro | |
347 | 41495 | jjdelcerro | /**
|
348 | * This method initializes tablesList
|
||
349 | *
|
||
350 | * @return javax.swing.JList
|
||
351 | */
|
||
352 | protected AvailableTablesCheckBoxList getTablesList() {
|
||
353 | if ( tablesList == null ) { |
||
354 | tablesList = new AvailableTablesCheckBoxList(this); |
||
355 | tablesList.addListSelectionListener(this);
|
||
356 | tablesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||
357 | } |
||
358 | 40435 | jjdelcerro | |
359 | 41495 | jjdelcerro | return tablesList;
|
360 | } |
||
361 | 40435 | jjdelcerro | |
362 | 41495 | jjdelcerro | /**
|
363 | * This method initializes layerNameTextField
|
||
364 | *
|
||
365 | * @return javax.swing.JTextField
|
||
366 | */
|
||
367 | /**
|
||
368 | * This method initializes jComboBox
|
||
369 | *
|
||
370 | * @return javax.swing.JComboBox
|
||
371 | */
|
||
372 | private JComboBox getDatasourceComboBox() { |
||
373 | if ( datasourceComboBox == null ) { |
||
374 | datasourceComboBox = new JComboBox(); |
||
375 | datasourceComboBox |
||
376 | .setBounds(new java.awt.Rectangle(10, 20, 446, 21)); |
||
377 | datasourceComboBox.addActionListener(this);
|
||
378 | } |
||
379 | 40435 | jjdelcerro | |
380 | 41495 | jjdelcerro | return datasourceComboBox;
|
381 | } |
||
382 | 40435 | jjdelcerro | |
383 | 41495 | jjdelcerro | public void actionPerformed(ActionEvent arg0) { |
384 | if ( datasourceComboBox.getItemCount() == 0 ) { |
||
385 | setEmptyPanels(); |
||
386 | } |
||
387 | Object src = arg0.getSource();
|
||
388 | 40435 | jjdelcerro | |
389 | 41495 | jjdelcerro | if ( src == datasourceComboBox ) {
|
390 | 43128 | jjdelcerro | final Object selected = datasourceComboBox.getSelectedItem(); |
391 | 41495 | jjdelcerro | if ( selected instanceof MyExplorer ) { |
392 | 43128 | jjdelcerro | // Lo lanzamos asi para evitar cuelgues depurando, ya que estamos
|
393 | // en el evento de un combo y ahi no se puede depurar sin mas.
|
||
394 | SwingUtilities.invokeLater(new Runnable() { |
||
395 | 40435 | jjdelcerro | |
396 | 43128 | jjdelcerro | @Override
|
397 | public void run() { |
||
398 | MyExplorer sel_obj = (MyExplorer) selected; |
||
399 | |||
400 | getDatasourceComboBox().repaint(); |
||
401 | dbExplorerParameters = sel_obj.getDbSeverExplorerParameters(); |
||
402 | updateTableList(dbExplorerParameters); |
||
403 | } |
||
404 | }); |
||
405 | 40435 | jjdelcerro | } |
406 | |||
407 | 41495 | jjdelcerro | } else if ( src == dbButton ) { |
408 | MyExplorer sel = addNewConnection(); |
||
409 | 43377 | jjdelcerro | if ( sel == null ) { |
410 | loadVectorialDBDatasourcesCombo(null);
|
||
411 | } else {
|
||
412 | 41495 | jjdelcerro | dbExplorerParameters = sel.getDbSeverExplorerParameters(); |
413 | loadVectorialDBDatasourcesCombo(sel); |
||
414 | getDatasourceComboBox().setSelectedItem(sel); |
||
415 | } |
||
416 | } |
||
417 | 40435 | jjdelcerro | |
418 | 41495 | jjdelcerro | } |
419 | |||
420 | @SuppressWarnings({"unchecked", "rawtypes"}) |
||
421 | 40435 | jjdelcerro | private MyExplorer addNewConnection() {
|
422 | 41495 | jjdelcerro | VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
|
423 | newco.showDialog(); |
||
424 | 43112 | jjdelcerro | if ( newco.isCanceled()) {
|
425 | 41495 | jjdelcerro | return null; |
426 | } |
||
427 | 43112 | jjdelcerro | MyExplorer myExplorer = new MyExplorer();
|
428 | myExplorer.setDbExplorerParameters(newco.getServerExplorerParameters()); |
||
429 | myExplorer.setName(newco.getConnectionName()); |
||
430 | loadVectorialDBDatasourcesCombo(myExplorer); |
||
431 | return myExplorer;
|
||
432 | 41495 | jjdelcerro | } |
433 | 40435 | jjdelcerro | |
434 | 41495 | jjdelcerro | protected TablesListItem createTabeListItem(DBStoreParameters param) {
|
435 | return new TablesListItem(param, this); |
||
436 | } |
||
437 | 40435 | jjdelcerro | |
438 | 41495 | jjdelcerro | /**
|
439 | * Subclasses of this wizard will return a filtered list
|
||
440 | * if necessary
|
||
441 | *
|
||
442 | * @param explorer
|
||
443 | * @return
|
||
444 | * @throws DataException
|
||
445 | */
|
||
446 | 43406 | jjdelcerro | protected List<DBStoreParameters> getTableList(DBServerExplorer explorer) throws DataException { |
447 | List<DBStoreParameters> l = new ArrayList<>(); |
||
448 | l.addAll(explorer.list()); |
||
449 | l.sort(new Comparator<DBStoreParameters>() { |
||
450 | @Override
|
||
451 | public int compare(DBStoreParameters o1, DBStoreParameters o2) { |
||
452 | try {
|
||
453 | return o1.getTable().compareToIgnoreCase(o2.getTable());
|
||
454 | } catch(Exception ex) { |
||
455 | return 0; |
||
456 | } |
||
457 | } |
||
458 | }); |
||
459 | return l;
|
||
460 | 41495 | jjdelcerro | } |
461 | |||
462 | @SuppressWarnings("rawtypes") |
||
463 | 40435 | jjdelcerro | protected void updateTableList( |
464 | 41495 | jjdelcerro | DBServerExplorerParameters dbSeverExplorerParameters2) { |
465 | if ( dbSeverExplorerParameters2 == null ) { |
||
466 | return;
|
||
467 | } |
||
468 | DataManager dm = DALLocator.getDataManager(); |
||
469 | String err_msg = Messages.getText("connection_error"); |
||
470 | try {
|
||
471 | dbExplorer = (DBServerExplorer) dm.openServerExplorer(dbSeverExplorerParameters2.getExplorerName(), dbSeverExplorerParameters2); |
||
472 | 40435 | jjdelcerro | |
473 | 41495 | jjdelcerro | List parameters = getTableList(dbExplorer);
|
474 | 40435 | jjdelcerro | |
475 | 41495 | jjdelcerro | DefaultListModel lmodel = new DefaultListModel(); |
476 | 40435 | jjdelcerro | |
477 | 41495 | jjdelcerro | Iterator iter = parameters.iterator();
|
478 | DBStoreParameters param; |
||
479 | int count = 0; |
||
480 | while ( iter.hasNext() ) {
|
||
481 | param = (DBStoreParameters) iter.next(); |
||
482 | lmodel.addElement(createTabeListItem(param)); |
||
483 | count++; |
||
484 | } |
||
485 | 40435 | jjdelcerro | |
486 | 41495 | jjdelcerro | getTablesList().setModel(lmodel); |
487 | getTablesScrollPane().setViewportView(tablesList); |
||
488 | tablesScrollPane.updateUI(); |
||
489 | } catch (Exception e) { |
||
490 | 42053 | jjdelcerro | logger.info("Error while getting table names: " + e.getMessage(),e);
|
491 | 40435 | jjdelcerro | |
492 | 41495 | jjdelcerro | String remove_question = Messages.getText(
|
493 | "_Remove_connection_parameters_from_list_question");
|
||
494 | |||
495 | int opt = JOptionPane.showConfirmDialog( |
||
496 | ApplicationLocator.getManager().getRootComponent(), |
||
497 | 43032 | jjdelcerro | WordUtils.wrap(err_msg + ": " + getLastMessage(e) + "\n" + remove_question, 80), |
498 | 41495 | jjdelcerro | err_msg, |
499 | JOptionPane.YES_NO_OPTION);
|
||
500 | |||
501 | if ( opt == JOptionPane.YES_OPTION ) { |
||
502 | removeDBPArameters(dbSeverExplorerParameters2); |
||
503 | // dbExplorer.
|
||
504 | loadVectorialDBDatasourcesCombo(null);
|
||
505 | } |
||
506 | } |
||
507 | |||
508 | } |
||
509 | |||
510 | /**
|
||
511 | * Removes DB parameters from list of pre-configured DB parameters
|
||
512 | *
|
||
513 | * @param dbSeverExplorerParameters2
|
||
514 | */
|
||
515 | private void removeDBPArameters( |
||
516 | DBServerExplorerParameters dbparams) { |
||
517 | |||
518 | 41126 | jldominguez | PluginsManager manager = PluginsLocator.getManager(); |
519 | DynObject values = manager.getPlugin(ExtDB_Spatial.class).getPluginProperties(); |
||
520 | Map connections = (Map) values.getDynValue("db_connections"); |
||
521 | 41495 | jjdelcerro | if ( connections == null ) { |
522 | 41126 | jldominguez | return;
|
523 | } |
||
524 | 41495 | jjdelcerro | |
525 | 41126 | jldominguez | DBServerExplorerParameters item = null;
|
526 | Iterator it = connections.entrySet().iterator();
|
||
527 | List<String> toremove = new ArrayList(); |
||
528 | 41495 | jjdelcerro | |
529 | while ( it.hasNext() ) {
|
||
530 | 41126 | jldominguez | Map.Entry entry = (Entry) it.next();
|
531 | item = (DBServerExplorerParameters) entry.getValue(); |
||
532 | 41495 | jjdelcerro | if ( dbparams.equals(item) ) {
|
533 | 41126 | jldominguez | toremove.add((String) entry.getKey());
|
534 | } |
||
535 | } |
||
536 | 41495 | jjdelcerro | |
537 | if ( toremove.size() == 0 ) { |
||
538 | 41126 | jldominguez | return;
|
539 | } |
||
540 | 41495 | jjdelcerro | |
541 | for ( int i = 0; i < toremove.size(); i++ ) { |
||
542 | 41126 | jldominguez | connections.remove(toremove.get(i)); |
543 | } |
||
544 | values.setDynValue("db_connections", connections);
|
||
545 | 41495 | jjdelcerro | |
546 | 41126 | jldominguez | } |
547 | |||
548 | public void valueChanged(ListSelectionEvent arg0) { |
||
549 | 41495 | jjdelcerro | Object src = arg0.getSource();
|
550 | 40435 | jjdelcerro | |
551 | 41495 | jjdelcerro | if ( src == tablesList ) {
|
552 | TablesListItem selected = (TablesListItem) tablesList |
||
553 | .getSelectedValue(); |
||
554 | 40435 | jjdelcerro | |
555 | 41495 | jjdelcerro | setSettingsPanels(selected); |
556 | checkFinishable(); |
||
557 | } |
||
558 | } |
||
559 | 40435 | jjdelcerro | |
560 | 41495 | jjdelcerro | public boolean areSettingsValid() { |
561 | int count = tablesList.getModel().getSize();
|
||
562 | 40435 | jjdelcerro | |
563 | 41495 | jjdelcerro | boolean at_least_one = false; |
564 | boolean resp = true; |
||
565 | 40435 | jjdelcerro | |
566 | 41495 | jjdelcerro | for ( int i = 0; i < count; i++ ) { |
567 | TablesListItem item = (TablesListItem) tablesList.getModel() |
||
568 | .getElementAt(i); |
||
569 | 40435 | jjdelcerro | |
570 | 41495 | jjdelcerro | if ( item.isSelected() ) {
|
571 | at_least_one = true;
|
||
572 | } |
||
573 | 40435 | jjdelcerro | |
574 | 41495 | jjdelcerro | if ( item.disturbsWizardValidity() ) {
|
575 | resp = false;
|
||
576 | } |
||
577 | } |
||
578 | 40435 | jjdelcerro | |
579 | 41495 | jjdelcerro | return (at_least_one && resp);
|
580 | } |
||
581 | 40435 | jjdelcerro | |
582 | 41495 | jjdelcerro | public void checkFinishable() { |
583 | boolean finishable = areSettingsValid();
|
||
584 | callStateChanged(finishable); |
||
585 | } |
||
586 | 40435 | jjdelcerro | |
587 | 41495 | jjdelcerro | /**
|
588 | * This method initializes jdbcButton
|
||
589 | *
|
||
590 | * @return javax.swing.JButton
|
||
591 | */
|
||
592 | private JButton getJdbcButton() { |
||
593 | if ( dbButton == null ) { |
||
594 | dbButton = new JButton(); |
||
595 | dbButton.addActionListener(this);
|
||
596 | dbButton.setToolTipText(PluginServices.getText(this,
|
||
597 | "add_connection"));
|
||
598 | dbButton.setBounds(new java.awt.Rectangle(465, 20, 26, 21)); |
||
599 | 40435 | jjdelcerro | |
600 | 41495 | jjdelcerro | dbButton.setIcon(IconThemeHelper.getImageIcon("geodb-connection-add"));
|
601 | } |
||
602 | 40435 | jjdelcerro | |
603 | 41495 | jjdelcerro | return dbButton;
|
604 | } |
||
605 | 40435 | jjdelcerro | |
606 | 41495 | jjdelcerro | private void showConnectionErrorMessage(String _msg) { |
607 | String msg = (_msg.length() > 300) ? "" : (": " + _msg); |
||
608 | String title = PluginServices.getText(this, "connection_error"); |
||
609 | JOptionPane.showMessageDialog(this, title + msg, title, |
||
610 | JOptionPane.ERROR_MESSAGE);
|
||
611 | } |
||
612 | 40435 | jjdelcerro | |
613 | 41495 | jjdelcerro | private java.net.URL createResourceUrl(String path) { |
614 | return getClass().getClassLoader().getResource(path);
|
||
615 | } |
||
616 | 40435 | jjdelcerro | |
617 | 41495 | jjdelcerro | public void setSettingsPanels(TablesListItem actTable) { |
618 | if ( actTable == null ) { |
||
619 | setEmptyPanels(); |
||
620 | 40435 | jjdelcerro | |
621 | 41495 | jjdelcerro | return;
|
622 | } |
||
623 | fieldsPanel = actTable.getUserSelectedFieldsPanel(); |
||
624 | 40435 | jjdelcerro | |
625 | 41495 | jjdelcerro | removeFieldPanels(); |
626 | add(fieldsPanel); |
||
627 | fieldsPanel.repaint(); |
||
628 | removeSettingsPanels(); |
||
629 | add(emptySettingsPanel); |
||
630 | 40435 | jjdelcerro | |
631 | 41495 | jjdelcerro | settingsPanel = createSettingsPanel(actTable); |
632 | 40435 | jjdelcerro | |
633 | 41495 | jjdelcerro | removeSettingsPanels(); |
634 | add(settingsPanel); |
||
635 | settingsPanel.repaint(); |
||
636 | 40435 | jjdelcerro | |
637 | 41495 | jjdelcerro | repaint(); |
638 | } |
||
639 | 40435 | jjdelcerro | |
640 | 41495 | jjdelcerro | protected UserTableSettingsPanel createSettingsPanel(TablesListItem actTable) {
|
641 | if ( actTable == null ) { |
||
642 | return new UserTableSettingsPanel(null, "", true, this, null); |
||
643 | } |
||
644 | 40435 | jjdelcerro | |
645 | 41495 | jjdelcerro | return actTable.getUserTableSettingsPanel();
|
646 | } |
||
647 | 40435 | jjdelcerro | |
648 | 41495 | jjdelcerro | protected void setEmptyPanels() { |
649 | removeFieldPanels(); |
||
650 | add(emptyFieldsPanel); |
||
651 | fieldsPanel = emptyFieldsPanel; |
||
652 | 40435 | jjdelcerro | |
653 | 41495 | jjdelcerro | repaint(); |
654 | } |
||
655 | 40435 | jjdelcerro | |
656 | 41495 | jjdelcerro | private void removeFieldPanels() { |
657 | for ( int i = 0; i < getComponentCount(); i++ ) { |
||
658 | if ( getComponent(i) instanceof UserSelectedFieldsPanel ) { |
||
659 | remove(i); |
||
660 | } |
||
661 | } |
||
662 | } |
||
663 | 40435 | jjdelcerro | |
664 | 41495 | jjdelcerro | public DataStoreParameters[] getParameters() { |
665 | try {
|
||
666 | TablesListItem[] selected = getSelectedTables();
|
||
667 | int count = selected.length;
|
||
668 | DBStoreParameters[] dbParameters = new DBStoreParameters[count]; |
||
669 | 40435 | jjdelcerro | |
670 | 41495 | jjdelcerro | for ( int i = 0; i < count; i++ ) { |
671 | TablesListItem item = selected[i]; |
||
672 | 40435 | jjdelcerro | |
673 | 41495 | jjdelcerro | dbParameters[i] = getParameterForTable(item); |
674 | } |
||
675 | 40435 | jjdelcerro | |
676 | 41495 | jjdelcerro | return dbParameters;// layerArrayToGroup(all_layers, groupName); |
677 | } catch (Exception e) { |
||
678 | logger.info("Error while creating jdbc layer: " + e.getMessage(), e);
|
||
679 | NotificationManager.addError("Error while loading layer: "
|
||
680 | + e.getMessage(), e); |
||
681 | } |
||
682 | 40435 | jjdelcerro | |
683 | 41495 | jjdelcerro | return null; |
684 | } |
||
685 | 40435 | jjdelcerro | |
686 | 41495 | jjdelcerro | /**
|
687 | * This method process the errors found in a layer
|
||
688 | *
|
||
689 | * @param lyr
|
||
690 | * @param mapControl
|
||
691 | */
|
||
692 | protected void processErrorsOfLayer(FLayer lyr, MapControl mapControl) { |
||
693 | this.getMapContext().callNewErrorEvent(null); |
||
694 | } |
||
695 | 40435 | jjdelcerro | |
696 | 41495 | jjdelcerro | private void removeSettingsPanels() { |
697 | for ( int i = 0; i < getComponentCount(); i++ ) { |
||
698 | if ( getComponent(i) instanceof UserTableSettingsPanel ) { |
||
699 | remove(i); |
||
700 | } |
||
701 | } |
||
702 | } |
||
703 | 40435 | jjdelcerro | |
704 | 41495 | jjdelcerro | protected PrepareContext getPrepareDataStoreContext() {
|
705 | if ( this.prepareDSContext == null ) { |
||
706 | this.prepareDSContext = new PrepareContext() { |
||
707 | public Window getOwnerWindow() { |
||
708 | return null; |
||
709 | } |
||
710 | 40435 | jjdelcerro | |
711 | 41495 | jjdelcerro | public IProjection getViewProjection() {
|
712 | return WizardDB.this.getMapContext().getProjection();
|
||
713 | } |
||
714 | 40435 | jjdelcerro | |
715 | 41495 | jjdelcerro | }; |
716 | } |
||
717 | return this.prepareDSContext; |
||
718 | } |
||
719 | 40435 | jjdelcerro | |
720 | 41495 | jjdelcerro | @Override
|
721 | public void close() { |
||
722 | 40435 | jjdelcerro | |
723 | 41495 | jjdelcerro | } |
724 | 40435 | jjdelcerro | |
725 | 41495 | jjdelcerro | protected String getLastMessage(Throwable ex) { |
726 | 40435 | jjdelcerro | |
727 | 41126 | jldominguez | Throwable p = ex;
|
728 | 41495 | jjdelcerro | while ( p.getCause() != null && p.getCause() != p ) { |
729 | 41126 | jldominguez | p = p.getCause(); |
730 | } |
||
731 | return p.getMessage();
|
||
732 | 41495 | jjdelcerro | } |
733 | 41126 | jldominguez | |
734 | 40435 | jjdelcerro | } // @jve:decl-index=0:visual-constraint="10,10" |