svn-gvsig-desktop / 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 @ 43020
History | View | Annotate | Download (12.6 KB)
1 | 41487 | jjdelcerro | /*
|
---|---|---|---|
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 | 42104 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.jdbc; |
7 | 41487 | jjdelcerro | |
8 | 42616 | jjdelcerro | import java.awt.Dimension; |
9 | 41487 | jjdelcerro | import java.awt.event.ItemEvent; |
10 | import java.awt.event.ItemListener; |
||
11 | import java.util.Iterator; |
||
12 | import java.util.List; |
||
13 | 41492 | jjdelcerro | import javax.swing.ComboBoxModel; |
14 | 42112 | jjdelcerro | import javax.swing.JComponent; |
15 | 42616 | jjdelcerro | import javax.swing.JLabel; |
16 | 41487 | jjdelcerro | import javax.swing.JTextField; |
17 | 42616 | jjdelcerro | import javax.swing.plaf.basic.BasicHTML; |
18 | import javax.swing.text.View; |
||
19 | 41487 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
20 | import org.gvsig.fmap.dal.DALLocator; |
||
21 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.DataFactory; |
22 | 41487 | jjdelcerro | import org.gvsig.fmap.dal.DataManager; |
23 | import org.gvsig.fmap.dal.DataServerExplorerParameters; |
||
24 | 41492 | jjdelcerro | import org.gvsig.fmap.dal.DataServerExplorerPool; |
25 | import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
||
26 | 41487 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
27 | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
||
28 | 42112 | jjdelcerro | import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionPanel; |
29 | 41749 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
30 | import org.gvsig.tools.i18n.I18nManager; |
||
31 | 41492 | jjdelcerro | import org.slf4j.Logger; |
32 | import org.slf4j.LoggerFactory; |
||
33 | 41487 | jjdelcerro | |
34 | 41631 | jjdelcerro | // org.gvsig.fmap.mapcontrol.dal.jdbc.JDBCConnectionPanel
|
35 | 42616 | jjdelcerro | public class DefaultJDBCConnectionPanel extends DefaultJDBCConnectionPanelView implements JDBCConnectionPanel{ |
36 | 41487 | jjdelcerro | |
37 | 42112 | jjdelcerro | private static final Logger |
38 | logger = LoggerFactory.getLogger(DefaultJDBCConnectionPanel.class); |
||
39 | private JDBCServerExplorerParameters forcedParameters;
|
||
40 | 41487 | jjdelcerro | |
41 | 42616 | jjdelcerro | @Override
|
42 | 42112 | jjdelcerro | public JComponent asJComponent() { |
43 | return this; |
||
44 | } |
||
45 | 42055 | jjdelcerro | |
46 | 41487 | jjdelcerro | private static class ServerExplorerParametersComboItem { |
47 | |||
48 | private JDBCServerExplorerParameters params;
|
||
49 | private String label; |
||
50 | |||
51 | public ServerExplorerParametersComboItem(String label, JDBCServerExplorerParameters params) { |
||
52 | this.params = params;
|
||
53 | this.label = label;
|
||
54 | } |
||
55 | |||
56 | public ServerExplorerParametersComboItem(JDBCServerExplorerParameters params) {
|
||
57 | this(params.getExplorerName(), params);
|
||
58 | } |
||
59 | |||
60 | 42616 | jjdelcerro | @Override
|
61 | 41487 | jjdelcerro | public String toString() { |
62 | return this.label; |
||
63 | } |
||
64 | |||
65 | public JDBCServerExplorerParameters getParams() {
|
||
66 | return this.params; |
||
67 | } |
||
68 | 41492 | jjdelcerro | |
69 | public String getLabel() { |
||
70 | return this.label; |
||
71 | } |
||
72 | 41487 | jjdelcerro | } |
73 | |||
74 | 42616 | jjdelcerro | @SuppressWarnings("OverridableMethodCallInConstructor") |
75 | 42104 | jjdelcerro | public DefaultJDBCConnectionPanel() {
|
76 | 41488 | jjdelcerro | initComponents(); |
77 | 41487 | jjdelcerro | } |
78 | |||
79 | protected void initComponents() { |
||
80 | this.cboConnections.setEditable(true); |
||
81 | this.cboConnections.addItemListener(new ItemListener() { |
||
82 | 42616 | jjdelcerro | @Override
|
83 | 41487 | jjdelcerro | public void itemStateChanged(ItemEvent e) { |
84 | onChangeConnection(); |
||
85 | } |
||
86 | }); |
||
87 | this.cboConnectors.addItemListener(new ItemListener() { |
||
88 | 42616 | jjdelcerro | @Override
|
89 | 41487 | jjdelcerro | public void itemStateChanged(ItemEvent e) { |
90 | onChangeConnector(); |
||
91 | } |
||
92 | }); |
||
93 | 41631 | jjdelcerro | try {
|
94 | fillConnections(); |
||
95 | fillConnectors(); |
||
96 | } catch(Throwable th) { |
||
97 | // Ignore it to allow use in GUI builders
|
||
98 | 43020 | jjdelcerro | logger.warn("Can't fill combos",th);
|
99 | 41631 | jjdelcerro | } |
100 | 41749 | jjdelcerro | this.translate();
|
101 | 42616 | jjdelcerro | adjustPreferedHeight(this.lblFoother,300); |
102 | 41487 | jjdelcerro | } |
103 | |||
104 | 42616 | jjdelcerro | private void adjustPreferedHeight(JLabel label, int prefWidth) { |
105 | JLabel resizer = new JLabel(); |
||
106 | resizer.setText(label.getText()); |
||
107 | View view = (View) resizer.getClientProperty(BasicHTML.propertyKey); |
||
108 | view.setSize(prefWidth,15);
|
||
109 | float w = view.getPreferredSpan(View.X_AXIS); |
||
110 | float h = view.getPreferredSpan(View.Y_AXIS); |
||
111 | Dimension dim = new Dimension( |
||
112 | (int) Math.ceil(w), |
||
113 | (int) Math.ceil(h) |
||
114 | ); |
||
115 | label.setPreferredSize(dim); |
||
116 | } |
||
117 | |||
118 | 41749 | jjdelcerro | private void translate() { |
119 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
120 | |||
121 | this.lblConnectionName.setText(i18nManager.getTranslation("_Connection_name")); |
||
122 | this.lblConnector.setText(i18nManager.getTranslation("_Driver_type")); |
||
123 | this.lblServer.setText(i18nManager.getTranslation("_Host")); |
||
124 | this.lblPort.setText(i18nManager.getTranslation("_Port")); |
||
125 | this.lblDataBase.setText(i18nManager.getTranslation("_Database")); |
||
126 | this.lblUsername.setText(i18nManager.getTranslation("_User")); |
||
127 | this.lblPassword.setText(i18nManager.getTranslation("_Password")); |
||
128 | this.lblFoother.setText("<html>"+i18nManager.getTranslation("_JDBCConecctionPanel_foother")+"</html>"); |
||
129 | } |
||
130 | |||
131 | 42616 | jjdelcerro | @Override
|
132 | 41487 | jjdelcerro | public void setServerExplorerParameters(JDBCServerExplorerParameters parameters) { |
133 | 42055 | jjdelcerro | this.forcedParameters = (JDBCServerExplorerParameters) parameters.getCopy();
|
134 | |||
135 | 41492 | jjdelcerro | int indexConnector = this.getIndexOfConnector(parameters); |
136 | if ( indexConnector >= 0 && this.cboConnectors.getSelectedIndex()!=indexConnector ) { |
||
137 | this.cboConnectors.setSelectedIndex(indexConnector);
|
||
138 | } |
||
139 | |||
140 | 41487 | jjdelcerro | this.txtServer.setText(parameters.getHost());
|
141 | 41488 | jjdelcerro | Integer port = parameters.getPort();
|
142 | 41492 | jjdelcerro | if ( port == null ) { |
143 | 41488 | jjdelcerro | this.txtPort.setText(""); |
144 | } else {
|
||
145 | this.txtPort.setText(String.valueOf(port)); |
||
146 | } |
||
147 | 41487 | jjdelcerro | this.txtDataBase.setText(parameters.getDBName());
|
148 | this.txtUsername.setText(parameters.getUser());
|
||
149 | this.txtPassword.setText(parameters.getPassword());
|
||
150 | } |
||
151 | |||
152 | 42616 | jjdelcerro | @Override
|
153 | 41487 | jjdelcerro | public JDBCServerExplorerParameters getServerExplorerParameters() {
|
154 | 42055 | jjdelcerro | JDBCServerExplorerParameters params; |
155 | 41487 | jjdelcerro | JDBCServerExplorerParameters connector = this.getConnector();
|
156 | 42055 | jjdelcerro | if( this.forcedParameters==null ) { |
157 | params = (JDBCServerExplorerParameters) connector.getCopy(); |
||
158 | } else {
|
||
159 | params = (JDBCServerExplorerParameters) this.forcedParameters.getCopy();
|
||
160 | } |
||
161 | String s = this.getServer(); |
||
162 | if( s!=null ) { |
||
163 | params.setHost(s); |
||
164 | } |
||
165 | int n = this.getPort(); |
||
166 | if( n>0 ) { |
||
167 | params.setPort(n); |
||
168 | } |
||
169 | s = this.getDataBaseName();
|
||
170 | if( s!=null ) { |
||
171 | params.setDBName(s); |
||
172 | } |
||
173 | s = this.getUsername();
|
||
174 | if( s!=null ) { |
||
175 | params.setUser(s); |
||
176 | } |
||
177 | s = this.getPassword();
|
||
178 | if( s!=null ) { |
||
179 | params.setPassword(s); |
||
180 | } |
||
181 | 41487 | jjdelcerro | |
182 | 41492 | jjdelcerro | if ( this.getConnectionName() != null ) { |
183 | DataManager dataManager = DALLocator.getDataManager(); |
||
184 | DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
||
185 | pool.add(this.getConnectionName(), params);
|
||
186 | 41488 | jjdelcerro | } |
187 | 41487 | jjdelcerro | return params;
|
188 | } |
||
189 | |||
190 | 41631 | jjdelcerro | protected void setConnectionName(String connectionName) { |
191 | 41487 | jjdelcerro | JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
192 | txtConnections.setText(connectionName); |
||
193 | } |
||
194 | |||
195 | 42616 | jjdelcerro | @Override
|
196 | 41487 | jjdelcerro | public String getConnectionName() { |
197 | JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
||
198 | String value = txtConnections.getText();
|
||
199 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(value, null); |
200 | 41487 | jjdelcerro | } |
201 | |||
202 | 41631 | jjdelcerro | protected JDBCServerExplorerParameters getConnector() {
|
203 | 41488 | jjdelcerro | ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
204 | JDBCServerExplorerParameters value = item.getParams(); |
||
205 | 41487 | jjdelcerro | return value;
|
206 | } |
||
207 | |||
208 | 41631 | jjdelcerro | protected String getConnectorName() { |
209 | 41487 | jjdelcerro | JDBCServerExplorerParameters value = this.getConnector();
|
210 | 41492 | jjdelcerro | if ( value == null ) { |
211 | 41487 | jjdelcerro | return null; |
212 | } |
||
213 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(value.getExplorerName(), null); |
214 | 41487 | jjdelcerro | } |
215 | |||
216 | 41631 | jjdelcerro | protected String getServer() { |
217 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(this.txtServer.getText(), null); |
218 | 41487 | jjdelcerro | } |
219 | |||
220 | 41631 | jjdelcerro | protected int getPort() { |
221 | 42104 | jjdelcerro | String svalue = (String) StringUtils.defaultIfBlank(this.txtPort.getText(), null); |
222 | 42616 | jjdelcerro | int ivalue;
|
223 | 41487 | jjdelcerro | try {
|
224 | ivalue = Integer.parseInt(svalue);
|
||
225 | } catch (Exception ex) { |
||
226 | ivalue = -1;
|
||
227 | } |
||
228 | return ivalue;
|
||
229 | } |
||
230 | |||
231 | 41631 | jjdelcerro | protected String getDataBaseName() { |
232 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(this.txtDataBase.getText(), null); |
233 | 41487 | jjdelcerro | } |
234 | |||
235 | 41631 | jjdelcerro | protected String getUsername() { |
236 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(this.txtUsername.getText(), null); |
237 | 41487 | jjdelcerro | } |
238 | |||
239 | 41631 | jjdelcerro | protected String getPassword() { |
240 | 42104 | jjdelcerro | return (String) StringUtils.defaultIfBlank(this.txtPassword.getText(), null); |
241 | 41487 | jjdelcerro | } |
242 | |||
243 | private void onChangeConnector() { |
||
244 | 41488 | jjdelcerro | ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
245 | 41631 | jjdelcerro | if( item==null ) { |
246 | return;
|
||
247 | } |
||
248 | 41488 | jjdelcerro | JDBCServerExplorerParameters connector = item.getParams(); |
249 | 41631 | jjdelcerro | |
250 | 41492 | jjdelcerro | if ( connector == null ) { |
251 | 41487 | jjdelcerro | return;
|
252 | } |
||
253 | this.setServerExplorerParameters(connector);
|
||
254 | } |
||
255 | |||
256 | private void onChangeConnection() { |
||
257 | 41492 | jjdelcerro | Object item = this.cboConnections.getSelectedItem(); |
258 | if ( item instanceof ServerExplorerParametersComboItem ) { |
||
259 | JDBCServerExplorerParameters connection = ((ServerExplorerParametersComboItem) item).getParams(); |
||
260 | if ( connection == null ) { |
||
261 | return;
|
||
262 | } |
||
263 | this.setServerExplorerParameters(connection);
|
||
264 | 41487 | jjdelcerro | } |
265 | } |
||
266 | 41631 | jjdelcerro | |
267 | 41487 | jjdelcerro | |
268 | 41492 | jjdelcerro | private int getIndexOfConnector(JDBCServerExplorerParameters explorerParameters) { |
269 | String code = null; |
||
270 | try {
|
||
271 | code = explorerParameters.toString(); |
||
272 | ComboBoxModel model = this.cboConnectors.getModel(); |
||
273 | for ( int i = 0; i < model.getSize(); i++ ) { |
||
274 | ServerExplorerParametersComboItem x = (ServerExplorerParametersComboItem) model.getElementAt(i); |
||
275 | if ( x.getLabel().equalsIgnoreCase(explorerParameters.getExplorerName()) ) {
|
||
276 | return i;
|
||
277 | } |
||
278 | } |
||
279 | } catch (Exception ex) { |
||
280 | logger.warn("Can't get index of exporer parameter '" + code + "'.", ex); |
||
281 | } |
||
282 | return -1; |
||
283 | } |
||
284 | |||
285 | 41487 | jjdelcerro | private void fillConnectors() { |
286 | DataManager dataManager = DALLocator.getDataManager(); |
||
287 | |||
288 | 43020 | jjdelcerro | ServerExplorerParametersComboItem last = null;
|
289 | |||
290 | for (DataFactory factory : dataManager.getServerExplorerRegister() ) {
|
||
291 | DataServerExplorerParameters params = (DataServerExplorerParameters) factory.createParameters(); |
||
292 | 41492 | jjdelcerro | if ( params instanceof JDBCServerExplorerParameters ) { |
293 | 43020 | jjdelcerro | if( params.getClass() == JDBCServerExplorerParameters.class &&
|
294 | last == null ) {
|
||
295 | // Si es el proveedor generico de JDBC, lo guarda para
|
||
296 | // a?adirlo al final del combo.
|
||
297 | last = new ServerExplorerParametersComboItem(
|
||
298 | factory.getDescription(), |
||
299 | (JDBCServerExplorerParameters) params |
||
300 | ); |
||
301 | 41648 | jjdelcerro | } else {
|
302 | this.cboConnectors.addItem(
|
||
303 | 43020 | jjdelcerro | new ServerExplorerParametersComboItem(
|
304 | factory.getDescription(), |
||
305 | (JDBCServerExplorerParameters) params |
||
306 | ) |
||
307 | 41648 | jjdelcerro | ); |
308 | } |
||
309 | 41487 | jjdelcerro | } |
310 | } |
||
311 | 41648 | jjdelcerro | if( last!=null ) { |
312 | 43020 | jjdelcerro | this.cboConnectors.addItem(last);
|
313 | 41648 | jjdelcerro | } |
314 | 41487 | jjdelcerro | } |
315 | 41492 | jjdelcerro | |
316 | private void fillConnections() { |
||
317 | DataManager dataManager = DALLocator.getDataManager(); |
||
318 | DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
||
319 | |||
320 | DataServerExplorerParameters params; |
||
321 | |||
322 | 42104 | jjdelcerro | Iterator it = pool.iterator();
|
323 | 41492 | jjdelcerro | while ( it.hasNext() ) {
|
324 | 42104 | jjdelcerro | DataServerExplorerPoolEntry entry = (DataServerExplorerPoolEntry) it.next(); |
325 | 41492 | jjdelcerro | if ( entry.getExplorerParameters() instanceof JDBCServerExplorerParameters ) { |
326 | JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
||
327 | this.cboConnections.addItem(
|
||
328 | new ServerExplorerParametersComboItem(entry.getName(), dbParams)
|
||
329 | ); |
||
330 | } |
||
331 | } |
||
332 | this.cboConnections.setSelectedIndex(-1); |
||
333 | } |
||
334 | |||
335 | 42616 | jjdelcerro | @Override
|
336 | 41631 | jjdelcerro | public void delete() { |
337 | String name = this.getConnectionName(); |
||
338 | DataManager dataManager = DALLocator.getDataManager(); |
||
339 | DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
||
340 | |||
341 | pool.remove(name); |
||
342 | } |
||
343 | |||
344 | 42616 | jjdelcerro | @Override
|
345 | 41631 | jjdelcerro | public void clear() { |
346 | this.cboConnections.setSelectedIndex(-1); |
||
347 | this.cboConnectors.setSelectedIndex(-1); |
||
348 | this.txtServer.setText(""); |
||
349 | this.txtPort.setText(""); |
||
350 | this.txtDataBase.setText(""); |
||
351 | this.txtUsername.setText(""); |
||
352 | this.txtPassword.setText(""); |
||
353 | } |
||
354 | 41487 | jjdelcerro | } |