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 @ 44591
History | View | Annotate | Download (21.7 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 | 44591 | jjdelcerro | Dimension sz = this.getPreferredSize(); |
142 | if( sz.width<400 ) { |
||
143 | sz.width = 400;
|
||
144 | this.setPreferredSize(sz);
|
||
145 | } |
||
146 | 41487 | jjdelcerro | } |
147 | |||
148 | 42616 | jjdelcerro | private void adjustPreferedHeight(JLabel label, int prefWidth) { |
149 | JLabel resizer = new JLabel(); |
||
150 | resizer.setText(label.getText()); |
||
151 | View view = (View) resizer.getClientProperty(BasicHTML.propertyKey); |
||
152 | view.setSize(prefWidth,15);
|
||
153 | float w = view.getPreferredSpan(View.X_AXIS); |
||
154 | float h = view.getPreferredSpan(View.Y_AXIS); |
||
155 | Dimension dim = new Dimension( |
||
156 | (int) Math.ceil(w), |
||
157 | (int) Math.ceil(h) |
||
158 | ); |
||
159 | label.setPreferredSize(dim); |
||
160 | } |
||
161 | |||
162 | 41749 | jjdelcerro | private void translate() { |
163 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
164 | |||
165 | 43377 | jjdelcerro | this.lblFile.setText(i18nManager.getTranslation("_File")); |
166 | 41749 | jjdelcerro | this.lblConnectionName.setText(i18nManager.getTranslation("_Connection_name")); |
167 | this.lblConnector.setText(i18nManager.getTranslation("_Driver_type")); |
||
168 | this.lblServer.setText(i18nManager.getTranslation("_Host")); |
||
169 | this.lblPort.setText(i18nManager.getTranslation("_Port")); |
||
170 | this.lblDataBase.setText(i18nManager.getTranslation("_Database")); |
||
171 | this.lblUsername.setText(i18nManager.getTranslation("_User")); |
||
172 | this.lblPassword.setText(i18nManager.getTranslation("_Password")); |
||
173 | this.lblFoother.setText("<html>"+i18nManager.getTranslation("_JDBCConecctionPanel_foother")+"</html>"); |
||
174 | } |
||
175 | 44419 | jjdelcerro | |
176 | @Override
|
||
177 | public void setLabeledServerExplorerParameters(LabeledValue<JDBCServerExplorerParameters> parameters) { |
||
178 | if( parameters == null ) { |
||
179 | return;
|
||
180 | } |
||
181 | if( parameters.getValue()!=null ) { |
||
182 | this.setServerExplorerParameters(parameters.getValue());
|
||
183 | } |
||
184 | if( !StringUtils.isBlank(parameters.getLabel()) ) {
|
||
185 | this.cboConnections.setSelectedItem(parameters.getLabel());
|
||
186 | } |
||
187 | } |
||
188 | 41749 | jjdelcerro | |
189 | 42616 | jjdelcerro | @Override
|
190 | 41487 | jjdelcerro | public void setServerExplorerParameters(JDBCServerExplorerParameters parameters) { |
191 | 42055 | jjdelcerro | this.forcedParameters = (JDBCServerExplorerParameters) parameters.getCopy();
|
192 | |||
193 | 41492 | jjdelcerro | int indexConnector = this.getIndexOfConnector(parameters); |
194 | if ( indexConnector >= 0 && this.cboConnectors.getSelectedIndex()!=indexConnector ) { |
||
195 | 43355 | jjdelcerro | this.cboConnectors.removeItemListener(this.onChangeConnectorItemListener); |
196 | 41492 | jjdelcerro | this.cboConnectors.setSelectedIndex(indexConnector);
|
197 | 43355 | jjdelcerro | this.cboConnectors.addItemListener(onChangeConnectorItemListener);
|
198 | 41492 | jjdelcerro | } |
199 | |||
200 | 41487 | jjdelcerro | this.txtServer.setText(parameters.getHost());
|
201 | 41488 | jjdelcerro | Integer port = parameters.getPort();
|
202 | 41492 | jjdelcerro | if ( port == null ) { |
203 | 41488 | jjdelcerro | this.txtPort.setText(""); |
204 | } else {
|
||
205 | this.txtPort.setText(String.valueOf(port)); |
||
206 | } |
||
207 | 41487 | jjdelcerro | this.txtDataBase.setText(parameters.getDBName());
|
208 | this.txtUsername.setText(parameters.getUser());
|
||
209 | this.txtPassword.setText(parameters.getPassword());
|
||
210 | 43114 | jjdelcerro | |
211 | DynClass definition = parameters.getDynClass(); |
||
212 | |||
213 | DynField field = definition.getDynField(JDBCServerExplorerParameters.HOST_PARAMTER_NAME); |
||
214 | this.txtServer.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
||
215 | field = definition.getDynField(JDBCServerExplorerParameters.PORT_PARAMTER_NAME); |
||
216 | this.txtPort.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
||
217 | field = definition.getDynField(JDBCServerExplorerParameters.DBNAME_PARAMTER_NAME); |
||
218 | this.txtDataBase.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
||
219 | field = definition.getDynField(JDBCServerExplorerParameters.USER_PARAMTER_NAME); |
||
220 | this.txtUsername.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
||
221 | field = definition.getDynField(JDBCServerExplorerParameters.PASSWORD_PARAMTER_NAME); |
||
222 | this.txtPassword.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
||
223 | 43377 | jjdelcerro | if( parameters instanceof FilesystemStoreParameters ) { |
224 | File f = ((FilesystemStoreParameters) parameters).getFile();
|
||
225 | if( f==null ) { |
||
226 | this.txtFile.setText(""); |
||
227 | } else {
|
||
228 | this.txtFile.setText(f.getAbsolutePath());
|
||
229 | } |
||
230 | this.btnDataBaseBrowser.setVisible(true); |
||
231 | this.txtFile.setVisible(true); |
||
232 | this.lblFile.setVisible(true); |
||
233 | } else {
|
||
234 | this.btnDataBaseBrowser.setVisible(false); |
||
235 | this.txtFile.setVisible(false); |
||
236 | this.lblFile.setVisible(false); |
||
237 | } |
||
238 | 41487 | jjdelcerro | } |
239 | |||
240 | 42616 | jjdelcerro | @Override
|
241 | 41487 | jjdelcerro | public JDBCServerExplorerParameters getServerExplorerParameters() {
|
242 | 42055 | jjdelcerro | JDBCServerExplorerParameters params; |
243 | 41487 | jjdelcerro | JDBCServerExplorerParameters connector = this.getConnector();
|
244 | 43355 | jjdelcerro | if( connector==null ) { |
245 | return null; |
||
246 | } |
||
247 | 42055 | jjdelcerro | if( this.forcedParameters==null ) { |
248 | params = (JDBCServerExplorerParameters) connector.getCopy(); |
||
249 | } else {
|
||
250 | params = (JDBCServerExplorerParameters) this.forcedParameters.getCopy();
|
||
251 | } |
||
252 | String s = this.getServer(); |
||
253 | if( s!=null ) { |
||
254 | params.setHost(s); |
||
255 | } |
||
256 | int n = this.getPort(); |
||
257 | if( n>0 ) { |
||
258 | params.setPort(n); |
||
259 | } |
||
260 | s = this.getDataBaseName();
|
||
261 | if( s!=null ) { |
||
262 | params.setDBName(s); |
||
263 | } |
||
264 | s = this.getUsername();
|
||
265 | if( s!=null ) { |
||
266 | params.setUser(s); |
||
267 | } |
||
268 | s = this.getPassword();
|
||
269 | if( s!=null ) { |
||
270 | params.setPassword(s); |
||
271 | } |
||
272 | 44591 | jjdelcerro | return params;
|
273 | } |
||
274 | |||
275 | @Override
|
||
276 | public LabeledValue<JDBCServerExplorerParameters> getLabeledServerExplorerParameters() {
|
||
277 | JDBCServerExplorerParameters params = this.getServerExplorerParameters();
|
||
278 | if( params == null ) { |
||
279 | return null; |
||
280 | } |
||
281 | 44419 | jjdelcerro | String name = this.getConnectionName(); |
282 | if ( StringUtils.isBlank(name) ) {
|
||
283 | if( params instanceof FilesystemStoreParameters ) { |
||
284 | File f = ((FilesystemStoreParameters) params).getFile();
|
||
285 | if( f!=null ) { |
||
286 | name = FilenameUtils.getBaseName(f.getPath()); |
||
287 | } |
||
288 | } |
||
289 | if( StringUtils.isBlank(name) ) {
|
||
290 | StringBuilder builder = new StringBuilder(); |
||
291 | builder.append(Objects.toString(params.getHost(),""));
|
||
292 | if( builder.length()>0 && !builder.toString().endsWith("-")) { |
||
293 | builder.append("-");
|
||
294 | } |
||
295 | builder.append(Objects.toString(params.getPort(),""));
|
||
296 | if( builder.length()>0 && !builder.toString().endsWith("-")) { |
||
297 | builder.append("-");
|
||
298 | } |
||
299 | builder.append(Objects.toString(params.getDBName(),""));
|
||
300 | name = builder.toString(); |
||
301 | } |
||
302 | 41488 | jjdelcerro | } |
303 | 44422 | jjdelcerro | |
304 | 44419 | jjdelcerro | if( StringUtils.isBlank(name) ) {
|
305 | name = getUniqueEntryName("Connection");
|
||
306 | 44422 | jjdelcerro | } else if( this.isNewConnection() ) { |
307 | 44419 | jjdelcerro | name = getUniqueEntryName(name); |
308 | } |
||
309 | return new LabeledValueImpl<>(name,params); |
||
310 | 41487 | jjdelcerro | } |
311 | 44419 | jjdelcerro | |
312 | 44422 | jjdelcerro | @Override
|
313 | public boolean areTheParametersValid() { |
||
314 | try {
|
||
315 | LabeledValue<JDBCServerExplorerParameters> params = this.getLabeledServerExplorerParameters();
|
||
316 | params.getValue().validate(); |
||
317 | return true; |
||
318 | } catch (ValidateDataParametersException ex) {
|
||
319 | return false; |
||
320 | } |
||
321 | } |
||
322 | |||
323 | @Override
|
||
324 | public String getParametersProblems() { |
||
325 | try {
|
||
326 | 44423 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
327 | 44591 | jjdelcerro | JDBCServerExplorerParameters params = this.getServerExplorerParameters();
|
328 | 44423 | jjdelcerro | if( params == null ) { |
329 | return i18n.getTranslation("You need to select a connector"); |
||
330 | } |
||
331 | 44591 | jjdelcerro | params.validate(); |
332 | 44422 | jjdelcerro | return null; |
333 | } catch (ValidateDataParametersException ex) {
|
||
334 | return ex.getLocalizedMessageStack();
|
||
335 | } |
||
336 | } |
||
337 | |||
338 | 44419 | jjdelcerro | private String getUniqueEntryName(String name) { |
339 | DataManager dataManager = DALLocator.getDataManager(); |
||
340 | DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
||
341 | String baseName = name;
|
||
342 | for (int i = 0; i < 1000; i++) { |
||
343 | if( !pool.contains(name) ) {
|
||
344 | return name;
|
||
345 | } |
||
346 | name = baseName+"-"+i;
|
||
347 | } |
||
348 | return name + System.currentTimeMillis(); |
||
349 | } |
||
350 | 41487 | jjdelcerro | |
351 | 41631 | jjdelcerro | protected void setConnectionName(String connectionName) { |
352 | 41487 | jjdelcerro | JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
353 | txtConnections.setText(connectionName); |
||
354 | } |
||
355 | |||
356 | 42616 | jjdelcerro | @Override
|
357 | 41487 | jjdelcerro | public String getConnectionName() { |
358 | 44438 | jjdelcerro | Object value = this.cboConnections.getSelectedItem(); |
359 | 44591 | jjdelcerro | String name = StringUtils.defaultIfBlank(Objects.toString(value, null), null); |
360 | if( StringUtils.isBlank(name) ) {
|
||
361 | name = getUniqueEntryName("Connection");
|
||
362 | } else if( this.isNewConnection() ) { |
||
363 | name = getUniqueEntryName(name); |
||
364 | } |
||
365 | return name;
|
||
366 | |||
367 | 41487 | jjdelcerro | } |
368 | |||
369 | 44422 | jjdelcerro | @Override
|
370 | public boolean isNewConnection() { |
||
371 | 44591 | jjdelcerro | Object value = this.cboConnections.getSelectedItem(); |
372 | String name = StringUtils.defaultIfBlank(Objects.toString(value, null), null); |
||
373 | 44422 | jjdelcerro | if( name == null ) { |
374 | return true; |
||
375 | } |
||
376 | ComboBoxModel<LabeledValue<JDBCServerExplorerParameters>> model = this.cboConnections.getModel(); |
||
377 | for (int i = 0; i < model.getSize(); i++) { |
||
378 | LabeledValue<JDBCServerExplorerParameters> conn = model.getElementAt(i); |
||
379 | if( StringUtils.equalsIgnoreCase(name, conn.getLabel()) ) {
|
||
380 | return false; |
||
381 | } |
||
382 | } |
||
383 | return true; |
||
384 | } |
||
385 | |||
386 | 41631 | jjdelcerro | protected JDBCServerExplorerParameters getConnector() {
|
387 | 44419 | jjdelcerro | LabeledValue<JDBCServerExplorerParameters> item = (LabeledValue<JDBCServerExplorerParameters>) this.cboConnectors.getSelectedItem();
|
388 | if( item == null ) { |
||
389 | return null; |
||
390 | } |
||
391 | return item.getValue();
|
||
392 | 41487 | jjdelcerro | } |
393 | |||
394 | 41631 | jjdelcerro | protected String getConnectorName() { |
395 | 41487 | jjdelcerro | JDBCServerExplorerParameters value = this.getConnector();
|
396 | 41492 | jjdelcerro | if ( value == null ) { |
397 | 41487 | jjdelcerro | return null; |
398 | } |
||
399 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(value.getExplorerName(), null); |
400 | 41487 | jjdelcerro | } |
401 | |||
402 | 41631 | jjdelcerro | protected String getServer() { |
403 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(this.txtServer.getText(), null); |
404 | 41487 | jjdelcerro | } |
405 | |||
406 | 41631 | jjdelcerro | protected int getPort() { |
407 | 42104 | jjdelcerro | String svalue = (String) StringUtils.defaultIfBlank(this.txtPort.getText(), null); |
408 | 42616 | jjdelcerro | int ivalue;
|
409 | 41487 | jjdelcerro | try {
|
410 | ivalue = Integer.parseInt(svalue);
|
||
411 | } catch (Exception ex) { |
||
412 | ivalue = -1;
|
||
413 | } |
||
414 | return ivalue;
|
||
415 | } |
||
416 | |||
417 | 41631 | jjdelcerro | protected String getDataBaseName() { |
418 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(this.txtDataBase.getText(), null); |
419 | 41487 | jjdelcerro | } |
420 | |||
421 | 41631 | jjdelcerro | protected String getUsername() { |
422 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(this.txtUsername.getText(), null); |
423 | 41487 | jjdelcerro | } |
424 | |||
425 | 41631 | jjdelcerro | protected String getPassword() { |
426 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(this.txtPassword.getText(), null); |
427 | 41487 | jjdelcerro | } |
428 | |||
429 | private void onChangeConnector() { |
||
430 | 44419 | jjdelcerro | LabeledValue<JDBCServerExplorerParameters> item = (LabeledValue<JDBCServerExplorerParameters>) this.cboConnectors.getSelectedItem();
|
431 | 41631 | jjdelcerro | if( item==null ) { |
432 | return;
|
||
433 | } |
||
434 | 44419 | jjdelcerro | JDBCServerExplorerParameters connector = item.getValue(); |
435 | 41631 | jjdelcerro | |
436 | 41492 | jjdelcerro | if ( connector == null ) { |
437 | 41487 | jjdelcerro | return;
|
438 | } |
||
439 | this.setServerExplorerParameters(connector);
|
||
440 | } |
||
441 | |||
442 | private void onChangeConnection() { |
||
443 | 44442 | jjdelcerro | Object value = this.cboConnections.getSelectedItem(); |
444 | if( value instanceof LabeledValue ) { |
||
445 | LabeledValue<JDBCServerExplorerParameters> item = (LabeledValue<JDBCServerExplorerParameters>) value; |
||
446 | JDBCServerExplorerParameters connection = item.getValue(); |
||
447 | if ( connection == null ) { |
||
448 | return;
|
||
449 | } |
||
450 | this.setServerExplorerParameters(connection);
|
||
451 | 41487 | jjdelcerro | } |
452 | } |
||
453 | 41631 | jjdelcerro | |
454 | 41487 | jjdelcerro | |
455 | 41492 | jjdelcerro | private int getIndexOfConnector(JDBCServerExplorerParameters explorerParameters) { |
456 | String code = null; |
||
457 | try {
|
||
458 | code = explorerParameters.toString(); |
||
459 | ComboBoxModel model = this.cboConnectors.getModel(); |
||
460 | for ( int i = 0; i < model.getSize(); i++ ) { |
||
461 | 44419 | jjdelcerro | LabeledValue<JDBCServerExplorerParameters> x = (LabeledValue<JDBCServerExplorerParameters>) model.getElementAt(i); |
462 | if ( x!=null && x.getValue()!=null && x.getValue().getExplorerName().equalsIgnoreCase(explorerParameters.getExplorerName()) ) { |
||
463 | 41492 | jjdelcerro | return i;
|
464 | } |
||
465 | } |
||
466 | } catch (Exception ex) { |
||
467 | 44294 | jjdelcerro | LOGGER.warn("Can't get index of exporer parameter '" + code + "'.", ex); |
468 | 41492 | jjdelcerro | } |
469 | return -1; |
||
470 | } |
||
471 | 43377 | jjdelcerro | |
472 | @Override
|
||
473 | public void setConnectorFilter(ConnectorFilter connectorFilter) { |
||
474 | this.connectorFilter = connectorFilter;
|
||
475 | fillConnections(); |
||
476 | fillConnectors(); |
||
477 | } |
||
478 | 41492 | jjdelcerro | |
479 | 41487 | jjdelcerro | private void fillConnectors() { |
480 | DataManager dataManager = DALLocator.getDataManager(); |
||
481 | |||
482 | 44419 | jjdelcerro | LabeledValue<JDBCServerExplorerParameters> last = null;
|
483 | 43377 | jjdelcerro | this.cboConnectors.removeAllItems();
|
484 | 44419 | jjdelcerro | this.cboConnectors.addItem(new LabeledValueImpl("",(JDBCServerExplorerParameters)null)); |
485 | 43020 | jjdelcerro | |
486 | for (DataFactory factory : dataManager.getServerExplorerRegister() ) {
|
||
487 | 43450 | jjdelcerro | if( factory.isHidden() ) {
|
488 | continue;
|
||
489 | } |
||
490 | 43020 | jjdelcerro | DataServerExplorerParameters params = (DataServerExplorerParameters) factory.createParameters(); |
491 | 41492 | jjdelcerro | if ( params instanceof JDBCServerExplorerParameters ) { |
492 | 43377 | jjdelcerro | if( this.connectorFilter==null || this.connectorFilter.accept((JDBCServerExplorerParameters) params) ) { |
493 | if( params.getClass() == JDBCServerExplorerParameters.class &&
|
||
494 | last == null ) {
|
||
495 | // Si es el proveedor generico de JDBC, lo guarda para
|
||
496 | // a?adirlo al final del combo.
|
||
497 | 44419 | jjdelcerro | last = new LabeledValueImpl(
|
498 | 43020 | jjdelcerro | factory.getDescription(), |
499 | (JDBCServerExplorerParameters) params |
||
500 | 43377 | jjdelcerro | ); |
501 | } else {
|
||
502 | this.cboConnectors.addItem(
|
||
503 | 44419 | jjdelcerro | new LabeledValueImpl(
|
504 | 43377 | jjdelcerro | factory.getDescription(), |
505 | (JDBCServerExplorerParameters) params |
||
506 | ) |
||
507 | ); |
||
508 | } |
||
509 | 41648 | jjdelcerro | } |
510 | 41487 | jjdelcerro | } |
511 | } |
||
512 | 41648 | jjdelcerro | if( last!=null ) { |
513 | 43020 | jjdelcerro | this.cboConnectors.addItem(last);
|
514 | 41648 | jjdelcerro | } |
515 | 41487 | jjdelcerro | } |
516 | 41492 | jjdelcerro | |
517 | private void fillConnections() { |
||
518 | DataManager dataManager = DALLocator.getDataManager(); |
||
519 | DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
||
520 | |||
521 | 43377 | jjdelcerro | this.cboConnections.removeAllItems();
|
522 | 44419 | jjdelcerro | for (DataServerExplorerPoolEntry entry : pool) {
|
523 | 41492 | jjdelcerro | if ( entry.getExplorerParameters() instanceof JDBCServerExplorerParameters ) { |
524 | 43377 | jjdelcerro | if( this.connectorFilter==null || this.connectorFilter.accept((JDBCServerExplorerParameters) entry.getExplorerParameters()) ) { |
525 | JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
||
526 | this.cboConnections.addItem(
|
||
527 | 44419 | jjdelcerro | new LabeledValueImpl<>(entry.getName(), dbParams)
|
528 | 43377 | jjdelcerro | ); |
529 | } |
||
530 | 41492 | jjdelcerro | } |
531 | } |
||
532 | this.cboConnections.setSelectedIndex(-1); |
||
533 | } |
||
534 | |||
535 | 42616 | jjdelcerro | @Override
|
536 | 41631 | jjdelcerro | public void delete() { |
537 | String name = this.getConnectionName(); |
||
538 | DataManager dataManager = DALLocator.getDataManager(); |
||
539 | DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
||
540 | |||
541 | pool.remove(name); |
||
542 | 43377 | jjdelcerro | fillConnections(); |
543 | 41631 | jjdelcerro | } |
544 | |||
545 | 42616 | jjdelcerro | @Override
|
546 | 41631 | jjdelcerro | public void clear() { |
547 | this.cboConnections.setSelectedIndex(-1); |
||
548 | this.cboConnectors.setSelectedIndex(-1); |
||
549 | this.txtServer.setText(""); |
||
550 | this.txtPort.setText(""); |
||
551 | this.txtDataBase.setText(""); |
||
552 | this.txtUsername.setText(""); |
||
553 | this.txtPassword.setText(""); |
||
554 | 44419 | jjdelcerro | this.txtFile.setText(""); |
555 | 41631 | jjdelcerro | } |
556 | 43114 | jjdelcerro | |
557 | private void onBrowseDatabase() { |
||
558 | if( this.forcedParameters==null ) { |
||
559 | return;
|
||
560 | } |
||
561 | ThreadSafeDialogsManager dlgmanager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
||
562 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
563 | File[] files = dlgmanager.showOpenFileDialog( |
||
564 | i18nManager.getTranslation("_Select_database"),
|
||
565 | null
|
||
566 | ); |
||
567 | if( ArrayUtils.isEmpty(files) ) {
|
||
568 | return;
|
||
569 | } |
||
570 | ((FilesystemStoreParameters)this.forcedParameters).setFile(files[0]); |
||
571 | 43377 | jjdelcerro | this.txtFile.setText(((FilesystemStoreParameters)(this.forcedParameters)).getFile().getAbsolutePath()); |
572 | 43114 | jjdelcerro | } |
573 | 41487 | jjdelcerro | } |