Revision 29544 branches/v2_0_0_prep/extensions/extOracleSpatial/src/org/gvsig/oraclespatial/gui/NewOracleSpatialTableWizard.java
NewOracleSpatialTableWizard.java | ||
---|---|---|
27 | 27 |
|
28 | 28 |
package org.gvsig.oraclespatial.gui; |
29 | 29 |
|
30 |
import java.awt.event.ActionEvent; |
|
31 |
import java.awt.event.ActionListener; |
|
32 | 30 |
import java.awt.event.ItemEvent; |
33 | 31 |
import java.awt.event.ItemListener; |
34 | 32 |
import java.beans.PropertyChangeEvent; |
35 | 33 |
import java.beans.PropertyChangeListener; |
36 |
import java.util.Iterator; |
|
37 |
import java.util.List; |
|
38 | 34 |
|
39 |
import javax.swing.ImageIcon; |
|
40 |
import javax.swing.JButton; |
|
41 |
import javax.swing.JComboBox; |
|
42 | 35 |
import javax.swing.JLabel; |
43 |
import javax.swing.JOptionPane; |
|
44 | 36 |
|
45 | 37 |
import jwizardcomponent.JWizardComponents; |
46 | 38 |
import jwizardcomponent.JWizardPanel; |
47 | 39 |
|
48 | 40 |
import org.gvsig.fmap.dal.DALLocator; |
49 | 41 |
import org.gvsig.fmap.dal.DataManager; |
50 |
import org.gvsig.fmap.dal.DataServerExplorer; |
|
51 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
|
52 | 42 |
import org.gvsig.fmap.dal.exception.DataException; |
53 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
54 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
|
55 | 43 |
import org.gvsig.fmap.dal.store.oraclespatial.OracleSpatialResource; |
56 | 44 |
import org.gvsig.fmap.dal.store.oraclespatial.OracleSpatialResourceParameters; |
57 | 45 |
import org.gvsig.fmap.dal.store.oraclespatial.OracleSpatialServerExplorerParameters; |
... | ... | |
59 | 47 |
import org.slf4j.LoggerFactory; |
60 | 48 |
|
61 | 49 |
import com.iver.andami.PluginServices; |
62 |
import com.iver.andami.messages.NotificationManager; |
|
63 | 50 |
import com.iver.utiles.swing.JPasswordDlg; |
64 |
import com.prodevelop.cit.gvsig.vectorialdb.wizard.VectorialDBConnectionParamsDialog;
|
|
51 |
import com.prodevelop.cit.gvsig.vectorialdb.wizard.MyExplorer;
|
|
65 | 52 |
|
66 | 53 |
/** |
67 | 54 |
* New oracle spatial table wizard |
... | ... | |
71 | 58 |
*/ |
72 | 59 |
|
73 | 60 |
public class NewOracleSpatialTableWizard extends JWizardPanel implements |
74 |
ItemListener, PropertyChangeListener, ActionListener {
|
|
61 |
ItemListener, PropertyChangeListener { |
|
75 | 62 |
|
76 | 63 |
private static final Logger logger = LoggerFactory |
77 | 64 |
.getLogger(NewOracleSpatialTableWizard.class); |
78 | 65 |
|
79 | 66 |
private JLabel chooseConnLabel = null; |
80 |
private JComboBox datasourceComboBox; |
|
81 |
private OracleSpatialServerExplorerParameters servParams = null; |
|
82 |
private JButton dbButton; |
|
67 |
private ConnectionChooserPanel connectionPanel = null; |
|
68 |
private MyExplorer selExplorer = null; |
|
83 | 69 |
|
84 |
private DataManager manager = DALLocator.getDataManager(); |
|
85 |
|
|
86 | 70 |
/** |
87 | 71 |
* Constructor |
88 | 72 |
* |
... | ... | |
93 | 77 |
wizardComponents.addPropertyChangeListener(this); |
94 | 78 |
initialize(); |
95 | 79 |
} |
96 |
|
|
80 |
|
|
97 | 81 |
/** |
98 | 82 |
* Events over panel |
99 | 83 |
*/ |
100 | 84 |
public void itemStateChanged(ItemEvent e) { |
101 | 85 |
|
102 | 86 |
// Combo |
103 |
if (e.getSource() == datasourceComboBox) {
|
|
87 |
if (e.getSource() == connectionPanel) {
|
|
104 | 88 |
|
105 |
String selectedExplorer = (String) datasourceComboBox |
|
106 |
.getSelectedItem(); |
|
89 |
MyExplorer selExp = connectionPanel.getSelectedServerExplorer(); |
|
107 | 90 |
|
108 |
if (selectedExplorer == null || selectedExplorer.compareTo("") == 0) {
|
|
91 |
if (selExp == null) {
|
|
109 | 92 |
getWizardComponents().getFinishButton().setEnabled(false); |
110 |
servParams = null;
|
|
93 |
selExplorer = null;
|
|
111 | 94 |
return; |
112 | 95 |
} |
113 |
|
|
114 |
OracleSpatialServerExplorerParameters exParam = null; |
|
115 |
try { |
|
116 |
exParam = (OracleSpatialServerExplorerParameters) manager |
|
117 |
.createServerExplorerParameters(selectedExplorer); |
|
118 |
} catch (InitializeException e1) { |
|
119 |
logger.error("Error initializing server explorer parameters",e1); |
|
120 |
} catch (ProviderNotRegisteredException e1) { |
|
121 |
logger.error("Error provider not registered",e1); |
|
122 |
} |
|
123 |
|
|
124 |
if (exParam == null) { |
|
125 |
getWizardComponents().getFinishButton().setEnabled(false); |
|
126 |
servParams = null; |
|
127 |
return; |
|
128 |
} |
|
129 |
org.gvsig.fmap.dal.resource.ResourceManager resMan = DALLocator |
|
130 |
.getResourceManager(); |
|
131 |
OracleSpatialResource res = (OracleSpatialResource) resMan |
|
132 |
.getResource(selectedExplorer); |
|
133 |
|
|
134 |
if (!res.isConnected()) { |
|
135 |
if (!tryToConnect(res)) { |
|
136 |
getWizardComponents().getFinishButton().setEnabled(false); |
|
137 |
servParams = null; |
|
138 |
datasourceComboBox.setSelectedIndex(0); |
|
139 |
return; |
|
140 |
} |
|
141 |
} |
|
142 |
|
|
143 |
servParams = exParam; |
|
144 | 96 |
getWizardComponents().getFinishButton().setEnabled(true); |
145 |
datasourceComboBox.repaint(); |
|
146 |
datasourceComboBox.validate(); |
|
97 |
selExplorer = selExp; |
|
147 | 98 |
} |
148 | 99 |
} |
149 |
|
|
100 |
|
|
150 | 101 |
/** |
151 | 102 |
* Property change event |
152 | 103 |
*/ |
... | ... | |
156 | 107 |
JWizardComponents.CURRENT_PANEL_PROPERTY) == 0) { |
157 | 108 |
|
158 | 109 |
if (evt.getNewValue() == this) { |
159 |
boolean valid_conn = !(servParams == null);
|
|
110 |
boolean valid_conn = !(selExplorer == null);
|
|
160 | 111 |
getWizardComponents().getFinishButton().setEnabled(valid_conn); |
161 | 112 |
} |
162 | 113 |
} |
163 | 114 |
} |
164 |
|
|
115 |
|
|
165 | 116 |
/** |
166 |
* Action performed event |
|
167 |
*/ |
|
168 |
public void actionPerformed(ActionEvent e) { |
|
169 |
// Button |
|
170 |
if (e.getSource() == dbButton) { |
|
171 |
OracleSpatialServerExplorerParameters sel = addNewConnection(); |
|
172 |
if (sel != null) { |
|
173 |
loadVectorialDBDatasourcesCombo(); |
|
174 |
getDatasourceComboBox().setSelectedItem(sel.getExplorerName()); |
|
175 |
} |
|
176 |
} |
|
177 |
} |
|
178 |
|
|
179 |
/** |
|
180 |
* Get server explorer parameters |
|
117 |
* Create a explorer parameters with name of selected explorer |
|
181 | 118 |
* |
182 | 119 |
* @return |
183 | 120 |
*/ |
184 |
public OracleSpatialServerExplorerParameters getServExplorerParams() { |
|
185 |
return servParams; |
|
121 |
public MyExplorer getSelectedServerExplorer() { |
|
122 |
|
|
123 |
Object obj = connectionPanel.getSelectedServerExplorer(); |
|
124 |
if (obj == null || obj instanceof String) { |
|
125 |
return null; |
|
126 |
} |
|
127 |
return (MyExplorer) obj; |
|
186 | 128 |
} |
187 | 129 |
|
188 | 130 |
/** |
... | ... | |
191 | 133 |
*/ |
192 | 134 |
private void initialize() { |
193 | 135 |
|
194 |
datasourceComboBox = getDatasourceComboBox(); |
|
195 |
loadVectorialDBDatasourcesCombo(); |
|
196 |
|
|
197 | 136 |
chooseConnLabel = new JLabel(); |
198 | 137 |
chooseConnLabel.setText(PluginServices.getText(this, |
199 | 138 |
"choose_connection")); |
200 | 139 |
chooseConnLabel.setBounds(new java.awt.Rectangle(14, 9, 300, 21)); |
201 | 140 |
|
202 |
dbButton = getJdbcButton();
|
|
203 |
|
|
141 |
connectionPanel = new ConnectionChooserPanel();
|
|
142 |
|
|
204 | 143 |
this.setLayout(null); |
205 | 144 |
this.setSize(new java.awt.Dimension(358, 263)); |
206 | 145 |
this.add(chooseConnLabel, null); |
207 |
this.add(datasourceComboBox, null); |
|
208 |
this.add(dbButton, null); |
|
146 |
this.add(connectionPanel, null); |
|
209 | 147 |
getWizardComponents().getFinishButton().setEnabled(false); |
210 | 148 |
} |
211 | 149 |
|
212 | 150 |
|
213 |
|
|
214 |
/** |
|
215 |
* get combobox |
|
216 |
* |
|
217 |
* @return |
|
218 |
*/ |
|
219 |
private JComboBox getDatasourceComboBox() { |
|
220 |
if (datasourceComboBox == null) { |
|
221 |
datasourceComboBox = new JComboBox(); |
|
222 |
datasourceComboBox |
|
223 |
.setBounds(new java.awt.Rectangle(14, 32, 300, 22)); |
|
224 |
datasourceComboBox.addItemListener(this); |
|
225 |
} |
|
226 |
return datasourceComboBox; |
|
227 |
} |
|
228 |
|
|
229 |
/** |
|
230 |
* Load Oracle explorers in combo |
|
231 |
*/ |
|
232 |
private void loadVectorialDBDatasourcesCombo() { |
|
233 |
|
|
234 |
getDatasourceComboBox().removeAllItems(); |
|
235 |
getDatasourceComboBox().addItem(""); |
|
236 |
|
|
237 |
List<String> explorers = manager.getExplorerProviders(); |
|
238 |
Iterator<String> iter = explorers.iterator(); |
|
239 |
DataServerExplorerParameters exParam = null; |
|
240 |
String name; |
|
241 |
while (iter.hasNext()) { |
|
242 |
name = iter.next(); |
|
243 |
try { |
|
244 |
exParam = manager.createServerExplorerParameters(name); |
|
245 |
} catch (DataException e) { |
|
246 |
NotificationManager.addError(e); |
|
247 |
} |
|
248 |
if (exParam instanceof OracleSpatialServerExplorerParameters) { |
|
249 |
getDatasourceComboBox().addItem(name); |
|
250 |
} |
|
251 |
} |
|
252 |
} |
|
253 |
|
|
254 |
|
|
255 |
|
|
256 |
/** |
|
257 |
* If the resource is not connected, try to connect it |
|
258 |
* @param res |
|
259 |
* @return |
|
260 |
*/ |
|
261 |
private boolean tryToConnect(OracleSpatialResource res) { |
|
262 |
OracleSpatialResourceParameters params = (OracleSpatialResourceParameters) res |
|
263 |
.getParameters(); |
|
264 |
JPasswordDlg dlg = new JPasswordDlg(); |
|
265 |
String strMessage = PluginServices.getText(this, "conectar_jdbc"); |
|
266 |
String strPassword = PluginServices.getText(this, "password"); |
|
267 |
dlg.setMessage(strMessage + " [" + params.getJDBCDriverClassName() |
|
268 |
+ ", " + params.getHost() + ", " + params.getPort() + ", " |
|
269 |
+ params.getDBName() + ", " + params.getUser() + "]. " |
|
270 |
+ strPassword + "?"); |
|
271 |
|
|
272 |
dlg.setVisible(true); |
|
273 |
dlg.repaint(); |
|
274 |
|
|
275 |
String clave = dlg.getPassword(); |
|
276 |
|
|
277 |
if (clave == null) { |
|
278 |
return false; |
|
279 |
} |
|
280 |
|
|
281 |
params.setPassword(clave); |
|
282 |
|
|
283 |
try { |
|
284 |
res.connect(); |
|
285 |
} catch (DataException e) { |
|
286 |
logger.error("Connect resource", e); |
|
287 |
return false; |
|
288 |
} |
|
289 |
|
|
290 |
return true; |
|
291 |
} |
|
292 |
|
|
293 |
private void showConnectionErrorMessage(String _msg) { |
|
294 |
String msg = (_msg.length() > 300) ? "" : (": " + _msg); |
|
295 |
String title = PluginServices.getText(this, "connection_error"); |
|
296 |
JOptionPane.showMessageDialog(this, title + msg, title, |
|
297 |
JOptionPane.ERROR_MESSAGE); |
|
298 |
} |
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
/** |
|
304 |
* add new connection creating a new explorer |
|
305 |
* @return |
|
306 |
*/ |
|
307 |
private OracleSpatialServerExplorerParameters addNewConnection() { |
|
308 |
OracleSpatialServerExplorerParameters servParams = null; |
|
309 |
|
|
310 |
VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog(); |
|
311 |
newco.showDialog(); |
|
312 |
|
|
313 |
if (newco.isOkPressed()) { |
|
314 |
String _drvname = newco.getConnectionDriverName(); |
|
315 |
String _host = newco.getConnectionServerUrl(); |
|
316 |
String _port = newco.getConnectionPort(); |
|
317 |
String _dbname = newco.getConnectionDBName(); |
|
318 |
String _user = newco.getConnectionUser(); |
|
319 |
String _pw = newco.getConnectionPassword(); |
|
320 |
String _conn_usr_name = newco.getConnectionName(); |
|
321 |
|
|
322 |
boolean hasToBeCon = newco.hasToBeConnected(); |
|
323 |
|
|
324 |
try { |
|
325 |
servParams = new OracleSpatialServerExplorerParameters(); |
|
326 |
servParams.setJDBCDriverClassName(_drvname); |
|
327 |
servParams.setUser(_user); |
|
328 |
servParams.setPassword(_pw); |
|
329 |
servParams.setHost(_host); |
|
330 |
servParams.setPort(new Integer(_port)); |
|
331 |
servParams.setDBName(_dbname); |
|
332 |
|
|
333 |
DataServerExplorer explorer = manager |
|
334 |
.createServerExplorer(servParams); |
|
335 |
if (explorer == null) { |
|
336 |
throw new Exception(); |
|
337 |
} |
|
338 |
} catch (Exception e) { |
|
339 |
showConnectionErrorMessage(e.getMessage()); |
|
340 |
return null; |
|
341 |
} |
|
342 |
|
|
343 |
return servParams; |
|
344 |
} else { |
|
345 |
return null; |
|
346 |
} |
|
347 |
} |
|
348 |
|
|
349 |
/** |
|
350 |
* This method initializes jdbcButton |
|
351 |
* |
|
352 |
* @return javax.swing.JButton |
|
353 |
*/ |
|
354 |
private JButton getJdbcButton() { |
|
355 |
if (dbButton == null) { |
|
356 |
dbButton = new JButton(); |
|
357 |
dbButton.addActionListener(this); |
|
358 |
dbButton.setToolTipText(PluginServices.getText(this, |
|
359 |
"add_connection")); |
|
360 |
dbButton.setBounds(new java.awt.Rectangle(320, 32, 26, 21)); |
|
361 |
|
|
362 |
String _file = createResourceUrl("images/jdbc.png").getFile(); |
|
363 |
dbButton.setIcon(new ImageIcon(_file)); |
|
364 |
} |
|
365 |
|
|
366 |
return dbButton; |
|
367 |
} |
|
368 |
|
|
369 |
/** |
|
370 |
* creating a resource url |
|
371 |
* @param path |
|
372 |
* @return |
|
373 |
*/ |
|
374 |
private java.net.URL createResourceUrl(String path) { |
|
375 |
return getClass().getClassLoader().getResource(path); |
|
376 |
} |
|
377 |
|
|
378 |
|
|
379 |
|
|
380 | 151 |
} |
Also available in: Unified diff