Statistics
| Revision:

svn-gvsig-desktop / 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 @ 41488

History | View | Annotate | Download (7.73 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.exportto.swing.prov.jdbc.panel;
7

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

    
23

    
24
public class DBConnectionPanel extends DBConnectionPanelLayout {
25

    
26
    Map<String, JDBCServerExplorerParameters> parameters = null;
27

    
28
    private static class ServerExplorerParametersComboItem {
29

    
30
        private JDBCServerExplorerParameters params;
31
        private String label;
32

    
33
        public ServerExplorerParametersComboItem(String label, JDBCServerExplorerParameters params) {
34
            this.params = params;
35
            this.label = label;
36
        }
37

    
38
        public ServerExplorerParametersComboItem(JDBCServerExplorerParameters params) {
39
            this(params.getExplorerName(), params);
40
        }
41

    
42
        public String toString() {
43
            return this.label;
44
        }
45

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

    
51
    public DBConnectionPanel() {
52
        initComponents();
53
    }
54

    
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
    protected void initComponents() {
76
        this.cboConnections.setEditable(true);
77
        this.cboConnections.addItemListener(new ItemListener() {
78
            public void itemStateChanged(ItemEvent e) {
79
                onChangeConnection();
80
            }
81
        });
82
        this.cboConnectors.addItemListener(new ItemListener() {
83
            public void itemStateChanged(ItemEvent e) {
84
                onChangeConnector();
85
            }
86
        });
87
        fillConnectors();
88
        this.botManage.setEnabled(false); // TODO
89

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

    
95
    public void setServerExplorerParameters(JDBCServerExplorerParameters parameters) {
96
        this.txtServer.setText(parameters.getHost());
97
        Integer port = parameters.getPort();
98
        if( port == null ) {
99
            this.txtPort.setText("");
100
        } else {
101
            this.txtPort.setText(String.valueOf(port));
102
        }
103
        this.txtDataBase.setText(parameters.getDBName());
104
        this.txtUsername.setText(parameters.getUser());
105
        this.txtPassword.setText(parameters.getPassword());
106
    }
107

    
108
    public JDBCServerExplorerParameters getServerExplorerParameters() {
109
        JDBCServerExplorerParameters connector = this.getConnector();
110
        JDBCServerExplorerParameters params = (JDBCServerExplorerParameters) connector.getCopy();
111
        params.setHost(this.getServer());
112
        params.setPort(this.getPort());
113
        params.setDBName(this.getDataBaseName());
114
        params.setUser(this.getUsername());
115
        params.setPassword(this.getPassword());
116

    
117
        if( this.getConnectionName()!=null ) {
118
            this.parameters.put(this.getConnectionName(), params);
119
        }
120
        return params;
121
    }
122

    
123
    public void setConnection(JDBCServerExplorerParameters parameters) {
124
        this.setServerExplorerParameters(parameters);
125
    }
126

    
127
    public JDBCServerExplorerParameters getConnection() {
128
        return this.getServerExplorerParameters();
129
    }
130

    
131
    public void setConnectionName(String connectionName) {
132
        JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent();
133
        txtConnections.setText(connectionName);
134
    }
135

    
136
    public String getConnectionName() {
137
        JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent();
138
        String value = txtConnections.getText();
139
        return StringUtils.defaultIfBlank(value, null);
140
    }
141

    
142
    public JDBCServerExplorerParameters getConnector() {
143
        ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
144
        JDBCServerExplorerParameters value = item.getParams();
145
        return value;
146
    }
147

    
148
    public String getConnectorName() {
149
        JDBCServerExplorerParameters value = this.getConnector();
150
        if (value == null) {
151
            return null;
152
        }
153
        return StringUtils.defaultIfBlank(value.getExplorerName(), null);
154
    }
155

    
156
    public String getServer() {
157
        return StringUtils.defaultIfBlank(this.txtServer.getText(), null);
158
    }
159

    
160
    public int getPort() {
161
        String svalue = StringUtils.defaultIfBlank(this.txtPort.getText(), null);
162
        int ivalue = -1;
163
        try {
164
            ivalue = Integer.parseInt(svalue);
165
        } catch (Exception ex) {
166
            ivalue = -1;
167
        }
168
        return ivalue;
169
    }
170

    
171
    public String getDataBaseName() {
172
        return StringUtils.defaultIfBlank(this.txtDataBase.getText(), null);
173
    }
174

    
175
    public String getUsername() {
176
        return StringUtils.defaultIfBlank(this.txtUsername.getText(), null);
177
    }
178

    
179
    public String getPassword() {
180
        return StringUtils.defaultIfBlank(this.txtPassword.getText(), null);
181
    }
182

    
183
    private void onChangeConnector() {
184
        ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
185
        JDBCServerExplorerParameters connector = item.getParams();
186
        if (connector == null) {
187
            return;
188
        }
189
        this.setServerExplorerParameters(connector);
190
    }
191

    
192
    private void onChangeConnection() {
193
        ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnections.getSelectedItem();
194
        JDBCServerExplorerParameters connection = item.getParams();
195
        if (connection == null) {
196
            return;
197
        }
198
        this.setServerExplorerParameters(connection);
199
    }
200

    
201
    private void fillConnectors() {
202
        DataManager dataManager = DALLocator.getDataManager();
203
        List<String> explorers = dataManager.getExplorerProviders();
204

    
205
        DataServerExplorerParameters params;
206

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