Revision 41492 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.exportto/org.gvsig.exportto.swing/org.gvsig.exportto.swing.prov/org.gvsig.exportto.swing.prov.jdbc/src/main/java/org/gvsig/exportto/swing/prov/jdbc/panel/DBConnectionPanel.java

View differences:

DBConnectionPanel.java
5 5
 */
6 6
package org.gvsig.exportto.swing.prov.jdbc.panel;
7 7

  
8
import java.awt.Color;
9 8
import java.awt.event.ItemEvent;
10 9
import java.awt.event.ItemListener;
11 10
import java.util.Iterator;
12 11
import java.util.List;
13 12
import java.util.Map;
13
import javax.swing.ComboBoxModel;
14 14
import javax.swing.JTextField;
15 15
import org.apache.commons.lang3.StringUtils;
16 16
import org.gvsig.fmap.dal.DALLocator;
17 17
import org.gvsig.fmap.dal.DataManager;
18 18
import org.gvsig.fmap.dal.DataServerExplorerParameters;
19
import org.gvsig.fmap.dal.DataServerExplorerPool;
20
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry;
19 21
import org.gvsig.fmap.dal.exception.DataException;
20
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorerParameters;
21 22
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
22 25

  
23

  
24 26
public class DBConnectionPanel extends DBConnectionPanelLayout {
25 27

  
26
    Map<String, JDBCServerExplorerParameters> parameters = null;
28
    private static final Logger logger = LoggerFactory.getLogger(DBConnectionPanel.class);
27 29

  
28 30
    private static class ServerExplorerParametersComboItem {
29 31

  
......
46 48
        public JDBCServerExplorerParameters getParams() {
47 49
            return this.params;
48 50
        }
51

  
52
        public String getLabel() {
53
            return this.label;
54
        }
49 55
    }
50 56

  
51 57
    public DBConnectionPanel() {
52 58
        initComponents();
53 59
    }
54 60

  
55
    public void setServerExplorerParameters(Map<String, JDBCServerExplorerParameters> parameters) {
56
        this.parameters = parameters;
57
        if (parameters == null) {
58
            this.lblConnectionName.setVisible(false);
59
            this.cboConnections.setVisible(false);
60
            this.botManage.setVisible(false);
61
            return;
62
        }
63
        Iterator<Map.Entry<String, JDBCServerExplorerParameters>> it = parameters.entrySet().iterator();
64
        while (it.hasNext()) {
65
            Map.Entry<String, JDBCServerExplorerParameters> entry = it.next();
66
            this.cboConnections.addItem(
67
                    new ServerExplorerParametersComboItem(entry.getKey(), entry.getValue())
68
            );
69
        }
70
        this.lblConnectionName.setVisible(true);
71
        this.cboConnections.setVisible(true);
72
        this.botManage.setVisible(true);
73
    }
74

  
75 61
    protected void initComponents() {
76 62
        this.cboConnections.setEditable(true);
77 63
        this.cboConnections.addItemListener(new ItemListener() {
......
84 70
                onChangeConnector();
85 71
            }
86 72
        });
73
        fillConnections();
87 74
        fillConnectors();
88 75
        this.botManage.setEnabled(false); // TODO
89

  
90
        this.lblConnectionName.setVisible(false);
91
        this.cboConnections.setVisible(false);
92
        this.botManage.setVisible(false);
93 76
    }
