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 @ 44422
History | View | Annotate | Download (13.3 KB)
1 | 40557 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40557 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | 41633 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
11 | 41633 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
16 | 41633 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
21 | 41633 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | 44308 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.jdbc; |
25 | 40435 | jjdelcerro | |
26 | 42616 | jjdelcerro | import java.awt.BorderLayout; |
27 | 40435 | jjdelcerro | import java.awt.event.ActionEvent; |
28 | import java.awt.event.ActionListener; |
||
29 | 44419 | jjdelcerro | 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 | 44422 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
43 | 44419 | jjdelcerro | 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 | 41633 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
49 | 42616 | jjdelcerro | import org.gvsig.fmap.dal.swing.DALSwingLocator; |
50 | 44308 | jjdelcerro | import org.gvsig.fmap.dal.swing.dataStoreParameters.DataStoreParametersPanel; |
51 | import org.gvsig.fmap.dal.swing.dataStoreParameters.DataStoreParametersPanelManager; |
||
52 | import org.gvsig.fmap.dal.swing.impl.dataStoreParameters.DataStoreDynObjectParametersPanel; |
||
53 | 42616 | jjdelcerro | import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionPanel; |
54 | 41633 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
55 | import org.gvsig.tools.i18n.I18nManager; |
||
56 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
57 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
58 | 40435 | jjdelcerro | |
59 | import org.slf4j.Logger; |
||
60 | import org.slf4j.LoggerFactory; |
||
61 | 44308 | jjdelcerro | import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionDialog; |
62 | 44419 | jjdelcerro | import org.gvsig.tools.folders.FoldersManager; |
63 | import org.gvsig.tools.persistence.PersistenceManager; |
||
64 | import org.gvsig.tools.persistence.PersistentState; |
||
65 | import org.gvsig.tools.swing.api.DropDown; |
||
66 | import org.gvsig.tools.swing.api.ToolsSwingManager; |
||
67 | import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
||
68 | import org.gvsig.tools.util.LabeledValue; |
||
69 | import org.gvsig.tools.util.LabeledValueImpl; |
||
70 | 40435 | jjdelcerro | |
71 | |||
72 | |||
73 | 41633 | jjdelcerro | |
74 | 44419 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
75 | 44308 | jjdelcerro | public class DefaultJDBCConnectionDialog extends DefaultJDBCConnectionDialogView implements JDBCConnectionDialog { |
76 | 41633 | jjdelcerro | |
77 | 40435 | jjdelcerro | private static final long serialVersionUID = -5493563028200403559L; |
78 | |||
79 | private static final Logger LOG = LoggerFactory |
||
80 | 44308 | jjdelcerro | .getLogger(DefaultJDBCConnectionDialog.class); |
81 | 40435 | jjdelcerro | |
82 | 41633 | jjdelcerro | private boolean isCanceled = false; |
83 | 42616 | jjdelcerro | private JDBCConnectionPanel jdbcServerExplorer;
|
84 | 44419 | jjdelcerro | private DropDown ddnMore;
|
85 | 40435 | jjdelcerro | |
86 | 44308 | jjdelcerro | public DefaultJDBCConnectionDialog() {
|
87 | 41633 | jjdelcerro | initComponents(); |
88 | 40435 | jjdelcerro | } |
89 | 41633 | jjdelcerro | |
90 | private void initComponents() { |
||
91 | 44419 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
92 | ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
||
93 | this.btnAcept.setText(i18n.getTranslation("ok")); |
||
94 | 42616 | jjdelcerro | this.btnAcept.addActionListener(new ActionListener() { |
95 | @Override
|
||
96 | 41633 | jjdelcerro | public void actionPerformed(ActionEvent ae) { |
97 | doAcept(); |
||
98 | 41627 | jjdelcerro | } |
99 | 41633 | jjdelcerro | }); |
100 | 44419 | jjdelcerro | this.btnCancel.setText(i18n.getTranslation("cancel")); |
101 | 42616 | jjdelcerro | this.btnCancel.addActionListener(new ActionListener() { |
102 | @Override
|
||
103 | 41633 | jjdelcerro | public void actionPerformed(ActionEvent ae) { |
104 | doCancel(); |
||
105 | 40435 | jjdelcerro | } |
106 | 41633 | jjdelcerro | }); |
107 | 44419 | jjdelcerro | DefaultComboBoxModel<LabeledValue<String>> model = new DefaultComboBoxModel<>(); |
108 | model.addElement(new LabeledValueImpl<>(i18n.getTranslation("advanced"), "advanced")); |
||
109 | model.addElement(new LabeledValueImpl<>(i18n.getTranslation("delete"), "delete")); |
||
110 | model.addElement(new LabeledValueImpl<>(i18n.getTranslation("load"), "load")); |
||
111 | model.addElement(new LabeledValueImpl<>(i18n.getTranslation("save"), "save")); |
||
112 | this.ddnMore = toolsSwingManager.createDropDown(this.btnMore); |
||
113 | this.ddnMore.setModel(model);
|
||
114 | this.ddnMore.addItemListener(new ItemListener() { |
||
115 | 42616 | jjdelcerro | @Override
|
116 | 44419 | jjdelcerro | public void itemStateChanged(ItemEvent e) { |
117 | LabeledValue<String> selectedItem = (LabeledValue<String>) ddnMore.getSelectedItem(); |
||
118 | switch(selectedItem.getValue()) {
|
||
119 | case "advanced": |
||
120 | doAdvanced(); |
||
121 | break;
|
||
122 | case "delete": |
||
123 | doDelete(); |
||
124 | break;
|
||
125 | case "load": |
||
126 | doLoad(); |
||
127 | break;
|
||
128 | case "save": |
||
129 | doSave(); |
||
130 | break;
|
||
131 | } |
||
132 | 42046 | jbadia | } |
133 | }); |
||
134 | 44419 | jjdelcerro | |
135 | 42616 | jjdelcerro | this.jdbcServerExplorer = DALSwingLocator.getSwingManager().createJDBCConnectionPanel();
|
136 | this.containerJDBCConnectionPanel.setLayout(new BorderLayout()); |
||
137 | this.containerJDBCConnectionPanel.add(
|
||
138 | this.jdbcServerExplorer.asJComponent(),
|
||
139 | BorderLayout.CENTER
|
||
140 | ); |
||
141 | 40435 | jjdelcerro | } |
142 | 44419 | jjdelcerro | |
143 | protected void doLoad() { |
||
144 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
145 | ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
||
146 | FoldersManager folderManager = ToolsLocator.getFoldersManager(); |
||
147 | DataManager dataManager = DALLocator.getDataManager(); |
||
148 | |||
149 | File initialPath = folderManager.getLastPath("CONNECTIONS", null); |
||
150 | File[] f = dialogs.showChooserDialog( |
||
151 | i18n.getTranslation("_Load_connection"),
|
||
152 | FileDialogChooser.OPEN_DIALOG, |
||
153 | FileDialogChooser.FILES_ONLY, |
||
154 | false,
|
||
155 | initialPath, |
||
156 | new FileFilter() { |
||
157 | @Override
|
||
158 | public boolean accept(File f) { |
||
159 | return Objects.toString(f,"").toLowerCase().endsWith(".gvdbcon"); |
||
160 | } |
||
161 | |||
162 | @Override
|
||
163 | public String getDescription() { |
||
164 | return "Database connection (gvdbcon)"; |
||
165 | } |
||
166 | }, |
||
167 | true
|
||
168 | ); |
||
169 | if( ArrayUtils.isEmpty(f) || f[0]==null ) { |
||
170 | return;
|
||
171 | } |
||
172 | folderManager.setLastPath("CONNECTIONS", f[0].getParentFile()); |
||
173 | InputStream in = null; |
||
174 | try {
|
||
175 | in = new FileInputStream(f[0]); |
||
176 | PersistenceManager persistence = ToolsLocator.getPersistenceManager(); |
||
177 | DataServerExplorerPoolEntry entry = (DataServerExplorerPoolEntry) persistence.getObject(in); |
||
178 | LabeledValueImpl<JDBCServerExplorerParameters> parameters = new LabeledValueImpl<>(
|
||
179 | entry.getName(), |
||
180 | (JDBCServerExplorerParameters)entry.getExplorerParameters() |
||
181 | ); |
||
182 | this.jdbcServerExplorer.setLabeledServerExplorerParameters(parameters);
|
||
183 | } catch(Throwable th) { |
||
184 | LOG.warn("Problems loading connection",th);
|
||
185 | dialogs.messageDialog( |
||
186 | i18n.getTranslation("_Problems_loading_the_connection"),
|
||
187 | i18n.getTranslation("_Load_connection"),
|
||
188 | JOptionPane.WARNING_MESSAGE
|
||
189 | ); |
||
190 | // return;
|
||
191 | } finally {
|
||
192 | IOUtils.closeQuietly(in); |
||
193 | } |
||
194 | } |
||
195 | 41633 | jjdelcerro | |
196 | 44419 | jjdelcerro | protected void doSave() { |
197 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
198 | ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
||
199 | FoldersManager folderManager = ToolsLocator.getFoldersManager(); |
||
200 | DataManager dataManager = DALLocator.getDataManager(); |
||
201 | |||
202 | 44422 | jjdelcerro | String problems = this.jdbcServerExplorer.getParametersProblems(); |
203 | if( !StringUtils.isBlank(problems) ) {
|
||
204 | dialogs.message( |
||
205 | i18n.getTranslation("_Some_values_are_invalids")+"\n\n"+problems, |
||
206 | JOptionPane.WARNING_MESSAGE
|
||
207 | ); |
||
208 | return;
|
||
209 | } |
||
210 | |||
211 | 44419 | jjdelcerro | LabeledValue<JDBCServerExplorerParameters> parameters = this.getLabeledServerExplorerParameters();
|
212 | if( parameters == null || parameters.getValue()==null ) { |
||
213 | dialogs.messageDialog( |
||
214 | i18n.getTranslation("_There_is_no_connection_to_save"),
|
||
215 | i18n.getTranslation("_Save_connection"),
|
||
216 | JOptionPane.INFORMATION_MESSAGE
|
||
217 | ); |
||
218 | return;
|
||
219 | } |
||
220 | File initialPath = folderManager.getLastPath("CONNECTIONS", null); |
||
221 | File[] f = dialogs.showChooserDialog( |
||
222 | i18n.getTranslation("_Save_connection"),
|
||
223 | FileDialogChooser.SAVE_DIALOG, |
||
224 | FileDialogChooser.FILES_ONLY, |
||
225 | false,
|
||
226 | initialPath, |
||
227 | new FileFilter() { |
||
228 | @Override
|
||
229 | public boolean accept(File f) { |
||
230 | return Objects.toString(f,"").toLowerCase().endsWith(".gvdbcon"); |
||
231 | } |
||
232 | |||
233 | @Override
|
||
234 | public String getDescription() { |
||
235 | return "Database connection (gvdbcon)"; |
||
236 | } |
||
237 | }, |
||
238 | true
|
||
239 | ); |
||
240 | if( ArrayUtils.isEmpty(f) || f[0]==null ) { |
||
241 | return;
|
||
242 | } |
||
243 | folderManager.setLastPath("CONNECTIONS", f[0].getParentFile()); |
||
244 | |||
245 | File paramsFile = f[0]; |
||
246 | if( !f[0].toString().toLowerCase().endsWith(".gvdbcon") ) { |
||
247 | paramsFile = new File(f[0].toString()+".gvdbcon"); |
||
248 | } |
||
249 | |||
250 | DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
||
251 | DataServerExplorerPoolEntry entry = pool.createEntry( |
||
252 | parameters.getLabel(), |
||
253 | null,
|
||
254 | parameters.getValue() |
||
255 | ); |
||
256 | |||
257 | OutputStream out = null; |
||
258 | try {
|
||
259 | out = new FileOutputStream(paramsFile); |
||
260 | PersistenceManager persistence = ToolsLocator.getPersistenceManager(); |
||
261 | PersistentState state = persistence.getState(entry); |
||
262 | persistence.saveState(state, out); |
||
263 | } catch(Throwable th) { |
||
264 | LOG.warn("Problems saving connection",th);
|
||
265 | dialogs.messageDialog( |
||
266 | i18n.getTranslation("_Problems_saving_the_connection"),
|
||
267 | i18n.getTranslation("_Save_connection"),
|
||
268 | JOptionPane.WARNING_MESSAGE
|
||
269 | ); |
||
270 | // return;
|
||
271 | } finally {
|
||
272 | IOUtils.closeQuietly(out); |
||
273 | } |
||
274 | } |
||
275 | |||
276 | 41633 | jjdelcerro | protected void doAdvanced() { |
277 | JDBCServerExplorerParameters myParams = this.getServerExplorerParameters();
|
||
278 | 40435 | jjdelcerro | try {
|
279 | myParams.validate(); |
||
280 | } catch (Exception e) { |
||
281 | // ignore... only for fill default values
|
||
282 | } |
||
283 | 44308 | jjdelcerro | DataStoreParametersPanelManager paramsManager = DALSwingLocator.getDataStoreParametersPanelManager(); |
284 | DataStoreParametersPanel panel = new DataStoreDynObjectParametersPanel(myParams);
|
||
285 | paramsManager.showPropertiesDialog(myParams, panel); |
||
286 | 40435 | jjdelcerro | } |
287 | 41633 | jjdelcerro | |
288 | protected void doAcept() { |
||
289 | this.isCanceled = false; |
||
290 | 44422 | jjdelcerro | String problems = this.jdbcServerExplorer.getParametersProblems(); |
291 | if( !StringUtils.isBlank(problems) ) {
|
||
292 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
293 | ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
||
294 | dialogs.message( |
||
295 | i18n.getTranslation("_Some_values_are_invalids")+"\n\n"+problems, |
||
296 | JOptionPane.WARNING_MESSAGE
|
||
297 | ); |
||
298 | return;
|
||
299 | } |
||
300 | 41633 | jjdelcerro | this.setVisible(false); |
301 | 40435 | jjdelcerro | } |
302 | 41633 | jjdelcerro | |
303 | protected void doCancel() { |
||
304 | this.isCanceled = true; |
||
305 | this.setVisible(false); |
||
306 | 40435 | jjdelcerro | } |
307 | 41633 | jjdelcerro | |
308 | 42046 | jbadia | protected void doDelete() { |
309 | 43377 | jjdelcerro | this.jdbcServerExplorer.delete();
|
310 | 42046 | jbadia | this.jdbcServerExplorer.clear();
|
311 | } |
||
312 | |||
313 | 44308 | jjdelcerro | @Override
|
314 | 41633 | jjdelcerro | public void showDialog() { |
315 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
316 | WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
||
317 | winmgr.showWindow( |
||
318 | this,
|
||
319 | i18nManager.getTranslation("_Connection_parameters"),
|
||
320 | WindowManager.MODE.DIALOG |
||
321 | ); |
||
322 | 40435 | jjdelcerro | } |
323 | 41633 | jjdelcerro | |
324 | 44308 | jjdelcerro | @Override
|
325 | 41633 | jjdelcerro | public boolean isCanceled() { |
326 | return this.isCanceled; |
||
327 | 40435 | jjdelcerro | } |
328 | 41633 | jjdelcerro | |
329 | 44308 | jjdelcerro | @Override
|
330 | 41633 | jjdelcerro | public JDBCServerExplorerParameters getServerExplorerParameters() {
|
331 | return this.jdbcServerExplorer.getServerExplorerParameters(); |
||
332 | 40435 | jjdelcerro | } |
333 | 41633 | jjdelcerro | |
334 | 44308 | jjdelcerro | @Override
|
335 | 44419 | jjdelcerro | public LabeledValue<JDBCServerExplorerParameters> getLabeledServerExplorerParameters() {
|
336 | return this.jdbcServerExplorer.getLabeledServerExplorerParameters(); |
||
337 | } |
||
338 | |||
339 | @Override
|
||
340 | 40435 | jjdelcerro | public String getConnectionName() { |
341 | 41633 | jjdelcerro | return this.jdbcServerExplorer.getConnectionName(); |
342 | 40435 | jjdelcerro | } |
343 | 41633 | jjdelcerro | |
344 | } |