1 30 package com.genimen.djeneric.tools.administrator.helpers; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Dimension ; 34 import java.awt.Toolkit ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.FocusEvent ; 37 import java.sql.Connection ; 38 39 import javax.swing.DefaultComboBoxModel ; 40 import javax.swing.JButton ; 41 import javax.swing.JCheckBox ; 42 import javax.swing.JComboBox ; 43 import javax.swing.JDialog ; 44 import javax.swing.JLabel ; 45 import javax.swing.JOptionPane ; 46 import javax.swing.JPanel ; 47 import javax.swing.JTextField ; 48 49 import com.genimen.djeneric.language.Messages; 50 import com.genimen.djeneric.repository.DjPersistenceManager; 51 import com.genimen.djeneric.repository.DjPersistenceManagerFactory; 52 import com.genimen.djeneric.repository.DjRepositoryDescriptor; 53 import com.genimen.djeneric.tools.administrator.Administrator; 54 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 55 import com.genimen.djeneric.ui.Util; 56 import com.genimen.djeneric.util.DjLogger; 57 58 63 public class DefineRepositoryDialog extends JDialog 64 { 65 private static final long serialVersionUID = 1L; 66 public static final String POINTBASE_DRIVER = "com.pointbase.jdbc.jdbcUniversalDriver"; 67 public static final String UNA_DRIVER = "com.inet.tds.TdsDriver"; 68 public static final String JTDS_DRIVER = "net.sourceforge.jtds.jdbc.Driver"; 69 public static final String HYPERSONIC_DRIVER = "org.hsqldb.jdbcDriver"; 70 public static final String DB2_DRIVER = "COM.ibm.db2.jdbc.app.DB2Driver"; 71 public static final String DERBY_DRIVER = "com.ibm.db2.jcc.DB2Driver"; 72 public static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver"; 73 public static final String DATASOURCE = DjRepositoryDescriptor.JNDI_DATASOURCE_KEY; 74 75 JPanel mainPanel = new JPanel (); 76 BorderLayout borderLayout1 = new BorderLayout (); 77 JPanel labelPanel = new JPanel (); 78 JPanel fieldsPanel = new JPanel (); 79 JPanel buttonPanel = new JPanel (); 80 BorderLayout borderLayout2 = new BorderLayout (); 81 JPanel jPanel4 = new JPanel (); 82 JButton _butOk = new JButton (); 83 JButton _butCancel = new JButton (); 84 JLabel jLabel1 = new JLabel (); 85 JLabel _lblDriver = new JLabel (); 86 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 87 JLabel jLabel3 = new JLabel (); 88 JTextField _edtName = new JTextField (); 89 DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout(); 90 JComboBox _cbbDriver = new JComboBox (); 91 JTextField _edtURL = new JTextField (); 92 JPanel jPanel5 = new JPanel (); 93 JButton _butTest = new JButton (); 94 boolean _wasCancelled = true; 95 DjRepositoryDescriptor _repositoryDescriptor; 96 JLabel jLabel4 = new JLabel (); 97 JComboBox _cbbManagerClass = new JComboBox (); 98 Administrator _administrator; 99 JLabel jLabel2 = new JLabel (); 100 JCheckBox _chkSharedConnection = new JCheckBox (); 101 JLabel jLabel5 = new JLabel (); 102 JLabel jLabel6 = new JLabel (); 103 JTextField _edtUser = new JTextField (); 104 JTextField _edtPassword = new JTextField (); 105 JLabel jLabel7 = new JLabel (); 106 JComboBox _cbbTraceLevel = new JComboBox (); 107 108 120 public DefineRepositoryDialog(Administrator administrator, String title, DjRepositoryDescriptor descr) 121 throws Exception 122 { 123 super(administrator, title, true); 124 _administrator = administrator; 125 _repositoryDescriptor = descr; 126 jbInit(); 127 128 setSize(new Dimension (480, 280)); 129 130 DefaultComboBoxModel model = new DefaultComboBoxModel (new String []{DjPersistenceManagerFactory.TYPE_POLYMORPH, 131 DjPersistenceManagerFactory.TYPE_DIRECT}); 132 _cbbManagerClass.setModel(model); 133 _cbbManagerClass.setEditable(true); 134 135 _cbbDriver.setModel(new DefaultComboBoxModel (new String []{ORACLE_DRIVER, DERBY_DRIVER, DB2_DRIVER, HYPERSONIC_DRIVER, 136 JTDS_DRIVER, UNA_DRIVER, POINTBASE_DRIVER, DATASOURCE})); 137 _edtName.setText(descr.getName()); 138 139 boolean isLocationSet = false; 140 for (int i = 0; i < model.getSize(); i++) 141 { 142 if (model.getElementAt(i).toString().equalsIgnoreCase(descr.getRepositoryLocation())) 143 { 144 _cbbManagerClass.setSelectedIndex(i); 145 isLocationSet = true; 146 } 147 } 148 149 if (!isLocationSet) _cbbManagerClass.getEditor().setItem(descr.getRepositoryLocation()); 150 151 _cbbDriver.setSelectedIndex(-1); 152 _cbbDriver.getEditor().setItem(descr.getDriver()); 153 _edtURL.setText(descr.getUrl()); 154 _chkSharedConnection.setSelected(descr.isSharedConnection()); 155 _edtUser.setText(descr.getSharedUser()); 156 _edtPassword.setText(descr.getSharedPassword()); 157 158 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 159 Dimension frameSize = getSize(); 160 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 161 162 _cbbManagerClass_actionPerformed(null); 163 164 DefaultComboBoxModel traceModel = new DefaultComboBoxModel (); 165 traceModel.addElement(new TraceLevel(DjPersistenceManager.TRACE_NOTHING, "Nothing")); 166 traceModel.addElement(new TraceLevel(DjPersistenceManager.TRACE_FLOW, "Flow")); 167 traceModel.addElement(new TraceLevel(DjPersistenceManager.TRACE_INTERNAL, "Internal")); 168 _cbbTraceLevel.setModel(traceModel); 169 170 int level = descr.getTraceLevel(); 171 int idx = 0; 172 if (level == DjPersistenceManager.TRACE_FLOW) idx = 1; 173 if (level == DjPersistenceManager.TRACE_INTERNAL) idx = 2; 174 _cbbTraceLevel.setSelectedIndex(idx); 175 176 setVisible(true); 177 } 178 179 184 public boolean wasCancelled() 185 { 186 return _wasCancelled; 187 } 188 189 void jbInit() throws Exception 190 { 191 mainPanel.setLayout(borderLayout1); 192 buttonPanel.setLayout(borderLayout2); 193 _butOk.setText(Messages.getString("global.Ok")); 194 _butOk.addActionListener(new java.awt.event.ActionListener () 195 { 196 public void actionPerformed(ActionEvent e) 197 { 198 _butOk_actionPerformed(e); 199 } 200 }); 201 _butCancel.setText(Messages.getString("global.Cancel")); 202 _butCancel.addActionListener(new java.awt.event.ActionListener () 203 { 204 public void actionPerformed(ActionEvent e) 205 { 206 _butCancel_actionPerformed(e); 207 } 208 }); 209 jLabel1.setText(Messages.getString("global.Name")); 210 _lblDriver.setText(Messages.getString("DefineRepositoryDialog.Driver")); 211 labelPanel.setLayout(verticalFlowLayout1); 212 jLabel3.setText(Messages.getString("DefineRepositoryDialog.URL")); 213 fieldsPanel.setLayout(verticalFlowLayout2); 214 _cbbDriver.setPreferredSize(new Dimension (240, 21)); 215 _cbbDriver.setEditable(true); 216 _butTest.setText(Messages.getString("DefineRepositoryDialog.Test")); 217 _butTest.addActionListener(new java.awt.event.ActionListener () 218 { 219 public void actionPerformed(ActionEvent e) 220 { 221 _butTest_actionPerformed(e); 222 } 223 }); 224 jLabel4.setText(Messages.getString("DefineRepositoryDialog.Location")); 225 _cbbManagerClass.setPreferredSize(new Dimension (130, 21)); 226 _cbbManagerClass.addActionListener(new java.awt.event.ActionListener () 227 { 228 public void actionPerformed(ActionEvent e) 229 { 230 _cbbManagerClass_actionPerformed(e); 231 } 232 }); 233 _edtURL.addFocusListener(new java.awt.event.FocusAdapter () 234 { 235 public void focusGained(FocusEvent e) 236 { 237 _edtURL_focusGained(e); 238 } 239 }); 240 jLabel2.setText(Messages.getString("DefineRepositoryDialog.SharedConnection")); 241 jLabel5.setText(Messages.getString("DefineRepositoryDialog.SharedUser")); 242 jLabel6.setText(Messages.getString("DefineRepositoryDialog.SharedPassword")); 243 jLabel7.setText(Messages.getString("DefineRepositoryDialog.TraceLevel")); 244 _edtUser.addFocusListener(new java.awt.event.FocusAdapter () 245 { 246 public void focusGained(FocusEvent e) 247 { 248 _edtUser_focusGained(e); 249 } 250 }); 251 getContentPane().add(mainPanel); 252 mainPanel.add(labelPanel, BorderLayout.WEST); 253 labelPanel.add(jLabel1, null); 254 labelPanel.add(jLabel4, null); 255 labelPanel.add(_lblDriver, null); 256 labelPanel.add(jLabel3, null); 257 labelPanel.add(jLabel2, null); 258 mainPanel.add(fieldsPanel, BorderLayout.CENTER); 259 fieldsPanel.add(_edtName, null); 260 fieldsPanel.add(_cbbManagerClass, null); 261 fieldsPanel.add(_cbbDriver, null); 262 fieldsPanel.add(_edtURL, null); 263 fieldsPanel.add(_chkSharedConnection, null); 264 mainPanel.add(buttonPanel, BorderLayout.SOUTH); 265 buttonPanel.add(jPanel4, BorderLayout.EAST); 266 jPanel4.add(_butCancel, null); 267 jPanel4.add(_butOk, null); 268 buttonPanel.add(jPanel5, BorderLayout.WEST); 269 jPanel5.add(_butTest, null); 270 labelPanel.add(jLabel5, null); 271 labelPanel.add(jLabel6, null); 272 labelPanel.add(jLabel7, null); 273 fieldsPanel.add(_edtUser, null); 274 fieldsPanel.add(_edtPassword, null); 275 fieldsPanel.add(_cbbTraceLevel, null); 276 Util.sizeButtons(buttonPanel); 277 Util.sizeLabels(labelPanel); 278 } 279 280 void _butCancel_actionPerformed(ActionEvent e) 281 { 282 setVisible(false); 283 } 284 285 void _butOk_actionPerformed(ActionEvent e) 286 { 287 if (_edtName.getText().trim().length() == 0) 288 { 289 JOptionPane.showMessageDialog(this, Messages.getString("DefineRepositoryDialog.NameRequired")); 290 return; 291 } 292 293 if (_chkSharedConnection.isSelected() 294 && (_edtUser.getText().trim().length() == 0 && _edtPassword.getText().trim().length() == 0)) 295 { 296 JOptionPane.showMessageDialog(this, Messages.getString("DefineRepositoryDialog.UserPasswordReqd")); 297 return; 298 } 299 _wasCancelled = false; 300 _repositoryDescriptor.setName(_edtName.getText()); 301 _repositoryDescriptor.setDriver(_cbbDriver.getEditor().getItem().toString()); 302 _repositoryDescriptor.setRepositoryLocation(getRepositoryLocation()); 303 _repositoryDescriptor.setUrl(_edtURL.getText()); 304 _repositoryDescriptor.setSharedConnection(_chkSharedConnection.isSelected()); 305 _repositoryDescriptor.setSharedUser(_edtUser.getText()); 306 _repositoryDescriptor.setSharedPassword(_edtPassword.getText()); 307 _repositoryDescriptor.setUrl(_edtURL.getText()); 308 _repositoryDescriptor.setTraceLevel(((TraceLevel) _cbbTraceLevel.getSelectedItem()).getLevel()); 309 310 setVisible(false); 311 } 312 313 318 public String getRepositoryLocation() 319 { 320 return _cbbManagerClass.getEditor().getItem().toString(); 321 } 322 323 328 public String getDriverLocation() 329 { 330 if (_cbbDriver.getSelectedItem() == null) return ""; 331 return _cbbDriver.getSelectedItem().toString(); 332 } 333 334 void _butTest_actionPerformed(ActionEvent e) 335 { 336 try 337 { 338 DjRepositoryDescriptor rd = new DjRepositoryDescriptor(_edtName.getText(), _cbbManagerClass.getSelectedItem() 339 .toString(), _cbbDriver.getEditor().getItem().toString(), _edtURL.getText()); 340 341 rd.setSharedConnection(_chkSharedConnection.isSelected()); 342 rd.setSharedUser(_edtUser.getText()); 343 rd.setSharedPassword(_edtPassword.getText()); 344 345 Connection conn = _administrator.getConnection(rd); 346 conn.close(); 347 JOptionPane.showMessageDialog(this, Messages.getString("DefineRepositoryDialog.ConnectionTestedOK")); 348 } 349 catch (Exception x) 350 { 351 DjLogger.log(x); 352 JOptionPane.showMessageDialog(this, x.getClass().getName() + ":\n" + x.getMessage(), Messages 353 .getString("DefineRepositoryDialog.ErrorDuringTest"), JOptionPane.ERROR_MESSAGE); 354 JOptionPane.showMessageDialog(this, Messages.getString("DefineRepositoryDialog.tips"), Messages 355 .getString("DefineRepositoryDialog.tipsTitle"), JOptionPane.INFORMATION_MESSAGE); 356 } 357 } 358 359 void _cbbManagerClass_actionPerformed(ActionEvent e) 360 { 361 } 366 367 void _edtURL_focusGained(FocusEvent e) 368 { 369 if (_edtURL.getText().trim().length() != 0) return; 370 371 String driver = getDriverLocation(); 372 373 if (driver.indexOf("OracleDriver") != -1) 374 { 375 _edtURL.setText("jdbc:oracle:thin:@<SERVER>:1521:<SID>"); 376 } 377 else if (driver.indexOf("DB2Driver") != -1) 378 { 379 _edtURL.setText("jdbc:db2:<DBNAME>"); 380 } 381 else if (driver.indexOf("hsqldb") != -1) 382 { 383 _edtURL.setText("jdbc:hsqldb:hsql://localhost"); 384 _edtUser.setText("sa"); 385 _edtPassword.setText(""); 386 } 387 else if (driver.indexOf("inet") != -1) 388 { 389 _edtURL.setText("jdbc:inetdae:<SERVER>?sql7=true&charset=Cp1250"); 390 } 391 else if (driver.indexOf("jtds") != -1) 392 { 393 _edtURL.setText("jdbc:jtds:sqlserver://<SERVER>/<DBNAME>;charset=Cp1250"); 394 } 395 else if (driver.indexOf("pointbase") != -1) 396 { 397 _edtURL.setText("jdbc:pointbase:server://localhost/<DBNAME>"); 398 _edtUser.setText("PBPUBLIC"); 399 _edtPassword.setText(""); 400 } 401 else if (driver.indexOf("JNDI") != -1) 402 { 403 _edtURL.setText("jdbc/<DBNAME>"); 404 _edtUser.setText("username"); 405 _edtPassword.setText(""); 406 } 407 } 408 409 void _edtUser_focusGained(FocusEvent e) 410 { 411 if (_edtUser.getText().trim().length() != 0) return; 412 String driver = getDriverLocation(); 413 414 if (driver.indexOf("hsqldb") != -1) 415 { 416 _edtUser.setText("sa"); 417 _edtPassword.setText(""); 418 } 419 else if (driver.indexOf("pointbase") != -1) 420 { 421 _edtUser.setText("PBPUBLIC"); 422 _edtPassword.setText(""); 423 } 424 425 } 426 } 427 428 class TraceLevel 429 { 430 431 int _level; 432 String _title; 433 434 442 public TraceLevel(int level, String title) 443 { 444 _level = level; 445 _title = title; 446 } 447 448 453 public String toString() 454 { 455 return _title; 456 } 457 458 463 public int getLevel() 464 { 465 return _level; 466 } 467 } | Popular Tags |