94 77

  
95 78
    public void setServerExplorerParameters(JDBCServerExplorerParameters parameters) {
79

  
80
        int indexConnector = this.getIndexOfConnector(parameters);
81
        if ( indexConnector >= 0 && this.cboConnectors.getSelectedIndex()!=indexConnector ) {
82
            this.cboConnectors.setSelectedIndex(indexConnector);
83
        }
84

  
96 85
        this.txtServer.setText(parameters.getHost());
97 86
        Integer port = parameters.getPort();
98
        if( port == null ) {
87
        if ( port == null ) {
99 88
            this.txtPort.setText("");
100 89
        } else {
101 90
            this.txtPort.setText(String.valueOf(port));
......
114 103
        params.setUser(this.getUsername());
115 104
        params.setPassword(this.getPassword());
116 105

  
117
        if( this.getConnectionName()!=null ) {
118
            this.parameters.put(this.getConnectionName(), params);
106
        if ( this.getConnectionName() != null ) {
107
            DataManager dataManager = DALLocator.getDataManager();
108
            DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
109
            pool.add(this.getConnectionName(), params);
119 110
        }
120 111
        return params;
121 112
    }
......
147 138

  
148 139
    public String getConnectorName() {
149 140
        JDBCServerExplorerParameters value = this.getConnector();
150
        if (value == null) {
141
        if ( value == null ) {
151 142
            return null;
152 143
        }
153 144
        return StringUtils.defaultIfBlank(value.getExplorerName(), null);
......
183 174
    private void onChangeConnector() {
184 175
        ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
185 176
        JDBCServerExplorerParameters connector = item.getParams();
186
        if (connector == null) {
177
        if ( connector == null ) {
187 178
            return;
188 179
        }
189 180
        this.setServerExplorerParameters(connector);
190 181
    }
191 182

  
192 183
    private void onChangeConnection() {
193
        ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnections.getSelectedItem();
194
        JDBCServerExplorerParameters connection = item.getParams();
195
        if (connection == null) {
196
            return;
184
        Object item = this.cboConnections.getSelectedItem();
185
        if ( item instanceof ServerExplorerParametersComboItem ) {
186
            JDBCServerExplorerParameters connection = ((ServerExplorerParametersComboItem) item).getParams();
187
            if ( connection == null ) {
188
                return;
189
            }
190
            this.setServerExplorerParameters(connection);
197 191
        }
198
        this.setServerExplorerParameters(connection);
199 192
    }
200 193

  
194
    private int getIndexOfConnector(JDBCServerExplorerParameters explorerParameters) {
195
        String code = null;
196
        try {
197
            code = explorerParameters.toString();
198
            ComboBoxModel model = this.cboConnectors.getModel();
199
            for ( int i = 0; i < model.getSize(); i++ ) {
200
                ServerExplorerParametersComboItem x = (ServerExplorerParametersComboItem) model.getElementAt(i);
201
                if ( x.getLabel().equalsIgnoreCase(explorerParameters.getExplorerName()) ) {
202
                    return i;
203
                }
204
            }
205
        } catch (Exception ex) {
206
            logger.warn("Can't get index of exporer parameter '" + code + "'.", ex);
207
        }
208
        return -1;
209
    }
210

  
201 211
    private void fillConnectors() {
202 212
        DataManager dataManager = DALLocator.getDataManager();
203 213
        List<String> explorers = dataManager.getExplorerProviders();
......
205 215
        DataServerExplorerParameters params;
206 216

  
207 217
        Iterator<String> it = explorers.iterator();
208
        while (it.hasNext()) {
218
        while ( it.hasNext() ) {
209 219
            String explorerName = it.next();
210 220
            try {
211 221
                params = dataManager.createServerExplorerParameters(explorerName);
212 222
            } catch (DataException e) {
213 223
                continue;
214 224
            }
215
            if (params instanceof JDBCServerExplorerParameters) {
225
            if ( params instanceof JDBCServerExplorerParameters ) {
216 226
                JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) params;
217 227
                this.cboConnectors.addItem(
218 228
                        new ServerExplorerParametersComboItem(dbParams)
......
220 230
            }
221 231
        }
222 232
    }
223
    
233

  
234
    private void fillConnections() {
235
        DataManager dataManager = DALLocator.getDataManager();
236
        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
237

  
238
        DataServerExplorerParameters params;
239

  
240
        Iterator<DataServerExplorerPoolEntry> it = pool.iterator();
241
        while ( it.hasNext() ) {
242
            DataServerExplorerPoolEntry entry = it.next();
243
            if ( entry.getExplorerParameters() instanceof JDBCServerExplorerParameters ) {
244
                JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters();
245
                this.cboConnections.addItem(
246
                        new ServerExplorerParametersComboItem(entry.getName(), dbParams)
247
                );
248
            }
249
        }
250
        this.cboConnections.setSelectedIndex(-1);
251
    }
252

  
224 253
}

Also available in: Unified diff