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 @ 44442
History | View | Annotate | Download (21.6 KB)
1 | 42104 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.jdbc; |
---|---|---|---|
2 | 41487 | jjdelcerro | |
3 | 42616 | jjdelcerro | import java.awt.Dimension; |
4 | 43114 | jjdelcerro | import java.awt.event.ActionEvent; |
5 | import java.awt.event.ActionListener; |
||
6 | 41487 | jjdelcerro | import java.awt.event.ItemEvent; |
7 | import java.awt.event.ItemListener; |
||
8 | 43114 | jjdelcerro | import java.io.File; |
9 | 44419 | jjdelcerro | import java.util.Objects; |
10 | 41492 | jjdelcerro | import javax.swing.ComboBoxModel; |
11 | 42112 | jjdelcerro | import javax.swing.JComponent; |
12 | 42616 | jjdelcerro | import javax.swing.JLabel; |
13 | 41487 | jjdelcerro | import javax.swing.JTextField; |
14 | 43114 | jjdelcerro | import javax.swing.SwingUtilities; |
15 | 42616 | jjdelcerro | import javax.swing.plaf.basic.BasicHTML; |
16 | import javax.swing.text.View; |
||
17 | 44419 | jjdelcerro | import org.apache.commons.io.FilenameUtils; |
18 | 43114 | jjdelcerro | import org.apache.commons.lang3.ArrayUtils; |
19 | 41487 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
20 | import org.gvsig.fmap.dal.DALLocator; |
||
21 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.DataFactory; |
22 | 41487 | jjdelcerro | import org.gvsig.fmap.dal.DataManager; |
23 | import org.gvsig.fmap.dal.DataServerExplorerParameters; |
||
24 | 41492 | jjdelcerro | import org.gvsig.fmap.dal.DataServerExplorerPool; |
25 | import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
||
26 | 44422 | jjdelcerro | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
27 | 43114 | jjdelcerro | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
28 | 41487 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
29 | 42112 | jjdelcerro | import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionPanel; |
30 | 41749 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
31 | 43114 | jjdelcerro | import org.gvsig.tools.dynobject.DynClass; |
32 | import org.gvsig.tools.dynobject.DynField; |
||
33 | 41749 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
34 | 43114 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
35 | import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
||
36 | 44419 | jjdelcerro | import org.gvsig.tools.util.LabeledValue; |
37 | import org.gvsig.tools.util.LabeledValueImpl; |
||
38 | 41492 | jjdelcerro | import org.slf4j.Logger; |
39 | import org.slf4j.LoggerFactory; |
||
40 | 41487 | jjdelcerro | |
41 | 41631 | jjdelcerro | // org.gvsig.fmap.mapcontrol.dal.jdbc.JDBCConnectionPanel
|
42 | 44294 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
43 | 42616 | jjdelcerro | public class DefaultJDBCConnectionPanel extends DefaultJDBCConnectionPanelView implements JDBCConnectionPanel{ |
44 | 41487 | jjdelcerro | |
45 | 44294 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJDBCConnectionPanel.class); |
46 | 43355 | jjdelcerro | private static final long serialVersionUID = -6119489353793596382L; |
47 | 42112 | jjdelcerro | private JDBCServerExplorerParameters forcedParameters;
|
48 | 43355 | jjdelcerro | private ItemListener onChangeConnectorItemListener; |
49 | 43377 | jjdelcerro | private ConnectorFilter connectorFilter=null; |
50 | 41487 | jjdelcerro | |
51 | 42616 | jjdelcerro | @Override
|
52 | 42112 | jjdelcerro | public JComponent asJComponent() { |
53 | return this; |
||
54 | } |
||
55 | 42055 | jjdelcerro | |
56 | 44419 | jjdelcerro | // 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 | 41487 | jjdelcerro | |
84 | 42616 | jjdelcerro | @SuppressWarnings("OverridableMethodCallInConstructor") |
85 | 42104 | jjdelcerro | public DefaultJDBCConnectionPanel() {
|
86 | 41488 | jjdelcerro | initComponents(); |
87 | 41487 | jjdelcerro | } |
88 | |||
89 | protected void initComponents() { |
||
90 | this.cboConnections.setEditable(true); |
||
91 | this.cboConnections.addItemListener(new ItemListener() { |
||
92 | 42616 | jjdelcerro | @Override
|
93 | 41487 | jjdelcerro | public void itemStateChanged(ItemEvent e) { |
94 | 43355 | jjdelcerro | if(e.getStateChange() != ItemEvent.SELECTED) { |
95 | return;
|
||
96 | } // Lo hago asi para evitar cuelgues durante la depuracion
|
||
97 | 43114 | jjdelcerro | // al poner puntos de ruptura en un evento de un combo.
|
98 | SwingUtilities.invokeLater(new Runnable() { |
||
99 | |||
100 | @Override
|
||
101 | public void run() { |
||
102 | onChangeConnection(); |
||
103 | } |
||
104 | }); |
||
105 | 41487 | jjdelcerro | } |
106 | }); |
||
107 | 43355 | jjdelcerro | this.onChangeConnectorItemListener = new ItemListener() { |
108 | 42616 | jjdelcerro | @Override
|
109 | 41487 | jjdelcerro | public void itemStateChanged(ItemEvent e) { |
110 | 43355 | jjdelcerro | if(e.getStateChange() != ItemEvent.SELECTED) { |
111 | return;
|
||
112 | } |
||
113 | 43114 | jjdelcerro | // Lo hago asi para evitar cuelgues durante la depuracion
|
114 | // al poner puntos de ruptura en un evento de un combo.
|
||
115 | SwingUtilities.invokeLater(new Runnable() { |
||
116 | |||
117 | @Override
|
||
118 | public void run() { |
||
119 | onChangeConnector(); |
||
120 | } |
||
121 | }); |
||
122 | 41487 | jjdelcerro | } |
123 | 43355 | jjdelcerro | }; |
124 | this.cboConnectors.addItemListener(onChangeConnectorItemListener);
|
||
125 | 41631 | jjdelcerro | try {
|
126 | fillConnections(); |
||
127 | fillConnectors(); |
||
128 | } catch(Throwable th) { |
||
129 | // Ignore it to allow use in GUI builders
|
||
130 | 44294 | jjdelcerro | LOGGER.warn("Can't fill combos",th);
|
131 | 41631 | jjdelcerro | } |
132 | 41749 | jjdelcerro | this.translate();
|
133 | 43114 | jjdelcerro | this.btnDataBaseBrowser.addActionListener(new ActionListener() { |
134 | |||
135 | @Override
|
||
136 | public void actionPerformed(ActionEvent e) { |
||
137 | onBrowseDatabase(); |
||
138 | } |
||
139 | }); |
||
140 | 42616 | jjdelcerro | adjustPreferedHeight(this.lblFoother,300); |
141 | 41487 | jjdelcerro | } |
142 | |||
143 | 42616 | jjdelcerro | private void adjustPreferedHeight(JLabel label, int prefWidth) { |
144 | JLabel resizer = new JLabel(); |
||
145 | resizer.setText(label.getText()); |
||
146 | View view = (View) resizer.getClientProperty(BasicHTML.propertyKey); |
||
147 | view.setSize(prefWidth,15);
|
||
148 | float w = view.getPreferredSpan(View.X_AXIS); |
||
149 | float h = view.getPreferredSpan(View.Y_AXIS); |
||
150 | Dimension dim = new Dimension( |
||
151 | (int) Math.ceil(w), |
||
152 | (int) Math.ceil(h) |
||
153 | ); |
||
154 | label.setPreferredSize(dim); |
||
155 | } |
||
156 | |||
157 | 41749 | jjdelcerro | private void translate() { |
158 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
159 | |||
160 | 43377 | jjdelcerro | this.lblFile.setText(i18nManager.getTranslation("_File")); |
161 | 41749 | jjdelcerro | this.lblConnectionName.setText(i18nManager.getTranslation("_Connection_name")); |
162 | this.lblConnector.setText(i18nManager.getTranslation("_Driver_type")); |
||
163 | this.lblServer.setText(i18nManager.getTranslation("_Host")); |
||
164 | this.lblPort.setText(i18nManager.getTranslation("_Port")); |
||
165 | this.lblDataBase.setText(i18nManager.getTranslation("_Database")); |
||
166 | this.lblUsername.setText(i18nManager.getTranslation("_User")); |
||
167 | this.lblPassword.setText(i18nManager.getTranslation("_Password")); |
||
168 | this.lblFoother.setText("<html>"+i18nManager.getTranslation("_JDBCConecctionPanel_foother")+"</html>"); |
||
169 | } |
||
170 | 44419 | jjdelcerro | |
171 | @Override
|
||
172 | public void setLabeledServerExplorerParameters(LabeledValue<JDBCServerExplorerParameters> parameters) { |
||
173 | if( parameters == null ) { |
||
174 | return;
|
||
175 | } |
||
176 | if( parameters.getValue()!=null ) { |
||
177 | this.setServerExplorerParameters(parameters.getValue());
|
||
178 | } |
||
179 | if( !StringUtils.isBlank(parameters.getLabel()) ) {
|
||
180 | this.cboConnections.setSelectedItem(parameters.getLabel());
|
||
181 | } |
||
182 | } |
||
183 | 41749 | jjdelcerro | |
184 | 42616 | jjdelcerro | @Override
|
185 | 41487 | jjdelcerro | public void setServerExplorerParameters(JDBCServerExplorerParameters parameters) { |
186 | 42055 | jjdelcerro | this.forcedParameters = (JDBCServerExplorerParameters) parameters.getCopy();
|
187 | |||
188 | 41492 | jjdelcerro | int indexConnector = this.getIndexOfConnector(parameters); |
189 | if ( indexConnector >= 0 && this.cboConnectors.getSelectedIndex()!=indexConnector ) { |
||
190 | 43355 | jjdelcerro | this.cboConnectors.removeItemListener(this.onChangeConnectorItemListener); |
191 | 41492 | jjdelcerro | this.cboConnectors.setSelectedIndex(indexConnector);
|
192 | 43355 | jjdelcerro | this.cboConnectors.addItemListener(onChangeConnectorItemListener);
|
193 | 41492 | jjdelcerro | } |
194 | |||
195 | 41487 | jjdelcerro | this.txtServer.setText(parameters.getHost());
|
196 | 41488 | jjdelcerro | Integer port = parameters.getPort();
|
197 | 41492 | jjdelcerro | if ( port == null ) { |
198 | 41488 | jjdelcerro | this.txtPort.setText(""); |
199 | } else {
|
||
200 | this.txtPort.setText(String.valueOf(port)); |
||
201 | } |
||
202 | 41487 | jjdelcerro | this.txtDataBase.setText(parameters.getDBName());
|
203 | this.txtUsername.setText(parameters.getUser());
|
||
204 | this.txtPassword.setText(parameters.getPassword());
|
||
205 | 43114 | jjdelcerro | |
206 | DynClass definition = parameters.getDynClass(); |
||
207 | |||
208 | DynField field = definition.getDynField(JDBCServerExplorerParameters.HOST_PARAMTER_NAME); |
||
209 | this.txtServer.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
||
210 | field = definition.getDynField(JDBCServerExplorerParameters.PORT_PARAMTER_NAME); |
||
211 | this.txtPort.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
||
212 | field = definition.getDynField(JDBCServerExplorerParameters.DBNAME_PARAMTER_NAME); |
||
213 | this.txtDataBase.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
||
214 | field = definition.getDynField(JDBCServerExplorerParameters.USER_PARAMTER_NAME); |
||
215 | this.txtUsername.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
||
216 | field = definition.getDynField(JDBCServerExplorerParameters.PASSWORD_PARAMTER_NAME); |
||
217 | this.txtPassword.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
||
218 | 43377 | jjdelcerro | if( parameters instanceof FilesystemStoreParameters ) { |
219 | File f = ((FilesystemStoreParameters) parameters).getFile();
|
||
220 | if( f==null ) { |
||
221 | this.txtFile.setText(""); |
||
222 | } else {
|
||
223 | this.txtFile.setText(f.getAbsolutePath());
|
||
224 | } |
||
225 | this.btnDataBaseBrowser.setVisible(true); |
||
226 | this.txtFile.setVisible(true); |
||
227 | this.lblFile.setVisible(true); |
||
228 | } else {
|
||
229 | this.btnDataBaseBrowser.setVisible(false); |
||
230 | this.txtFile.setVisible(false); |
||
231 | this.lblFile.setVisible(false); |
||
232 | } |
||
233 | 41487 | jjdelcerro | } |
234 | |||
235 | 42616 | jjdelcerro | @Override
|
236 | 41487 | jjdelcerro | public JDBCServerExplorerParameters getServerExplorerParameters() {
|
237 | 44419 | jjdelcerro | LabeledValue<JDBCServerExplorerParameters> labeledConnection = this.getLabeledServerExplorerParameters();
|
238 | if( labeledConnection==null ) { |
||
239 | return null; |
||
240 | } |
||
241 | return labeledConnection.getValue();
|
||
242 | } |
||
243 | |||
244 | @Override
|
||
245 | public LabeledValue<JDBCServerExplorerParameters> getLabeledServerExplorerParameters() {
|
||
246 | 42055 | jjdelcerro | JDBCServerExplorerParameters params; |
247 | 41487 | jjdelcerro | JDBCServerExplorerParameters connector = this.getConnector();
|
248 | 43355 | jjdelcerro | if( connector==null ) { |
249 | return null; |
||
250 | } |
||
251 | 42055 | jjdelcerro | if( this.forcedParameters==null ) { |
252 | params = (JDBCServerExplorerParameters) connector.getCopy(); |
||
253 | } else {
|
||
254 | params = (JDBCServerExplorerParameters) this.forcedParameters.getCopy();
|
||
255 | } |
||
256 | String s = this.getServer(); |
||
257 | if( s!=null ) { |
||
258 | params.setHost(s); |
||
259 | } |
||
260 | int n = this.getPort(); |
||
261 | if( n>0 ) { |
||
262 | params.setPort(n); |
||
263 | } |
||
264 | s = this.getDataBaseName();
|
||
265 | if( s!=null ) { |
||
266 | params.setDBName(s); |
||
267 | } |
||
268 | s = this.getUsername();
|
||
269 | if( s!=null ) { |
||
270 | params.setUser(s); |
||
271 | } |
||
272 | s = this.getPassword();
|
||
273 | if( s!=null ) { |
||
274 | params.setPassword(s); |
||
275 | } |
||
276 | 44422 | jjdelcerro | |
277 | 44419 | jjdelcerro | String name = this.getConnectionName(); |
278 | if ( StringUtils.isBlank(name) ) {
|
||
279 | if( params instanceof FilesystemStoreParameters ) { |
||
280 | File f = ((FilesystemStoreParameters) params).getFile();
|
||
281 | if( f!=null ) { |
||
282 | name = FilenameUtils.getBaseName(f.getPath()); |
||
283 | } |
||
284 | } |
||
285 | if( StringUtils.isBlank(name) ) {
|
||
286 | StringBuilder builder = new StringBuilder(); |
||
287 | builder.append(Objects.toString(params.getHost(),""));
|
||
288 | if( builder.length()>0 && !builder.toString().endsWith("-")) { |
||
289 | builder.append("-");
|
||
290 | } |
||
291 | builder.append(Objects.toString(params.getPort(),""));
|
||
292 | if( builder.length()>0 && !builder.toString().endsWith("-")) { |
||
293 | builder.append("-");
|
||
294 | } |
||
295 | builder.append(Objects.toString(params.getDBName(),""));
|
||
296 | name = builder.toString(); |
||
297 | } |
||
298 | 41488 | jjdelcerro | } |
299 | 44422 | jjdelcerro | |
300 | 44419 | jjdelcerro | if( StringUtils.isBlank(name) ) {
|
301 | name = getUniqueEntryName("Connection");
|
||
302 | 44422 | jjdelcerro | } else if( this.isNewConnection() ) { |
303 | 44419 | jjdelcerro | name = getUniqueEntryName(name); |
304 | } |
||
305 | |||
306 | 44422 | jjdelcerro | try {
|
307 | params.validate(); |
||
308 | DataManager dataManager = DALLocator.getDataManager(); |
||
309 | DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
||
310 | pool.add(name, params); |
||
311 | } catch (ValidateDataParametersException ex) {
|
||
312 | |||
313 | } |
||
314 | |||
315 | 44419 | jjdelcerro | return new LabeledValueImpl<>(name,params); |
316 | 41487 | jjdelcerro | } |
317 | 44419 | jjdelcerro | |
318 | 44422 | jjdelcerro | @Override
|
319 | public boolean areTheParametersValid() { |
||
320 | try {
|
||
321 | LabeledValue<JDBCServerExplorerParameters> params = this.getLabeledServerExplorerParameters();
|
||
322 | params.getValue().validate(); |
||
323 | return true; |
||
324 | } catch (ValidateDataParametersException ex) {
|
||
325 | return false; |
||
326 | } |
||
327 | } |
||
328 | |||
329 | @Override
|
||
330 | public String getParametersProblems() { |
||
331 | try {
|
||
332 | 44423 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
333 | 44422 | jjdelcerro | LabeledValue<JDBCServerExplorerParameters> params = this.getLabeledServerExplorerParameters();
|
334 | 44423 | jjdelcerro | if( params == null ) { |
335 | return i18n.getTranslation("You need to select a connector"); |
||
336 | } |
||
337 | 44422 | jjdelcerro | params.getValue().validate(); |
338 | return null; |
||
339 | } catch (ValidateDataParametersException ex) {
|
||
340 | return ex.getLocalizedMessageStack();
|
||
341 | } |
||
342 | } |
||
343 | |||
344 | 44419 | jjdelcerro | private String getUniqueEntryName(String name) { |
345 | DataManager dataManager = DALLocator.getDataManager(); |
||
346 | DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
||
347 | String baseName = name;
|
||
348 | for (int i = 0; i < 1000; i++) { |
||
349 | if( !pool.contains(name) ) {
|
||
350 | return name;
|
||
351 | } |
||
352 | name = baseName+"-"+i;
|
||
353 | } |
||
354 | return name + System.currentTimeMillis(); |
||
355 | } |
||
356 | 41487 | jjdelcerro | |
357 | 41631 | jjdelcerro | protected void setConnectionName(String connectionName) { |
358 | 41487 | jjdelcerro | JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
359 | txtConnections.setText(connectionName); |
||
360 | } |
||
361 | |||
362 | 42616 | jjdelcerro | @Override
|
363 | 41487 | jjdelcerro | public String getConnectionName() { |
364 | 44438 | jjdelcerro | Object value = this.cboConnections.getSelectedItem(); |
365 | return StringUtils.defaultIfBlank(Objects.toString(value, null), null); |
||
366 | 41487 | jjdelcerro | } |
367 | |||
368 | 44422 | jjdelcerro | @Override
|
369 | public boolean isNewConnection() { |
||
370 | String name = this.getConnectionName(); |
||
371 | if( name == null ) { |
||
372 | return true; |
||
373 | } |
||
374 | ComboBoxModel<LabeledValue<JDBCServerExplorerParameters>> model = this.cboConnections.getModel(); |
||
375 | for (int i = 0; i < model.getSize(); i++) { |
||
376 | LabeledValue<JDBCServerExplorerParameters> conn = model.getElementAt(i); |
||
377 | if( StringUtils.equalsIgnoreCase(name, conn.getLabel()) ) {
|
||
378 | return false; |
||
379 | } |
||
380 | } |
||
381 | return true; |
||
382 | } |
||
383 | |||
384 | 41631 | jjdelcerro | protected JDBCServerExplorerParameters getConnector() {
|
385 | 44419 | jjdelcerro | LabeledValue<JDBCServerExplorerParameters> item = (LabeledValue<JDBCServerExplorerParameters>) this.cboConnectors.getSelectedItem();
|
386 | if( item == null ) { |
||
387 | return null; |
||
388 | } |
||
389 | return item.getValue();
|
||
390 | 41487 | jjdelcerro | } |
391 | |||
392 | 41631 | jjdelcerro | protected String getConnectorName() { |
393 | 41487 | jjdelcerro | JDBCServerExplorerParameters value = this.getConnector();
|
394 | 41492 | jjdelcerro | if ( value == null ) { |
395 | 41487 | jjdelcerro | return null; |
396 | } |
||
397 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(value.getExplorerName(), null); |
398 | 41487 | jjdelcerro | } |
399 | |||
400 | 41631 | jjdelcerro | protected String getServer() { |
401 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(this.txtServer.getText(), null); |
402 | 41487 | jjdelcerro | } |
403 | |||
404 | 41631 | jjdelcerro | protected int getPort() { |
405 | 42104 | jjdelcerro | String svalue = (String) StringUtils.defaultIfBlank(this.txtPort.getText(), null); |
406 | 42616 | jjdelcerro | int ivalue;
|
407 | 41487 | jjdelcerro | try {
|
408 | ivalue = Integer.parseInt(svalue);
|
||
409 | } catch (Exception ex) { |
||
410 | ivalue = -1;
|
||
411 | } |
||
412 | return ivalue;
|
||
413 | } |
||
414 | |||
415 | 41631 | jjdelcerro | protected String getDataBaseName() { |
416 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(this.txtDataBase.getText(), null); |
417 | 41487 | jjdelcerro | } |
418 | |||
419 | 41631 | jjdelcerro | protected String getUsername() { |
420 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(this.txtUsername.getText(), null); |
421 | 41487 | jjdelcerro | } |
422 | |||
423 | 41631 | jjdelcerro | protected String getPassword() { |
424 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(this.txtPassword.getText(), null); |
425 | 41487 | jjdelcerro | } |
426 | |||
427 | private void onChangeConnector() { |
||
428 | 44419 | jjdelcerro | LabeledValue<JDBCServerExplorerParameters> item = (LabeledValue<JDBCServerExplorerParameters>) this.cboConnectors.getSelectedItem();
|
429 | 41631 | jjdelcerro | if( item==null ) { |
430 | return;
|
||
431 | } |
||
432 | 44419 | jjdelcerro | JDBCServerExplorerParameters connector = item.getValue(); |
433 | 41631 | jjdelcerro | |
434 | 41492 | jjdelcerro | if ( connector == null ) { |
435 | 41487 | jjdelcerro | return;
|
436 | } |
||
437 | this.setServerExplorerParameters(connector);
|
||
438 | } |
||
439 | |||
440 | private void onChangeConnection() { |
||
441 | 44442 | jjdelcerro | Object value = this.cboConnections.getSelectedItem(); |
442 | if( value instanceof LabeledValue ) { |
||
443 | LabeledValue<JDBCServerExplorerParameters> item = (LabeledValue<JDBCServerExplorerParameters>) value; |
||
444 | JDBCServerExplorerParameters connection = item.getValue(); |
||
445 | if ( connection == null ) { |
||
446 | return;
|
||
447 | } |
||
448 | this.setServerExplorerParameters(connection);
|
||
449 | 41487 | jjdelcerro | } |
450 | } |
||
451 | 41631 | jjdelcerro | |
452 | 41487 | jjdelcerro | |
453 | 41492 | jjdelcerro | private int getIndexOfConnector(JDBCServerExplorerParameters explorerParameters) { |
454 | String code = null; |
||
455 | try {
|
||
456 | code = explorerParameters.toString(); |
||
457 | ComboBoxModel model = this.cboConnectors.getModel(); |
||
458 | for ( int i = 0; i < model.getSize(); i++ ) { |
||
459 | 44419 | jjdelcerro | LabeledValue<JDBCServerExplorerParameters> x = (LabeledValue<JDBCServerExplorerParameters>) model.getElementAt(i); |
460 | if ( x!=null && x.getValue()!=null && x.getValue().getExplorerName().equalsIgnoreCase(explorerParameters.getExplorerName()) ) { |
||
461 | 41492 | jjdelcerro | return i;
|
462 | } |
||
463 | } |
||
464 | } catch (Exception ex) { |
||
465 | 44294 | jjdelcerro | LOGGER.warn("Can't get index of exporer parameter '" + code + "'.", ex); |
466 | 41492 | jjdelcerro | } |
467 | return -1; |
||
468 | } |
||
469 | 43377 | jjdelcerro | |
470 | @Override
|
||
471 | public void setConnectorFilter(ConnectorFilter connectorFilter) { |
||
472 | this.connectorFilter = connectorFilter;
|
||
473 | fillConnections(); |
||
474 | fillConnectors(); |
||
475 | } |
||
476 | 41492 | jjdelcerro | |
477 | 41487 | jjdelcerro | private void fillConnectors() { |
478 | DataManager dataManager = DALLocator.getDataManager(); |
||
479 | |||
480 | 44419 | jjdelcerro | LabeledValue<JDBCServerExplorerParameters> last = null;
|
481 | 43377 | jjdelcerro | this.cboConnectors.removeAllItems();
|
482 | 44419 | jjdelcerro | this.cboConnectors.addItem(new LabeledValueImpl("",(JDBCServerExplorerParameters)null)); |
483 | 43020 | jjdelcerro | |
484 | for (DataFactory factory : dataManager.getServerExplorerRegister() ) {
|
||
485 | 43450 | jjdelcerro | if( factory.isHidden() ) {
|
486 | continue;
|
||
487 | } |
||
488 | 43020 | jjdelcerro | DataServerExplorerParameters params = (DataServerExplorerParameters) factory.createParameters(); |
489 | 41492 | jjdelcerro | if ( params instanceof JDBCServerExplorerParameters ) { |
490 | 43377 | jjdelcerro | if( this.connectorFilter==null || this.connectorFilter.accept((JDBCServerExplorerParameters) params) ) { |
491 | if( params.getClass() == JDBCServerExplorerParameters.class &&
|
||
492 | last == null ) {
|
||
493 | // Si es el proveedor generico de JDBC, lo guarda para
|
||
494 | // a?adirlo al final del combo.
|
||
495 | 44419 | jjdelcerro | last = new LabeledValueImpl(
|
496 | 43020 | jjdelcerro | factory.getDescription(), |
497 | (JDBCServerExplorerParameters) params |
||
498 | 43377 | jjdelcerro | ); |
499 | } else {
|
||
500 | this.cboConnectors.addItem(
|
||
501 | 44419 | jjdelcerro | new LabeledValueImpl(
|
502 | 43377 | jjdelcerro | factory.getDescription(), |
503 | (JDBCServerExplorerParameters) params |
||
504 | ) |
||
505 | ); |
||
506 | } |
||
507 | 41648 | jjdelcerro | } |
508 | 41487 | jjdelcerro | } |
509 | } |
||
510 | 41648 | jjdelcerro | if( last!=null ) { |
511 | 43020 | jjdelcerro | this.cboConnectors.addItem(last);
|
512 | 41648 | jjdelcerro | } |
513 | 41487 | jjdelcerro | } |
514 | 41492 | jjdelcerro | |
515 | private void fillConnections() { |
||
516 | DataManager dataManager = DALLocator.getDataManager(); |
||
517 | DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
||
518 | |||
519 | 43377 | jjdelcerro | this.cboConnections.removeAllItems();
|
520 | 44419 | jjdelcerro | for (DataServerExplorerPoolEntry entry : pool) {
|
521 | 41492 | jjdelcerro | if ( entry.getExplorerParameters() instanceof JDBCServerExplorerParameters ) { |
522 | 43377 | jjdelcerro | if( this.connectorFilter==null || this.connectorFilter.accept((JDBCServerExplorerParameters) entry.getExplorerParameters()) ) { |
523 | JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
||
524 | this.cboConnections.addItem(
|
||
525 | 44419 | jjdelcerro | new LabeledValueImpl<>(entry.getName(), dbParams)
|
526 | 43377 | jjdelcerro | ); |
527 | } |
||
528 | 41492 | jjdelcerro | } |
529 | } |
||
530 | this.cboConnections.setSelectedIndex(-1); |
||
531 | } |
||
532 | |||
533 | 42616 | jjdelcerro | @Override
|
534 | 41631 | jjdelcerro | public void delete() { |
535 | String name = this.getConnectionName(); |
||
536 | DataManager dataManager = DALLocator.getDataManager(); |
||
537 | DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
||
538 | |||
539 | pool.remove(name); |
||
540 | 43377 | jjdelcerro | fillConnections(); |
541 | 41631 | jjdelcerro | } |
542 | |||
543 | 42616 | jjdelcerro | @Override
|
544 | 41631 | jjdelcerro | public void clear() { |
545 | this.cboConnections.setSelectedIndex(-1); |
||
546 | this.cboConnectors.setSelectedIndex(-1); |
||
547 | this.txtServer.setText(""); |
||
548 | this.txtPort.setText(""); |
||
549 | this.txtDataBase.setText(""); |
||
550 | this.txtUsername.setText(""); |
||
551 | this.txtPassword.setText(""); |
||
552 | 44419 | jjdelcerro | this.txtFile.setText(""); |
553 | 41631 | jjdelcerro | } |
554 | 43114 | jjdelcerro | |
555 | private void onBrowseDatabase() { |
||
556 | if( this.forcedParameters==null ) { |
||
557 | return;
|
||
558 | } |
||
559 | ThreadSafeDialogsManager dlgmanager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
||
560 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
561 | File[] files = dlgmanager.showOpenFileDialog( |
||
562 | i18nManager.getTranslation("_Select_database"),
|
||
563 | null
|
||
564 | ); |
||
565 | if( ArrayUtils.isEmpty(files) ) {
|
||
566 | return;
|
||
567 | } |
||
568 | ((FilesystemStoreParameters)this.forcedParameters).setFile(files[0]); |
||
569 | 43377 | jjdelcerro | this.txtFile.setText(((FilesystemStoreParameters)(this.forcedParameters)).getFile().getAbsolutePath()); |
570 | 43114 | jjdelcerro | } |
571 | 41487 | jjdelcerro | } |