1 24 25 package org.objectweb.cjdbc.console.wizard.tab; 26 27 import java.awt.GridBagConstraints ; 28 import java.awt.GridBagLayout ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.FocusEvent ; 32 import java.awt.event.FocusListener ; 33 import java.awt.event.ItemEvent ; 34 import java.awt.event.ItemListener ; 35 import java.util.ArrayList ; 36 import java.util.Hashtable ; 37 38 import javax.swing.BorderFactory ; 39 import javax.swing.JButton ; 40 import javax.swing.JCheckBox ; 41 import javax.swing.JComboBox ; 42 import javax.swing.JComponent ; 43 import javax.swing.JLabel ; 44 import javax.swing.JPanel ; 45 import javax.swing.JTextField ; 46 47 import org.objectweb.cjdbc.common.i18n.WizardTranslate; 48 import org.objectweb.cjdbc.console.wizard.WizardConstants; 49 import org.objectweb.cjdbc.console.wizard.WizardTab; 50 import org.objectweb.cjdbc.console.wizard.WizardTabs; 51 import org.objectweb.cjdbc.console.wizard.objects.Backend; 52 import org.objectweb.cjdbc.console.wizard.objects.ConnectionInfo; 53 import org.objectweb.cjdbc.console.wizard.objects.ConnectionParameterDialog; 54 import org.objectweb.cjdbc.console.wizard.objects.ConnectionTypeInfo; 55 import org.objectweb.cjdbc.console.wizard.objects.User; 56 57 65 public class BackendTab extends WizardTab 66 implements 67 ItemListener , 68 FocusListener , 69 ActionListener 70 { 71 72 public JComboBox backendsCombo; 73 private JTextField backendName; 74 private JTextField backendUrl; 75 private JButton buttonAdd; 76 private JButton buttonRemove; 77 private JTextField backendDriver; 78 private JTextField backendDriverPath; 79 private JTextField backendStatement; 80 private JCheckBox gatherSystemTables; 81 private JComboBox dynamicPrecision; 82 private JComboBox users; 83 private GridBagConstraints connectionconstraints; 84 private JPanel connections; 85 private JTextField rLogin; 86 private JTextField rPassword; 87 private JTextField urlparameters; 88 private JComboBox connectiontype; 89 private JButton connectionParameter; 90 91 96 public BackendTab(WizardTabs tabs) 97 { 98 super(tabs, WizardConstants.TAB_BACKENDS); 99 100 JPanel list = new JPanel (); 104 list.setBorder(BorderFactory.createTitledBorder(WizardTranslate 105 .get("label.list"))); 106 list.setLayout(new GridBagLayout ()); 107 GridBagConstraints listconstraints = new GridBagConstraints (); 108 listconstraints.fill = GridBagConstraints.HORIZONTAL; 109 110 listconstraints.weightx = 1.0; 111 listconstraints.weighty = 1.0; 112 listconstraints.gridy = 0; 113 listconstraints.gridwidth = 2; 114 115 backendsCombo = new JComboBox (new Object []{}); 116 backendsCombo.addItemListener(this); 117 118 list.add(backendsCombo, listconstraints); 119 120 listconstraints.gridy = ++listconstraints.gridy; 121 listconstraints.gridwidth = 1; 122 123 listconstraints.gridx = 0; 124 buttonAdd = new JButton (WizardTranslate.get("label.addbackend")); 125 buttonAdd.setActionCommand(WizardConstants.COMMAND_ADD_BACKEND); 126 buttonAdd.addActionListener(this); 127 list.add(buttonAdd, listconstraints); 128 129 listconstraints.gridx = 1; 130 buttonRemove = new JButton (WizardTranslate.get("label.removebackend")); 131 buttonRemove.setActionCommand(WizardConstants.COMMAND_REMOVE_BACKEND); 132 buttonRemove.addActionListener(this); 133 list.add(buttonRemove, listconstraints); 134 135 this.add(list, constraints); 136 137 constraints.gridy = ++constraints.gridy; 138 139 JPanel general = new JPanel (); 143 general.setBorder(BorderFactory.createTitledBorder(WizardTranslate 144 .get("label.general"))); 145 general.setLayout(new GridBagLayout ()); 146 GridBagConstraints generalconstraints = new GridBagConstraints (); 147 generalconstraints.fill = GridBagConstraints.HORIZONTAL; 148 generalconstraints.weightx = 1.0; 149 150 generalconstraints.gridy = ++generalconstraints.gridy; 151 generalconstraints.gridx = 0; 152 general.add(new JLabel (WizardTranslate.get("label.backendName")), 153 generalconstraints); 154 generalconstraints.gridx = 1; 155 backendName = new JTextField (); 156 backendName.addFocusListener(this); 157 general.add(backendName, generalconstraints); 158 159 generalconstraints.gridy = ++generalconstraints.gridy; 160 generalconstraints.gridx = 0; 161 general.add(new JLabel (WizardTranslate.get("label.backendUrl")), 162 generalconstraints); 163 generalconstraints.gridx = 1; 164 backendUrl = new JTextField (); 165 backendUrl.addFocusListener(this); 166 general.add(backendUrl, generalconstraints); 167 168 generalconstraints.gridy = ++generalconstraints.gridy; 169 generalconstraints.gridx = 0; 170 general.add(new JLabel (WizardTranslate.get("label.backendDriver")), 171 generalconstraints); 172 generalconstraints.gridx = 1; 173 backendDriver = new JTextField (); 174 backendDriver.addFocusListener(this); 175 general.add(backendDriver, generalconstraints); 176 177 generalconstraints.gridy = ++generalconstraints.gridy; 178 generalconstraints.gridx = 0; 179 general.add(new JLabel (WizardTranslate.get("label.backendDriverPath")), 180 generalconstraints); 181 generalconstraints.gridx = 1; 182 backendDriverPath = new JTextField (); 183 backendDriverPath.addFocusListener(this); 184 general.add(backendDriverPath, generalconstraints); 185 186 generalconstraints.gridy = ++generalconstraints.gridy; 187 generalconstraints.gridx = 0; 188 general.add(new JLabel (WizardTranslate.get("label.backendStatement")), 189 generalconstraints); 190 generalconstraints.gridx = 1; 191 backendStatement = new JTextField (); 192 backendStatement.addFocusListener(this); 193 general.add(backendStatement, generalconstraints); 194 195 generalconstraints.gridy = ++generalconstraints.gridy; 196 generalconstraints.gridx = 0; 197 general.add(new JLabel (WizardTranslate.get("label.gatherSystemTables")), 198 generalconstraints); 199 generalconstraints.gridx = 1; 200 gatherSystemTables = new JCheckBox (); 201 gatherSystemTables.setName("label.gatherSystemTables"); 202 gatherSystemTables.addFocusListener(this); 203 general.add(gatherSystemTables, generalconstraints); 204 205 generalconstraints.gridy = ++generalconstraints.gridy; 206 dynamicPrecision = new JComboBox (WizardConstants.DYNAMIC_PRECISION); 207 dynamicPrecision.setSelectedItem(WizardConstants.DEFAULT_DYNAMIC_PRECISION); 208 dynamicPrecision.addItemListener(this); 209 dynamicPrecision.addFocusListener(this); 210 generalconstraints.gridx = 0; 211 general.add(new JLabel (WizardTranslate.get("label.dynamicPrecision")), 212 generalconstraints); 213 generalconstraints.gridx = 1; 214 general.add(dynamicPrecision, generalconstraints); 215 216 this.add(general, constraints); 217 218 constraints.gridy = ++constraints.gridy; 219 220 224 connections = new JPanel (); 225 connections.setBorder(BorderFactory.createTitledBorder(WizardTranslate 226 .get("label.connections"))); 227 connections.setLayout(new GridBagLayout ()); 228 connectionconstraints = new GridBagConstraints (); 229 connectionconstraints.fill = GridBagConstraints.HORIZONTAL; 230 connectionconstraints.weightx = 1.0; 231 232 setUsersComboBox(); 234 235 connectionconstraints.gridy = ++connectionconstraints.gridy; 237 connectionconstraints.gridx = 0; 238 connections.add(new JLabel (WizardTranslate.get("label.rLogin")), 239 connectionconstraints); 240 connectionconstraints.gridx = 1; 241 rLogin = new JTextField (); 242 rLogin.addFocusListener(this); 243 connections.add(rLogin, connectionconstraints); 244 245 connectionconstraints.gridy = ++connectionconstraints.gridy; 247 connectionconstraints.gridx = 0; 248 connections.add(new JLabel (WizardTranslate.get("label.rPassword")), 249 connectionconstraints); 250 connectionconstraints.gridx = 1; 251 rPassword = new JTextField (); 252 rPassword.addFocusListener(this); 253 connections.add(rPassword, connectionconstraints); 254 255 connectionconstraints.gridy = ++connectionconstraints.gridy; 257 connectionconstraints.gridx = 0; 258 connections.add(new JLabel (WizardTranslate.get("label.urlparameters")), 259 connectionconstraints); 260 connectionconstraints.gridx = 1; 261 urlparameters = new JTextField (); 262 urlparameters.addFocusListener(this); 263 connections.add(urlparameters, connectionconstraints); 264 265 connectionconstraints.gridy = ++connectionconstraints.gridy; 267 connectiontype = new JComboBox (WizardConstants.CONNECTION_MANAGERS); 268 connectionconstraints.gridx = 0; 269 connectiontype.addFocusListener(this); 270 connectiontype.addItemListener(this); 271 connections.add(new JLabel (WizardTranslate.get("label.connectiontype")), 272 connectionconstraints); 273 connectionconstraints.gridx = 1; 274 connections.add(connectiontype, connectionconstraints); 275 276 connectionconstraints.gridy = ++connectionconstraints.gridy; 278 connectionParameter = new JButton (WizardTranslate 279 .get("label.edit.connection.parameters")); 280 connectionParameter 281 .setActionCommand(WizardConstants.COMMAND_EDIT_CONNECTION_PARAM); 282 connectionconstraints.gridx = 1; 283 connectionParameter.setEnabled(false); 284 connectionParameter.addActionListener(this); 285 connections.add(connectionParameter, connectionconstraints); 286 287 this.add(connections, constraints); 288 289 } 290 291 294 public void setUsersComboBox() 295 { 296 if (users != null) 297 connections.remove(users); 298 users = new JComboBox (tabs.getUsers().toArray()); 299 users.addItemListener(this); 300 connectionconstraints.gridy = 0; 301 connectionconstraints.gridx = 0; 302 connections.add(new JLabel (WizardTranslate.get("label.users")), 303 connectionconstraints); 304 connectionconstraints.gridx = 1; 305 connections.add(users, connectionconstraints); 306 } 307 308 311 public void focusGained(FocusEvent e) 312 { 313 314 } 315 316 319 public void usersChanged() 320 { 321 setUsersComboBox(); 322 } 323 324 327 public void focusLost(FocusEvent e) 328 { 329 Backend backend = ((Backend) backendsCombo.getSelectedItem()); 330 if (backend == null) 331 return; 332 backend.setName(backendName.getText()); 333 backend.setUrl(backendUrl.getText()); 334 backend.setConnectionTestStatement(backendStatement.getText()); 335 backend.setDriver(backendDriver.getText()); 336 backend.setDriverPath(backendDriverPath.getText()); 337 String string = (String ) dynamicPrecision.getSelectedItem(); 338 backend.setDynamicPrecision(string); 339 if (gatherSystemTables.isSelected()) 340 backend.setGatherSystemTables("true"); 341 else 342 backend.setGatherSystemTables(null); 343 344 setBackendInfo(); 345 backendsCombo.repaint(); 346 } 347 348 private void setBackendInfo() 349 { 350 Backend backend = ((Backend) backendsCombo.getSelectedItem()); 351 352 if (backend == null) 353 return; 354 User user = (User) users.getSelectedItem(); 355 if (user != null) 356 { 357 Hashtable con = backend.getConnectionManagers(); 358 ConnectionInfo info = (ConnectionInfo) con.get(user); 359 360 if (info == null) 361 info = new ConnectionInfo(); 362 info.setRLogin(rLogin.getText()); 363 info.setRPassword(rPassword.getText()); 364 info.setUrlParameters(urlparameters.getText()); 365 ConnectionTypeInfo cinfo = info.getConnectionTypeInfo(); 366 if (cinfo == null) 367 { 368 new ConnectionTypeInfo(); 369 } 370 cinfo.setType((String ) connectiontype.getSelectedItem()); 371 info.setConnectionTypeInfo(cinfo); 372 con.put(user, info); 373 backend.setConnectionManagers(con); 374 } 375 } 376 377 380 public void actionPerformed(ActionEvent e) 381 { 382 String command = e.getActionCommand(); 383 384 if (command.equals(WizardConstants.COMMAND_ADD_BACKEND)) 385 { 386 387 String select = WizardTab.showBackendSelectDialog(); 388 Backend backend = new Backend(); 389 390 if (select != null) 391 { 392 backend.setName(select + backendsCombo.getItemCount()); 393 backend.setUrl(types.getString(select + ".url")); 394 backend.setDriver(types.getString(select + ".driver")); 395 backend.setConnectionTestStatement(types.getString(select 396 + ".statement")); 397 } 398 399 backendsCombo.addItem(backend); 400 backendsCombo.setSelectedItem(backend); 401 backendsCombo.validate(); 402 tabs.backendListChanged(); 403 } 404 else if (command.equals(WizardConstants.COMMAND_REMOVE_BACKEND)) 405 { 406 Backend backend = (Backend) backendsCombo.getSelectedItem(); 407 backendsCombo.removeItem(backend); 408 backendsCombo.validate(); 409 backendsCombo.repaint(); 410 tabs.backendListChanged(); 411 } 412 else if (command.equals(WizardConstants.COMMAND_EDIT_CONNECTION_PARAM)) 413 { 414 Backend backend = (Backend) backendsCombo.getSelectedItem(); 415 User user = (User) users.getSelectedItem(); 416 if (backend == null || user == null) 417 return; 418 419 Hashtable managers = backend.getConnectionManagers(); 420 ConnectionInfo info = ((ConnectionInfo) managers.get(user)); 421 ConnectionTypeInfo infoType; 422 if (info == null) 423 { 424 info = new ConnectionInfo(); 425 infoType = new ConnectionTypeInfo(); 426 infoType.setType((String ) connectiontype.getSelectedItem()); 427 info.setConnectionTypeInfo(infoType); 428 managers.put(user, info); 429 } 430 else 431 infoType = info.getConnectionTypeInfo(); 432 433 ConnectionParameterDialog cpd = new ConnectionParameterDialog(infoType); 434 435 infoType.setValues(cpd.getValues()); 436 info.setConnectionTypeInfo(infoType); 437 managers.put(user, info); 438 backend.setConnectionManagers(managers); 439 } 440 441 } 442 443 446 public void itemStateChanged(ItemEvent e) 447 { 448 JComponent comp = (JComponent ) e.getSource(); 449 if (comp == dynamicPrecision) 450 return; 451 452 Backend backend = (Backend) backendsCombo.getSelectedItem(); 453 User user = (User) users.getSelectedItem(); 455 if (user == null || backend == null) 456 return; 457 458 Hashtable con = backend.getConnectionManagers(); 459 ConnectionInfo info = (ConnectionInfo) con.get(user); 460 461 if (info == null) 462 { 463 info = new ConnectionInfo(); 464 } 465 466 if (comp == connectiontype) 467 { 468 int selectedIndex = connectiontype.getSelectedIndex(); 469 if (selectedIndex == 0) 470 connectionParameter.setEnabled(false); 471 else 472 connectionParameter.setEnabled(true); 473 connectionParameter.repaint(); 474 return; 475 } 476 477 rLogin.setText(info.getRLogin()); 478 rPassword.setText(info.getRPassword()); 479 urlparameters.setText(info.getUrlParameters()); 480 connectiontype.setSelectedItem(info.getConnectionTypeInfo().getType()); 481 482 if (backend == null) 483 { 484 backendName.setText(""); 485 backendUrl.setText(""); 486 backendDriver.setText(""); 487 backendDriverPath.setText(""); 488 backendStatement.setText(""); 489 gatherSystemTables.setSelected(false); 490 dynamicPrecision 491 .setSelectedItem(WizardConstants.DEFAULT_DYNAMIC_PRECISION); 492 } 493 else 494 { 495 backendName.setText(backend.getName()); 496 backendUrl.setText(backend.getUrl()); 497 backendDriver.setText(backend.getDriver()); 498 backendDriverPath.setText(backend.getDriverPath()); 499 backendStatement.setText(backend.getConnectionTestStatement()); 500 gatherSystemTables.setSelected(backend.getGatherSystemTables() != null); 501 if (backend.getDynamicPrecision() == null) 502 dynamicPrecision 503 .setSelectedItem(WizardConstants.DEFAULT_DYNAMIC_PRECISION); 504 else 505 dynamicPrecision.setSelectedItem(backend.getDynamicPrecision()); 506 } 507 508 } 509 510 515 public ArrayList getBackends() 516 { 517 return WizardConstants.getItemsFromCombo(this.backendsCombo); 518 } 519 } | Popular Tags |