svn-gvsig-desktop / branches / org.gvsig.desktop-cvsgis1 / 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 @ 45422
History | View | Annotate | Download (13.4 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.jdbc.JDBCServerExplorerParameters; |
49 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
50 |
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 |
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionPanel; |
54 |
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 |
|
59 |
import org.slf4j.Logger; |
60 |
import org.slf4j.LoggerFactory; |
61 |
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionDialog; |
62 |
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 |
|
71 |
|
72 |
|
73 |
|
74 |
@SuppressWarnings("UseSpecificCatch") |
75 |
public class DefaultJDBCConnectionDialog extends DefaultJDBCConnectionDialogView implements JDBCConnectionDialog { |
76 |
|
77 |
private static final long serialVersionUID = -5493563028200403559L; |
78 |
|
79 |
private static final Logger LOG = LoggerFactory |
80 |
.getLogger(DefaultJDBCConnectionDialog.class); |
81 |
|
82 |
private boolean isCanceled = true; |
83 |
private JDBCConnectionPanel jdbcServerExplorer;
|
84 |
private DropDown ddnMore;
|
85 |
|
86 |
public DefaultJDBCConnectionDialog() {
|
87 |
initComponents(); |
88 |
} |
89 |
|
90 |
private void initComponents() { |
91 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
92 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
93 |
this.btnAcept.setText(i18n.getTranslation("ok")); |
94 |
this.btnAcept.addActionListener(new ActionListener() { |
95 |
@Override
|
96 |
public void actionPerformed(ActionEvent ae) { |
97 |
doAcept(); |
98 |
} |
99 |
}); |
100 |
this.btnCancel.setText(i18n.getTranslation("cancel")); |
101 |
this.btnCancel.addActionListener(new ActionListener() { |
102 |
@Override
|
103 |
public void actionPerformed(ActionEvent ae) { |
104 |
doCancel(); |
105 |
} |
106 |
}); |
107 |
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 |
@Override
|
116 |
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 |
} |
133 |
}); |
134 |
|
135 |
this.jdbcServerExplorer = DALSwingLocator.getSwingManager().createJDBCConnectionPanel();
|
136 |
this.containerJDBCConnectionPanel.setLayout(new BorderLayout()); |
137 |
this.containerJDBCConnectionPanel.add(
|
138 |
this.jdbcServerExplorer.asJComponent(),
|
139 |
BorderLayout.CENTER
|
140 |
); |
141 |
} |
142 |
|
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 |
|
196 |
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 |
String problems = this.jdbcServerExplorer.getParametersProblems(); |
203 |
if( !StringUtils.isBlank(problems) ) {
|
204 |
dialogs.messageDialog( |
205 |
i18n.getTranslation("_Some_values_are_invalids")+"\n\n"+problems, |
206 |
i18n.getTranslation("_Save_connection"),
|
207 |
JOptionPane.WARNING_MESSAGE
|
208 |
); |
209 |
return;
|
210 |
} |
211 |
|
212 |
LabeledValue<JDBCServerExplorerParameters> parameters = this.getLabeledServerExplorerParameters();
|
213 |
if( parameters == null || parameters.getValue()==null ) { |
214 |
dialogs.messageDialog( |
215 |
i18n.getTranslation("_There_is_no_connection_to_save"),
|
216 |
i18n.getTranslation("_Save_connection"),
|
217 |
JOptionPane.INFORMATION_MESSAGE
|
218 |
); |
219 |
return;
|
220 |
} |
221 |
File initialPath = folderManager.getLastPath("CONNECTIONS", null); |
222 |
File[] f = dialogs.showChooserDialog( |
223 |
i18n.getTranslation("_Save_connection"),
|
224 |
FileDialogChooser.SAVE_DIALOG, |
225 |
FileDialogChooser.FILES_ONLY, |
226 |
false,
|
227 |
initialPath, |
228 |
new FileFilter() { |
229 |
@Override
|
230 |
public boolean accept(File f) { |
231 |
return Objects.toString(f,"").toLowerCase().endsWith(".gvdbcon"); |
232 |
} |
233 |
|
234 |
@Override
|
235 |
public String getDescription() { |
236 |
return "Database connection (gvdbcon)"; |
237 |
} |
238 |
}, |
239 |
true
|
240 |
); |
241 |
if( ArrayUtils.isEmpty(f) || f[0]==null ) { |
242 |
return;
|
243 |
} |
244 |
folderManager.setLastPath("CONNECTIONS", f[0].getParentFile()); |
245 |
|
246 |
File paramsFile = f[0]; |
247 |
if( !f[0].toString().toLowerCase().endsWith(".gvdbcon") ) { |
248 |
paramsFile = new File(f[0].toString()+".gvdbcon"); |
249 |
} |
250 |
|
251 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
252 |
DataServerExplorerPoolEntry entry = pool.createEntry( |
253 |
parameters.getLabel(), |
254 |
null,
|
255 |
parameters.getValue() |
256 |
); |
257 |
|
258 |
OutputStream out = null; |
259 |
try {
|
260 |
out = new FileOutputStream(paramsFile); |
261 |
PersistenceManager persistence = ToolsLocator.getPersistenceManager(); |
262 |
PersistentState state = persistence.getState(entry); |
263 |
persistence.saveState(state, out); |
264 |
} catch(Throwable th) { |
265 |
LOG.warn("Problems saving connection",th);
|
266 |
dialogs.messageDialog( |
267 |
i18n.getTranslation("_Problems_saving_the_connection"),
|
268 |
i18n.getTranslation("_Save_connection"),
|
269 |
JOptionPane.WARNING_MESSAGE
|
270 |
); |
271 |
// return;
|
272 |
} finally {
|
273 |
IOUtils.closeQuietly(out); |
274 |
} |
275 |
} |
276 |
|
277 |
protected void doAdvanced() { |
278 |
JDBCServerExplorerParameters myParams = this.getServerExplorerParameters();
|
279 |
try {
|
280 |
myParams.validate(); |
281 |
} catch (Exception e) { |
282 |
// ignore... only for fill default values
|
283 |
} |
284 |
DataStoreParametersPanelManager paramsManager = DALSwingLocator.getDataStoreParametersPanelManager(); |
285 |
DataStoreParametersPanel panel = new DataStoreDynObjectParametersPanel(myParams);
|
286 |
paramsManager.showPropertiesDialog(myParams, panel); |
287 |
} |
288 |
|
289 |
protected void doAcept() { |
290 |
this.isCanceled = false; |
291 |
String problems = this.jdbcServerExplorer.getParametersProblems(); |
292 |
if( !StringUtils.isBlank(problems) ) {
|
293 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
294 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
295 |
dialogs.messageDialog( |
296 |
i18n.getTranslation("_Some_values_are_invalids")+"\n\n"+problems, |
297 |
i18n.getTranslation("_Connection"),
|
298 |
JOptionPane.WARNING_MESSAGE
|
299 |
); |
300 |
return;
|
301 |
} |
302 |
this.setVisible(false); |
303 |
} |
304 |
|
305 |
protected void doCancel() { |
306 |
this.isCanceled = true; |
307 |
this.setVisible(false); |
308 |
} |
309 |
|
310 |
protected void doDelete() { |
311 |
this.jdbcServerExplorer.delete();
|
312 |
this.jdbcServerExplorer.clear();
|
313 |
} |
314 |
|
315 |
@Override
|
316 |
public void showDialog() { |
317 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
318 |
WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
319 |
winmgr.showWindow( |
320 |
this,
|
321 |
i18nManager.getTranslation("_Connection_parameters"),
|
322 |
WindowManager.MODE.DIALOG |
323 |
); |
324 |
} |
325 |
|
326 |
@Override
|
327 |
public boolean isCanceled() { |
328 |
return this.isCanceled; |
329 |
} |
330 |
|
331 |
@Override
|
332 |
public JDBCServerExplorerParameters getServerExplorerParameters() {
|
333 |
return this.jdbcServerExplorer.getServerExplorerParameters(); |
334 |
} |
335 |
|
336 |
@Override
|
337 |
public LabeledValue<JDBCServerExplorerParameters> getLabeledServerExplorerParameters() {
|
338 |
return this.jdbcServerExplorer.getLabeledServerExplorerParameters(); |
339 |
} |
340 |
|
341 |
@Override
|
342 |
public String getConnectionName() { |
343 |
return this.jdbcServerExplorer.getConnectionName(); |
344 |
} |
345 |
|
346 |
} |
347 |
|