svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / jdbc / DefaultJDBCConnectionDialog.java @ 46450
History | View | Annotate | Download (15.3 KB)
1 |
/**
|
---|---|
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 |
* 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 |
* 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 |
package org.gvsig.fmap.dal.swing.impl.jdbc; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ActionListener; |
29 |
import java.awt.event.ItemEvent; |
30 |
import java.awt.event.ItemListener; |
31 |
import java.io.File; |
32 |
import java.io.FileInputStream; |
33 |
import java.io.FileOutputStream; |
34 |
import java.io.InputStream; |
35 |
import java.io.OutputStream; |
36 |
import java.util.Objects; |
37 |
import javax.swing.DefaultComboBoxModel; |
38 |
import javax.swing.JOptionPane; |
39 |
import javax.swing.filechooser.FileFilter; |
40 |
import org.apache.commons.io.IOUtils; |
41 |
import org.apache.commons.lang3.ArrayUtils; |
42 |
import org.apache.commons.lang3.StringUtils; |
43 |
import org.gvsig.filedialogchooser.FileDialogChooser; |
44 |
import org.gvsig.fmap.dal.DALLocator; |
45 |
import org.gvsig.fmap.dal.DataManager; |
46 |
import org.gvsig.fmap.dal.DataServerExplorerPool; |
47 |
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
48 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
49 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
50 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
51 |
import org.gvsig.fmap.dal.swing.dataStoreParameters.DataStoreParametersPanel; |
52 |
import org.gvsig.fmap.dal.swing.dataStoreParameters.DataStoreParametersPanelManager; |
53 |
import org.gvsig.fmap.dal.swing.impl.dataStoreParameters.DataStoreDynObjectParametersPanel; |
54 |
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionPanel; |
55 |
import org.gvsig.tools.ToolsLocator; |
56 |
import org.gvsig.tools.i18n.I18nManager; |
57 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
58 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
59 |
|
60 |
import org.slf4j.Logger; |
61 |
import org.slf4j.LoggerFactory; |
62 |
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionDialog; |
63 |
import org.gvsig.tools.folders.FoldersManager; |
64 |
import org.gvsig.tools.persistence.PersistenceManager; |
65 |
import org.gvsig.tools.persistence.PersistentState; |
66 |
import org.gvsig.tools.swing.api.DropDown; |
67 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
68 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
69 |
import org.gvsig.tools.util.LabeledValue; |
70 |
import org.gvsig.tools.util.LabeledValueImpl; |
71 |
|
72 |
|
73 |
|
74 |
|
75 |
@SuppressWarnings("UseSpecificCatch") |
76 |
public class DefaultJDBCConnectionDialog extends DefaultJDBCConnectionDialogView implements JDBCConnectionDialog { |
77 |
|
78 |
private static final long serialVersionUID = -5493563028200403559L; |
79 |
|
80 |
private static final Logger LOG = LoggerFactory |
81 |
.getLogger(DefaultJDBCConnectionDialog.class); |
82 |
|
83 |
private boolean isCanceled = true; |
84 |
private JDBCConnectionPanel jdbcServerExplorer;
|
85 |
private DropDown ddnMore;
|
86 |
|
87 |
public DefaultJDBCConnectionDialog() {
|
88 |
initComponents(); |
89 |
} |
90 |
|
91 |
private void initComponents() { |
92 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
93 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
94 |
this.btnAcept.setText(i18n.getTranslation("ok")); |
95 |
this.btnAcept.addActionListener(new ActionListener() { |
96 |
@Override
|
97 |
public void actionPerformed(ActionEvent ae) { |
98 |
doAcept(); |
99 |
} |
100 |
}); |
101 |
this.btnCancel.setText(i18n.getTranslation("cancel")); |
102 |
this.btnCancel.addActionListener(new ActionListener() { |
103 |
@Override
|
104 |
public void actionPerformed(ActionEvent ae) { |
105 |
doCancel(); |
106 |
} |
107 |
}); |
108 |
|
109 |
toolsSwingManager.translate(this.btnMore);
|
110 |
DefaultComboBoxModel<LabeledValue<String>> model = new DefaultComboBoxModel<>(); |
111 |
model.addElement(new LabeledValueImpl<>(i18n.getTranslation("advanced"), "advanced")); |
112 |
model.addElement(new LabeledValueImpl<>(i18n.getTranslation("delete"), "delete")); |
113 |
model.addElement(new LabeledValueImpl<>(i18n.getTranslation("load"), "load")); |
114 |
model.addElement(new LabeledValueImpl<>(i18n.getTranslation("save"), "save")); |
115 |
model.addElement(new LabeledValueImpl<>(i18n.getTranslation("_Test_connection"), "test")); |
116 |
this.ddnMore = toolsSwingManager.createDropDown(this.btnMore); |
117 |
this.ddnMore.setModel(model);
|
118 |
this.ddnMore.addItemListener(new ItemListener() { |
119 |
@Override
|
120 |
public void itemStateChanged(ItemEvent e) { |
121 |
LabeledValue<String> selectedItem = (LabeledValue<String>) ddnMore.getSelectedItem(); |
122 |
switch(selectedItem.getValue()) {
|
123 |
case "advanced": |
124 |
doAdvanced(); |
125 |
break;
|
126 |
case "delete": |
127 |
doDelete(); |
128 |
break;
|
129 |
case "load": |
130 |
doLoad(); |
131 |
break;
|
132 |
case "save": |
133 |
doSave(); |
134 |
break;
|
135 |
case "test": |
136 |
doTest(); |
137 |
break;
|
138 |
} |
139 |
} |
140 |
}); |
141 |
|
142 |
this.jdbcServerExplorer = DALSwingLocator.getSwingManager().createJDBCConnectionPanel();
|
143 |
this.containerJDBCConnectionPanel.setLayout(new BorderLayout()); |
144 |
this.containerJDBCConnectionPanel.add(
|
145 |
this.jdbcServerExplorer.asJComponent(),
|
146 |
BorderLayout.CENTER
|
147 |
); |
148 |
} |
149 |
|
150 |
protected void doLoad() { |
151 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
152 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
153 |
FoldersManager folderManager = ToolsLocator.getFoldersManager(); |
154 |
DataManager dataManager = DALLocator.getDataManager(); |
155 |
|
156 |
File initialPath = folderManager.getLastPath("CONNECTIONS", null); |
157 |
File[] f = dialogs.showChooserDialog( |
158 |
i18n.getTranslation("_Load_connection"),
|
159 |
FileDialogChooser.OPEN_DIALOG, |
160 |
FileDialogChooser.FILES_ONLY, |
161 |
false,
|
162 |
initialPath, |
163 |
new FileFilter() { |
164 |
@Override
|
165 |
public boolean accept(File f) { |
166 |
return Objects.toString(f,"").toLowerCase().endsWith(".gvdbcon"); |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public String getDescription() { |
171 |
return "Database connection (gvdbcon)"; |
172 |
} |
173 |
}, |
174 |
true
|
175 |
); |
176 |
if( ArrayUtils.isEmpty(f) || f[0]==null ) { |
177 |
return;
|
178 |
} |
179 |
folderManager.setLastPath("CONNECTIONS", f[0].getParentFile()); |
180 |
InputStream in = null; |
181 |
try {
|
182 |
in = new FileInputStream(f[0]); |
183 |
PersistenceManager persistence = ToolsLocator.getPersistenceManager(); |
184 |
DataServerExplorerPoolEntry entry = (DataServerExplorerPoolEntry) persistence.getObject(in); |
185 |
LabeledValueImpl<JDBCServerExplorerParameters> parameters = new LabeledValueImpl<>(
|
186 |
entry.getName(), |
187 |
(JDBCServerExplorerParameters)entry.getExplorerParameters() |
188 |
); |
189 |
this.jdbcServerExplorer.setLabeledServerExplorerParameters(parameters);
|
190 |
} catch(Throwable th) { |
191 |
LOG.warn("Problems loading connection",th);
|
192 |
dialogs.messageDialog( |
193 |
i18n.getTranslation("_Problems_loading_the_connection"),
|
194 |
i18n.getTranslation("_Load_connection"),
|
195 |
JOptionPane.WARNING_MESSAGE
|
196 |
); |
197 |
// return;
|
198 |
} finally {
|
199 |
IOUtils.closeQuietly(in); |
200 |
} |
201 |
} |
202 |
|
203 |
protected void doSave() { |
204 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
205 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
206 |
FoldersManager folderManager = ToolsLocator.getFoldersManager(); |
207 |
DataManager dataManager = DALLocator.getDataManager(); |
208 |
|
209 |
String problems = this.jdbcServerExplorer.getParametersProblems(); |
210 |
if( !StringUtils.isBlank(problems) ) {
|
211 |
dialogs.messageDialog( |
212 |
i18n.getTranslation("_Some_values_are_invalids")+"\n\n"+problems, |
213 |
i18n.getTranslation("_Save_connection"),
|
214 |
JOptionPane.WARNING_MESSAGE
|
215 |
); |
216 |
return;
|
217 |
} |
218 |
|
219 |
LabeledValue<JDBCServerExplorerParameters> parameters = this.getLabeledServerExplorerParameters();
|
220 |
if( parameters == null || parameters.getValue()==null ) { |
221 |
dialogs.messageDialog( |
222 |
i18n.getTranslation("_There_is_no_connection_to_save"),
|
223 |
i18n.getTranslation("_Save_connection"),
|
224 |
JOptionPane.INFORMATION_MESSAGE
|
225 |
); |
226 |
return;
|
227 |
} |
228 |
File initialPath = folderManager.getLastPath("CONNECTIONS", null); |
229 |
File[] f = dialogs.showChooserDialog( |
230 |
i18n.getTranslation("_Save_connection"),
|
231 |
FileDialogChooser.SAVE_DIALOG, |
232 |
FileDialogChooser.FILES_ONLY, |
233 |
false,
|
234 |
initialPath, |
235 |
new FileFilter() { |
236 |
@Override
|
237 |
public boolean accept(File f) { |
238 |
return Objects.toString(f,"").toLowerCase().endsWith(".gvdbcon"); |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public String getDescription() { |
243 |
return "Database connection (gvdbcon)"; |
244 |
} |
245 |
}, |
246 |
true
|
247 |
); |
248 |
if( ArrayUtils.isEmpty(f) || f[0]==null ) { |
249 |
return;
|
250 |
} |
251 |
folderManager.setLastPath("CONNECTIONS", f[0].getParentFile()); |
252 |
|
253 |
File paramsFile = f[0]; |
254 |
if( !f[0].toString().toLowerCase().endsWith(".gvdbcon") ) { |
255 |
paramsFile = new File(f[0].toString()+".gvdbcon"); |
256 |
} |
257 |
|
258 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
259 |
DataServerExplorerPoolEntry entry = pool.createEntry( |
260 |
parameters.getLabel(), |
261 |
null,
|
262 |
parameters.getValue() |
263 |
); |
264 |
|
265 |
OutputStream out = null; |
266 |
try {
|
267 |
out = new FileOutputStream(paramsFile); |
268 |
PersistenceManager persistence = ToolsLocator.getPersistenceManager(); |
269 |
PersistentState state = persistence.getState(entry); |
270 |
persistence.saveState(state, out); |
271 |
} catch(Throwable th) { |
272 |
LOG.warn("Problems saving connection",th);
|
273 |
dialogs.messageDialog( |
274 |
i18n.getTranslation("_Problems_saving_the_connection"),
|
275 |
i18n.getTranslation("_Save_connection"),
|
276 |
JOptionPane.WARNING_MESSAGE
|
277 |
); |
278 |
// return;
|
279 |
} finally {
|
280 |
IOUtils.closeQuietly(out); |
281 |
} |
282 |
} |
283 |
|
284 |
protected void doTest() { |
285 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
286 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
287 |
try {
|
288 |
String problems = this.jdbcServerExplorer.getParametersProblems(); |
289 |
if( StringUtils.isBlank(problems) ) {
|
290 |
JDBCServerExplorerParameters params = this.jdbcServerExplorer.getServerExplorerParametersWithoutRegister();
|
291 |
JDBCServerExplorer server = (JDBCServerExplorer) DALLocator.getDataManager().openServerExplorer( |
292 |
params.getProviderName(), params |
293 |
); |
294 |
if( server.exists() ) {
|
295 |
dialogs.messageDialog( |
296 |
i18n.getTranslation("_Connection_ok"),
|
297 |
i18n.getTranslation("_Connection"),
|
298 |
JOptionPane.WARNING_MESSAGE
|
299 |
); |
300 |
return;
|
301 |
} |
302 |
} |
303 |
dialogs.messageDialog( |
304 |
i18n.getTranslation("_Test_connection_failed")+(problems==null?"":("\n\n"+problems)), |
305 |
i18n.getTranslation("_Connection"),
|
306 |
JOptionPane.WARNING_MESSAGE
|
307 |
); |
308 |
} catch (Throwable ex) { |
309 |
dialogs.messageDialog( |
310 |
i18n.getTranslation("_Test_connection_failed"),
|
311 |
i18n.getTranslation("_Connection"),
|
312 |
JOptionPane.WARNING_MESSAGE
|
313 |
); |
314 |
} |
315 |
} |
316 |
|
317 |
protected void doAdvanced() { |
318 |
JDBCServerExplorerParameters myParams = this.jdbcServerExplorer.getServerExplorerParametersWithoutRegister();
|
319 |
try {
|
320 |
myParams.validate(); |
321 |
} catch (Exception e) { |
322 |
// ignore... only for fill default values
|
323 |
} |
324 |
DataStoreParametersPanelManager paramsManager = DALSwingLocator.getDataStoreParametersPanelManager(); |
325 |
DataStoreParametersPanel panel = new DataStoreDynObjectParametersPanel(myParams);
|
326 |
paramsManager.showPropertiesDialog(myParams, panel); |
327 |
this.jdbcServerExplorer.setServerExplorerParameters(myParams);
|
328 |
} |
329 |
|
330 |
protected void doAcept() { |
331 |
this.isCanceled = false; |
332 |
String problems = this.jdbcServerExplorer.getParametersProblems(); |
333 |
if( !StringUtils.isBlank(problems) ) {
|
334 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
335 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
336 |
dialogs.messageDialog( |
337 |
i18n.getTranslation("_Some_values_are_invalids")+"\n\n"+problems, |
338 |
i18n.getTranslation("_Connection"),
|
339 |
JOptionPane.WARNING_MESSAGE
|
340 |
); |
341 |
return;
|
342 |
} |
343 |
this.setVisible(false); |
344 |
} |
345 |
|
346 |
protected void doCancel() { |
347 |
this.isCanceled = true; |
348 |
this.setVisible(false); |
349 |
} |
350 |
|
351 |
protected void doDelete() { |
352 |
this.jdbcServerExplorer.delete();
|
353 |
this.jdbcServerExplorer.clear();
|
354 |
} |
355 |
|
356 |
@Override
|
357 |
public void showDialog() { |
358 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
359 |
WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
360 |
winmgr.showWindow( |
361 |
this,
|
362 |
i18nManager.getTranslation("_Connection_parameters"),
|
363 |
WindowManager.MODE.DIALOG |
364 |
); |
365 |
} |
366 |
|
367 |
@Override
|
368 |
public boolean isCanceled() { |
369 |
return this.isCanceled; |
370 |
} |
371 |
|
372 |
@Override
|
373 |
public JDBCServerExplorerParameters getServerExplorerParameters() {
|
374 |
return this.jdbcServerExplorer.getServerExplorerParametersAndRegister(); |
375 |
} |
376 |
|
377 |
@Override
|
378 |
public LabeledValue<JDBCServerExplorerParameters> getLabeledServerExplorerParameters() {
|
379 |
return this.jdbcServerExplorer.getLabeledServerExplorerParameters(); |
380 |
} |
381 |
|
382 |
@Override
|
383 |
public String getConnectionName() { |
384 |
return this.jdbcServerExplorer.getConnectionName(); |
385 |
} |
386 |
|
387 |
} |
388 |
|