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 @ 41749
History | View | Annotate | Download (10.7 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.gvsig.tools.ToolsLocator; |
23 |
import org.gvsig.tools.i18n.I18nManager; |
24 |
import org.slf4j.Logger; |
25 |
import org.slf4j.LoggerFactory; |
26 |
|
27 |
// org.gvsig.fmap.mapcontrol.dal.jdbc.JDBCConnectionPanel
|
28 |
public class JDBCConnectionPanel extends JDBCConnectionPanelLayout { |
29 |
|
30 |
private static final Logger logger = LoggerFactory.getLogger(JDBCConnectionPanel.class); |
31 |
|
32 |
private static class ServerExplorerParametersComboItem { |
33 |
|
34 |
private JDBCServerExplorerParameters params;
|
35 |
private String label; |
36 |
|
37 |
public ServerExplorerParametersComboItem(String label, JDBCServerExplorerParameters params) { |
38 |
this.params = params;
|
39 |
this.label = label;
|
40 |
} |
41 |
|
42 |
public ServerExplorerParametersComboItem(JDBCServerExplorerParameters params) {
|
43 |
this(params.getExplorerName(), params);
|
44 |
} |
45 |
|
46 |
public String toString() { |
47 |
return this.label; |
48 |
} |
49 |
|
50 |
public JDBCServerExplorerParameters getParams() {
|
51 |
return this.params; |
52 |
} |
53 |
|
54 |
public String getLabel() { |
55 |
return this.label; |
56 |
} |
57 |
} |
58 |
|
59 |
public JDBCConnectionPanel() {
|
60 |
initComponents(); |
61 |
} |
62 |
|
63 |
protected void initComponents() { |
64 |
this.cboConnections.setEditable(true); |
65 |
this.cboConnections.addItemListener(new ItemListener() { |
66 |
public void itemStateChanged(ItemEvent e) { |
67 |
onChangeConnection(); |
68 |
} |
69 |
}); |
70 |
this.cboConnectors.addItemListener(new ItemListener() { |
71 |
public void itemStateChanged(ItemEvent e) { |
72 |
onChangeConnector(); |
73 |
} |
74 |
}); |
75 |
try {
|
76 |
fillConnections(); |
77 |
fillConnectors(); |
78 |
} catch(Throwable th) { |
79 |
// Ignore it to allow use in GUI builders
|
80 |
} |
81 |
this.translate();
|
82 |
} |
83 |
|
84 |
private void translate() { |
85 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
86 |
|
87 |
this.lblConnectionName.setText(i18nManager.getTranslation("_Connection_name")); |
88 |
this.lblConnector.setText(i18nManager.getTranslation("_Driver_type")); |
89 |
this.lblServer.setText(i18nManager.getTranslation("_Host")); |
90 |
this.lblPort.setText(i18nManager.getTranslation("_Port")); |
91 |
this.lblDataBase.setText(i18nManager.getTranslation("_Database")); |
92 |
this.lblUsername.setText(i18nManager.getTranslation("_User")); |
93 |
this.lblPassword.setText(i18nManager.getTranslation("_Password")); |
94 |
this.lblFoother.setText("<html>"+i18nManager.getTranslation("_JDBCConecctionPanel_foother")+"</html>"); |
95 |
} |
96 |
|
97 |
public void setServerExplorerParameters(JDBCServerExplorerParameters parameters) { |
98 |
|
99 |
int indexConnector = this.getIndexOfConnector(parameters); |
100 |
if ( indexConnector >= 0 && this.cboConnectors.getSelectedIndex()!=indexConnector ) { |
101 |
this.cboConnectors.setSelectedIndex(indexConnector);
|
102 |
} |
103 |
|
104 |
this.txtServer.setText(parameters.getHost());
|
105 |
Integer port = parameters.getPort();
|
106 |
if ( port == null ) { |
107 |
this.txtPort.setText(""); |
108 |
} else {
|
109 |
this.txtPort.setText(String.valueOf(port)); |
110 |
} |
111 |
this.txtDataBase.setText(parameters.getDBName());
|
112 |
this.txtUsername.setText(parameters.getUser());
|
113 |
this.txtPassword.setText(parameters.getPassword());
|
114 |
} |
115 |
|
116 |
public JDBCServerExplorerParameters getServerExplorerParameters() {
|
117 |
JDBCServerExplorerParameters connector = this.getConnector();
|
118 |
JDBCServerExplorerParameters params = (JDBCServerExplorerParameters) connector.getCopy(); |
119 |
params.setHost(this.getServer());
|
120 |
params.setPort(this.getPort());
|
121 |
params.setDBName(this.getDataBaseName());
|
122 |
params.setUser(this.getUsername());
|
123 |
params.setPassword(this.getPassword());
|
124 |
|
125 |
if ( this.getConnectionName() != null ) { |
126 |
DataManager dataManager = DALLocator.getDataManager(); |
127 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
128 |
pool.add(this.getConnectionName(), params);
|
129 |
} |
130 |
return params;
|
131 |
} |
132 |
|
133 |
protected void setConnectionName(String connectionName) { |
134 |
JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
135 |
txtConnections.setText(connectionName); |
136 |
} |
137 |
|
138 |
public String getConnectionName() { |
139 |
JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
140 |
String value = txtConnections.getText();
|
141 |
return StringUtils.defaultIfBlank(value, null); |
142 |
} |
143 |
|
144 |
protected JDBCServerExplorerParameters getConnector() {
|
145 |
ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
146 |
JDBCServerExplorerParameters value = item.getParams(); |
147 |
return value;
|
148 |
} |
149 |
|
150 |
protected String getConnectorName() { |
151 |
JDBCServerExplorerParameters value = this.getConnector();
|
152 |
if ( value == null ) { |
153 |
return null; |
154 |
} |
155 |
return StringUtils.defaultIfBlank(value.getExplorerName(), null); |
156 |
} |
157 |
|
158 |
protected String getServer() { |
159 |
return StringUtils.defaultIfBlank(this.txtServer.getText(), null); |
160 |
} |
161 |
|
162 |
protected int getPort() { |
163 |
String svalue = StringUtils.defaultIfBlank(this.txtPort.getText(), null); |
164 |
int ivalue = -1; |
165 |
try {
|
166 |
ivalue = Integer.parseInt(svalue);
|
167 |
} catch (Exception ex) { |
168 |
ivalue = -1;
|
169 |
} |
170 |
return ivalue;
|
171 |
} |
172 |
|
173 |
protected String getDataBaseName() { |
174 |
return StringUtils.defaultIfBlank(this.txtDataBase.getText(), null); |
175 |
} |
176 |
|
177 |
protected String getUsername() { |
178 |
return StringUtils.defaultIfBlank(this.txtUsername.getText(), null); |
179 |
} |
180 |
|
181 |
protected String getPassword() { |
182 |
return StringUtils.defaultIfBlank(this.txtPassword.getText(), null); |
183 |
} |
184 |
|
185 |
private void onChangeConnector() { |
186 |
ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
187 |
if( item==null ) { |
188 |
return;
|
189 |
} |
190 |
JDBCServerExplorerParameters connector = item.getParams(); |
191 |
|
192 |
if ( connector == null ) { |
193 |
return;
|
194 |
} |
195 |
this.setServerExplorerParameters(connector);
|
196 |
} |
197 |
|
198 |
private void onChangeConnection() { |
199 |
Object item = this.cboConnections.getSelectedItem(); |
200 |
if ( item instanceof ServerExplorerParametersComboItem ) { |
201 |
JDBCServerExplorerParameters connection = ((ServerExplorerParametersComboItem) item).getParams(); |
202 |
if ( connection == null ) { |
203 |
return;
|
204 |
} |
205 |
this.setServerExplorerParameters(connection);
|
206 |
} |
207 |
} |
208 |
|
209 |
|
210 |
private int getIndexOfConnector(JDBCServerExplorerParameters explorerParameters) { |
211 |
String code = null; |
212 |
try {
|
213 |
code = explorerParameters.toString(); |
214 |
ComboBoxModel model = this.cboConnectors.getModel(); |
215 |
for ( int i = 0; i < model.getSize(); i++ ) { |
216 |
ServerExplorerParametersComboItem x = (ServerExplorerParametersComboItem) model.getElementAt(i); |
217 |
if ( x.getLabel().equalsIgnoreCase(explorerParameters.getExplorerName()) ) {
|
218 |
return i;
|
219 |
} |
220 |
} |
221 |
} catch (Exception ex) { |
222 |
logger.warn("Can't get index of exporer parameter '" + code + "'.", ex); |
223 |
} |
224 |
return -1; |
225 |
} |
226 |
|
227 |
private void fillConnectors() { |
228 |
DataManager dataManager = DALLocator.getDataManager(); |
229 |
List<String> explorers = dataManager.getExplorerProviders(); |
230 |
|
231 |
DataServerExplorerParameters params; |
232 |
|
233 |
JDBCServerExplorerParameters last = null;
|
234 |
Iterator<String> it = explorers.iterator(); |
235 |
while ( it.hasNext() ) {
|
236 |
String explorerName = it.next();
|
237 |
try {
|
238 |
params = dataManager.createServerExplorerParameters(explorerName); |
239 |
} catch (DataException e) {
|
240 |
continue;
|
241 |
} |
242 |
if ( params instanceof JDBCServerExplorerParameters ) { |
243 |
JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) params; |
244 |
if( dbParams.getClass().getName().equals(JDBCServerExplorerParameters.class.getName()) ) {
|
245 |
last = dbParams; |
246 |
} else {
|
247 |
this.cboConnectors.addItem(
|
248 |
new ServerExplorerParametersComboItem(dbParams)
|
249 |
); |
250 |
} |
251 |
} |
252 |
} |
253 |
if( last!=null ) { |
254 |
this.cboConnectors.addItem(
|
255 |
new ServerExplorerParametersComboItem(last)
|
256 |
); |
257 |
} |
258 |
} |
259 |
|
260 |
private void fillConnections() { |
261 |
DataManager dataManager = DALLocator.getDataManager(); |
262 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
263 |
|
264 |
DataServerExplorerParameters params; |
265 |
|
266 |
Iterator<DataServerExplorerPoolEntry> it = pool.iterator();
|
267 |
while ( it.hasNext() ) {
|
268 |
DataServerExplorerPoolEntry entry = it.next(); |
269 |
if ( entry.getExplorerParameters() instanceof JDBCServerExplorerParameters ) { |
270 |
JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
271 |
this.cboConnections.addItem(
|
272 |
new ServerExplorerParametersComboItem(entry.getName(), dbParams)
|
273 |
); |
274 |
} |
275 |
} |
276 |
this.cboConnections.setSelectedIndex(-1); |
277 |
} |
278 |
|
279 |
public void delete() { |
280 |
String name = this.getConnectionName(); |
281 |
DataManager dataManager = DALLocator.getDataManager(); |
282 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
283 |
|
284 |
pool.remove(name); |
285 |
} |
286 |
|
287 |
public void clear() { |
288 |
this.cboConnections.setSelectedIndex(-1); |
289 |
this.cboConnectors.setSelectedIndex(-1); |
290 |
this.txtServer.setText(""); |
291 |
this.txtPort.setText(""); |
292 |
this.txtDataBase.setText(""); |
293 |
this.txtUsername.setText(""); |
294 |
this.txtPassword.setText(""); |
295 |
} |
296 |
} |