root / trunk / applications / appgvSIG / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / NewVectorDBConnectionPanel.java @ 38370
History | View | Annotate | Download (12.8 KB)
1 |
package com.prodevelop.cit.gvsig.vectorialdb.wizard; |
---|---|
2 |
|
3 |
|
4 |
import java.awt.Component; |
5 |
import java.awt.GridBagConstraints; |
6 |
import java.awt.Insets; |
7 |
import java.awt.event.ActionEvent; |
8 |
import java.awt.event.ActionListener; |
9 |
import java.awt.event.ItemEvent; |
10 |
import java.awt.event.ItemListener; |
11 |
import java.awt.event.KeyEvent; |
12 |
import java.awt.event.KeyListener; |
13 |
import java.beans.PropertyChangeEvent; |
14 |
import java.beans.PropertyChangeListener; |
15 |
import java.sql.ResultSet; |
16 |
import java.sql.SQLException; |
17 |
import java.util.Vector; |
18 |
|
19 |
import javax.swing.ImageIcon; |
20 |
import javax.swing.JButton; |
21 |
import javax.swing.JComboBox; |
22 |
import javax.swing.JLabel; |
23 |
import javax.swing.JOptionPane; |
24 |
import javax.swing.JTextField; |
25 |
|
26 |
|
27 |
import org.apache.log4j.Logger; |
28 |
|
29 |
|
30 |
import com.iver.andami.PluginServices; |
31 |
import com.iver.cit.gvsig.SingleVectorialDBConnectionExtension; |
32 |
import com.iver.cit.gvsig.fmap.drivers.ConnectionJDBC; |
33 |
import com.iver.cit.gvsig.fmap.drivers.DBException; |
34 |
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver; |
35 |
import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams; |
36 |
import com.iver.cit.gvsig.fmap.drivers.db.utils.SingleDBConnectionManager; |
37 |
import com.iver.utiles.swing.JPasswordDlg; |
38 |
import com.prodevelop.cit.gvsig.vectorialdb.wizard.DBConnectionParamsDialog; |
39 |
|
40 |
// import es.prodevelop.cit.gvsig.fmap.drivers.jdbc.oracle.OracleSpatialDriver;
|
41 |
|
42 |
|
43 |
import jwizardcomponent.JWizardComponents; |
44 |
import jwizardcomponent.JWizardPanel; |
45 |
|
46 |
|
47 |
public class NewVectorDBConnectionPanel extends JWizardPanel |
48 |
implements ItemListener, PropertyChangeListener, ActionListener, KeyListener { |
49 |
|
50 |
|
51 |
private static int NEW_TABLE_NAME_INDEX = 0; |
52 |
private static Logger logger = Logger.getLogger(NewVectorDBConnectionPanel.class.getName()); |
53 |
|
54 |
private String drvName = ""; |
55 |
private int idMaxLen = 10; |
56 |
|
57 |
|
58 |
public NewVectorDBConnectionPanel(
|
59 |
JWizardComponents wizardComponents, |
60 |
String drv_name,
|
61 |
int id_max_len) {
|
62 |
|
63 |
super(wizardComponents);
|
64 |
drvName = drv_name; |
65 |
idMaxLen = id_max_len; |
66 |
wizardComponents.addPropertyChangeListener(this);
|
67 |
initialize(); |
68 |
} |
69 |
|
70 |
private JLabel tableNameLabel = null; |
71 |
private JTextField tableNameField = null; |
72 |
|
73 |
private JLabel chooseConnLabel = null; |
74 |
private JComboBox datasourceComboBox; |
75 |
private ConnectionWithParams theConnWithParams = null; |
76 |
private JButton dbButton; |
77 |
private JLabel schemaLB; |
78 |
private JComboBox schemaCB; |
79 |
|
80 |
/**
|
81 |
* This method initializes this
|
82 |
*
|
83 |
*/
|
84 |
private void initialize() { |
85 |
|
86 |
datasourceComboBox = getDatasourceComboBox(); |
87 |
loadVectorialDBDatasourcesCombo(); |
88 |
|
89 |
chooseConnLabel = new JLabel(); |
90 |
chooseConnLabel.setText(PluginServices.getText(this,
|
91 |
"choose_connection") + ":"); |
92 |
chooseConnLabel.setBounds(new java.awt.Rectangle(15, 10, 300, 20)); |
93 |
dbButton = getJdbcButton(); |
94 |
|
95 |
this.setLayout(null); |
96 |
this.setSize(new java.awt.Dimension(358,263)); |
97 |
|
98 |
this.add(getTableNameLabel(), null); |
99 |
this.add(getTableNameField(), null); |
100 |
|
101 |
this.add(chooseConnLabel, null); |
102 |
this.add(datasourceComboBox, null); |
103 |
this.add(dbButton, null); |
104 |
|
105 |
this.add(getSchemaLB(), null); |
106 |
this.add(getSchemaCB(), null); |
107 |
|
108 |
this.updateFinishButton();
|
109 |
getTableNameField().setText(getNewTableName()); |
110 |
} |
111 |
|
112 |
private JLabel getSchemaLB() { |
113 |
if (schemaLB == null) { |
114 |
schemaLB = new JLabel(PluginServices.getText(this, "schema") + ":"); |
115 |
schemaLB.setBounds(new java.awt.Rectangle(15, 70, 300, 20)); |
116 |
} |
117 |
return schemaLB;
|
118 |
} |
119 |
|
120 |
private JComboBox getSchemaCB() { |
121 |
if (schemaCB == null) { |
122 |
schemaCB = new JComboBox(); |
123 |
schemaCB.setBounds(new java.awt.Rectangle(15, 95, 300, 20)); |
124 |
loadAvaliableSchemas(); |
125 |
} |
126 |
return schemaCB;
|
127 |
} |
128 |
|
129 |
private void loadAvaliableSchemas() { |
130 |
|
131 |
if ((theConnWithParams == null) || (!theConnWithParams.isConnected())) { |
132 |
return;
|
133 |
} |
134 |
|
135 |
try {
|
136 |
schemaCB.removeAllItems(); |
137 |
ResultSet rs = ((ConnectionJDBC) theConnWithParams.getConnection())
|
138 |
.getConnection().getMetaData().getSchemas(); |
139 |
while (rs.next()) {
|
140 |
schemaCB.addItem(rs.getString("TABLE_SCHEM"));
|
141 |
} |
142 |
} catch (SQLException e) { |
143 |
e.printStackTrace(); |
144 |
} |
145 |
} |
146 |
|
147 |
private String getNewTableName() { |
148 |
String resp = "GVSIG_" + NEW_TABLE_NAME_INDEX; |
149 |
NEW_TABLE_NAME_INDEX++; |
150 |
return resp;
|
151 |
} |
152 |
|
153 |
private JTextField getTableNameField() { |
154 |
|
155 |
if (tableNameField == null) { |
156 |
tableNameField = new JTextField(); |
157 |
tableNameField.setBounds(new java.awt.Rectangle(15, 155, 300, 20)); |
158 |
tableNameField.addKeyListener(this);
|
159 |
} |
160 |
return tableNameField;
|
161 |
|
162 |
} |
163 |
|
164 |
private JLabel getTableNameLabel() { |
165 |
|
166 |
if (tableNameLabel == null) { |
167 |
tableNameLabel = new JLabel(); |
168 |
tableNameLabel.setText(PluginServices.getText(this, "Tabla") + ":"); |
169 |
tableNameLabel |
170 |
.setBounds(new java.awt.Rectangle(15, 130, 300, 20)); |
171 |
} |
172 |
return tableNameLabel;
|
173 |
} |
174 |
|
175 |
public ConnectionWithParams getConnectionWithParams() {
|
176 |
return theConnWithParams;
|
177 |
} |
178 |
|
179 |
public String getTableName() { |
180 |
return getTableNameField().getText().toUpperCase();
|
181 |
} |
182 |
|
183 |
private JComboBox getDatasourceComboBox() { |
184 |
if (datasourceComboBox == null) { |
185 |
datasourceComboBox = new JComboBox(); |
186 |
datasourceComboBox |
187 |
.setBounds(new java.awt.Rectangle(15, 35, 300, 20)); |
188 |
datasourceComboBox.addItemListener(this);
|
189 |
} |
190 |
|
191 |
return datasourceComboBox;
|
192 |
} |
193 |
|
194 |
private void loadVectorialDBDatasourcesCombo() { |
195 |
|
196 |
getDatasourceComboBox().removeAllItems(); |
197 |
getDatasourceComboBox().addItem(new ConnectionWithParams());
|
198 |
ConnectionWithParams[] conn =
|
199 |
SingleDBConnectionManager.instance().getAllConnections(); |
200 |
|
201 |
if (conn == null) { |
202 |
return;
|
203 |
} |
204 |
|
205 |
for (int i = 0; i < conn.length; i++) { |
206 |
if (conn[i].getDrvName().compareToIgnoreCase(drvName) == 0) { |
207 |
getDatasourceComboBox().addItem(conn[i]); |
208 |
} |
209 |
} |
210 |
|
211 |
} |
212 |
|
213 |
public void itemStateChanged(ItemEvent e) { |
214 |
|
215 |
Object src = e.getSource();
|
216 |
if (src == datasourceComboBox) {
|
217 |
|
218 |
Object sel_obj = datasourceComboBox.getSelectedItem();
|
219 |
|
220 |
if (sel_obj == null) { |
221 |
getWizardComponents().getFinishButton().setEnabled(false);
|
222 |
theConnWithParams = null;
|
223 |
this.updateFinishButton();
|
224 |
return;
|
225 |
} |
226 |
|
227 |
if (!(sel_obj instanceof ConnectionWithParams)) { |
228 |
getWizardComponents().getFinishButton().setEnabled(false);
|
229 |
theConnWithParams = null;
|
230 |
this.updateFinishButton();
|
231 |
return;
|
232 |
} |
233 |
|
234 |
ConnectionWithParams cwp = (ConnectionWithParams) sel_obj; |
235 |
|
236 |
if (cwp.isNull()) {
|
237 |
getWizardComponents().getFinishButton().setEnabled(false);
|
238 |
theConnWithParams = null;
|
239 |
this.updateFinishButton();
|
240 |
return;
|
241 |
} |
242 |
|
243 |
if (!cwp.isConnected()) {
|
244 |
if (!tryToConnect(cwp)) {
|
245 |
getWizardComponents().getFinishButton().setEnabled(false);
|
246 |
theConnWithParams = null;
|
247 |
datasourceComboBox.setSelectedIndex(0);
|
248 |
this.updateFinishButton();
|
249 |
return;
|
250 |
} |
251 |
} |
252 |
|
253 |
theConnWithParams = cwp; |
254 |
loadAvaliableSchemas(); |
255 |
this.updateFinishButton();
|
256 |
datasourceComboBox.repaint(); |
257 |
} |
258 |
} |
259 |
|
260 |
private boolean tryToConnect(ConnectionWithParams _cwp) { |
261 |
JPasswordDlg dlg = new JPasswordDlg();
|
262 |
dlg.setLocationRelativeTo((Component)PluginServices.getMainFrame());
|
263 |
String strMessage = PluginServices.getText(this, "conectar_jdbc"); |
264 |
String strPassword = PluginServices.getText(this, "password"); |
265 |
dlg.setMessage(strMessage + " [" + _cwp.getDrvName() + ", " + |
266 |
_cwp.getHost() + ", " + _cwp.getPort() + ", " + _cwp.getDb() + |
267 |
", " + _cwp.getUser() + "]. " + strPassword + "?"); |
268 |
|
269 |
dlg.setVisible(true);
|
270 |
|
271 |
String clave = dlg.getPassword();
|
272 |
|
273 |
if (clave == null) { |
274 |
return false; |
275 |
} |
276 |
|
277 |
try {
|
278 |
_cwp.connect(clave); |
279 |
} |
280 |
catch (DBException e) {
|
281 |
showConnectionErrorMessage(e.getMessage()); |
282 |
|
283 |
return false; |
284 |
} |
285 |
|
286 |
return true; |
287 |
} |
288 |
|
289 |
private void showConnectionErrorMessage(String _msg) { |
290 |
String msg = (_msg.length() > 300) ? "" : (": " + _msg); |
291 |
String title = PluginServices.getText(this, "connection_error"); |
292 |
JOptionPane.showMessageDialog(this, title + msg, title, |
293 |
JOptionPane.ERROR_MESSAGE);
|
294 |
} |
295 |
|
296 |
public void propertyChange(PropertyChangeEvent evt) { |
297 |
|
298 |
if (evt.getPropertyName().compareToIgnoreCase(JWizardComponents.CURRENT_PANEL_PROPERTY) == 0) { |
299 |
|
300 |
if (evt.getNewValue() == this) { |
301 |
this.updateFinishButton();
|
302 |
} |
303 |
|
304 |
} |
305 |
|
306 |
} |
307 |
|
308 |
|
309 |
private ConnectionWithParams addNewConnection() {
|
310 |
ConnectionWithParams resp = null;
|
311 |
|
312 |
DBConnectionParamsDialog newco = new DBConnectionParamsDialog(
|
313 |
new Class[] { IVectorialDatabaseDriver.class }); |
314 |
newco.showDialog(); |
315 |
|
316 |
if (newco.isOkPressed()) {
|
317 |
String _drvname = newco.getConnectionDriverName();
|
318 |
String _host = newco.getConnectionServerUrl();
|
319 |
String _port = newco.getConnectionPort();
|
320 |
String _dbname = newco.getConnectionDBName();
|
321 |
String _user = newco.getConnectionUser();
|
322 |
String _pw = newco.getConnectionPassword();
|
323 |
String _sche = newco.getConnectionSchema();
|
324 |
String _conn_usr_name = newco.getConnectionName();
|
325 |
|
326 |
boolean hasToBeCon = newco.hasToBeConnected();
|
327 |
|
328 |
try {
|
329 |
resp = SingleDBConnectionManager.instance() |
330 |
.getConnection(_drvname, |
331 |
_user, _pw, _conn_usr_name, _host, _port, _dbname, _sche, |
332 |
hasToBeCon); |
333 |
} |
334 |
catch (DBException e) {
|
335 |
showConnectionErrorMessage(e.getMessage()); |
336 |
|
337 |
return null; |
338 |
} |
339 |
SingleVectorialDBConnectionExtension.saveAllToPersistence(); |
340 |
return resp;
|
341 |
} |
342 |
else {
|
343 |
return null; |
344 |
} |
345 |
} |
346 |
|
347 |
/**
|
348 |
* This method initializes jdbcButton
|
349 |
*
|
350 |
* @return javax.swing.JButton
|
351 |
*/
|
352 |
private JButton getJdbcButton() { |
353 |
if (dbButton == null) { |
354 |
dbButton = new JButton(); |
355 |
dbButton.addActionListener(this);
|
356 |
dbButton.setToolTipText(PluginServices.getText(this,
|
357 |
"add_connection"));
|
358 |
dbButton.setBounds(new java.awt.Rectangle(320, 32, 26, 21)); |
359 |
String _file = createResourceUrl("images/jdbc.png").getFile(); |
360 |
dbButton.setIcon(new ImageIcon(_file)); |
361 |
} |
362 |
|
363 |
return dbButton;
|
364 |
} |
365 |
|
366 |
private java.net.URL createResourceUrl(String path) { |
367 |
return getClass().getClassLoader().getResource(path);
|
368 |
} |
369 |
|
370 |
public void actionPerformed(ActionEvent e) { |
371 |
|
372 |
Object src = e.getSource();
|
373 |
|
374 |
if (src == dbButton) {
|
375 |
ConnectionWithParams sel = addNewConnection(); |
376 |
|
377 |
if (sel != null) { |
378 |
loadVectorialDBDatasourcesCombo(); |
379 |
getDatasourceComboBox().setSelectedItem(sel); |
380 |
} |
381 |
} |
382 |
|
383 |
} |
384 |
|
385 |
public void keyPressed(KeyEvent e) { |
386 |
// TODO Auto-generated method stub
|
387 |
|
388 |
} |
389 |
|
390 |
public void keyReleased(KeyEvent e) { |
391 |
updateFinishButton(); |
392 |
} |
393 |
|
394 |
public void keyTyped(KeyEvent e) { |
395 |
} |
396 |
|
397 |
private void updateFinishButton() { |
398 |
if ((theConnWithParams != null) && (theConnWithParams.isConnected()) |
399 |
&& (schemaCB.getSelectedItem() != null)
|
400 |
&& (schemaCB.getSelectedItem().toString().length() > 0)) {
|
401 |
String aux_table_name = getTableName();
|
402 |
boolean active = (theConnWithParams != null) && validTableName(aux_table_name); |
403 |
getWizardComponents().getFinishButton().setEnabled(active); |
404 |
} |
405 |
} |
406 |
|
407 |
private boolean validTableName(String str) { |
408 |
|
409 |
int len = str.length();
|
410 |
|
411 |
if ((len == 0) || (len > (idMaxLen - 3))) { |
412 |
return false; |
413 |
} |
414 |
|
415 |
if (!validInitialChar(str.substring(0, 1))) { |
416 |
return false; |
417 |
} |
418 |
|
419 |
String onechar = ""; |
420 |
for (int i=1; i<len; i++) { |
421 |
onechar = str.substring(i, i+1);
|
422 |
if (!validChar(onechar)) return false; |
423 |
} |
424 |
return true; |
425 |
} |
426 |
|
427 |
private static String[] VALID_CHAR = { |
428 |
"_", "A", "B", "C", "D", "E", "F", "G", "H", "I", |
429 |
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", |
430 |
"T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", |
431 |
"4", "5", "6", "7", "8", "9", "0" |
432 |
}; |
433 |
|
434 |
private static String[] VALID_INITIAL_CHAR = { |
435 |
"_", "A", "B", "C", "D", "E", "F", "G", "H", "I", |
436 |
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", |
437 |
"T", "U", "V", "W", "X", "Y", "Z" |
438 |
}; |
439 |
|
440 |
|
441 |
private boolean validChar(String onechar) { |
442 |
int len = VALID_CHAR.length;
|
443 |
for (int i=0; i<len; i++) { |
444 |
if (onechar.compareTo(VALID_CHAR[i]) == 0) return true; |
445 |
} |
446 |
return false; |
447 |
} |
448 |
|
449 |
private boolean validInitialChar(String onechar) { |
450 |
int len = VALID_INITIAL_CHAR.length;
|
451 |
for (int i=0; i<len; i++) { |
452 |
if (onechar.compareTo(VALID_INITIAL_CHAR[i]) == 0) return true; |
453 |
} |
454 |
return false; |
455 |
} |
456 |
|
457 |
|
458 |
} |
459 |
|
460 |
// [eiel-gestion-conexiones]
|