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