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 | 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 | 41631 | jjdelcerro | package org.gvsig.fmap.mapcontrol.dal.jdbc; |
7 | 41487 | jjdelcerro | |
8 | import java.awt.event.ItemEvent; |
||
9 | import java.awt.event.ItemListener; |
||
10 | import java.util.Iterator; |
||
11 | import java.util.List; |
||
12 | 41492 | jjdelcerro | import javax.swing.ComboBoxModel; |
13 | 41487 | jjdelcerro | 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 | 41492 | jjdelcerro | import org.gvsig.fmap.dal.DataServerExplorerPool; |
19 | import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
||
20 | 41487 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
21 | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
||
22 | 41492 | jjdelcerro | import org.slf4j.Logger; |
23 | import org.slf4j.LoggerFactory; |
||
24 | 41487 | jjdelcerro | |
25 | 41631 | jjdelcerro | // org.gvsig.fmap.mapcontrol.dal.jdbc.JDBCConnectionPanel
|
26 | public class JDBCConnectionPanel extends JDBCConnectionPanelLayout { |
||
27 | 41487 | jjdelcerro | |
28 | 41631 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(JDBCConnectionPanel.class); |
29 | 41487 | jjdelcerro | |
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 | 41492 | jjdelcerro | |
52 | public String getLabel() { |
||
53 | return this.label; |
||
54 | } |
||
55 | 41487 | jjdelcerro | } |
56 | |||
57 | 41631 | jjdelcerro | public JDBCConnectionPanel() {
|
58 | 41488 | jjdelcerro | initComponents(); |
59 | 41487 | jjdelcerro | } |
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 | 41631 | jjdelcerro | try {
|
74 | fillConnections(); |
||
75 | fillConnectors(); |
||
76 | } catch(Throwable th) { |
||
77 | // Ignore it to allow use in GUI builders
|
||
78 | } |
||
79 | 41487 | jjdelcerro | } |
80 | |||
81 | public void setServerExplorerParameters(JDBCServerExplorerParameters parameters) { |
||
82 | 41492 | jjdelcerro | |
83 | int indexConnector = this.getIndexOfConnector(parameters); |
||
84 | if ( indexConnector >= 0 && this.cboConnectors.getSelectedIndex()!=indexConnector ) { |
||
85 | this.cboConnectors.setSelectedIndex(indexConnector);
|
||
86 | } |
||
87 | |||
88 | 41487 | jjdelcerro | this.txtServer.setText(parameters.getHost());
|
89 | 41488 | jjdelcerro | Integer port = parameters.getPort();
|
90 | 41492 | jjdelcerro | if ( port == null ) { |
91 | 41488 | jjdelcerro | this.txtPort.setText(""); |
92 | } else {
|
||
93 | this.txtPort.setText(String.valueOf(port)); |
||
94 | } |
||
95 | 41487 | jjdelcerro | 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 | 41492 | jjdelcerro | if ( this.getConnectionName() != null ) { |
110 | DataManager dataManager = DALLocator.getDataManager(); |
||
111 | DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
||
112 | pool.add(this.getConnectionName(), params);
|
||
113 | 41488 | jjdelcerro | } |
114 | 41487 | jjdelcerro | return params;
|
115 | } |
||
116 | |||
117 | 41631 | jjdelcerro | protected void setConnectionName(String connectionName) { |
118 | 41487 | jjdelcerro | 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 | 41631 | jjdelcerro | protected JDBCServerExplorerParameters getConnector() {
|
129 | 41488 | jjdelcerro | ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
130 | JDBCServerExplorerParameters value = item.getParams(); |
||
131 | 41487 | jjdelcerro | return value;
|
132 | } |
||
133 | |||
134 | 41631 | jjdelcerro | protected String getConnectorName() { |
135 | 41487 | jjdelcerro | JDBCServerExplorerParameters value = this.getConnector();
|
136 | 41492 | jjdelcerro | if ( value == null ) { |
137 | 41487 | jjdelcerro | return null; |
138 | } |
||
139 | return StringUtils.defaultIfBlank(value.getExplorerName(), null); |
||
140 | } |
||
141 | |||
142 | 41631 | jjdelcerro | protected String getServer() { |
143 | 41487 | jjdelcerro | return StringUtils.defaultIfBlank(this.txtServer.getText(), null); |
144 | } |
||
145 | |||
146 | 41631 | jjdelcerro | protected int getPort() { |
147 | 41487 | jjdelcerro | 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 | 41631 | jjdelcerro | protected String getDataBaseName() { |
158 | 41487 | jjdelcerro | return StringUtils.defaultIfBlank(this.txtDataBase.getText(), null); |
159 | } |
||
160 | |||
161 | 41631 | jjdelcerro | protected String getUsername() { |
162 | 41487 | jjdelcerro | return StringUtils.defaultIfBlank(this.txtUsername.getText(), null); |
163 | } |
||
164 | |||
165 | 41631 | jjdelcerro | protected String getPassword() { |
166 | 41487 | jjdelcerro | return StringUtils.defaultIfBlank(this.txtPassword.getText(), null); |
167 | } |
||
168 | |||
169 | private void onChangeConnector() { |
||
170 | 41488 | jjdelcerro | ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
171 | 41631 | jjdelcerro | if( item==null ) { |
172 | return;
|
||
173 | } |
||
174 | 41488 | jjdelcerro | JDBCServerExplorerParameters connector = item.getParams(); |
175 | 41631 | jjdelcerro | |
176 | 41492 | jjdelcerro | if ( connector == null ) { |
177 | 41487 | jjdelcerro | return;
|
178 | } |
||
179 | this.setServerExplorerParameters(connector);
|
||
180 | } |
||
181 | |||
182 | private void onChangeConnection() { |
||
183 | 41492 | jjdelcerro | 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 | 41487 | jjdelcerro | } |
191 | } |
||
192 | 41631 | jjdelcerro | |
193 | 41487 | jjdelcerro | |
194 | 41492 | jjdelcerro | 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 | 41487 | jjdelcerro | private void fillConnectors() { |
212 | DataManager dataManager = DALLocator.getDataManager(); |
||
213 | List<String> explorers = dataManager.getExplorerProviders(); |
||
214 | |||
215 | DataServerExplorerParameters params; |
||
216 | |||
217 | 41648 | jjdelcerro | JDBCServerExplorerParameters last = null;
|
218 | 41487 | jjdelcerro | Iterator<String> it = explorers.iterator(); |
219 | 41492 | jjdelcerro | while ( it.hasNext() ) {
|
220 | 41487 | jjdelcerro | String explorerName = it.next();
|
221 | try {
|
||
222 | params = dataManager.createServerExplorerParameters(explorerName); |
||
223 | } catch (DataException e) {
|
||
224 | continue;
|
||
225 | } |
||
226 | 41492 | jjdelcerro | if ( params instanceof JDBCServerExplorerParameters ) { |
227 | 41487 | jjdelcerro | JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) params; |
228 | 41648 | jjdelcerro | if( dbParams.getClass().getName().equals(JDBCServerExplorerParameters.class.getName()) ) {
|
229 | last = dbParams; |
||
230 | } else {
|
||
231 | this.cboConnectors.addItem(
|
||
232 | new ServerExplorerParametersComboItem(dbParams)
|
||
233 | ); |
||
234 | } |
||
235 | 41487 | jjdelcerro | } |
236 | } |
||
237 | 41648 | jjdelcerro | if( last!=null ) { |
238 | this.cboConnectors.addItem(
|
||
239 | new ServerExplorerParametersComboItem(last)
|
||
240 | ); |
||
241 | } |
||
242 | 41487 | jjdelcerro | } |
243 | 41492 | jjdelcerro | |
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 | 41631 | jjdelcerro | 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 | 41487 | jjdelcerro | } |