root / tags / v1_1_Build_1009 / libraries / libIverUtiles / src / com / iver / utiles / connections / JDBCManager.java @ 12649
History | View | Annotate | Download (27.3 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.utiles.connections; |
42 |
|
43 |
import java.awt.Component; |
44 |
import java.awt.FlowLayout; |
45 |
import java.awt.event.MouseEvent; |
46 |
import java.sql.SQLException; |
47 |
import java.util.MissingResourceException; |
48 |
import java.util.PropertyResourceBundle; |
49 |
import java.util.ResourceBundle; |
50 |
|
51 |
import javax.swing.JButton; |
52 |
import javax.swing.JComboBox; |
53 |
import javax.swing.JDialog; |
54 |
import javax.swing.JLabel; |
55 |
import javax.swing.JMenuItem; |
56 |
import javax.swing.JOptionPane; |
57 |
import javax.swing.JPanel; |
58 |
import javax.swing.JPasswordField; |
59 |
import javax.swing.JPopupMenu; |
60 |
import javax.swing.JRadioButton; |
61 |
import javax.swing.JScrollPane; |
62 |
import javax.swing.JTabbedPane; |
63 |
import javax.swing.JTextField; |
64 |
import javax.swing.JTree; |
65 |
import javax.swing.tree.DefaultMutableTreeNode; |
66 |
import javax.swing.tree.DefaultTreeModel; |
67 |
import javax.swing.tree.TreePath; |
68 |
|
69 |
//import com.iver.andami.messages.Messages;
|
70 |
|
71 |
|
72 |
public class JDBCManager extends JPanel{ |
73 |
|
74 |
private JTabbedPane tabbedPanel = null; |
75 |
private JPanel pparameters = null; |
76 |
private JPanel popciones = null; |
77 |
private JComboBox cbDrivers = null; |
78 |
private JTextField txtName = null; |
79 |
private JPanel pServer = null; |
80 |
private JLabel lblDrivers = null; |
81 |
private JLabel lblName = null; |
82 |
private JPanel phost = null; |
83 |
private JTextField txtHost = null; |
84 |
private JLabel lblHost = null; |
85 |
private JLabel lblPort = null; |
86 |
private JTextField txtPort = null; |
87 |
private JPanel pUser = null; |
88 |
private JLabel lblUser = null; |
89 |
private JTextField txtUser = null; |
90 |
private JLabel lblPassword = null; |
91 |
private JPasswordField txtPassword = null; |
92 |
private JPanel pDatabase = null; |
93 |
private JLabel lblDB = null; |
94 |
private JTextField txtDB = null; |
95 |
private JButton bTestConnection = null; |
96 |
private JPanel pConnectionsTree = null; |
97 |
private JButton bAcept = null; |
98 |
private JButton bCancel = null; |
99 |
private JTree treeConnection = null; |
100 |
private ConnectionDB connectionDB=null; |
101 |
private ConnectionTrans connectionTrans=null; |
102 |
private DefaultMutableTreeNode root=null; |
103 |
private DefaultTreeModel treeModel=null; |
104 |
private JScrollPane scrollTree = null; |
105 |
private JRadioButton rbPassword = null; |
106 |
private JPopupMenu popupMenu = null; |
107 |
private JMenuItem menuConnect = null; |
108 |
private JMenuItem menuDisConnect = null; |
109 |
private JMenuItem menuDel = null; |
110 |
private JMenuItem menuRefresh = null; |
111 |
private ConnectionTrans ctTree;
|
112 |
private DefaultMutableTreeNode nodeSelected=null; |
113 |
private static PropertyResourceBundle resourceBundle; |
114 |
/**
|
115 |
* This is the default constructor
|
116 |
*/
|
117 |
public JDBCManager(ConnectionTrans[] ct) { |
118 |
super();
|
119 |
|
120 |
/* try {
|
121 |
Class.forName("com.mysql.jdbc.Driver", true,
|
122 |
JDBCManager.class.getClassLoader());
|
123 |
} catch (ClassNotFoundException e) {
|
124 |
// TODO Auto-generated catch block
|
125 |
e.printStackTrace();
|
126 |
} */
|
127 |
|
128 |
|
129 |
|
130 |
connectionDB=ConnectionDB.getInstance(); |
131 |
connectionDB.setConnTrans(ct); |
132 |
initialize(); |
133 |
} |
134 |
|
135 |
/**
|
136 |
* This method initializes scrollTree
|
137 |
*
|
138 |
* @return javax.swing.JScrollPane
|
139 |
*/
|
140 |
private JScrollPane getScrollTree() { |
141 |
if (scrollTree == null) { |
142 |
scrollTree = new JScrollPane(); |
143 |
scrollTree.setPreferredSize(new java.awt.Dimension(175,255)); |
144 |
scrollTree.setAutoscrolls(true);
|
145 |
scrollTree.setViewportView(getTreeConnection()); |
146 |
} |
147 |
return scrollTree;
|
148 |
} |
149 |
/**
|
150 |
* This method initializes rbPassword
|
151 |
*
|
152 |
* @return javax.swing.JRadioButton
|
153 |
*/
|
154 |
private JRadioButton getRbPassword() { |
155 |
if (rbPassword == null) { |
156 |
rbPassword = new JRadioButton(); |
157 |
rbPassword.setText(getTranslation("guardar_clave"));
|
158 |
rbPassword.setSelected(true);
|
159 |
rbPassword.setPreferredSize(new java.awt.Dimension(160,24)); |
160 |
} |
161 |
return rbPassword;
|
162 |
} |
163 |
/**
|
164 |
* This method initializes popupMenu
|
165 |
*
|
166 |
* @return javax.swing.JPopupMenu
|
167 |
*/
|
168 |
private JPopupMenu getPopupMenu() { |
169 |
if (popupMenu == null) { |
170 |
popupMenu = new JPopupMenu(); |
171 |
popupMenu.add(getMenuConectar()); |
172 |
popupMenu.add(getMenuDisConnect()); |
173 |
popupMenu.add(getMenuDel()); |
174 |
popupMenu.add(getMenuRefresh()); |
175 |
} |
176 |
return popupMenu;
|
177 |
} |
178 |
|
179 |
/**
|
180 |
* This method initializes menuConectar
|
181 |
*
|
182 |
* @return javax.swing.JMenuItem
|
183 |
*/
|
184 |
private JMenuItem getMenuConectar() { |
185 |
if (menuConnect == null) { |
186 |
menuConnect = new JMenuItem(getTranslation("conectar")); |
187 |
menuConnect.setEnabled(true);
|
188 |
menuConnect.addActionListener(new java.awt.event.ActionListener() {
|
189 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
190 |
|
191 |
try {
|
192 |
connectionTrans=PaneltoConnectTrans(); |
193 |
if (connectionDB.testDB(connectionTrans)){
|
194 |
connectionTrans.setConnected(true);
|
195 |
registerConnection(connectionTrans); |
196 |
} |
197 |
|
198 |
} catch (ConnectionException e1) {
|
199 |
JOptionPane.showMessageDialog((Component)JDBCManager.this,e1); |
200 |
} |
201 |
treeModel.reload(); |
202 |
} |
203 |
}); |
204 |
} |
205 |
return menuConnect;
|
206 |
} |
207 |
|
208 |
/**
|
209 |
* This method initializes menuDisConnect
|
210 |
*
|
211 |
* @return javax.swing.JMenuItem
|
212 |
*/
|
213 |
private JMenuItem getMenuDisConnect() { |
214 |
if (menuDisConnect == null) { |
215 |
menuDisConnect = new JMenuItem(); |
216 |
menuDisConnect.setText(getTranslation("desconectar"));
|
217 |
menuDisConnect.addActionListener(new java.awt.event.ActionListener() {
|
218 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
219 |
ctTree.setConnected(false);
|
220 |
for (int i=0;i<root.getChildCount();i++){ |
221 |
DefaultMutableTreeNode node=(DefaultMutableTreeNode)root.getChildAt(i); |
222 |
if (node.getUserObject().equals(ctTree)){
|
223 |
node.removeAllChildren(); |
224 |
} |
225 |
} |
226 |
treeModel.reload(); |
227 |
} |
228 |
}); |
229 |
} |
230 |
return menuDisConnect;
|
231 |
} |
232 |
|
233 |
/**
|
234 |
* This method initializes menuDel
|
235 |
*
|
236 |
* @return javax.swing.JMenuItem
|
237 |
*/
|
238 |
private JMenuItem getMenuDel() { |
239 |
if (menuDel == null) { |
240 |
menuDel = new JMenuItem(); |
241 |
menuDel.setText("eliminar");
|
242 |
menuDel.addActionListener(new java.awt.event.ActionListener() {
|
243 |
|
244 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
245 |
for (int i=0;i<root.getChildCount();i++){ |
246 |
DefaultMutableTreeNode node=(DefaultMutableTreeNode)root.getChildAt(i); |
247 |
if (node.getUserObject().equals(ctTree)){
|
248 |
treeModel.removeNodeFromParent(node); |
249 |
treeConnection.repaint(); |
250 |
} |
251 |
} |
252 |
connectionDB.delPersistence(ctTree.getHost()+"_"+ctTree.getName());
|
253 |
} |
254 |
}); |
255 |
} |
256 |
return menuDel;
|
257 |
} |
258 |
|
259 |
/**
|
260 |
* This method initializes menuRefresh
|
261 |
*
|
262 |
* @return javax.swing.JMenuItem
|
263 |
*/
|
264 |
private JMenuItem getMenuRefresh() { |
265 |
if (menuRefresh == null) { |
266 |
menuRefresh = new JMenuItem(); |
267 |
menuRefresh.setText(getTranslation("refrescar"));
|
268 |
menuRefresh.addActionListener(new java.awt.event.ActionListener() {
|
269 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
270 |
init(ctTree); |
271 |
} |
272 |
}); |
273 |
} |
274 |
return menuRefresh;
|
275 |
} |
276 |
|
277 |
public static void main(String[] args) { |
278 |
JDialog dialog=new JDialog(); |
279 |
|
280 |
ConnectionTransInit[] connDrivers=new ConnectionTransInit[2]; |
281 |
connDrivers[0]=new ConnectionTransInit(); |
282 |
connDrivers[0].setHost("localhost"); |
283 |
connDrivers[0].setName("MYSQL DataBase"); |
284 |
connDrivers[0].setConnBegining("jdbc:mysql:"); |
285 |
connDrivers[0].setPort("3306"); |
286 |
connDrivers[0].setDb("test"); |
287 |
connDrivers[0].setUser("root"); |
288 |
connDrivers[0].setPassword("aquilina"); |
289 |
try {
|
290 |
Class.forName("com.mysql.jdbc.Driver"); |
291 |
Class.forName("org.postgresql.Driver"); |
292 |
} catch (ClassNotFoundException e) { |
293 |
throw new RuntimeException(e); |
294 |
} |
295 |
|
296 |
connDrivers[1]=new ConnectionTransInit(); |
297 |
connDrivers[1].setHost("localhost"); |
298 |
connDrivers[1].setName("POSTGRES DataBase"); |
299 |
connDrivers[1].setConnBegining("jdbc:postgresql:"); |
300 |
connDrivers[1].setPort("5432"); |
301 |
connDrivers[1].setDb("latin1"); |
302 |
connDrivers[1].setUser("postgres"); |
303 |
connDrivers[1].setPassword("aquilina"); |
304 |
|
305 |
|
306 |
JDBCManager sm=new JDBCManager(connDrivers);
|
307 |
dialog.getContentPane().add(sm); |
308 |
dialog.setSize(600,350); |
309 |
dialog.setVisible(true);
|
310 |
} |
311 |
/**
|
312 |
* This method initializes this
|
313 |
*
|
314 |
* @return void
|
315 |
* @throws ConectadoException
|
316 |
* @throws SQLException
|
317 |
*/
|
318 |
private void initialize(){ |
319 |
this.setSize(604, 305); |
320 |
setResourceBundle(); |
321 |
this.add(getPConnectionsTree(), null); |
322 |
this.add(getParameters(), null); |
323 |
this.add(getBAcept(), null); |
324 |
this.add(getBCancel(), null); |
325 |
try {
|
326 |
init(); |
327 |
} catch (Exception e) { |
328 |
// TODO Auto-generated catch block
|
329 |
e.printStackTrace(); |
330 |
} |
331 |
} |
332 |
|
333 |
/**
|
334 |
* This method initializes parameters
|
335 |
*
|
336 |
* @return javax.swing.JTabbedPane
|
337 |
*/
|
338 |
private JTabbedPane getParameters() { |
339 |
if (tabbedPanel == null) { |
340 |
tabbedPanel = new JTabbedPane(); |
341 |
tabbedPanel.addTab(getTranslation("parametros"), null, getPparameters(), null); |
342 |
//tabbedPanel.addTab(getTraduction("opciones"), null, getPopciones(), null);
|
343 |
} |
344 |
return tabbedPanel;
|
345 |
} |
346 |
|
347 |
/**
|
348 |
* This method initializes pparameters
|
349 |
*
|
350 |
* @return javax.swing.JPanel
|
351 |
*/
|
352 |
private JPanel getPparameters() { |
353 |
if (pparameters == null) { |
354 |
pparameters = new JPanel(); |
355 |
pparameters.setLayout(null);
|
356 |
pparameters.setPreferredSize(new java.awt.Dimension(400,230)); |
357 |
pparameters.add(getPServer(), null);
|
358 |
pparameters.add(getPhost(), null);
|
359 |
pparameters.add(getPUser(), null);
|
360 |
pparameters.add(getPDatabase(), null);
|
361 |
pparameters.add(getBTestConnection(), null);
|
362 |
} |
363 |
return pparameters;
|
364 |
} |
365 |
|
366 |
/**
|
367 |
* This method initializes popciones
|
368 |
*
|
369 |
* @return javax.swing.JPanel
|
370 |
*/
|
371 |
private JPanel getPopciones() { |
372 |
if (popciones == null) { |
373 |
popciones = new JPanel(); |
374 |
} |
375 |
return popciones;
|
376 |
} |
377 |
|
378 |
/**
|
379 |
* This method initializes cbDrivers
|
380 |
*
|
381 |
* @return javax.swing.JComboBox
|
382 |
*/
|
383 |
private JComboBox getCbDrivers() { |
384 |
if (cbDrivers == null) { |
385 |
cbDrivers = new JComboBox(); |
386 |
/*Driver[] drivers=null;
|
387 |
try {
|
388 |
drivers = getDrivers();
|
389 |
} catch (DriverLoadException e) {
|
390 |
NotificationManager.addError(PluginServices.getText(this,"Error obteniendo los drivers JDBC"),e);
|
391 |
}
|
392 |
if (drivers!=null)
|
393 |
*/
|
394 |
for (int i=0;i<connectionDB.getDefaultTrans().length;i++){ |
395 |
cbDrivers.addItem(connectionDB.getDefaultTrans()[i]); |
396 |
} |
397 |
//cbDrivers.addItem("mysql");
|
398 |
cbDrivers.setPreferredSize(new java.awt.Dimension(150,25)); |
399 |
cbDrivers.setBounds(new java.awt.Rectangle(24,35,179,20)); |
400 |
cbDrivers.addActionListener(new java.awt.event.ActionListener() {
|
401 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
402 |
initPort(((ConnectionTrans)cbDrivers.getSelectedItem()).getPort()); |
403 |
} |
404 |
}); |
405 |
} |
406 |
return cbDrivers;
|
407 |
} |
408 |
|
409 |
/**
|
410 |
* This method initializes txtName
|
411 |
*
|
412 |
* @return javax.swing.JTextField
|
413 |
*/
|
414 |
private JTextField getTxtName() { |
415 |
if (txtName == null) { |
416 |
txtName = new JTextField(); |
417 |
txtName.setBounds(new java.awt.Rectangle(212,35,162,20)); |
418 |
} |
419 |
return txtName;
|
420 |
} |
421 |
|
422 |
/**
|
423 |
* This method initializes pServer
|
424 |
*
|
425 |
* @return javax.swing.JPanel
|
426 |
*/
|
427 |
private JPanel getPServer() { |
428 |
if (pServer == null) { |
429 |
lblName = new JLabel(); |
430 |
lblName.setBounds(new java.awt.Rectangle(212,13,81,18)); |
431 |
lblName.setText(getTranslation("nombre"));
|
432 |
lblDrivers = new JLabel(); |
433 |
lblDrivers.setBounds(new java.awt.Rectangle(25,18,105,16)); |
434 |
lblDrivers.setText(getTranslation("drivers"));
|
435 |
pServer = new JPanel(); |
436 |
pServer.setLayout(null);
|
437 |
pServer.setBounds(new java.awt.Rectangle(2,2,400,69)); |
438 |
pServer.setBorder(javax.swing.BorderFactory.createTitledBorder(null, getTranslation("servidor"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
439 |
pServer.add(getCbDrivers(), null);
|
440 |
pServer.add(getTxtName(), null);
|
441 |
pServer.add(lblDrivers, null);
|
442 |
pServer.add(lblName, null);
|
443 |
} |
444 |
return pServer;
|
445 |
} |
446 |
|
447 |
/**
|
448 |
* This method initializes phost
|
449 |
*
|
450 |
* @return javax.swing.JPanel
|
451 |
*/
|
452 |
private JPanel getPhost() { |
453 |
if (phost == null) { |
454 |
lblPort = new JLabel(); |
455 |
lblPort.setText(getTranslation("puerto"));
|
456 |
lblPort.setPreferredSize(new java.awt.Dimension(60,16)); |
457 |
lblHost = new JLabel(); |
458 |
lblHost.setText(getTranslation("hostname"));
|
459 |
lblHost.setPreferredSize(new java.awt.Dimension(60,16)); |
460 |
phost = new JPanel(); |
461 |
phost.setLayout(new FlowLayout()); |
462 |
phost.setBounds(new java.awt.Rectangle(3,73,195,85)); |
463 |
phost.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
|
464 |
phost.add(lblHost, null);
|
465 |
phost.add(getTxtHost(), null);
|
466 |
phost.add(lblPort, null);
|
467 |
phost.add(getTxtPort(), null);
|
468 |
} |
469 |
return phost;
|
470 |
} |
471 |
|
472 |
/**
|
473 |
* This method initializes txtHost
|
474 |
*
|
475 |
* @return javax.swing.JTextField
|
476 |
*/
|
477 |
private JTextField getTxtHost() { |
478 |
if (txtHost == null) { |
479 |
txtHost = new JTextField(); |
480 |
txtHost.setPreferredSize(new java.awt.Dimension(110,20)); |
481 |
} |
482 |
return txtHost;
|
483 |
} |
484 |
|
485 |
/**
|
486 |
* This method initializes txtPort
|
487 |
*
|
488 |
* @return javax.swing.JTextField
|
489 |
*/
|
490 |
private JTextField getTxtPort() { |
491 |
if (txtPort == null) { |
492 |
txtPort = new JTextField(); |
493 |
ConnectionTrans ct=(ConnectionTrans)getCbDrivers().getSelectedItem(); |
494 |
txtPort.setText(ct.getPort()); |
495 |
txtPort.setPreferredSize(new java.awt.Dimension(110,20)); |
496 |
} |
497 |
return txtPort;
|
498 |
} |
499 |
|
500 |
/**
|
501 |
* This method initializes pUser
|
502 |
*
|
503 |
* @return javax.swing.JPanel
|
504 |
*/
|
505 |
private JPanel getPUser() { |
506 |
if (pUser == null) { |
507 |
lblPassword = new JLabel(); |
508 |
lblPassword.setText(getTranslation("clave"));
|
509 |
lblPassword.setPreferredSize(new java.awt.Dimension(60,16)); |
510 |
lblUser = new JLabel(); |
511 |
lblUser.setText(getTranslation("usuario"));
|
512 |
lblUser.setPreferredSize(new java.awt.Dimension(60,16)); |
513 |
pUser = new JPanel(); |
514 |
pUser.setBounds(new java.awt.Rectangle(201,73,195,85)); |
515 |
pUser.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
|
516 |
pUser.add(lblUser, null);
|
517 |
pUser.add(getTxtUser(), null);
|
518 |
pUser.add(lblPassword, null);
|
519 |
pUser.add(getTxtPassword(), null);
|
520 |
pUser.add(getRbPassword(), null);
|
521 |
} |
522 |
return pUser;
|
523 |
} |
524 |
|
525 |
/**
|
526 |
* This method initializes txtUser
|
527 |
*
|
528 |
* @return javax.swing.JTextField
|
529 |
*/
|
530 |
private JTextField getTxtUser() { |
531 |
if (txtUser == null) { |
532 |
txtUser = new JTextField(); |
533 |
txtUser.setPreferredSize(new java.awt.Dimension(100,20)); |
534 |
} |
535 |
return txtUser;
|
536 |
} |
537 |
|
538 |
/**
|
539 |
* This method initializes txtPassword
|
540 |
*
|
541 |
* @return javax.swing.JTextField
|
542 |
*/
|
543 |
private JPasswordField getTxtPassword() { |
544 |
if (txtPassword == null) { |
545 |
txtPassword = new JPasswordField(); |
546 |
txtPassword.setPreferredSize(new java.awt.Dimension(100,20)); |
547 |
} |
548 |
return txtPassword;
|
549 |
} |
550 |
|
551 |
/**
|
552 |
* This method initializes pDatabase
|
553 |
*
|
554 |
* @return javax.swing.JPanel
|
555 |
*/
|
556 |
private JPanel getPDatabase() { |
557 |
if (pDatabase == null) { |
558 |
lblDB = new JLabel(); |
559 |
lblDB.setText(getTranslation("base_datos"));
|
560 |
pDatabase = new JPanel(); |
561 |
pDatabase.setBounds(new java.awt.Rectangle(4,160,391,34)); |
562 |
pDatabase.add(lblDB, null);
|
563 |
pDatabase.add(getTxtDB(), null);
|
564 |
} |
565 |
return pDatabase;
|
566 |
} |
567 |
|
568 |
/**
|
569 |
* This method initializes txtDB
|
570 |
*
|
571 |
* @return javax.swing.JTextField
|
572 |
*/
|
573 |
private JTextField getTxtDB() { |
574 |
if (txtDB == null) { |
575 |
txtDB = new JTextField(); |
576 |
txtDB.setPreferredSize(new java.awt.Dimension(200,20)); |
577 |
} |
578 |
return txtDB;
|
579 |
} |
580 |
|
581 |
/**
|
582 |
* This method initializes bTestConnection
|
583 |
*
|
584 |
* @return javax.swing.JButton
|
585 |
*/
|
586 |
private JButton getBTestConnection() { |
587 |
if (bTestConnection == null) { |
588 |
bTestConnection = new JButton(); |
589 |
bTestConnection.setBounds(new java.awt.Rectangle(4,197,392,26)); |
590 |
bTestConnection.setText(getTranslation("prueba_conexion"));
|
591 |
bTestConnection.addActionListener(new java.awt.event.ActionListener() {
|
592 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
593 |
ConnectionTrans ct=new ConnectionTrans();
|
594 |
ct.setDriver(getCbDrivers().getSelectedItem().toString()); |
595 |
ct.setName(getTxtName().getText()); |
596 |
ct.setHost(getTxtHost().getText()); |
597 |
ct.setPort(getTxtPort().getText()); |
598 |
ct.setUser(getTxtUser().getText()); |
599 |
ct.setPassword(getTxtPassword().getText()); |
600 |
ct.setDb(getTxtDB().getText()); |
601 |
ct.setConnBegining(((ConnectionTrans)getCbDrivers().getSelectedItem()).getConnBeginning()); |
602 |
//System.out.println("Bot?n prueba conexi?n");
|
603 |
boolean correct=true; |
604 |
try {
|
605 |
connectionDB.testDB(ct); |
606 |
} catch (ConnectionException e1) {
|
607 |
JOptionPane.showMessageDialog((Component)JDBCManager.this,e1); |
608 |
correct=false;
|
609 |
} |
610 |
if (correct)
|
611 |
JOptionPane.showMessageDialog((Component)JDBCManager.this,getTranslation("conexion_correcta")); |
612 |
} |
613 |
}); |
614 |
} |
615 |
return bTestConnection;
|
616 |
} |
617 |
|
618 |
/**
|
619 |
* This method initializes pConnectionsTree
|
620 |
*
|
621 |
* @return javax.swing.JPanel
|
622 |
*/
|
623 |
private JPanel getPConnectionsTree() { |
624 |
if (pConnectionsTree == null) { |
625 |
pConnectionsTree = new JPanel(); |
626 |
pConnectionsTree.setPreferredSize(new java.awt.Dimension(170,260)); |
627 |
pConnectionsTree.setBackground(java.awt.SystemColor.control); |
628 |
pConnectionsTree.add(getScrollTree(), null);
|
629 |
} |
630 |
return pConnectionsTree;
|
631 |
} |
632 |
private ConnectionTrans PaneltoConnectTrans(){
|
633 |
connectionTrans=new ConnectionTrans();
|
634 |
connectionTrans.setDriver(((ConnectionTrans)getCbDrivers().getSelectedItem()).getName()); |
635 |
connectionTrans.setName(getTxtName().getText()); |
636 |
connectionTrans.setHost(getTxtHost().getText()); |
637 |
connectionTrans.setPort(getTxtPort().getText()); |
638 |
connectionTrans.setUser(getTxtUser().getText()); |
639 |
connectionTrans.setSavePassword(getRbPassword().isSelected()); |
640 |
connectionTrans.setPassword(getTxtPassword().getText()); |
641 |
connectionTrans.setDb(getTxtDB().getText()); |
642 |
connectionTrans.setConnBegining(((ConnectionTrans)getCbDrivers().getSelectedItem()).getConnBeginning()); |
643 |
return connectionTrans;
|
644 |
} |
645 |
//public Connection getConnection(String host,String name) throws SQLException{
|
646 |
// return connectionDB.getConnectionByName(host+"_"+name);
|
647 |
//}
|
648 |
/**
|
649 |
* This method initializes bAcept
|
650 |
*
|
651 |
* @return javax.swing.JButton
|
652 |
*/
|
653 |
private JButton getBAcept() { |
654 |
if (bAcept == null) { |
655 |
bAcept = new JButton(); |
656 |
bAcept.setText(getTranslation("aceptar"));
|
657 |
bAcept.addActionListener(new java.awt.event.ActionListener() {
|
658 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
659 |
try {
|
660 |
connectionTrans=PaneltoConnectTrans(); |
661 |
if (connectionDB.testDB(connectionTrans)){
|
662 |
connectionTrans.setConnected(true);
|
663 |
registerConnection(connectionTrans); |
664 |
} |
665 |
|
666 |
} catch (ConnectionException e1) {
|
667 |
JOptionPane.showMessageDialog((Component)JDBCManager.this,e1); |
668 |
} |
669 |
treeModel.reload(); |
670 |
|
671 |
} |
672 |
|
673 |
|
674 |
}); |
675 |
} |
676 |
return bAcept;
|
677 |
} |
678 |
|
679 |
private void registerConnection(ConnectionTrans connectionTrans) { |
680 |
boolean same=false; |
681 |
boolean samewithChange=false; |
682 |
DefaultMutableTreeNode node=null; |
683 |
for (int i=0;i<root.getChildCount();i++){ |
684 |
//Object obj=((DefaultMutableTreeNode)root.getChildAt(i)).getUserObject();
|
685 |
|
686 |
if (connectionTrans.equals(ctTree)){
|
687 |
same=true;
|
688 |
if (connectionTrans.isConnected()!=ctTree.isConnected()|| ctTree.isConnected()==false || connectionTrans.isSavePassword()!=ctTree.isSavePassword() || connectionTrans.isSavePassword()==false){ |
689 |
node=(DefaultMutableTreeNode)root.getChildAt(i);
|
690 |
samewithChange=true;
|
691 |
ctTree.setConnected(true);
|
692 |
} |
693 |
}else{
|
694 |
//((ConnectionTrans)obj).setConnected(true);
|
695 |
} |
696 |
} |
697 |
|
698 |
try{
|
699 |
if (!same){
|
700 |
try {
|
701 |
connectionDB.setupDriver(connectionTrans); |
702 |
connectionDB.setPersistence(connectionTrans); |
703 |
} catch (Exception e) { |
704 |
e.printStackTrace(); |
705 |
} |
706 |
initTree(connectionTrans); |
707 |
} |
708 |
if (samewithChange){
|
709 |
try {
|
710 |
connectionDB.setupDriver(connectionTrans); |
711 |
connectionDB.setPersistence(connectionTrans); |
712 |
} catch (Exception e) { |
713 |
e.printStackTrace(); |
714 |
} |
715 |
modifyTreeNode(connectionTrans,node); |
716 |
|
717 |
} |
718 |
}catch (Exception e1) { |
719 |
// TODO: handle exception
|
720 |
} |
721 |
treeConnection.repaint(); |
722 |
//PluginServices.getMDIManager().closeView(JDBCManager.this);
|
723 |
|
724 |
|
725 |
} |
726 |
|
727 |
/**
|
728 |
* This method initializes bCancel
|
729 |
*
|
730 |
* @return javax.swing.JButton
|
731 |
*/
|
732 |
private JButton getBCancel() { |
733 |
if (bCancel == null) { |
734 |
bCancel = new JButton(); |
735 |
bCancel.setText(getTranslation("cancelar"));
|
736 |
bCancel.addActionListener(new java.awt.event.ActionListener() {
|
737 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
738 |
|
739 |
} |
740 |
}); |
741 |
} |
742 |
return bCancel;
|
743 |
} |
744 |
|
745 |
/**
|
746 |
* This method initializes treeConnection
|
747 |
*
|
748 |
* @return javax.swing.JTree
|
749 |
*/
|
750 |
private JTree getTreeConnection() { |
751 |
if (treeConnection == null) { |
752 |
treeConnection = new JTree(); |
753 |
root = new DefaultMutableTreeNode(java.lang.Object.class); |
754 |
treeModel = new DefaultTreeModel(root); |
755 |
//TOCRenderer m_TocRenderer = new TOCRenderer();
|
756 |
//treeConnection.setCellRenderer(m_TocRenderer);
|
757 |
treeConnection.setRootVisible(false);
|
758 |
treeConnection.setSize(new java.awt.Dimension(170,252)); |
759 |
treeConnection.setCellRenderer(new ConnRenderer());
|
760 |
//treeConnection.setShowsRootHandles(true);
|
761 |
|
762 |
//Posibilidad de seleccionar de forma aleatoria nodos de la leyenda.
|
763 |
treeConnection.setModel(treeModel); |
764 |
treeConnection.addMouseListener(new java.awt.event.MouseAdapter() {
|
765 |
public void mouseClicked(MouseEvent e) { |
766 |
super.mouseClicked(e);
|
767 |
int x = e.getX();
|
768 |
int y = e.getY();
|
769 |
int row = treeConnection.getRowForLocation(x, y);
|
770 |
TreePath path = treeConnection.getPathForRow(row);
|
771 |
if (path != null) { |
772 |
nodeSelected = (DefaultMutableTreeNode) path.getLastPathComponent();
|
773 |
} |
774 |
if (e.getButton()==MouseEvent.BUTTON1){ |
775 |
System.out.println("mouseClicked()"); // TODO Auto-generated Event stub mousePressed() |
776 |
///int x = e.getX();
|
777 |
///int y = e.getY();
|
778 |
///int row = treeConnection.getRowForLocation(x, y);
|
779 |
///TreePath path = treeConnection.getPathForRow(row);
|
780 |
///if (path != null) {
|
781 |
/// nodeSelected = (DefaultMutableTreeNode) path.getLastPathComponent();
|
782 |
|
783 |
if (nodeSelected.getUserObject() instanceof ConnectionTrans){ |
784 |
ctTree=(ConnectionTrans)nodeSelected.getUserObject(); |
785 |
init(ctTree); |
786 |
} |
787 |
/// }
|
788 |
}else if (e.getButton()==MouseEvent.BUTTON3){ |
789 |
if (nodeSelected.getUserObject() instanceof ConnectionTrans){ |
790 |
ctTree=(ConnectionTrans)nodeSelected.getUserObject(); |
791 |
} |
792 |
if (!ctTree.isConnected()){
|
793 |
getMenuDisConnect().setEnabled(false);
|
794 |
getMenuConectar().setEnabled(true);
|
795 |
}else{
|
796 |
getMenuConectar().setEnabled(false);
|
797 |
getMenuDisConnect().setEnabled(true);
|
798 |
} |
799 |
JPopupMenu popup=getPopupMenu();
|
800 |
treeConnection.add(popup); |
801 |
|
802 |
//System.out.println("boton derecho");
|
803 |
popup.show(e.getComponent(), e.getX(), e.getY()); |
804 |
} |
805 |
} |
806 |
|
807 |
|
808 |
}); |
809 |
} |
810 |
return treeConnection;
|
811 |
} |
812 |
/*public ViewInfo getViewInfo() {
|
813 |
ViewInfo m_viewinfo = new ViewInfo(ViewInfo.MODALDIALOG);
|
814 |
m_viewinfo.setTitle(PluginServices.getText(this,
|
815 |
"DBManager"));
|
816 |
|
817 |
return m_viewinfo;
|
818 |
}*/
|
819 |
private void init(ConnectionTrans ct){ |
820 |
|
821 |
try {
|
822 |
initDriver(ct.getDriver()); |
823 |
} catch (ConnectionException e) {
|
824 |
} |
825 |
initName(ct.getName()); |
826 |
initHost(ct.getHost()); |
827 |
initPort(ct.getPort()); |
828 |
initUser(ct.getUser()); |
829 |
initSavePassword(ct.isSavePassword()); |
830 |
initPassword(ct.getPassword()); |
831 |
initDB(ct.getDb()); |
832 |
} |
833 |
private void init() throws Exception{ |
834 |
ConnectionTrans[] conns=null; |
835 |
conns=connectionDB.getPersistence(); |
836 |
if (conns == null) |
837 |
conns = connectionDB.getDefaultTrans(); |
838 |
int j=-1; |
839 |
for (j=0;j<conns.length;j++){ |
840 |
initTree(conns[j]); |
841 |
} |
842 |
if (j>=0){ |
843 |
j=j-1;
|
844 |
init(conns[j]); |
845 |
} |
846 |
|
847 |
} |
848 |
private void modifyTreeNode(ConnectionTrans cs,DefaultMutableTreeNode node) throws ConnectionException{ |
849 |
|
850 |
String[] s=new String[0]; |
851 |
s=connectionDB.getTableNames(connectionDB.getConnection(cs)); |
852 |
if (cs.isSavePassword()){
|
853 |
((ConnectionTrans)node.getUserObject()).setSavePassword(true);
|
854 |
|
855 |
}else{
|
856 |
((ConnectionTrans)node.getUserObject()).setSavePassword(false);
|
857 |
|
858 |
} |
859 |
((ConnectionTrans)node.getUserObject()).setPassword(cs.getPassword()); |
860 |
node.removeAllChildren(); |
861 |
for(int i=0;i<s.length;i++){ |
862 |
DefaultMutableTreeNode table=new DefaultMutableTreeNode(s[i]); |
863 |
treeModel.insertNodeInto(table,node,node.getChildCount()); |
864 |
} |
865 |
|
866 |
treeModel.reload(); |
867 |
} |
868 |
|
869 |
private void initTreeWithoutPassword(ConnectionTrans cs){ |
870 |
DefaultMutableTreeNode aux=new DefaultMutableTreeNode(cs); |
871 |
treeModel.insertNodeInto(aux,root,root.getChildCount()); |
872 |
} |
873 |
private void initTree(ConnectionTrans cs) throws ConnectionException{ |
874 |
if (cs.isSavePassword()){
|
875 |
String[] s=new String[0]; |
876 |
s=connectionDB.getTableNames(connectionDB.getConnection(cs)); |
877 |
DefaultMutableTreeNode aux=new DefaultMutableTreeNode(cs); |
878 |
treeModel.insertNodeInto(aux,root,root.getChildCount()); |
879 |
for(int i=0;i<s.length;i++){ |
880 |
DefaultMutableTreeNode table=new DefaultMutableTreeNode(s[i]); |
881 |
treeModel.insertNodeInto(table,aux,aux.getChildCount()); |
882 |
} |
883 |
}else{
|
884 |
initTreeWithoutPassword(cs); |
885 |
} |
886 |
treeModel.reload(); |
887 |
|
888 |
} |
889 |
private void initDriver(String s) throws ConnectionException{ |
890 |
//VectorialJDBCDriver driver=connectionDB.getDriver(s);
|
891 |
for (int i=0;i<getCbDrivers().getItemCount();i++){ |
892 |
ConnectionTrans ct=(ConnectionTrans)getCbDrivers().getItemAt(i); |
893 |
if (ct.getName().equals(s)){
|
894 |
getCbDrivers().setSelectedItem(ct); |
895 |
} |
896 |
} |
897 |
|
898 |
} |
899 |
private void initName(String s){ |
900 |
getTxtName().setText(s); |
901 |
} |
902 |
private void initHost(String s){ |
903 |
getTxtHost().setText(s); |
904 |
} |
905 |
private void initPort(String s){ |
906 |
getTxtPort().setText(s); |
907 |
} |
908 |
private void initUser(String s){ |
909 |
getTxtUser().setText(s); |
910 |
} |
911 |
private boolean initSavePassword(boolean b){ |
912 |
getRbPassword().setSelected(b); |
913 |
return b;
|
914 |
} |
915 |
private void initPassword(String s){ |
916 |
getTxtPassword().setText(s); |
917 |
} |
918 |
private void initDB(String s){ |
919 |
getTxtDB().setText(s); |
920 |
} |
921 |
|
922 |
void setResourceBundle() {
|
923 |
try {
|
924 |
resourceBundle = (PropertyResourceBundle) ResourceBundle.getBundle("com.iver.utiles.connections.text"); |
925 |
} catch (MissingResourceException e) { |
926 |
System.out.println("Exception = "+e); |
927 |
// logger.error(Messages.getString("PluginServices.No_se_encontro_el_recurso_de_traducciones") + name, e);
|
928 |
} |
929 |
} |
930 |
public static String getTranslation(String key) { |
931 |
if (resourceBundle == null) { |
932 |
return key;
|
933 |
} |
934 |
|
935 |
if (key == null) { |
936 |
return null; |
937 |
} |
938 |
|
939 |
try {
|
940 |
return resourceBundle.getString(key);
|
941 |
} catch (MissingResourceException e) { |
942 |
//logger.warn(Messages.getString("PluginServices.No_se_encontro_la_traduccion_para") + " " + key);
|
943 |
|
944 |
return key;
|
945 |
} |
946 |
} |
947 |
|
948 |
/*private Driver[] getDrivers() throws DriverLoadException{
|
949 |
Class[] classes = new Class[] { VectorialJDBCDriver.class };
|
950 |
|
951 |
ArrayList ret = new ArrayList();
|
952 |
String[] driverNames = LayerFactory.getDM().getDriverNames();
|
953 |
|
954 |
for (int i = 0; i < driverNames.length; i++) {
|
955 |
for (int j = 0; j < classes.length; j++) {
|
956 |
if (LayerFactory.getDM().isA(driverNames[i], classes[j])) {
|
957 |
ret.add(LayerFactory.getDM().getDriver(driverNames[i]));
|
958 |
}
|
959 |
}
|
960 |
}
|
961 |
|
962 |
return (Driver[]) ret.toArray(new Driver[0]);
|
963 |
|
964 |
}
|
965 |
*/
|
966 |
} // @jve:decl-index=0:visual-constraint="10,10"
|