Revision 44419 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

View differences:

DefaultJDBCConnectionPanel.java
6 6
import java.awt.event.ItemEvent;
7 7
import java.awt.event.ItemListener;
8 8
import java.io.File;
9
import java.util.Iterator;
9
import java.util.Objects;
10 10
import javax.swing.ComboBoxModel;
11 11
import javax.swing.JComponent;
12 12
import javax.swing.JLabel;
......
14 14
import javax.swing.SwingUtilities;
15 15
import javax.swing.plaf.basic.BasicHTML;
16 16
import javax.swing.text.View;
17
import org.apache.commons.io.FilenameUtils;
17 18
import org.apache.commons.lang3.ArrayUtils;
18 19
import org.apache.commons.lang3.StringUtils;
19 20
import org.gvsig.fmap.dal.DALLocator;
......
31 32
import org.gvsig.tools.i18n.I18nManager;
32 33
import org.gvsig.tools.swing.api.ToolsSwingLocator;
33 34
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
35
import org.gvsig.tools.util.LabeledValue;
36
import org.gvsig.tools.util.LabeledValueImpl;
34 37
import org.slf4j.Logger;
35 38
import org.slf4j.LoggerFactory;
36 39

  
......
49 52
        return this;
50 53
    }
51 54
    
52
    private static class ServerExplorerParametersComboItem {
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
//    }
53 82

  
54
        private JDBCServerExplorerParameters params;
55
        private String label;
56

  
57
        public ServerExplorerParametersComboItem(String label, JDBCServerExplorerParameters params) {
58
            this.params = params;
59
            this.label = label;
60
        }
61

  
62
        public ServerExplorerParametersComboItem(JDBCServerExplorerParameters params) {
63
            this(params.getExplorerName(), params);
64
        }
65

  
66
        @Override
67
        public String toString() {
68
            return this.label;
69
        }
70

  
71
        public JDBCServerExplorerParameters getParams() {
72
            return this.params;
73
        }
74

  
75
        public String getLabel() {
76
            return this.label;
77
        }
78
    }
79

  
80 83
    @SuppressWarnings("OverridableMethodCallInConstructor")
81 84
    public DefaultJDBCConnectionPanel() {
82 85
        initComponents();
......
163 166
        this.lblPassword.setText(i18nManager.getTranslation("_Password"));
164 167
        this.lblFoother.setText("<html>"+i18nManager.getTranslation("_JDBCConecctionPanel_foother")+"</html>");
165 168
    }
169

  
170
    @Override
171
    public void setLabeledServerExplorerParameters(LabeledValue<JDBCServerExplorerParameters> parameters) {
172
        if( parameters == null ) {
173
            return;
174
        }
175
        if( parameters.getValue()!=null ) {
176
            this.setServerExplorerParameters(parameters.getValue());
177
        }
178
        if( !StringUtils.isBlank(parameters.getLabel()) ) {
179
            this.cboConnections.setSelectedItem(parameters.getLabel());
180
        }
181
    }
166 182
    
167 183
    @Override
