1 30 31 package com.genimen.djeneric.tools.administrator; 32 33 import java.awt.AWTEvent ; 34 import java.awt.BorderLayout ; 35 import java.awt.Dimension ; 36 import java.awt.Image ; 37 import java.awt.Toolkit ; 38 import java.awt.event.ActionEvent ; 39 import java.awt.event.MouseEvent ; 40 import java.awt.event.WindowEvent ; 41 import java.net.URL ; 42 import java.sql.Connection ; 43 import java.sql.DriverManager ; 44 import java.sql.SQLException ; 45 46 import javax.swing.BorderFactory ; 47 import javax.swing.DefaultComboBoxModel ; 48 import javax.swing.ImageIcon ; 49 import javax.swing.JButton ; 50 import javax.swing.JLabel ; 51 import javax.swing.JList ; 52 import javax.swing.JMenu ; 53 import javax.swing.JMenuBar ; 54 import javax.swing.JMenuItem ; 55 import javax.swing.JOptionPane ; 56 import javax.swing.JPanel ; 57 import javax.swing.JScrollPane ; 58 import javax.swing.border.Border ; 59 60 import com.genimen.djeneric.language.Messages; 61 import com.genimen.djeneric.repository.DjPersistenceManager; 62 import com.genimen.djeneric.repository.DjPersistenceManagerFactory; 63 import com.genimen.djeneric.repository.DjRepositoryDescriptor; 64 import com.genimen.djeneric.repository.exceptions.DjenericException; 65 import com.genimen.djeneric.repository.exceptions.LogonException; 66 import com.genimen.djeneric.tools.administrator.helpers.AdminPanel; 67 import com.genimen.djeneric.tools.administrator.helpers.DefineRepositoryDialog; 68 import com.genimen.djeneric.tools.administrator.helpers.DescriptorWizard; 69 import com.genimen.djeneric.tools.common.AboutDialog; 70 import com.genimen.djeneric.tools.common.DjenericTool; 71 import com.genimen.djeneric.tools.common.LoginDialog; 72 import com.genimen.djeneric.ui.DjTabbedPane; 73 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 74 import com.genimen.djeneric.util.DjEnvironment; 75 import com.genimen.djeneric.util.DjLogPrintStream; 76 import com.genimen.djeneric.util.DjLogger; 77 import com.genimen.djeneric.util.DjVersion; 78 79 public class Administrator extends DjenericTool 80 { 81 private static final long serialVersionUID = 1L; 82 83 private final String PROPERTIES_FILE_NAME = DjEnvironment.getAbsoluteFileName("administrator.properties"); 84 private final static String LOG_FILE_NAME = DjEnvironment.getAbsoluteFileName("administrator.log"); 85 86 public final static String OK_MSG = Messages.getString("global.Ok"); 87 BorderLayout borderLayout1 = new BorderLayout (); 88 JPanel _pnlStatus = new JPanel (); 89 JLabel _lblStatus = new JLabel (); 90 BorderLayout borderLayout2 = new BorderLayout (); 91 Border border1; 92 DjTabbedPane _tabMain = new DjTabbedPane(); 93 String _configFileName; 94 DefaultComboBoxModel _repositoryModel; 95 DjPersistenceManagerFactory _fact; 96 JMenuBar jMenuBar1 = new JMenuBar (); 97 JMenu jMenu1 = new JMenu (); 98 JMenuItem jMenuItem1 = new JMenuItem (); 99 JMenu jMenu2 = new JMenu (); 100 JMenuItem jMenuItem2 = new JMenuItem (); 101 JMenu jMenu3 = new JMenu (); 102 JMenuItem jMenuItem3 = new JMenuItem (); 103 JMenu jMenu4 = new JMenu (); 104 JMenuItem _mnuNextTab = new JMenuItem (); 105 JMenuItem _mnuPrevTab = new JMenuItem (); 106 JMenuItem _mnuCloseTab = new JMenuItem (); 107 JPanel _pnlRepositories = new JPanel (); 108 JButton _butCopyRepos = new JButton (); 109 JButton _butAdmin = new JButton (); 110 JScrollPane _scrRepositories = new JScrollPane (); 111 JButton _butDeleteRepos = new JButton (); 112 JButton _butEditRepos = new JButton (); 113 BorderLayout borderLayout4 = new BorderLayout (); 114 DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout(); 115 JList _lstRepositories = new JList (); 116 JButton _butCreateRepos = new JButton (); 117 JPanel jPanel4 = new JPanel (); 118 JLabel jLabel1 = new JLabel (); 119 120 public Administrator(String configFileName) 121 { 122 enableEvents(AWTEvent.WINDOW_EVENT_MASK); 123 try 124 { 125 setStatusLabel(_lblStatus); 126 _configFileName = configFileName; 127 128 jbInit(); 129 this.setIconImage(getImage("admin.gif")); 130 _fact = new DjPersistenceManagerFactory(this, configFileName); 131 132 _repositoryModel = new DefaultComboBoxModel (_fact.getRepositoryDescriptors()); 133 _lstRepositories.setModel(_repositoryModel); 134 if (_repositoryModel.getSize() > 0) 135 { 136 _lstRepositories.setSelectedIndex(0); 137 } 138 139 _butEditRepos.setIcon(getImageIcon("edit.gif")); 140 _butCreateRepos.setIcon(getImageIcon("new.gif")); 141 _butDeleteRepos.setIcon(getImageIcon("delete.gif")); 142 _butAdmin.setIcon(getImageIcon("admin.gif")); 143 _butCopyRepos.setIcon(getImageIcon("copy.gif")); 144 145 loadProps(PROPERTIES_FILE_NAME); 146 147 int width = Integer.parseInt(getProperty("window.width", "400")); 148 int height = Integer.parseInt(getProperty("window.height", "400")); 149 setSize(new Dimension (width, height)); 150 151 } 152 catch (Exception e) 153 { 154 handleException(e); 155 System.exit(-1); 156 } 157 } 158 159 public String getConfigFileName() 160 { 161 return _configFileName; 162 } 163 164 170 private void jbInit() throws Exception 171 { 172 setTitle("Djeneric Administrator v" + DjVersion.getVersion()); 173 border1 = BorderFactory.createEmptyBorder(0, 5, 0, 0); 174 this.getContentPane().setLayout(borderLayout1); 175 _lblStatus.setText(Messages.getString("global.Ok")); 176 _pnlStatus.setLayout(borderLayout2); 177 _pnlStatus.setBorder(border1); 178 jMenu1.setMnemonic('F'); 179 jMenu1.setText(Messages.getString("global.File")); 180 jMenuItem1.setMnemonic('X'); 181 jMenuItem1.setText(Messages.getString("global.Exit")); 182 jMenuItem1.addActionListener(new java.awt.event.ActionListener () 183 { 184 public void actionPerformed(ActionEvent e) 185 { 186 mnuExit(e); 187 } 188 }); 189 jMenu2.setMnemonic('T'); 190 jMenu2.setText(Messages.getString("global.Tools")); 191 jMenuItem2.setActionCommand(Messages.getString("Administrator.KillModelLock1")); 192 jMenuItem2.setMnemonic('K'); 193 jMenuItem2.setText(Messages.getString("Administrator.KillModelLock")); 194 jMenuItem2.addActionListener(new java.awt.event.ActionListener () 195 { 196 public void actionPerformed(ActionEvent e) 197 { 198 mnuKillModelLock(e); 199 } 200 }); 201 jMenu3.setMnemonic('H'); 202 jMenu3.setText(Messages.getString("global.Help")); 203 jMenuItem3.setText(Messages.getString("global.About")); 204 jMenuItem3.addActionListener(new java.awt.event.ActionListener () 205 { 206 public void actionPerformed(ActionEvent e) 207 { 208 mnuAbout(e); 209 } 210 }); 211 jMenu4.setMnemonic('A'); 212 jMenu4.setText(Messages.getString("global.Tabs")); 213 _mnuNextTab.setText(Messages.getString("global.NextTab")); 214 _mnuNextTab.setAccelerator(javax.swing.KeyStroke.getKeyStroke(117, java.awt.event.KeyEvent.CTRL_MASK, false)); 215 _mnuNextTab.addActionListener(new java.awt.event.ActionListener () 216 { 217 public void actionPerformed(ActionEvent e) 218 { 219 mnuNextTab(e); 220 } 221 }); 222 _mnuPrevTab.setText(Messages.getString("global.PreviousTab")); 223 _mnuPrevTab.setAccelerator(javax.swing.KeyStroke.getKeyStroke(117, java.awt.event.KeyEvent.CTRL_MASK 224 | java.awt.event.KeyEvent.SHIFT_MASK, false)); 225 _mnuPrevTab.addActionListener(new java.awt.event.ActionListener () 226 { 227 public void actionPerformed(ActionEvent e) 228 { 229 mnuPrevTab(e); 230 } 231 }); 232 _mnuCloseTab.setText(Messages.getString("global.CloseTab")); 233 _mnuCloseTab.setAccelerator(javax.swing.KeyStroke.getKeyStroke(115, java.awt.event.KeyEvent.CTRL_MASK, false)); 234 _mnuCloseTab.addActionListener(new java.awt.event.ActionListener () 235 { 236 public void actionPerformed(ActionEvent e) 237 { 238 mnuCloseTab(e); 239 } 240 }); 241 _pnlRepositories.setLayout(borderLayout4); 242 _butCopyRepos.addActionListener(new java.awt.event.ActionListener () 243 { 244 public void actionPerformed(ActionEvent e) 245 { 246 _butCopyRepos_actionPerformed(e); 247 } 248 }); 249 _butCopyRepos.setToolTipText(Messages.getString("Administrator.DuplicateRepositoryDescriptor")); 250 _butCopyRepos.setPreferredSize(new Dimension (24, 24)); 251 _butAdmin.addActionListener(new java.awt.event.ActionListener () 252 { 253 public void actionPerformed(ActionEvent e) 254 { 255 _butAdmin_actionPerformed(e); 256 } 257 }); 258 _butAdmin.setPreferredSize(new Dimension (24, 24)); 259 _butAdmin.setToolTipText(Messages.getString("Administrator.AdministerRepository")); 260 _butDeleteRepos.setPreferredSize(new Dimension (24, 24)); 261 _butDeleteRepos.setToolTipText(Messages.getString("global.DelDescriptorTitle")); 262 _butDeleteRepos.addActionListener(new java.awt.event.ActionListener () 263 { 264 public void actionPerformed(ActionEvent e) 265 { 266 _butDeleteRepos_actionPerformed(e); 267 } 268 }); 269 _butEditRepos.setPreferredSize(new Dimension (24, 24)); 270 _butEditRepos.setToolTipText(Messages.getString("Administrator.EditRepositoryDescriptor")); 271 _butEditRepos.addActionListener(new java.awt.event.ActionListener () 272 { 273 public void actionPerformed(ActionEvent e) 274 { 275 _butEditRepos_actionPerformed(e); 276 } 277 }); 278 _lstRepositories.addMouseListener(new java.awt.event.MouseAdapter () 279 { 280 public void mouseClicked(MouseEvent e) 281 { 282 _lstRepositories_mouseClicked(e); 283 } 284 }); 285 _butCreateRepos.setPreferredSize(new Dimension (24, 24)); 286 _butCreateRepos.setToolTipText(Messages.getString("Administrator.CreateRepositoryDescriptor")); 287 _butCreateRepos.addActionListener(new java.awt.event.ActionListener () 288 { 289 public void actionPerformed(ActionEvent e) 290 { 291 _butCreateRepos_actionPerformed(e); 292 } 293 }); 294 jPanel4.setLayout(verticalFlowLayout2); 295 _scrRepositories.setPreferredSize(new Dimension (200, 132)); 296 _pnlRepositories.setPreferredSize(new Dimension (250, 150)); 297 jLabel1.setPreferredSize(new Dimension (5, 5)); 298 _pnlStatus.add(_lblStatus, BorderLayout.CENTER); 299 this.getContentPane().add(_pnlRepositories, BorderLayout.WEST); 300 _pnlRepositories.add(_scrRepositories, BorderLayout.CENTER); 301 _pnlRepositories.add(jPanel4, BorderLayout.WEST); 302 _scrRepositories.getViewport().add(_lstRepositories, null); 303 jPanel4.add(_butAdmin, null); 304 jPanel4.add(jLabel1, null); 305 jPanel4.add(_butEditRepos, null); 306 jPanel4.add(_butCopyRepos, null); 307 jPanel4.add(_butCreateRepos, null); 308 jPanel4.add(_butDeleteRepos, null); 309 this.getContentPane().add(_tabMain, BorderLayout.CENTER); 310 this.getContentPane().add(_pnlStatus, BorderLayout.SOUTH); 311 jMenuBar1.add(jMenu1); 312 jMenuBar1.add(jMenu2); 313 jMenuBar1.add(jMenu4); 314 jMenuBar1.add(jMenu3); 315 jMenu1.add(jMenuItem1); 316 jMenu2.add(jMenuItem2); 317 jMenu3.add(jMenuItem3); 318 jMenu4.add(_mnuNextTab); 319 jMenu4.add(_mnuPrevTab); 320 jMenu4.add(_mnuCloseTab); 321 setJMenuBar(jMenuBar1); 322 } 323 324 protected void processWindowEvent(WindowEvent e) 325 { 326 try 327 { 328 if (e.getID() == WindowEvent.WINDOW_CLOSING) 329 { 330 exitProgram(); 331 } 332 else super.processWindowEvent(e); 333 334 } 335 catch (Exception x) 336 { 337 setStatusMessage(x); 338 } 339 } 340 341 protected void updateProperties() 342 { 343 setProperty("window.width", "" + getWidth()); 344 setProperty("window.height", "" + getHeight()); 345 } 346 347 public static ImageIcon getImageIcon(String fileName) 348 { 349 URL url = Administrator.class.getResource("images/" + fileName); 350 if (url != null) return new ImageIcon (url); 351 else return new ImageIcon (""); 352 } 354 355 public static Image getImage(String fileName) 356 { 357 URL url = Administrator.class.getResource("images/" + fileName); 358 if (url != null) return Toolkit.getDefaultToolkit().getImage(url); 359 360 else return Toolkit.getDefaultToolkit().getImage(""); 361 } 362 363 void _butEditRepos_actionPerformed(ActionEvent e) 364 { 365 try 366 { 367 if (_lstRepositories.getSelectedIndex() != -1) 368 { 369 DjRepositoryDescriptor rd = (DjRepositoryDescriptor) _lstRepositories.getSelectedValue(); 370 DefineRepositoryDialog dlg = new DefineRepositoryDialog(this, Messages 371 .getString("Administrator.EditRepositoryDescriptor"), rd); 372 if (!dlg.wasCancelled()) 373 { 374 _fact.saveRepositoryDescriptors(_configFileName); 375 setStatusMessage(Messages.getString("Administrator.DefsWrittenTo", _configFileName)); 376 } 377 updateRepositoryList(rd); 378 } 379 } 380 catch (Exception x) 381 { 382 setStatusMessage(x); 383 } 384 385 } 386 387 void updateRepositoryList(DjRepositoryDescriptor selectThis) 388 { 389 _repositoryModel = new DefaultComboBoxModel (_fact.getRepositoryDescriptors()); 390 _lstRepositories.setModel(_repositoryModel); 391 for (int i = 0; i < _repositoryModel.getSize(); i++) 392 { 393 if (_repositoryModel.getElementAt(i).equals(selectThis)) 394 { 395 _lstRepositories.setSelectedIndex(i); 396 break; 397 } 398 } 399 } 400 401 void _butCreateRepos_actionPerformed(ActionEvent e) 402 { 403 try 404 { 405 DescriptorWizard dw = new DescriptorWizard(this); 406 if (!dw.wasCancelled()) 407 { 408 DjRepositoryDescriptor rd = new DjRepositoryDescriptor(dw.getName(), 409 DjPersistenceManagerFactory.TYPE_POLYMORPH, dw.getDriver(), dw.getUrl()); 410 rd.setSharedUser(dw.getUser()); 411 rd.setSharedPassword(dw.getPassword()); 412 rd.setSharedConnection(true); 413 DefineRepositoryDialog dlg = new DefineRepositoryDialog(this, Messages 414 .getString("Administrator.CreateRepositoryDescriptor"), rd); 415 if (!dlg.wasCancelled()) 416 { 417 _fact.addRepositoryDescriptor(rd); 418 _fact.saveRepositoryDescriptors(_configFileName); 419 420 updateRepositoryList(rd); 421 } 422 } 423 } 424 catch (Exception x) 425 { 426 setStatusMessage(x); 427 } 428 429 } 430 431 void _butDeleteRepos_actionPerformed(ActionEvent e) 432 { 433 try 434 { 435 if (_lstRepositories.getSelectedIndex() != -1) 436 { 437 DjRepositoryDescriptor rd = (DjRepositoryDescriptor) _lstRepositories.getSelectedValue(); 438 int result = JOptionPane.showOptionDialog(this, 439 Messages.getString("Administrator.DelDescriptor", rd.getName()), 440 Messages.getString("global.DelDescriptorTitle"), 441 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, 442 new String []{Messages.getString("global.Cancel"), 443 Messages.getString("global.Delete")}, null); 444 if (result != 1) 445 { 446 return; 447 } 448 449 _fact.removeRepositoryDescriptor(rd); 450 _fact.saveRepositoryDescriptors(_configFileName); 451 452 int idx = _lstRepositories.getSelectedIndex(); 453 if (idx > 0) idx--; 454 _repositoryModel = new DefaultComboBoxModel (_fact.getRepositoryDescriptors()); 455 _lstRepositories.setModel(_repositoryModel); 456 if (_repositoryModel.getSize() > 0) _lstRepositories.setSelectedIndex(idx); 457 } 458 } 459 catch (Exception x) 460 { 461 setStatusMessage(x); 462 } 463 464 } 465 466 void _lstRepositories_mouseClicked(MouseEvent e) 467 { 468 if (e.getClickCount() > 1) _butAdmin_actionPerformed(null); 469 } 470 471 void _butAdmin_actionPerformed(ActionEvent e) 472 { 473 try 474 { 475 if (_lstRepositories.getSelectedIndex() != -1) 476 { 477 DjRepositoryDescriptor rd = (DjRepositoryDescriptor) _lstRepositories.getSelectedValue(); 478 setStatusMessageNow(Messages.getString("Administrator.Connecting", rd.toString()), true); 479 AdminPanel ap = new AdminPanel(this, rd); 480 _tabMain.addTab(rd.getName(), getImageIcon("schema.gif"), ap); 481 _tabMain.setSelectedComponent(ap); 482 setStatusMessage(OK_MSG); 483 } 484 } 485 catch (Exception de) 486 { 487 handleException(de); 488 } 489 } 490 491 void mnuExit(ActionEvent e) 492 { 493 try 494 { 495 exitProgram(); 496 } 497 catch (Exception x) 498 { 499 setStatusMessage(x); 500 } 501 } 502 503 void mnuKillModelLock(ActionEvent e) 504 { 505 try 506 { 507 DjPersistenceManager mgr = _fact.createInstance(); 508 String tag = mgr.getCurrentLockTag(); 509 if (tag == null) 510 { 511 setStatusMessage(Messages.getString("Administrator.NoLockActive")); 512 return; 513 } 514 515 int result = JOptionPane.showOptionDialog(this, Messages.getString("Administrator.AboutToKillLock", tag), 516 Messages.getString("Administrator.AboutToKillLockTitle"), 517 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, 518 new String []{Messages.getString("global.Cancel"), 519 Messages.getString("Administrator.KillLock")}, null); 520 if (result == 1) mgr.forceUnlockModel(); 521 setStatusMessage(Messages.getString("Administrator.LockKilled")); 522 mgr.close(); 523 } 524 catch (Exception x) 525 { 526 setStatusMessage(x); 527 } 528 } 529 530 void _butCopyRepos_actionPerformed(ActionEvent e) 531 { 532 try 533 { 534 if (_lstRepositories.getSelectedIndex() != -1) 535 { 536 DjRepositoryDescriptor rd = (DjRepositoryDescriptor) _lstRepositories.getSelectedValue(); 537 DjRepositoryDescriptor nrd = (DjRepositoryDescriptor) rd.clone(); 538 nrd.setName(Messages.getString("global.CopyOf", nrd.getName())); 539 540 _fact.addRepositoryDescriptor(nrd); 541 _fact.saveRepositoryDescriptors(_configFileName); 542 543 updateRepositoryList(nrd); 544 } 545 } 546 catch (Exception x) 547 { 548 setStatusMessage(x); 549 } 550 } 551 552 void mnuAbout(ActionEvent e) 553 { 554 new AboutDialog(this); 555 } 556 557 void mnuNextTab(ActionEvent e) 558 { 559 int idx = _tabMain.getSelectedIndex(); 560 if (idx < _tabMain.getComponentCount() - 1) idx++; 561 else idx = 0; 562 _tabMain.setSelectedIndex(idx); 563 } 564 565 void mnuCloseTab(ActionEvent e) 566 { 567 if (_tabMain.getSelectedComponent() instanceof AdminPanel) 568 { 569 AdminPanel editor = (AdminPanel) _tabMain.getSelectedComponent(); 570 if (editor != null) 571 { 572 editor.close(); 573 } 574 } 575 576 } 577 578 public void removeTab(AdminPanel panel) 579 { 580 _tabMain.remove(panel); 581 } 582 583 void mnuPrevTab(ActionEvent e) 584 { 585 int idx = _tabMain.getSelectedIndex(); 586 if (idx > 0) idx--; 587 else idx = _tabMain.getComponentCount() - 1; 588 _tabMain.setSelectedIndex(idx); 589 } 590 591 void _lstRepositories_mousePressed(MouseEvent e) 592 { 593 594 } 595 596 void _lstRepositories_mouseReleased(MouseEvent e) 597 { 598 599 } 600 601 void _lstRepositories_mouseEntered(MouseEvent e) 602 { 603 604 } 605 606 void _lstRepositories_mouseExited(MouseEvent e) 607 { 608 609 } 610 611 public Connection getConnection(DjRepositoryDescriptor repositoryDescriptor) throws LogonException, 612 DjenericException, ClassNotFoundException , SQLException 613 { 614 String username; 615 String password; 616 617 if (repositoryDescriptor.isSharedConnection()) 618 { 619 username = repositoryDescriptor.getSharedUser(); 620 password = repositoryDescriptor.getSharedPassword(); 621 } 622 else 623 { 624 LoginDialog lid = new LoginDialog(this, repositoryDescriptor); 625 if (lid.isCancelled()) throw new DjenericException(Messages.getString("Administrator.CreationAborted")); 626 username = lid.getUserId(); 627 password = lid.getPassword(); 628 } 629 630 Class.forName(repositoryDescriptor.getDriver()); 631 Connection conn = DriverManager.getConnection(repositoryDescriptor.getUrl(), username, password); 632 633 return conn; 634 } 635 636 public static void main(String [] args) 637 { 638 try 639 { 640 DjenericTool.setLookAndFeel(); 641 642 if (args.length < 1) 643 { 644 System.out.println("Usage: java " + Administrator.class.getName() + " <repositories.xml>"); 645 return; 646 } 647 String banner = Administrator.class.getName() + " version " + DjVersion.getVersion(); 648 DjLogPrintStream.logAll(banner, LOG_FILE_NAME); 649 650 System.out.println(Messages.getString("global.Reading", args[0])); 651 new Administrator(args[0]).startApp(); 652 } 653 catch (Exception e) 654 { 655 DjLogger.log(e); 656 } 657 } 658 659 } | Popular Tags |