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 / DefaultJDBCConnectionPanel.java @ 43114
History | View | Annotate | Download (15.5 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.fmap.dal.swing.impl.jdbc; |
7 |
|
8 |
import java.awt.Dimension; |
9 |
import java.awt.event.ActionEvent; |
10 |
import java.awt.event.ActionListener; |
11 |
import java.awt.event.ItemEvent; |
12 |
import java.awt.event.ItemListener; |
13 |
import java.io.File; |
14 |
import java.util.Iterator; |
15 |
import java.util.List; |
16 |
import javax.swing.ComboBoxModel; |
17 |
import javax.swing.JComponent; |
18 |
import javax.swing.JLabel; |
19 |
import javax.swing.JTextField; |
20 |
import javax.swing.SwingUtilities; |
21 |
import javax.swing.plaf.basic.BasicHTML; |
22 |
import javax.swing.text.View; |
23 |
import org.apache.commons.lang3.ArrayUtils; |
24 |
import org.apache.commons.lang3.StringUtils; |
25 |
import org.gvsig.fmap.dal.DALLocator; |
26 |
import org.gvsig.fmap.dal.DataFactory; |
27 |
import org.gvsig.fmap.dal.DataManager; |
28 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
29 |
import org.gvsig.fmap.dal.DataServerExplorerPool; |
30 |
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
31 |
import org.gvsig.fmap.dal.exception.DataException; |
32 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
33 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
34 |
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionPanel; |
35 |
import org.gvsig.tools.ToolsLocator; |
36 |
import org.gvsig.tools.dynobject.DynClass; |
37 |
import org.gvsig.tools.dynobject.DynField; |
38 |
import org.gvsig.tools.i18n.I18nManager; |
39 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
40 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
41 |
import org.slf4j.Logger; |
42 |
import org.slf4j.LoggerFactory; |
43 |
|
44 |
// org.gvsig.fmap.mapcontrol.dal.jdbc.JDBCConnectionPanel
|
45 |
public class DefaultJDBCConnectionPanel extends DefaultJDBCConnectionPanelView implements JDBCConnectionPanel{ |
46 |
|
47 |
private static final Logger |
48 |
logger = LoggerFactory.getLogger(DefaultJDBCConnectionPanel.class); |
49 |
private JDBCServerExplorerParameters forcedParameters;
|
50 |
|
51 |
@Override
|
52 |
public JComponent asJComponent() { |
53 |
return this; |
54 |
} |
55 |
|
56 |
private static class ServerExplorerParametersComboItem { |
57 |
|
58 |
private JDBCServerExplorerParameters params;
|
59 |
private String label; |
60 |
|
61 |
public ServerExplorerParametersComboItem(String label, JDBCServerExplorerParameters params) { |
62 |
this.params = params;
|
63 |
this.label = label;
|
64 |
} |
65 |
|
66 |
public ServerExplorerParametersComboItem(JDBCServerExplorerParameters params) {
|
67 |
this(params.getExplorerName(), params);
|
68 |
} |
69 |
|
70 |
@Override
|
71 |
public String toString() { |
72 |
return this.label; |
73 |
} |
74 |
|
75 |
public JDBCServerExplorerParameters getParams() {
|
76 |
return this.params; |
77 |
} |
78 |
|
79 |
public String getLabel() { |
80 |
return this.label; |
81 |
} |
82 |
} |
83 |
|
84 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
85 |
public DefaultJDBCConnectionPanel() {
|
86 |
initComponents(); |
87 |
} |
88 |
|
89 |
protected void initComponents() { |
90 |
this.cboConnections.setEditable(true); |
91 |
this.cboConnections.addItemListener(new ItemListener() { |
92 |
@Override
|
93 |
public void itemStateChanged(ItemEvent e) { |
94 |
// Lo hago asi para evitar cuelgues durante la depuracion
|
95 |
// al poner puntos de ruptura en un evento de un combo.
|
96 |
SwingUtilities.invokeLater(new Runnable() { |
97 |
|
98 |
@Override
|
99 |
public void run() { |
100 |
onChangeConnection(); |
101 |
} |
102 |
}); |
103 |
} |
104 |
}); |
105 |
this.cboConnectors.addItemListener(new ItemListener() { |
106 |
@Override
|
107 |
public void itemStateChanged(ItemEvent e) { |
108 |
// Lo hago asi para evitar cuelgues durante la depuracion
|
109 |
// al poner puntos de ruptura en un evento de un combo.
|
110 |
SwingUtilities.invokeLater(new Runnable() { |
111 |
|
112 |
@Override
|
113 |
public void run() { |
114 |
onChangeConnector(); |
115 |
} |
116 |
}); |
117 |
} |
118 |
}); |
119 |
try {
|
120 |
fillConnections(); |
121 |
fillConnectors(); |
122 |
} catch(Throwable th) { |
123 |
// Ignore it to allow use in GUI builders
|
124 |
logger.warn("Can't fill combos",th);
|
125 |
} |
126 |
this.translate();
|
127 |
this.btnDataBaseBrowser.addActionListener(new ActionListener() { |
128 |
|
129 |
@Override
|
130 |
public void actionPerformed(ActionEvent e) { |
131 |
onBrowseDatabase(); |
132 |
} |
133 |
}); |
134 |
adjustPreferedHeight(this.lblFoother,300); |
135 |
} |
136 |
|
137 |
private void adjustPreferedHeight(JLabel label, int prefWidth) { |
138 |
JLabel resizer = new JLabel(); |
139 |
resizer.setText(label.getText()); |
140 |
View view = (View) resizer.getClientProperty(BasicHTML.propertyKey); |
141 |
view.setSize(prefWidth,15);
|
142 |
float w = view.getPreferredSpan(View.X_AXIS); |
143 |
float h = view.getPreferredSpan(View.Y_AXIS); |
144 |
Dimension dim = new Dimension( |
145 |
(int) Math.ceil(w), |
146 |
(int) Math.ceil(h) |
147 |
); |
148 |
label.setPreferredSize(dim); |
149 |
} |
150 |
|
151 |
private void translate() { |
152 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
153 |
|
154 |
this.lblConnectionName.setText(i18nManager.getTranslation("_Connection_name")); |
155 |
this.lblConnector.setText(i18nManager.getTranslation("_Driver_type")); |
156 |
this.lblServer.setText(i18nManager.getTranslation("_Host")); |
157 |
this.lblPort.setText(i18nManager.getTranslation("_Port")); |
158 |
this.lblDataBase.setText(i18nManager.getTranslation("_Database")); |
159 |
this.lblUsername.setText(i18nManager.getTranslation("_User")); |
160 |
this.lblPassword.setText(i18nManager.getTranslation("_Password")); |
161 |
this.lblFoother.setText("<html>"+i18nManager.getTranslation("_JDBCConecctionPanel_foother")+"</html>"); |
162 |
} |
163 |
|
164 |
@Override
|
165 |
public void setServerExplorerParameters(JDBCServerExplorerParameters parameters) { |
166 |
this.forcedParameters = (JDBCServerExplorerParameters) parameters.getCopy();
|
167 |
|
168 |
int indexConnector = this.getIndexOfConnector(parameters); |
169 |
if ( indexConnector >= 0 && this.cboConnectors.getSelectedIndex()!=indexConnector ) { |
170 |
this.cboConnectors.setSelectedIndex(indexConnector);
|
171 |
} |
172 |
|
173 |
this.txtServer.setText(parameters.getHost());
|
174 |
Integer port = parameters.getPort();
|
175 |
if ( port == null ) { |
176 |
this.txtPort.setText(""); |
177 |
} else {
|
178 |
this.txtPort.setText(String.valueOf(port)); |
179 |
} |
180 |
this.txtDataBase.setText(parameters.getDBName());
|
181 |
this.txtUsername.setText(parameters.getUser());
|
182 |
this.txtPassword.setText(parameters.getPassword());
|
183 |
|
184 |
DynClass definition = parameters.getDynClass(); |
185 |
|
186 |
DynField field = definition.getDynField(JDBCServerExplorerParameters.HOST_PARAMTER_NAME); |
187 |
this.txtServer.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
188 |
field = definition.getDynField(JDBCServerExplorerParameters.PORT_PARAMTER_NAME); |
189 |
this.txtPort.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
190 |
field = definition.getDynField(JDBCServerExplorerParameters.DBNAME_PARAMTER_NAME); |
191 |
this.txtDataBase.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
192 |
field = definition.getDynField(JDBCServerExplorerParameters.USER_PARAMTER_NAME); |
193 |
this.txtUsername.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
194 |
field = definition.getDynField(JDBCServerExplorerParameters.PASSWORD_PARAMTER_NAME); |
195 |
this.txtPassword.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
196 |
this.btnDataBaseBrowser.setVisible(parameters instanceof FilesystemStoreParameters); |
197 |
} |
198 |
|
199 |
@Override
|
200 |
public JDBCServerExplorerParameters getServerExplorerParameters() {
|
201 |
JDBCServerExplorerParameters params; |
202 |
JDBCServerExplorerParameters connector = this.getConnector();
|
203 |
if( this.forcedParameters==null ) { |
204 |
params = (JDBCServerExplorerParameters) connector.getCopy(); |
205 |
} else {
|
206 |
params = (JDBCServerExplorerParameters) this.forcedParameters.getCopy();
|
207 |
} |
208 |
String s = this.getServer(); |
209 |
if( s!=null ) { |
210 |
params.setHost(s); |
211 |
} |
212 |
int n = this.getPort(); |
213 |
if( n>0 ) { |
214 |
params.setPort(n); |
215 |
} |
216 |
s = this.getDataBaseName();
|
217 |
if( s!=null ) { |
218 |
params.setDBName(s); |
219 |
} |
220 |
s = this.getUsername();
|
221 |
if( s!=null ) { |
222 |
params.setUser(s); |
223 |
} |
224 |
s = this.getPassword();
|
225 |
if( s!=null ) { |
226 |
params.setPassword(s); |
227 |
} |
228 |
|
229 |
if ( this.getConnectionName() != null ) { |
230 |
DataManager dataManager = DALLocator.getDataManager(); |
231 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
232 |
pool.add(this.getConnectionName(), params);
|
233 |
} |
234 |
return params;
|
235 |
} |
236 |
|
237 |
protected void setConnectionName(String connectionName) { |
238 |
JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
239 |
txtConnections.setText(connectionName); |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public String getConnectionName() { |
244 |
JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
245 |
String value = txtConnections.getText();
|
246 |
return (String) StringUtils.defaultIfBlank(value, null); |
247 |
} |
248 |
|
249 |
protected JDBCServerExplorerParameters getConnector() {
|
250 |
ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
251 |
JDBCServerExplorerParameters value = item.getParams(); |
252 |
return value;
|
253 |
} |
254 |
|
255 |
protected String getConnectorName() { |
256 |
JDBCServerExplorerParameters value = this.getConnector();
|
257 |
if ( value == null ) { |
258 |
return null; |
259 |
} |
260 |
return (String) StringUtils.defaultIfBlank(value.getExplorerName(), null); |
261 |
} |
262 |
|
263 |
protected String getServer() { |
264 |
return (String) StringUtils.defaultIfBlank(this.txtServer.getText(), null); |
265 |
} |
266 |
|
267 |
protected int getPort() { |
268 |
String svalue = (String) StringUtils.defaultIfBlank(this.txtPort.getText(), null); |
269 |
int ivalue;
|
270 |
try {
|
271 |
ivalue = Integer.parseInt(svalue);
|
272 |
} catch (Exception ex) { |
273 |
ivalue = -1;
|
274 |
} |
275 |
return ivalue;
|
276 |
} |
277 |
|
278 |
protected String getDataBaseName() { |
279 |
return (String) StringUtils.defaultIfBlank(this.txtDataBase.getText(), null); |
280 |
} |
281 |
|
282 |
protected String getUsername() { |
283 |
return (String) StringUtils.defaultIfBlank(this.txtUsername.getText(), null); |
284 |
} |
285 |
|
286 |
protected String getPassword() { |
287 |
return (String) StringUtils.defaultIfBlank(this.txtPassword.getText(), null); |
288 |
} |
289 |
|
290 |
private void onChangeConnector() { |
291 |
ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
292 |
if( item==null ) { |
293 |
return;
|
294 |
} |
295 |
JDBCServerExplorerParameters connector = item.getParams(); |
296 |
|
297 |
if ( connector == null ) { |
298 |
return;
|
299 |
} |
300 |
this.setServerExplorerParameters(connector);
|
301 |
} |
302 |
|
303 |
private void onChangeConnection() { |
304 |
Object item = this.cboConnections.getSelectedItem(); |
305 |
if ( item instanceof ServerExplorerParametersComboItem ) { |
306 |
JDBCServerExplorerParameters connection = ((ServerExplorerParametersComboItem) item).getParams(); |
307 |
if ( connection == null ) { |
308 |
return;
|
309 |
} |
310 |
this.setServerExplorerParameters(connection);
|
311 |
} |
312 |
} |
313 |
|
314 |
|
315 |
private int getIndexOfConnector(JDBCServerExplorerParameters explorerParameters) { |
316 |
String code = null; |
317 |
try {
|
318 |
code = explorerParameters.toString(); |
319 |
ComboBoxModel model = this.cboConnectors.getModel(); |
320 |
for ( int i = 0; i < model.getSize(); i++ ) { |
321 |
ServerExplorerParametersComboItem x = (ServerExplorerParametersComboItem) model.getElementAt(i); |
322 |
if ( x.getLabel().equalsIgnoreCase(explorerParameters.getExplorerName()) ) {
|
323 |
return i;
|
324 |
} |
325 |
} |
326 |
} catch (Exception ex) { |
327 |
logger.warn("Can't get index of exporer parameter '" + code + "'.", ex); |
328 |
} |
329 |
return -1; |
330 |
} |
331 |
|
332 |
private void fillConnectors() { |
333 |
DataManager dataManager = DALLocator.getDataManager(); |
334 |
|
335 |
ServerExplorerParametersComboItem last = null;
|
336 |
|
337 |
for (DataFactory factory : dataManager.getServerExplorerRegister() ) {
|
338 |
DataServerExplorerParameters params = (DataServerExplorerParameters) factory.createParameters(); |
339 |
if ( params instanceof JDBCServerExplorerParameters ) { |
340 |
if( params.getClass() == JDBCServerExplorerParameters.class &&
|
341 |
last == null ) {
|
342 |
// Si es el proveedor generico de JDBC, lo guarda para
|
343 |
// a?adirlo al final del combo.
|
344 |
last = new ServerExplorerParametersComboItem(
|
345 |
factory.getDescription(), |
346 |
(JDBCServerExplorerParameters) params |
347 |
); |
348 |
} else {
|
349 |
this.cboConnectors.addItem(
|
350 |
new ServerExplorerParametersComboItem(
|
351 |
factory.getDescription(), |
352 |
(JDBCServerExplorerParameters) params |
353 |
) |
354 |
); |
355 |
} |
356 |
} |
357 |
} |
358 |
if( last!=null ) { |
359 |
this.cboConnectors.addItem(last);
|
360 |
} |
361 |
} |
362 |
|
363 |
private void fillConnections() { |
364 |
DataManager dataManager = DALLocator.getDataManager(); |
365 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
366 |
|
367 |
DataServerExplorerParameters params; |
368 |
|
369 |
Iterator it = pool.iterator();
|
370 |
while ( it.hasNext() ) {
|
371 |
DataServerExplorerPoolEntry entry = (DataServerExplorerPoolEntry) it.next(); |
372 |
if ( entry.getExplorerParameters() instanceof JDBCServerExplorerParameters ) { |
373 |
JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
374 |
this.cboConnections.addItem(
|
375 |
new ServerExplorerParametersComboItem(entry.getName(), dbParams)
|
376 |
); |
377 |
} |
378 |
} |
379 |
this.cboConnections.setSelectedIndex(-1); |
380 |
} |
381 |
|
382 |
@Override
|
383 |
public void delete() { |
384 |
String name = this.getConnectionName(); |
385 |
DataManager dataManager = DALLocator.getDataManager(); |
386 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
387 |
|
388 |
pool.remove(name); |
389 |
} |
390 |
|
391 |
@Override
|
392 |
public void clear() { |
393 |
this.cboConnections.setSelectedIndex(-1); |
394 |
this.cboConnectors.setSelectedIndex(-1); |
395 |
this.txtServer.setText(""); |
396 |
this.txtPort.setText(""); |
397 |
this.txtDataBase.setText(""); |
398 |
this.txtUsername.setText(""); |
399 |
this.txtPassword.setText(""); |
400 |
} |
401 |
|
402 |
private void onBrowseDatabase() { |
403 |
if( this.forcedParameters==null ) { |
404 |
return;
|
405 |
} |
406 |
ThreadSafeDialogsManager dlgmanager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
407 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
408 |
File[] files = dlgmanager.showOpenFileDialog( |
409 |
i18nManager.getTranslation("_Select_database"),
|
410 |
null
|
411 |
); |
412 |
if( ArrayUtils.isEmpty(files) ) {
|
413 |
return;
|
414 |
} |
415 |
((FilesystemStoreParameters)this.forcedParameters).setFile(files[0]); |
416 |
this.txtDataBase.setText(this.forcedParameters.getDBName()); |
417 |
} |
418 |
} |