168 184
    public void setServerExplorerParameters(JDBCServerExplorerParameters parameters) {
......
217 233

  
218 234
    @Override
219 235
    public JDBCServerExplorerParameters getServerExplorerParameters() {
236
        LabeledValue<JDBCServerExplorerParameters> labeledConnection = this.getLabeledServerExplorerParameters();
237
        if( labeledConnection==null ) {
238
            return null;
239
        }
240
        return labeledConnection.getValue();
241
    }
242
    
243
    @Override
244
    public LabeledValue<JDBCServerExplorerParameters> getLabeledServerExplorerParameters() {
220 245
        JDBCServerExplorerParameters params;
221 246
        JDBCServerExplorerParameters connector = this.getConnector();
222 247
        if( connector==null ) {
......
248 273
            params.setPassword(s);
249 274
        }
250 275

  
251
        if ( this.getConnectionName() != null ) {
252
            DataManager dataManager = DALLocator.getDataManager();
253
            DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
254
            pool.add(this.getConnectionName(), params);
276
        String name = this.getConnectionName();
277
        if ( StringUtils.isBlank(name) ) {
278
            if( params instanceof FilesystemStoreParameters ) {
279
                File f = ((FilesystemStoreParameters) params).getFile();
280
                if( f!=null ) {
281
                    name = FilenameUtils.getBaseName(f.getPath());
282
                }
283
            }
284
            if( StringUtils.isBlank(name) ) {
285
                StringBuilder builder = new StringBuilder();
286
                builder.append(Objects.toString(params.getHost(),""));
287
                if( builder.length()>0 && !builder.toString().endsWith("-")) {
288
                    builder.append("-");
289
                }
290
                builder.append(Objects.toString(params.getPort(),""));
291
                if( builder.length()>0 && !builder.toString().endsWith("-")) {
292
                    builder.append("-");
293
                }
294
                builder.append(Objects.toString(params.getDBName(),""));
295
                name = builder.toString();
296
            }
255 297
        }
256
        return params;
298
        if( StringUtils.isBlank(name) ) {
299
            name = getUniqueEntryName("Connection");
300
        } else {
301
            name = getUniqueEntryName(name);
302
        }
303

  
304
        DataManager dataManager = DALLocator.getDataManager();
305
        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
306
        pool.add(name, params);
307
        return new LabeledValueImpl<>(name,params);
257 308
    }
309
    
310
    private String getUniqueEntryName(String name) {
311
        DataManager dataManager = DALLocator.getDataManager();
312
        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
313
        String baseName = name;
314
        for (int i = 0; i < 1000; i++) {
315
            if( !pool.contains(name) ) {
316
                return name;
317
            }
318
            name = baseName+"-"+i;
319
        }
320
        return name + System.currentTimeMillis();
321
    }
258 322

  
259 323
    protected void setConnectionName(String connectionName) {
260 324
        JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent();
......
269 333
    }
270 334

  
271 335
    protected JDBCServerExplorerParameters getConnector() {
272
        ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
273
        JDBCServerExplorerParameters value = item.getParams();
274
        return value;
336
        LabeledValue<JDBCServerExplorerParameters> item = (LabeledValue<JDBCServerExplorerParameters>) this.cboConnectors.getSelectedItem();
337
        if( item == null ) {
338
            return null;
339
        }
340
        return item.getValue();
275 341
    }
276 342

  
277 343
    protected String getConnectorName() {
......
310 376
    }
311 377

  
312 378
    private void onChangeConnector() {
313
        ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
379
        LabeledValue<JDBCServerExplorerParameters> item = (LabeledValue<JDBCServerExplorerParameters>) this.cboConnectors.getSelectedItem();
314 380
        if( item==null ) {
315 381
            return;
316 382
        }
317
        JDBCServerExplorerParameters connector = item.getParams();
383
        JDBCServerExplorerParameters connector = item.getValue();
318 384
        
319 385
        if ( connector == null ) {
320 386
            return;
......
323 389
    }
324 390

  
325 391
    private void onChangeConnection() {
326
        Object item = this.cboConnections.getSelectedItem();
327
        if ( item instanceof ServerExplorerParametersComboItem ) {
328
            JDBCServerExplorerParameters connection = ((ServerExplorerParametersComboItem) item).getParams();
329
            if ( connection == null ) {
330
                return;
331
            }
332
            this.setServerExplorerParameters(connection);
392
        LabeledValue<JDBCServerExplorerParameters> item = (LabeledValue<JDBCServerExplorerParameters>) this.cboConnections.getSelectedItem();
393
        if( item == null ) {
394
            return;
333 395
        }
396
        JDBCServerExplorerParameters connection = item.getValue();
397
        if ( connection == null ) {
398
            return;
399
        }
400
        this.setServerExplorerParameters(connection);
334 401
    }
335 402
    
336 403

  
......
340 407
            code = explorerParameters.toString();
341 408
            ComboBoxModel model = this.cboConnectors.getModel();
342 409
            for ( int i = 0; i < model.getSize(); i++ ) {
343
                ServerExplorerParametersComboItem x = (ServerExplorerParametersComboItem) model.getElementAt(i);
344
                if ( x.getParams()!=null && x.getParams().getExplorerName().equalsIgnoreCase(explorerParameters.getExplorerName()) ) {
410
                LabeledValue<JDBCServerExplorerParameters> x = (LabeledValue<JDBCServerExplorerParameters>) model.getElementAt(i);
411
                if ( x!=null && x.getValue()!=null && x.getValue().getExplorerName().equalsIgnoreCase(explorerParameters.getExplorerName()) ) {
345 412
                    return i;
346 413
                }
347 414
            }
......
361 428
    private void fillConnectors() {
362 429
        DataManager dataManager = DALLocator.getDataManager();
363 430

  
364
        ServerExplorerParametersComboItem last = null;
431
        LabeledValue<JDBCServerExplorerParameters> last = null;
365 432
        this.cboConnectors.removeAllItems();
366
        this.cboConnectors.addItem(new ServerExplorerParametersComboItem("",null));
433
        this.cboConnectors.addItem(new LabeledValueImpl("",(JDBCServerExplorerParameters)null));
367 434
        
368 435
        for (DataFactory factory :  dataManager.getServerExplorerRegister() ) {
369 436
            if( factory.isHidden() ) {
......
376 443
                        last == null ) {
377 444
                        // Si es el proveedor generico de JDBC, lo guarda para 
378 445
                        // a?adirlo al final del combo.
379
                        last = new ServerExplorerParametersComboItem(
446
                        last = new LabeledValueImpl(
380 447
                                factory.getDescription(),
381 448
                                (JDBCServerExplorerParameters) params
382 449
                        );
383 450
                    } else {
384 451
                        this.cboConnectors.addItem(
385
                            new ServerExplorerParametersComboItem(
452
                            new LabeledValueImpl(
386 453
                                    factory.getDescription(),
387 454
                                    (JDBCServerExplorerParameters) params
388 455
                            )
......
400 467
        DataManager dataManager = DALLocator.getDataManager();
401 468
        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
402 469

  
403
        DataServerExplorerParameters params;
404 470
        this.cboConnections.removeAllItems();
405
        Iterator it = pool.iterator();
406
        while ( it.hasNext() ) {
407
            DataServerExplorerPoolEntry entry = (DataServerExplorerPoolEntry) it.next();
471
        for (DataServerExplorerPoolEntry entry : pool) {
408 472
            if ( entry.getExplorerParameters() instanceof JDBCServerExplorerParameters ) {
409 473
                if( this.connectorFilter==null || this.connectorFilter.accept((JDBCServerExplorerParameters) entry.getExplorerParameters()) ) {
410 474
                    JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters();
411 475
                    this.cboConnections.addItem(
412
                            new ServerExplorerParametersComboItem(entry.getName(), dbParams)
476
                            new LabeledValueImpl<>(entry.getName(), dbParams)
413 477
                    );
414 478
                }
415 479
            }
......
436 500
        this.txtDataBase.setText("");
437 501
        this.txtUsername.setText("");
438 502
        this.txtPassword.setText("");
503
        this.txtFile.setText("");
439 504
    }
440 505
    
441 506
    private void onBrowseDatabase() {

Also available in: Unified diff