svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / dal / jdbc / JDBCConnectionPanel.java @ 41648
History | View | Annotate | Download (9.85 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.fmap.mapcontrol.dal.jdbc; |
7 |
|
8 |
import java.awt.event.ItemEvent; |
9 |
import java.awt.event.ItemListener; |
10 |
import java.util.Iterator; |
11 |
import java.util.List; |
12 |
import javax.swing.ComboBoxModel; |
13 |
import javax.swing.JTextField; |
14 |
import org.apache.commons.lang3.StringUtils; |
15 |
import org.gvsig.fmap.dal.DALLocator; |
16 |
import org.gvsig.fmap.dal.DataManager; |
17 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
18 |
import org.gvsig.fmap.dal.DataServerExplorerPool; |
19 |
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
20 |
import org.gvsig.fmap.dal.exception.DataException; |
21 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
22 |
import org.slf4j.Logger; |
23 |
import org.slf4j.LoggerFactory; |
24 |
|
25 |
// org.gvsig.fmap.mapcontrol.dal.jdbc.JDBCConnectionPanel
|
26 |
public class JDBCConnectionPanel extends JDBCConnectionPanelLayout { |
27 |
|
28 |
private static final Logger logger = LoggerFactory.getLogger(JDBCConnectionPanel.class); |
29 |
|
30 |
private static class ServerExplorerParametersComboItem { |
31 |
|
32 |
private JDBCServerExplorerParameters params;
|
33 |
private String label; |
34 |
|
35 |
public ServerExplorerParametersComboItem(String label, JDBCServerExplorerParameters params) { |
36 |
this.params = params;
|
37 |
this.label = label;
|
38 |
} |
39 |
|
40 |
public ServerExplorerParametersComboItem(JDBCServerExplorerParameters params) {
|
41 |
this(params.getExplorerName(), params);
|
42 |
} |
43 |
|
44 |
public String toString() { |
45 |
return this.label; |
46 |
} |
47 |
|
48 |
public JDBCServerExplorerParameters getParams() {
|
49 |
return this.params; |
50 |
} |
51 |
|
52 |
public String getLabel() { |
53 |
return this.label; |
54 |
} |
55 |
} |
56 |
|
57 |
public JDBCConnectionPanel() {
|
58 |
initComponents(); |
59 |
} |
60 |
|
61 |
protected void initComponents() { |
62 |
this.cboConnections.setEditable(true); |
63 |
this.cboConnections.addItemListener(new ItemListener() { |
64 |
public void itemStateChanged(ItemEvent e) { |
65 |
onChangeConnection(); |
66 |
} |
67 |
}); |
68 |
this.cboConnectors.addItemListener(new ItemListener() { |
69 |
public void itemStateChanged(ItemEvent e) { |
70 |
onChangeConnector(); |
71 |
} |
72 |
}); |
73 |
try {
|
74 |
fillConnections(); |
75 |
fillConnectors(); |
76 |
} catch(Throwable th) { |
77 |
// Ignore it to allow use in GUI builders
|
78 |
} |
79 |
} |
80 |
|
81 |
public void setServerExplorerParameters(JDBCServerExplorerParameters parameters) { |
82 |
|
83 |
int indexConnector = this.getIndexOfConnector(parameters); |
84 |
if ( indexConnector >= 0 && this.cboConnectors.getSelectedIndex()!=indexConnector ) { |
85 |
this.cboConnectors.setSelectedIndex(indexConnector);
|
86 |
} |
87 |
|
88 |
this.txtServer.setText(parameters.getHost());
|
89 |
Integer port = parameters.getPort();
|
90 |
if ( port == null ) { |
91 |
this.txtPort.setText(""); |
92 |
} else {
|
93 |
this.txtPort.setText(String.valueOf(port)); |
94 |
} |
95 |
this.txtDataBase.setText(parameters.getDBName());
|
96 |
this.txtUsername.setText(parameters.getUser());
|
97 |
this.txtPassword.setText(parameters.getPassword());
|
98 |
} |
99 |
|
100 |
public JDBCServerExplorerParameters getServerExplorerParameters() {
|
101 |
JDBCServerExplorerParameters connector = this.getConnector();
|
102 |
JDBCServerExplorerParameters params = (JDBCServerExplorerParameters) connector.getCopy(); |
103 |
params.setHost(this.getServer());
|
104 |
params.setPort(this.getPort());
|
105 |
params.setDBName(this.getDataBaseName());
|
106 |
params.setUser(this.getUsername());
|
107 |
params.setPassword(this.getPassword());
|
108 |
|
109 |
if ( this.getConnectionName() != null ) { |
110 |
DataManager dataManager = DALLocator.getDataManager(); |
111 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
112 |
pool.add(this.getConnectionName(), params);
|
113 |
} |
114 |
return params;
|
115 |
} |
116 |
|
117 |
protected void setConnectionName(String connectionName) { |
118 |
JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
119 |
txtConnections.setText(connectionName); |
120 |
} |
121 |
|
122 |
public String getConnectionName() { |
123 |
JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
124 |
String value = txtConnections.getText();
|
125 |
return StringUtils.defaultIfBlank(value, null); |
126 |
} |
127 |
|
128 |
protected JDBCServerExplorerParameters getConnector() {
|
129 |
ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
130 |
JDBCServerExplorerParameters value = item.getParams(); |
131 |
return value;
|
132 |
} |
133 |
|
134 |
protected String getConnectorName() { |
135 |
JDBCServerExplorerParameters value = this.getConnector();
|
136 |
if ( value == null ) { |
137 |
return null; |
138 |
} |
139 |
return StringUtils.defaultIfBlank(value.getExplorerName(), null); |
140 |
} |
141 |
|
142 |
protected String getServer() { |
143 |
return StringUtils.defaultIfBlank(this.txtServer.getText(), null); |
144 |
} |
145 |
|
146 |
protected int getPort() { |
147 |
String svalue = StringUtils.defaultIfBlank(this.txtPort.getText(), null); |
148 |
int ivalue = -1; |
149 |
try {
|
150 |
ivalue = Integer.parseInt(svalue);
|
151 |
} catch (Exception ex) { |
152 |
ivalue = -1;
|
153 |
} |
154 |
return ivalue;
|
155 |
} |
156 |
|
157 |
protected String getDataBaseName() { |
158 |
return StringUtils.defaultIfBlank(this.txtDataBase.getText(), null); |
159 |
} |
160 |
|
161 |
protected String getUsername() { |
162 |
return StringUtils.defaultIfBlank(this.txtUsername.getText(), null); |
163 |
} |
164 |
|
165 |
protected String getPassword() { |
166 |
return StringUtils.defaultIfBlank(this.txtPassword.getText(), null); |
167 |
} |
168 |
|
169 |
private void onChangeConnector() { |
170 |
ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
171 |
if( item==null ) { |
172 |
return;
|
173 |
} |
174 |
JDBCServerExplorerParameters connector = item.getParams(); |
175 |
|
176 |
if ( connector == null ) { |
177 |
return;
|
178 |
} |
179 |
this.setServerExplorerParameters(connector);
|
180 |
} |
181 |
|
182 |
private void onChangeConnection() { |
183 |
Object item = this.cboConnections.getSelectedItem(); |
184 |
if ( item instanceof ServerExplorerParametersComboItem ) { |
185 |
JDBCServerExplorerParameters connection = ((ServerExplorerParametersComboItem) item).getParams(); |
186 |
if ( connection == null ) { |
187 |
return;
|
188 |
} |
189 |
this.setServerExplorerParameters(connection);
|
190 |
} |
191 |
} |
192 |
|
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 |
|
211 |
private void fillConnectors() { |
212 |
DataManager dataManager = DALLocator.getDataManager(); |
213 |
List<String> explorers = dataManager.getExplorerProviders(); |
214 |
|
215 |
DataServerExplorerParameters params; |
216 |
|
217 |
JDBCServerExplorerParameters last = null;
|
218 |
Iterator<String> it = explorers.iterator(); |
219 |
while ( it.hasNext() ) {
|
220 |
String explorerName = it.next();
|
221 |
try {
|
222 |
params = dataManager.createServerExplorerParameters(explorerName); |
223 |
} catch (DataException e) {
|
224 |
continue;
|
225 |
} |
226 |
if ( params instanceof JDBCServerExplorerParameters ) { |
227 |
JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) params; |
228 |
if( dbParams.getClass().getName().equals(JDBCServerExplorerParameters.class.getName()) ) {
|
229 |
last = dbParams; |
230 |
} else {
|
231 |
this.cboConnectors.addItem(
|
232 |
new ServerExplorerParametersComboItem(dbParams)
|
233 |
); |
234 |
} |
235 |
} |
236 |
} |
237 |
if( last!=null ) { |
238 |
this.cboConnectors.addItem(
|
239 |
new ServerExplorerParametersComboItem(last)
|
240 |
); |
241 |
} |
242 |
} |
243 |
|
244 |
private void fillConnections() { |
245 |
DataManager dataManager = DALLocator.getDataManager(); |
246 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
247 |
|
248 |
DataServerExplorerParameters params; |
249 |
|
250 |
Iterator<DataServerExplorerPoolEntry> it = pool.iterator();
|
251 |
while ( it.hasNext() ) {
|
252 |
DataServerExplorerPoolEntry entry = it.next(); |
253 |
if ( entry.getExplorerParameters() instanceof JDBCServerExplorerParameters ) { |
254 |
JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
255 |
this.cboConnections.addItem(
|
256 |
new ServerExplorerParametersComboItem(entry.getName(), dbParams)
|
257 |
); |
258 |
} |
259 |
} |
260 |
this.cboConnections.setSelectedIndex(-1); |
261 |
} |
262 |
|
263 |
public void delete() { |
264 |
String name = this.getConnectionName(); |
265 |
DataManager dataManager = DALLocator.getDataManager(); |
266 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
267 |
|
268 |
pool.remove(name); |
269 |
} |
270 |
|
271 |
public void clear() { |
272 |
this.cboConnections.setSelectedIndex(-1); |
273 |
this.cboConnectors.setSelectedIndex(-1); |
274 |
this.txtServer.setText(""); |
275 |
this.txtPort.setText(""); |
276 |
this.txtDataBase.setText(""); |
277 |
this.txtUsername.setText(""); |
278 |
this.txtPassword.setText(""); |
279 |
} |
280 |
} |