1 30 31 package com.genimen.djeneric.tools.specifier; 32 33 import java.awt.AWTEvent ; 34 import java.awt.BorderLayout ; 35 import java.awt.Color ; 36 import java.awt.Component ; 37 import java.awt.Dimension ; 38 import java.awt.Image ; 39 import java.awt.SystemColor ; 40 import java.awt.Toolkit ; 41 import java.awt.event.ActionEvent ; 42 import java.awt.event.ActionListener ; 43 import java.awt.event.InputEvent ; 44 import java.awt.event.KeyEvent ; 45 import java.awt.event.MouseEvent ; 46 import java.awt.event.WindowEvent ; 47 import java.io.BufferedWriter ; 48 import java.io.File ; 49 import java.io.FileInputStream ; 50 import java.io.FileNotFoundException ; 51 import java.io.FileWriter ; 52 import java.io.IOException ; 53 import java.io.InputStream ; 54 import java.net.URL ; 55 import java.util.ArrayList ; 56 import java.util.HashMap ; 57 import java.util.Iterator ; 58 import java.util.StringTokenizer ; 59 60 import javax.swing.AbstractAction ; 61 import javax.swing.ActionMap ; 62 import javax.swing.ImageIcon ; 63 import javax.swing.InputMap ; 64 import javax.swing.JButton ; 65 import javax.swing.JComponent ; 66 import javax.swing.JFileChooser ; 67 import javax.swing.JLabel ; 68 import javax.swing.JMenu ; 69 import javax.swing.JMenuBar ; 70 import javax.swing.JMenuItem ; 71 import javax.swing.JOptionPane ; 72 import javax.swing.JPanel ; 73 import javax.swing.JPopupMenu ; 74 import javax.swing.JScrollPane ; 75 import javax.swing.JSplitPane ; 76 import javax.swing.KeyStroke ; 77 import javax.swing.event.TreeExpansionEvent ; 78 import javax.swing.event.TreeSelectionEvent ; 79 import javax.swing.tree.DefaultMutableTreeNode ; 80 import javax.swing.tree.DefaultTreeModel ; 81 import javax.swing.tree.ExpandVetoException ; 82 import javax.swing.tree.TreePath ; 83 84 import com.genimen.djeneric.language.Messages; 85 import com.genimen.djeneric.repository.DjExtent; 86 import com.genimen.djeneric.repository.DjList; 87 import com.genimen.djeneric.repository.DjModelView; 88 import com.genimen.djeneric.repository.DjObject; 89 import com.genimen.djeneric.repository.DjPersistenceManager; 90 import com.genimen.djeneric.repository.DjPersistenceManagerFactory; 91 import com.genimen.djeneric.repository.DjSession; 92 import com.genimen.djeneric.repository.DjUid; 93 import com.genimen.djeneric.repository.exceptions.DjenericException; 94 import com.genimen.djeneric.repository.util.DjObjectCloner; 95 import com.genimen.djeneric.repository.util.DjObjectExporter; 96 import com.genimen.djeneric.repository.util.DjObjectImporter; 97 import com.genimen.djeneric.repository.util.ImportAbortedException; 98 import com.genimen.djeneric.structure.EditorDefinition; 99 import com.genimen.djeneric.structure.ExtentUsage; 100 import com.genimen.djeneric.structure.ResourceDefinition; 101 import com.genimen.djeneric.structure.ScriptDefinition; 102 import com.genimen.djeneric.tools.common.AboutDialog; 103 import com.genimen.djeneric.tools.common.DjenericTool; 104 import com.genimen.djeneric.tools.common.ModelViewDialog; 105 import com.genimen.djeneric.tools.io.DjenericDocumentHandler; 106 import com.genimen.djeneric.tools.specifier.dialogs.ExportOptionsDialog; 107 import com.genimen.djeneric.tools.specifier.dialogs.ImportOptionsDialog; 108 import com.genimen.djeneric.tools.specifier.dialogs.ImportResultDialog; 109 import com.genimen.djeneric.tools.specifier.dialogs.UidResolverDialog; 110 import com.genimen.djeneric.tools.specifier.editor.EditorPanel; 111 import com.genimen.djeneric.tools.specifier.editor.ScriptRunnerPanel; 112 import com.genimen.djeneric.tools.specifier.interfaces.DjenericEditorEnvironment; 113 import com.genimen.djeneric.tools.specifier.interfaces.ObjectEditor; 114 import com.genimen.djeneric.tools.specifier.interfaces.SpecifierPanel; 115 import com.genimen.djeneric.tools.specifier.menusystem.MenuExtensionAction; 116 import com.genimen.djeneric.tools.specifier.menusystem.MenuExtensionDescr; 117 import com.genimen.djeneric.tools.specifier.menusystem.MenuFactory; 118 import com.genimen.djeneric.tools.specifier.tree.DjenericFolder; 119 import com.genimen.djeneric.tools.specifier.tree.DjenericNode; 120 import com.genimen.djeneric.tools.specifier.tree.DjenericTree; 121 import com.genimen.djeneric.tools.specifier.tree.DjenericTreeCellRenderer; 122 import com.genimen.djeneric.tools.specifier.tree.DjenericTreeNode; 123 import com.genimen.djeneric.ui.DjCloseable; 124 import com.genimen.djeneric.ui.DjTabbedPane; 125 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 126 import com.genimen.djeneric.ui.Util; 127 import com.genimen.djeneric.util.DjEnvironment; 128 import com.genimen.djeneric.util.DjFileFilter; 129 import com.genimen.djeneric.util.DjLogPrintStream; 130 import com.genimen.djeneric.util.DjLogger; 131 import com.genimen.djeneric.util.DjResourceClassLoader; 132 import com.genimen.djeneric.util.DjStringReplacer; 133 import com.genimen.djeneric.util.DjVersion; 134 135 public class Specifier extends DjenericTool implements DjenericEditorEnvironment 136 { 137 private static final long serialVersionUID = 1L; 138 private static final String DJENERICMENU_XML = "djenericmenu.xml"; 139 private static final String EXPORTDIR = "exportdir"; 140 protected final String PROPERTIES_FILE_NAME = DjEnvironment.getAbsoluteFileName("specifier.properties"); 141 protected final static String LOG_FILE_NAME = DjEnvironment.getAbsoluteFileName("specifier.log"); 142 public final static String OK_MSG = Messages.getString("global.Ok"); 143 JSplitPane _splitpane = new JSplitPane (); 144 JPanel jPanel1 = new JPanel (); 145 BorderLayout borderLayout1 = new BorderLayout (); 146 JPanel jPanel2 = new JPanel (); 147 JButton _butExpandAll = new JButton (); 148 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 149 JScrollPane jScrollPane1 = new JScrollPane (); 150 DjenericTree _tree = new DjenericTree(); 151 JButton _butCollapseAll = new JButton (); 152 JButton _butDelete = new JButton (); 153 JButton _butCopy = new JButton (); 154 JButton _butExport = new JButton (); 155 JButton _butImport = new JButton (); 156 JButton _butCreate = new JButton (); 157 JButton _butEdit = new JButton (); 158 JButton _butReload = new JButton (); 159 DjPersistenceManager _mgr; 160 DjSession _session; 161 DjenericFolder _root = new DjenericFolder(); 162 DefaultTreeModel _treeModel = new DefaultTreeModel (_root); 163 DjenericTreeCellRenderer _treeCellRendrer = new DjenericTreeCellRenderer(); 164 JLabel _lblStatus = new JLabel (); 165 ExtentUsage[] navigatorStructure = null; 166 JPanel _desktopPanel = new JPanel (); 167 BorderLayout borderLayout2 = new BorderLayout (); 168 DjTabbedPane _tabs = new DjTabbedPane(); 169 JButton _butFilter = new JButton (); 170 JLabel jLabel1 = new JLabel (); 171 JLabel jLabel2 = new JLabel (); 172 JLabel jLabel3 = new JLabel (); 173 EditorDefinition[] _editors; 174 HashMap _resources = new HashMap (); 175 HashMap _cachedExtentions = new HashMap (); 176 177 DjModelView _selectedView; 178 179 public Specifier(String repositories) 180 { 181 enableEvents(AWTEvent.WINDOW_EVENT_MASK); 182 setIconImage(getImage("specifier.gif")); 183 try 184 { 185 loadProps(PROPERTIES_FILE_NAME); 186 setStatusLabel(_lblStatus); 187 setupPersistenceManager(repositories); 188 setupSession(); 189 setupContext(); 190 jbInit(); 191 _selectedView = loadView(); 192 DjenericDocumentHandler handler = _selectedView.getDocumentHandler(); 193 setupMenu(handler); 194 setupButtons(); 195 setupWindow(); 196 setupNavigator(handler); 197 setupEditors(handler); 198 setupResources(handler); 199 } 200 catch (Exception e) 201 { 202 handleException(e); 203 System.exit(-1); 204 } 205 } 206 207 private void setupMenu(DjenericDocumentHandler handler) throws DjenericException 208 { 209 JMenuBar menuBar = new JMenuBar (); 210 JMenu mnuFile = new JMenu (); 211 JMenuItem mnuOpen = new JMenuItem (); 212 JMenuItem mnuExit = new JMenuItem (); 213 JMenu mnuHelp = new JMenu (); 214 JMenuItem mnuAbout = new JMenuItem (); 215 JMenuItem mnuReloadAll = new JMenuItem (); 216 mnuReloadAll.setText(Messages.getString("Specifier.ReloadView")); 217 mnuFile.setText(Messages.getString("global.File")); 218 mnuOpen.setText(Messages.getString("ModelEditor.OpenView")); 219 mnuExit.setText(Messages.getString("global.Exit")); 220 mnuHelp.setText(Messages.getString("global.Help")); 221 mnuAbout.setText(Messages.getString("Specifier.About")); 222 mnuReloadAll.setAccelerator(javax.swing.KeyStroke.getKeyStroke(KeyEvent.VK_F12, 0, false)); 223 mnuOpen.setMnemonic('O'); 224 mnuOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(79, java.awt.event.KeyEvent.CTRL_MASK, false)); 225 mnuReloadAll.addActionListener(new java.awt.event.ActionListener () 226 { 227 public void actionPerformed(ActionEvent e) 228 { 229 mnuReloadView(e); 230 } 231 }); 232 mnuExit.addActionListener(new java.awt.event.ActionListener () 233 { 234 public void actionPerformed(ActionEvent e) 235 { 236 mnuExit(e); 237 } 238 }); 239 mnuOpen.addActionListener(new java.awt.event.ActionListener () 240 { 241 public void actionPerformed(ActionEvent e) 242 { 243 mnuOpen(e); 244 } 245 }); 246 mnuAbout.addActionListener(new java.awt.event.ActionListener () 247 { 248 public void actionPerformed(ActionEvent e) 249 { 250 mnuAbout(e); 251 } 252 }); 253 menuBar.add(mnuFile); 254 255 InputStream is = getResourceAsStream(DJENERICMENU_XML); 256 if (is != null) 257 { 258 MenuFactory factory = new MenuFactory(this); 259 try 260 { 261 JMenu [] menus = factory.createMenus(is); 262 for (int i = 0; i < menus.length; i++) 263 menuBar.add(menus[i]); 264 } 265 catch (Exception e) 266 { 267 setStatusMessage(e); 268 } 269 } 270 menuBar.add(mnuHelp); 271 mnuFile.add(mnuOpen); 272 mnuFile.add(mnuReloadAll); 273 mnuFile.add(mnuExit); 274 mnuHelp.add(mnuAbout); 275 ScriptDefinition[] scripts = handler.getScripts(this); 276 for (int i = 0; i < scripts.length; i++) 277 { 278 String menuName = Messages.getString("Specifier.Actions"); 279 String menuItemName = scripts[i].getTitle(); 280 int idx = menuItemName.lastIndexOf('|'); 281 if (idx != -1) 282 { 283 menuName = menuItemName.substring(0, idx).trim(); 284 menuItemName = menuItemName.substring(idx + 1).trim(); 285 if (menuItemName.length() == 0) menuItemName = Messages.getString("Specifier.UnnamedAction"); 286 } 287 StringTokenizer stok = new StringTokenizer (menuName, "|"); 288 JMenu menu = getMenu(menuBar, stok.nextToken()); 289 while (stok.hasMoreTokens()) 290 { 291 menu = getMenu(menu, stok.nextToken()); 292 } 293 JMenuItem menuItem = new JMenuItem (); 294 menuItem.setText(menuItemName); 295 menu.add(menuItem); 296 menuItem.addActionListener(new ScriptAction(this, scripts[i].getCode())); 297 } 298 setJMenuBar(menuBar); 299 invalidate(); 300 validate(); 301 } 302 303 protected void mnuOpen(ActionEvent e) 304 { 305 try 306 { 307 if (isAtLeastOneEditorOpen()) throw new DjenericException(Messages.getString("Specifier.CloseEditorsFirst")); 308 _selectedView = loadView(); 309 reloadViewDefinition(); 310 } 311 catch (DjenericException e1) 312 { 313 setStatusMessage(e1); 314 } 315 } 316 317 protected void mnuReloadView(ActionEvent e) 318 { 319 try 320 { 321 reloadViewDefinition(); 322 setStatusMessage(Messages.getString("Specifier.ViewReloaded"), true); 323 } 324 catch (DjenericException e1) 325 { 326 setStatusMessage(e1); 327 } 328 } 329 330 private JMenu getMenu(JMenu ownerMenu, String menuItemName) 331 { 332 for (int i = 0; i < ownerMenu.getMenuComponentCount(); i++) 333 { 334 if (!(ownerMenu.getMenuComponent(i) instanceof JMenu )) continue; 335 JMenu menu = (JMenu ) ownerMenu.getMenuComponent(i); 336 if (menu.getText().equalsIgnoreCase(menuItemName)) return menu; 337 } 338 JMenu menu = new JMenu (); 339 menu.setText(menuItemName); 340 ownerMenu.add(menu, ownerMenu.getMenuComponentCount()); 341 return menu; 342 } 343 344 private JMenu getMenu(JMenuBar bar, String menuName) 345 { 346 for (int i = 0; i < bar.getMenuCount(); i++) 347 { 348 JMenu menu = bar.getMenu(i); 349 if (menu.getText().equalsIgnoreCase(menuName)) return menu; 350 } 351 JMenu menu = new JMenu (); 352 menu.setText(menuName); 353 bar.add(menu, bar.getMenuCount() - 1); 355 return menu; 356 } 357 358 protected void setupPersistenceManager(String repositories) throws DjenericException, FileNotFoundException 359 { 360 DjPersistenceManagerFactory fact = new DjPersistenceManagerFactory(this, repositories); 361 _mgr = fact.createInstance(); 362 } 363 364 protected void setupSession() throws DjenericException 365 { 366 _session = _mgr.createSession(); 367 _session.setValidateObjectsAfterLoading(false); 368 } 369 370 protected void setupContext() throws DjenericException 371 { 372 _mgr.determineContext(); 373 } 374 375 protected void setupButtons() 376 { 377 _butCreate.setIcon(getImageIcon("new.gif")); 378 _butCollapseAll.setIcon(getImageIcon("collapseall.gif")); 379 _butCopy.setIcon(getImageIcon("copy.gif")); 380 _butExport.setIcon(getImageIcon("export.gif")); 381 _butImport.setIcon(getImageIcon("import.gif")); 382 _butDelete.setIcon(getImageIcon("delete.gif")); 383 _butEdit.setIcon(getImageIcon("edit.gif")); 384 _butExpandAll.setIcon(getImageIcon("expandall.gif")); 385 _butReload.setIcon(getImageIcon("reload.gif")); 386 _butFilter.setIcon(getImageIcon("filter.gif")); 387 } 388 389 protected void setupWindow() 390 { 391 int divloc = Integer.parseInt(getProperty("window.dividerloc", "225")); 392 _splitpane.setDividerLocation(divloc); 393 int width = Integer.parseInt(getProperty("window.width", "400")); 394 int height = Integer.parseInt(getProperty("window.height", "400")); 395 setSize(new Dimension (width, height)); 396 setTitle(Messages.getString("Specifier.title", DjVersion.getVersion(), _selectedView.getCode(), 397 getPersistenceManager().getConnectionTitle())); 398 JComponent contentPane = (JComponent ) getContentPane(); 399 InputMap inputMap = contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 400 String keyNextTab = "NextTab"; 401 String keyPrevTab = "prevTab"; 402 String keyCloseTab = "closeTab"; 403 String keyCloseAllTab = "closeAllTab"; 404 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0, true), keyNextTab); 405 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, KeyEvent.SHIFT_DOWN_MASK, true), keyPrevTab); 406 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, KeyEvent.CTRL_DOWN_MASK, true), keyCloseTab); 407 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK, true), 408 keyCloseAllTab); 409 ActionMap actionMap = contentPane.getActionMap(); 410 actionMap.put(keyNextTab, new AbstractAction () 411 { 412 private static final long serialVersionUID = 1L; 413 414 public void actionPerformed(ActionEvent e) 415 { 416 nextTab(); 417 } 418 }); 419 actionMap.put(keyPrevTab, new AbstractAction () 420 { 421 private static final long serialVersionUID = 1L; 422 423 public void actionPerformed(ActionEvent e) 424 { 425 prevTab(); 426 } 427 }); 428 actionMap.put(keyCloseTab, new AbstractAction () 429 { 430 private static final long serialVersionUID = 1L; 431 432 public void actionPerformed(ActionEvent e) 433 { 434 closeTab(); 435 } 436 }); 437 actionMap.put(keyCloseAllTab, new AbstractAction () 438 { 439 private static final long serialVersionUID = 1L; 440 441 public void actionPerformed(ActionEvent e) 442 { 443 closeAllTabs(); 444 } 445 }); 446 } 447 448 protected void prevTab() 449 { 450 int idx = _tabs.getSelectedIndex(); 451 if (idx == -1) return; 452 if (idx > 0) 453 { 454 idx--; 455 } 456 else 457 { 458 idx = _tabs.getComponentCount() - 1; 459 } 460 _tabs.setSelectedIndex(idx); 461 } 462 463 protected void nextTab() 464 { 465 int idx = _tabs.getSelectedIndex(); 466 if (idx == -1) return; 467 if (idx < _tabs.getComponentCount() - 1) 468 { 469 idx++; 470 } 471 else 472 { 473 idx = 0; 474 } 475 _tabs.setSelectedIndex(idx); 476 } 477 478 public void removeTab(SpecifierPanel panel) 479 { 480 if (panel instanceof Component ) _tabs.remove((Component ) panel); 481 } 482 483 protected void closeTab() 484 { 485 if (_tabs.getSelectedComponent() instanceof DjCloseable) 486 { 487 DjCloseable te = (DjCloseable) _tabs.getSelectedComponent(); 488 if (te.canClose()) 489 { 490 te.close(); 491 } 492 } 493 else if (_tabs.getSelectedIndex() != 0) 494 { 495 _tabs.remove(_tabs.getSelectedIndex()); 496 } 497 } 498 499 public void closeAllTabs() 500 { 501 while (_tabs.getTabCount() > 0) 502 { 503 if (_tabs.getSelectedComponent() instanceof DjCloseable) 504 { 505 DjCloseable te = (DjCloseable) _tabs.getSelectedComponent(); 506 if (te.canClose()) 507 { 508 te.close(); 509 } 510 else return; 511 } 512 else if (_tabs.getSelectedIndex() != 0) 513 { 514 _tabs.remove(_tabs.getSelectedIndex()); 515 } 516 } 517 } 518 519 protected void reloadView() throws DjenericException 520 { 521 DjSession session = _mgr.createSession(); 522 try 523 { 524 _selectedView.reload(session); 525 } 526 finally 527 { 528 session.close(); 529 } 530 } 531 532 protected boolean isAtLeastOneEditorOpen() 533 { 534 return _tabs.getComponentCount() > 0; 535 } 536 537 protected void reloadViewDefinition() throws DjenericException 538 { 539 boolean modelReloaded = false; 540 if (_mgr.isModelUpdatedExternally()) 541 { 542 if (isAtLeastOneEditorOpen()) throw new DjenericException(Messages.getString("Specifier.CloseEditorsFirst")); 543 _mgr.loadModel(); 544 _session.reset(); 545 modelReloaded = true; 546 } 547 if (modelReloaded) DjLogger.log("Model reloaded"); 548 else DjLogger.log("No need to reload model"); 549 String currentPath = getCurrentSelectionPath(); 550 reloadView(); 551 _root = new DjenericFolder(); 552 _root.setSpecifierPanelContainer(this); 553 _treeModel = new DefaultTreeModel (_root); 554 _root.setTree(_tree); 555 _tree.setModel(_treeModel); 556 DjenericDocumentHandler handler = _selectedView.getDocumentHandler(); 557 setupMenu(handler); 558 setupNavigator(handler); 559 setupEditors(handler); 560 setupResources(handler); 561 setCurrentSelectionPath(currentPath); 562 } 563 564 public void reloadTree() throws DjenericException 565 { 566 String currentPath = getCurrentSelectionPath(); 567 getSession().reset(); 568 for (int i = 0; i < _root.getChildCount(); i++) 569 { 570 if (_root.getChildAt(i) instanceof DjenericTreeNode) 571 { 572 DjenericTreeNode node = (DjenericTreeNode) _root.getChildAt(i); 573 node.reload(); 574 } 575 } 576 setCurrentSelectionPath(currentPath); 577 } 578 579 private void setCurrentSelectionPath(String currentPath) 580 { 581 _root.selectNode(currentPath); 582 } 583 584 private String getCurrentSelectionPath() 585 { 586 DjenericTreeNode[] sels = getSelectedNodes(); 587 String currentPath = ""; 588 if (sels.length > 0) currentPath = sels[0].getPathString(); 589 return currentPath; 590 } 591 592 protected void setupNavigator(DjenericDocumentHandler handler) throws DjenericException 593 { 594 _root.setSpecifierPanelContainer(this); 595 navigatorStructure = _selectedView.getExtentUsages(); 596 for (int i = 0; i < navigatorStructure.length; i++) 597 { 598 DjenericFolder folder = new DjenericFolder(); 599 folder.setExtentUsage(navigatorStructure[i]); 600 _root.insertAsFolder(folder); 601 } 602 _treeModel.nodeStructureChanged(_root); 603 _tree_valueChanged(null); 604 } 605 606 private void setupEditors(DjenericDocumentHandler handler) throws DjenericException 607 { 608 _editors = handler.getEditors(_mgr); 609 } 610 611 protected void setupResources(DjenericDocumentHandler handler) throws DjenericException 612 { 613 ResourceDefinition[] res = handler.getResources(); 614 for (int i = 0; i < res.length; i++) 615 { 616 if (res[i].getType().equalsIgnoreCase("class")) 617 { 618 String name = res[i].getName(); 619 DjStringReplacer sr = new DjStringReplacer(res[i].getPath()); 620 name = sr.replace("/", ".") + name; 621 while (name.startsWith(".")) 622 name = name.substring(1); 623 _resources.put(name, res[i]); 624 } 625 _resources.put(res[i].getPath() + res[i].getName() + "." + res[i].getType(), res[i]); 626 } 627 } 628 629 private DjModelView loadView() throws DjenericException 630 { 631 ModelViewDialog vd = new ModelViewDialog(getContentPane(), _mgr.getCurrentUser()); 632 DjModelView selectedView = vd.getView(); 633 return selectedView; 634 } 635 636 642 protected void jbInit() throws Exception 643 { 644 jPanel1.setLayout(borderLayout1); 645 _butExpandAll.setPreferredSize(new Dimension (24, 24)); 646 _butExpandAll.setToolTipText(Messages.getString("Specifier.ExpandAll")); 647 _butExpandAll.addActionListener(new java.awt.event.ActionListener () 648 { 649 public void actionPerformed(ActionEvent e) 650 { 651 expandAll(e); 652 } 653 }); 654 jPanel2.setLayout(verticalFlowLayout1); 655 _butCollapseAll.setPreferredSize(new Dimension (24, 24)); 656 _butCollapseAll.setToolTipText(Messages.getString("Specifier.CollapseAll")); 657 _butCollapseAll.addActionListener(new java.awt.event.ActionListener () 658 { 659 public void actionPerformed(ActionEvent e) 660 { 661 collapseAll(e); 662 } 663 }); 664 _butDelete.setPreferredSize(new Dimension (24, 24)); 665 _butDelete.setToolTipText(Messages.getString("Specifier.DeleteSelected")); 666 _butDelete.addActionListener(new java.awt.event.ActionListener () 667 { 668 public void actionPerformed(ActionEvent e) 669 { 670 deleteObject(e); 671 } 672 }); 673 _butCopy.setPreferredSize(new Dimension (24, 24)); 674 _butCopy.setToolTipText(Messages.getString("Specifier.CopySelected")); 675 _butCopy.addActionListener(new java.awt.event.ActionListener () 676 { 677 public void actionPerformed(ActionEvent e) 678 { 679 copyObject(); 680 } 681 }); 682 _butExport.setPreferredSize(new Dimension (24, 24)); 683 _butExport.setToolTipText(Messages.getString("Specifier.ExportSelected")); 684 _butExport.addActionListener(new java.awt.event.ActionListener () 685 { 686 public void actionPerformed(ActionEvent e) 687 { 688 exportObject(); 689 } 690 }); 691 _butImport.setPreferredSize(new Dimension (24, 24)); 692 _butImport.setToolTipText(Messages.getString("Specifier.Import")); 693 _butImport.addActionListener(new java.awt.event.ActionListener () 694 { 695 public void actionPerformed(ActionEvent e) 696 { 697 importObject(); 698 } 699 }); 700 _butCreate.setPreferredSize(new Dimension (24, 24)); 701 _butCreate.setToolTipText(Messages.getString("Specifier.CreateNew")); 702 _butCreate.addActionListener(new java.awt.event.ActionListener () 703 { 704 public void actionPerformed(ActionEvent e) 705 { 706 createObject(e); 707 } 708 }); 709 _butEdit.setPreferredSize(new Dimension (24, 24)); 710 _butEdit.setToolTipText(Messages.getString("Specifier.EditSelected")); 711 _butEdit.addActionListener(new java.awt.event.ActionListener () 712 { 713 public void actionPerformed(ActionEvent e) 714 { 715 editObject(e); 716 } 717 }); 718 _butReload.setPreferredSize(new Dimension (24, 24)); 719 _butReload.setToolTipText(Messages.getString("Specifier.ReloadSelected")); 720 _butReload.addActionListener(new java.awt.event.ActionListener () 721 { 722 public void actionPerformed(ActionEvent e) 723 { 724 reloadNode(e); 725 } 726 }); 727 _lblStatus.setText(Messages.getString("global.Ok")); 728 _tree.setRootVisible(false); 729 _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener () 730 { 731 public void valueChanged(TreeSelectionEvent e) 732 { 733 _tree_valueChanged(e); 734 } 735 }); 736 _tree.addMouseListener(new java.awt.event.MouseAdapter () 737 { 738 public void mouseClicked(MouseEvent e) 739 { 740 _tree_mouseClicked(e); 741 } 742 }); 743 _tree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener () 744 { 745 public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException 746 { 747 _tree_treeWillExpand(e); 748 } 749 750 public void treeWillCollapse(TreeExpansionEvent e) 751 { 752 } 753 }); 754 _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener () 755 { 756 public void valueChanged(TreeSelectionEvent e) 757 { 758 _tree_valueChanged(e); 759 } 760 }); 761 _desktopPanel.setLayout(borderLayout2); 762 _desktopPanel.setBackground(SystemColor.desktop); 763 _desktopPanel.setMinimumSize(new Dimension (10, 10)); 764 _butFilter.addActionListener(new java.awt.event.ActionListener () 765 { 766 public void actionPerformed(ActionEvent e) 767 { 768 filterFolder(e); 769 } 770 }); 771 _butFilter.setToolTipText(Messages.getString("Specifier.Find")); 772 _butFilter.setPreferredSize(new Dimension (24, 24)); 773 jLabel1.setText(" "); 774 jLabel2.setText(" "); 775 jLabel3.setText(" "); 776 jLabel1.setPreferredSize(new Dimension (3, 6)); 777 jLabel2.setPreferredSize(new Dimension (3, 6)); 778 jLabel3.setPreferredSize(new Dimension (3, 6)); 779 jPanel1.setMinimumSize(new Dimension (10, 10)); 780 _splitpane.setOneTouchExpandable(true); 781 jPanel2.add(_butReload, null); 782 jPanel2.add(_butFilter, null); 783 jPanel2.add(jLabel1, null); 784 jPanel2.add(_butEdit, null); 785 jPanel2.add(_butCopy, null); 786 jPanel2.add(_butCreate, null); 787 jPanel2.add(_butDelete, null); 788 jPanel2.add(jLabel2, null); 789 jPanel2.add(_butExport, null); 790 jPanel2.add(_butImport, null); 791 jPanel2.add(jLabel3, null); 792 jPanel2.add(_butExpandAll, null); 793 jPanel2.add(_butCollapseAll, null); 794 this.getContentPane().add(_splitpane, BorderLayout.CENTER); 795 _splitpane.add(jPanel1, JSplitPane.LEFT); 796 jPanel1.add(jPanel2, BorderLayout.WEST); 797 jPanel1.add(jScrollPane1, BorderLayout.CENTER); 798 _splitpane.add(_desktopPanel, JSplitPane.RIGHT); 799 _desktopPanel.add(_tabs, BorderLayout.CENTER); 800 this.getContentPane().add(_lblStatus, BorderLayout.SOUTH); 801 jScrollPane1.getViewport().add(_tree, null); 802 _splitpane.setDividerLocation(270); 803 _tree.setCellRenderer(_treeCellRendrer); 804 _tree.setModel(_treeModel); 805 _tree.setShowsRootHandles(true); 806 _tree.setAutoscrolls(true); 807 _tree.setDoubleBuffered(true); 808 _tree.setToggleClickCount(0); 809 _tree.setManager(getPersistenceManager()); 810 _tree.setSession(getSession()); 811 _tree.putClientProperty("JTree.lineStyle", "Angled"); 812 setTabsVisible(false); 813 _root.setTree(_tree); 814 } 815 816 822 protected void processWindowEvent(WindowEvent e) 823 { 824 if (e.getID() == WindowEvent.WINDOW_CLOSING) 825 { 826 for (int i = 0; i < _tabs.getComponentCount(); i++) 827 { 828 DjCloseable closeable = (DjCloseable) _tabs.getComponent(i); 829 _tabs.setSelectedComponent((Component ) closeable); 830 if (!closeable.canClose()) return; 831 } 832 exitProgram(); 833 } 834 super.processWindowEvent(e); 835 } 836 837 protected void updateProperties() 838 { 839 setProperty("window.width", "" + getWidth()); 840 setProperty("window.height", "" + getHeight()); 841 setProperty("window.dividerloc", "" + _splitpane.getDividerLocation()); 842 } 843 844 public static ImageIcon getImageIcon(String fileName) 845 { 846 URL url = Specifier.class.getResource("images/" + fileName); 847 if (url != null) return new ImageIcon (url); 848 else return new ImageIcon (""); 849 } 851 852 public static Image getImage(String fileName) 853 { 854 URL url = Specifier.class.getResource("images/" + fileName); 855 if (url != null) return Toolkit.getDefaultToolkit().getImage(url); 856 else return Toolkit.getDefaultToolkit().getImage(""); 857 } 858 859 public DjPersistenceManager getPersistenceManager() 860 { 861 return _mgr; 862 } 863 864 public DjSession getSession() 865 { 866 return _session; 867 } 868 869 void mnuExit(ActionEvent e) 870 { 871 exitProgram(); 872 } 873 874 void _tree_valueChanged(TreeSelectionEvent e) 875 { 876 try 877 { 878 updateTreeButtonStatus(); 879 } 880 catch (DjenericException dje) 881 { 882 setStatusMessage(dje); 883 } 884 } 885 886 private void updateTreeButtonStatus() throws DjenericException 887 { 888 DjenericTreeNode[] sels = getSelectedNodes(); 889 boolean canEdit = false; 890 boolean canCreate = false; 891 boolean canCopy = false; 892 boolean canExport = false; 893 boolean canImport = true; 894 boolean canDelete = false; 895 boolean canCollapse = false; 896 boolean canExpand = false; 897 boolean canReload = false; 898 boolean canFilter = false; 899 for (int i = 0; i < sels.length; i++) 900 { 901 canExport |= sels[i].canExport(); 902 canEdit |= sels[i].canEdit(); 903 canCreate |= sels[i].canCreate(); 904 canDelete |= sels[i].canDelete(); 905 canFilter |= sels[i].canFilter(); 906 canCopy |= sels[i].canCopy(); 907 canCollapse = true; 908 canExpand |= (sels[i].getChildCount() > 0); 909 canReload = true; 910 } 911 912 canEdit &= _session.canModify() | _session.canQuery(); 913 canCreate &= _session.canCreate(); 914 canCopy &= _session.canCreate(); 915 canExport &= _session.canQuery(); 916 canImport &= _session.canCreate() && _session.canModify(); 917 canDelete &= _session.canDelete(); 918 canReload &= _session.canQuery(); 919 canFilter &= _session.canQuery(); 920 921 _butEdit.setEnabled(canEdit); 922 _butCreate.setEnabled(canCreate); 923 _butCopy.setEnabled(canCopy); 924 _butExport.setEnabled(canExport); 925 _butImport.setEnabled(canImport); 926 _butDelete.setEnabled(canDelete); 927 _butCollapseAll.setEnabled(canCollapse); 928 _butExpandAll.setEnabled(canExpand); 929 _butCollapseAll.setEnabled(canExpand); 930 _butReload.setEnabled(canReload); 931 _butFilter.setEnabled(canFilter); 932 } 933 934 public DjenericTreeNode[] getSelectedNodes() 935 { 936 if (_tree.getSelectionPath() == null) return new DjenericTreeNode[0]; 937 ArrayList result = new ArrayList (10); 938 TreePath [] tp = _tree.getSelectionPaths(); 939 for (int qq = 0; qq < tp.length; qq++) 940 { 941 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) (tp[qq].getLastPathComponent()); 942 if (node instanceof DjenericTreeNode) result.add(node); 943 } 944 return (DjenericTreeNode[]) result.toArray(new DjenericTreeNode[0]); 945 } 946 947 public DjObject[] getSelectedObjects() throws DjenericException 948 { 949 ArrayList result = new ArrayList (); 950 951 DjenericTreeNode[] selected = getSelectedNodes(); 952 for (int i = 0; i < selected.length; i++) 953 { 954 if (selected[i] instanceof DjenericNode) 955 { 956 DjenericNode node = (DjenericNode) selected[i]; 957 DjObject obj = node.getObject(); 958 if (!result.contains(obj)) result.add(obj); 959 } 960 else if (selected[i] instanceof DjenericFolder) 961 { 962 DjenericFolder folder = (DjenericFolder) selected[i]; 963 Iterator it = folder.getObjects().iterator(); 964 while (it.hasNext()) 965 { 966 DjObject obj = (DjObject) it.next(); 967 if (!result.contains(obj)) result.add(obj); 968 } 969 } 970 } 971 return (DjObject[]) result.toArray(new DjObject[result.size()]); 972 } 973 974 public DjObject[] getSelectedObjects(String typeName) throws DjenericException 975 { 976 DjExtent extent = getPersistenceManager().getExtent(typeName); 977 ArrayList result = new ArrayList (); 978 979 DjObject[] selected = getSelectedObjects(); 980 for (int i = 0; i < selected.length; i++) 981 { 982 if (extent == null || selected[i].isInstanceOf(extent)) result.add(selected[i]); 983 } 984 return (DjObject[]) result.toArray(new DjObject[result.size()]); 985 } 986 987 void _tree_treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException 988 { 989 try 990 { 991 if (e.getPath().getLastPathComponent() instanceof DjenericTreeNode) 992 { 993 DjenericTreeNode node = (DjenericTreeNode) e.getPath().getLastPathComponent(); 994 node.expandNode(); 995 } 996 setStatusMessage(OK_MSG); 997 } 998 catch (Exception x) 999 { 1000 setStatusMessage(x); 1001 throw new ExpandVetoException (new TreeExpansionEvent (this, e.getPath()), x.getMessage()); 1002 } 1003 } 1004 1005 void reloadNode(ActionEvent e) 1006 { 1007 DjenericTreeNode[] sels = getSelectedNodes(); 1008 try 1009 { 1010 for (int i = 0; i < sels.length; i++) 1011 sels[i].reload(); 1012 setStatusMessage(OK_MSG); 1013 } 1014 catch (Throwable x) 1015 { 1016 setStatusMessage(x); 1017 } 1018 } 1019 1020 void exportObject() 1021 { 1022 DjenericTreeNode[] sels = getSelectedNodes(); 1023 if (sels.length == 0) return; 1024 1025 try 1026 { 1027 ExportOptionsDialog dlg = new ExportOptionsDialog(this); 1028 if (dlg.wasCanceled()) return; 1029 1030 boolean recursive = dlg.isIncludingChildren(); 1031 1032 JFileChooser fc = new JFileChooser (); 1033 1034 DjFileFilter filter = new DjFileFilter(new String []{"xml"}, "Xml export"); 1035 fc.setFileFilter(filter); 1036 1037 fc.setDialogTitle(Messages.getString("Specifier.Export2File")); 1038 String path = getProperty(EXPORTDIR, System.getProperties().getProperty("user.dir")) + "/" + sels[0].toString() 1039 + ".xml"; 1040 fc.setCurrentDirectory(new File (path)); 1041 fc.setSelectedFile(new File (path)); 1042 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 1043 1044 if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) 1045 { 1046 String fileName = fc.getSelectedFile().getAbsolutePath(); 1047 setProperty(EXPORTDIR, fileName); 1048 1049 File file = new File (fileName); 1050 boolean skip = false; 1051 1052 if (file.exists()) 1053 { 1054 skip = !Util.isOkToOverwrite(this, file); 1055 } 1056 1057 if (!skip) 1058 { 1059 DjObjectExporter oe = new DjObjectExporter(); 1060 oe.setStatusDisplayer(this); 1061 1062 for (int i = 0; i < sels.length; i++) 1063 if (sels[i] instanceof DjenericNode) exportNode(oe, (DjenericNode) sels[i], recursive); 1064 else if (sels[i] instanceof DjenericFolder) exportFolder(oe, (DjenericFolder) sels[i], recursive); 1065 1066 BufferedWriter w = new BufferedWriter (new FileWriter (fileName)); 1067 w.write(oe.export()); 1068 w.close(); 1069 } 1070 } 1071 } 1072 catch (Exception e1) 1073 { 1074 setStatusMessage(e1); 1075 } 1076 } 1077 1078 private void exportFolder(DjObjectExporter oe, DjenericFolder folder, boolean recursive) throws Exception , 1079 DjenericException 1080 { 1081 DjenericNode[] nodes = folder.getChildNodes(); 1082 for (int c = 0; c < nodes.length; c++) 1083 { 1084 if (folder.getExtentUsage().isRecursive()) recursive = false; 1085 exportNode(oe, nodes[c], recursive); 1086 } 1087 } 1088 1089 private void exportNode(DjObjectExporter oe, DjenericNode node, boolean recursive) throws Exception 1090 { 1091 EditorDefinition editorDefinition = node.getExtentUsage().getEditor(); 1092 if (node.canExport()) oe.markForExport(node.getTargetObject(), editorDefinition.getUsages()); 1093 if (recursive) 1094 { 1095 node.expandNode(); 1096 for (int i = 0; i < node.getChildCount(); i++) 1097 { 1098 if (node.getChildAt(i) instanceof DjenericFolder) 1099 { 1100 exportFolder(oe, (DjenericFolder) node.getChildAt(i), recursive); 1101 } 1102 } 1103 } 1104 } 1105 1106 void importObject() 1107 { 1108 try 1109 { 1110 JFileChooser fc = new JFileChooser (); 1111 1112 DjFileFilter filter = new DjFileFilter(new String []{"xml"}, "Xml export"); 1113 fc.setFileFilter(filter); 1114 1115 fc.setDialogTitle(Messages.getString("Specifier.ImportFromFile")); 1116 String path = getProperty(EXPORTDIR, System.getProperties().getProperty("user.dir")); 1117 fc.setCurrentDirectory(new File (path)); 1118 fc.setSelectedFile(new File (path)); 1119 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 1120 1121 if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) 1122 { 1123 1124 ImportOptionsDialog iod = new ImportOptionsDialog(this); 1125 if (iod.wasCanceled()) return; 1126 1127 String fileName = fc.getSelectedFile().getAbsolutePath(); 1128 setProperty(EXPORTDIR, fileName); 1129 1130 File file = new File (fileName); 1131 FileInputStream fis = new FileInputStream (file); 1132 DjSession session = getPersistenceManager().createSession(); 1133 DjObjectImporter importer = new DjObjectImporter(session); 1134 importer.setStatusDisplayer(this); 1135 try 1136 { 1137 boolean aborted = false; 1138 1139 importer.load(fis, iod.shouldMerge()); 1140 DjUid[] unresolved = importer.resolve(); 1141 if (unresolved.length > 0) 1142 { 1143 aborted = !resolveReferences(session, unresolved, importer.getInteral2ExternalMapping()); 1144 } 1145 1146 if (!aborted) 1147 { 1148 importer.apply(); 1149 1150 ImportResultDialog dlg = new ImportResultDialog(this, importer); 1151 if (dlg.wasCancelled()) 1152 { 1153 setStatusMessage(Messages.getString("Specifier.ImportAborted")); 1154 } 1155 else 1156 { 1157 session.commit(); 1158 reloadTree(); 1159 setStatusMessage(Messages.getString("Specifier.ImportDone")); 1160 } 1161 } 1162 } 1163 finally 1164 { 1165 session.close(); 1166 } 1167 } 1168 } 1169 catch (ImportAbortedException iae) 1170 { 1171 setStatusMessage(Messages.getString("Specifier.ImportAborted")); 1172 } 1173 catch (Exception e1) 1174 { 1175 setStatusMessage(e1); 1176 } 1177 } 1178 1179 void copyObject() 1180 { 1181 DjObjectCloner cloner = new DjObjectCloner(); 1182 HashMap allFolders = new HashMap (); 1183 1184 try 1185 { 1186 DjenericTreeNode[] sels = getSelectedNodes(); 1187 1188 for (int i = 0; i < sels.length; i++) 1189 if (sels[i] instanceof DjenericNode) 1190 { 1191 DjenericNode node = (DjenericNode) sels[i]; 1192 if (!node.canCreate()) continue; 1193 EditorDefinition editorDefinition = node.getExtentUsage().getEditor(); 1194 if (editorDefinition != null) 1195 { 1196 DjSession session = getPersistenceManager().createSession(); 1197 try 1198 { 1199 DjObject source = session.getObject(node.getObject().getExtent(), node.getObject().getObjectId()); 1200 DjObject dest = cloner.clone(source, editorDefinition.getUsages()); 1201 session.commit(); 1202 DjenericTreeNode containingFolder = node.getParentTreeNode(); 1203 if (containingFolder != null) allFolders.put(containingFolder, containingFolder); 1204 DjList lst = new DjList(); 1205 lst.add(dest); 1206 editObject(containingFolder, lst); 1207 } 1208 finally 1209 { 1210 session.close(); 1211 } 1212 } 1213 } 1214 Iterator it = allFolders.keySet().iterator(); 1215 while (it.hasNext()) 1216 { 1217 DjenericTreeNode fldr = (DjenericTreeNode) it.next(); 1218 fldr.reload(); 1219 } 1220 } 1221 catch (Exception dje) 1222 { 1223 setStatusMessage(dje); 1224 } 1225 } 1226 1227 void deleteObject(ActionEvent e) 1228 { 1229 DjenericTreeNode[] sels = getSelectedNodes(); 1230 try 1231 { 1232 boolean dontask = false; 1233 for (int i = 0; i < sels.length; i++) 1234 if (sels[i] instanceof DjenericNode) 1235 { 1236 DjenericNode node = (DjenericNode) sels[i]; 1237 if (!node.canDelete()) continue; 1238 if (!dontask) 1239 { 1240 int result = JOptionPane.showOptionDialog(this, Messages.getString("Specifier.Sure2Delete", node 1241 .getExtentUsage().getExtent().getNameSingular(), node.toString()), Messages 1242 .getString("Specifier.DeleteObject"), JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, 1243 null, new String []{Messages.getString("global.Yes"), 1244 Messages.getString("global.Yes2All"), 1245 Messages.getString("global.Cancel")}, null); 1246 if ((result != 0) && (result != 1)) return; 1247 if (result == 1) dontask = true; 1249 } 1250 sels[i].delete(); 1251 } 1252 setStatusMessage(OK_MSG); 1253 } 1254 catch (Throwable x) 1255 { 1256 setStatusMessage(x); 1257 } 1258 } 1259 1260 void collapseAll(ActionEvent e) 1261 { 1262 DjenericTreeNode[] sels = getSelectedNodes(); 1263 try 1264 { 1265 for (int i = 0; i < sels.length; i++) 1266 sels[i].collapseAll(); 1267 setStatusMessage(OK_MSG); 1268 } 1269 catch (Throwable x) 1270 { 1271 setStatusMessage(x); 1272 } 1273 } 1274 1275 void expandAll(ActionEvent e) 1276 { 1277 DjenericTreeNode[] sels = getSelectedNodes(); 1278 try 1279 { 1280 for (int i = 0; i < sels.length; i++) 1281 sels[i].expandAll(0); 1282 setStatusMessage(OK_MSG); 1283 } 1284 catch (Throwable x) 1285 { 1286 setStatusMessage(x); 1287 } 1288 } 1289 1290 public void editorApplied(ObjectEditor editor) 1291 { 1292 _tabs.setTitleAt(getEditorIndex(editor), editor.getTitle()); 1293 } 1294 1295 public void notifyClosed(SpecifierPanel editor) 1296 { 1297 _tabs.remove((Component ) editor); 1298 if (_tabs.getComponentCount() == 0) setTabsVisible(false); 1299 setStatusMessage(OK_MSG); 1300 } 1301 1302 public void keyTyped(KeyEvent e) 1303 { 1304 if (_tabs.getComponentCount() == 0) return; 1305 if ((e.getKeyCode() == KeyEvent.VK_F6) && ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) 1306 && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) 1307 { 1308 int idx = _tabs.getSelectedIndex(); 1309 if (idx > 0) idx--; 1310 else idx = _tabs.getComponentCount() - 1; 1311 _tabs.setSelectedIndex(idx); 1312 } 1313 else if ((e.getKeyCode() == KeyEvent.VK_F6) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) 1314 { 1315 int idx = _tabs.getSelectedIndex(); 1316 if (idx < _tabs.getComponentCount() - 1) idx++; 1317 else idx = 0; 1318 _tabs.setSelectedIndex(idx); 1319 } 1320 else if ((e.getKeyCode() == KeyEvent.VK_F4) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) 1321 { 1322 ObjectEditor editor = (ObjectEditor) _tabs.getSelectedComponent(); 1323 if (editor != null) if (editor.canClose()) editor.close(); 1324 } 1325 } 1326 1327 public ObjectEditor findEditor(DjObject obj) throws DjenericException 1328 { 1329 if (obj == null) return null; 1330 for (int i = 0; i < _tabs.getComponentCount(); i++) 1331 { 1332 if (!(_tabs.getComponent(i) instanceof ObjectEditor)) continue; 1333 ObjectEditor editor = (ObjectEditor) _tabs.getComponent(i); 1334 DjList masterObjects = editor.getMasterObjects(); 1335 for (int o = 0; o < masterObjects.size(); o++) 1336 { 1337 if (masterObjects.getDjenericObjectAt(o).getObjectId() == obj.getObjectId()) 1338 { 1339 try 1340 { 1341 editor.setSelectedIndexOfMaster(o); 1342 } 1343 catch (Exception x) 1344 { 1345 setStatusMessage(x); 1346 } 1347 return editor; 1348 } 1349 } 1350 } 1351 return null; 1352 } 1353 1354 protected int getEditorIndex(ObjectEditor pnl) 1355 { 1356 for (int i = 0; i < _tabs.getComponentCount(); i++) 1357 if (_tabs.getComponent(i) == pnl) return i; 1358 return -1; 1359 } 1360 1361 void editObject(ActionEvent e) 1362 { 1363 DjenericTreeNode[] sels = getSelectedNodes(); 1364 try 1365 { 1366 for (int i = 0; i < sels.length; i++) 1367 { 1368 DjList objects = new DjList(); 1369 1370 if (sels[i] instanceof DjenericNode) 1371 { 1372 objects.add(((DjenericNode) sels[i]).getTargetObject()); 1373 } 1374 if (sels[i] instanceof DjenericFolder) 1375 { 1376 DjenericNode[] children = ((DjenericFolder) sels[i]).getChildNodes(); 1377 for (int c = 0; c < children.length; c++) 1378 { 1379 objects.add(children[c].getTargetObject()); 1380 } 1381 } 1382 1383 editObject(sels[i], objects); 1384 } 1385 } 1386 catch (Throwable x) 1387 { 1388 setStatusMessage(x); 1389 } 1390 } 1391 1392 private void editObject(DjenericTreeNode node, DjList objects) throws Exception , InstantiationException , 1393 IllegalAccessException , ClassNotFoundException , DjenericException 1394 { 1395 ObjectEditor editor = null; 1396 if (objects != null && objects.size() > 0) 1397 { 1398 editor = findEditor(objects.getDjenericObjectAt(0)); 1399 } 1400 EditorDefinition editorDefinition = node.getExtentUsage().getEditor(); 1401 1402 if (editor == null && (editorDefinition != null)) 1404 { 1405 if (editorDefinition.getCustomEditorClass() == null) 1406 { 1407 editor = new EditorPanel(); 1408 } 1409 else 1410 { 1411 editor = (ObjectEditor) loadClass(editorDefinition.getCustomEditorClass()).newInstance(); 1412 } 1413 editor.setSpecifierPanelContainer(this); 1414 editor.setAssociatedTreeNode(node); 1415 editor.construct(editorDefinition, objects); 1416 editor.initializeUsage(node.getExtentUsage()); 1417 1418 ImageIcon imageIcon = null; 1419 ResourceDefinition iconDef = node.getExtentUsage().getImageIconResource(); 1420 if (iconDef != null) 1421 { 1422 imageIcon = new ImageIcon (iconDef.getBytes()); 1423 } 1424 addTab(editor, editor.getTitle(), imageIcon); 1425 setStatusMessage(OK_MSG); 1426 } 1427 if (editor != null) 1428 { 1429 _tabs.setSelectedComponent((Component ) editor); 1430 } 1431 } 1432 1433 void createObject(ActionEvent e) 1434 { 1435 DjenericTreeNode[] sels = getSelectedNodes(); 1436 if (sels.length == 0) return; 1437 try 1438 { 1439 ExtentUsage extentUsage = sels[0].getExtentUsage(); 1440 DjenericTreeNode node2refresh = sels[0]; 1441 if (node2refresh instanceof DjenericNode) node2refresh = node2refresh.getParentTreeNode(); 1442 ObjectEditor editor; 1443 EditorDefinition editorDefinition = extentUsage.getEditor(); 1444 if (editorDefinition.getCustomEditorClass() == null) 1445 { 1446 editor = new EditorPanel(); 1447 } 1448 else 1449 { 1450 editor = (ObjectEditor) loadClass(editorDefinition.getCustomEditorClass()).newInstance(); 1451 } 1452 editor.setSpecifierPanelContainer(this); 1453 editor.setAssociatedTreeNode(node2refresh); 1454 editor.construct(editorDefinition, new DjList()); 1455 editor.initializeUsage(node2refresh.getExtentUsage()); 1456 addTab(editor, editor.getTitle(), null); 1457 setStatusMessage(OK_MSG); 1458 } 1459 catch (Throwable x) 1460 { 1461 setStatusMessage(x); 1462 } 1463 } 1464 1465 public void addTab(SpecifierPanel panel, String title, ImageIcon imageIcon) throws DjenericException 1466 { 1467 if (!(panel instanceof Component )) throw new DjenericException(Messages.getString("Specifier.ObjectEditorNotAComp")); 1468 if (!_tabs.isVisible()) setTabsVisible(true); 1469 if (imageIcon != null) _tabs.addTab(title, imageIcon, (Component ) panel); 1470 else _tabs.addTab(title, (Component ) panel); 1471 _tabs.setSelectedComponent((Component ) panel); 1472 panel.requestFocus(); 1473 } 1474 1475 void _tree_mouseClicked(MouseEvent e) 1476 { 1477 if (e.getButton() == MouseEvent.BUTTON3) 1478 { 1479 handleExtensions(e.getX(), e.getY()); 1480 } 1481 else 1482 { 1483 if (e.getClickCount() >= 2) editObject(null); 1484 } 1485 } 1486 1487 private void handleExtensions(int x, int y) 1488 { 1489 try 1490 { 1491 DjenericTreeNode[] sels = getSelectedNodes(); 1492 ArrayList types = new ArrayList (); 1493 1494 for (int i = 0; i < sels.length; i++) 1495 { 1496 String typeName = sels[i].getExtentUsage().getExtent().getName(); 1497 if (!types.contains(typeName)) types.add(typeName); 1498 } 1499 1500 ArrayList extensionList = new ArrayList (); 1501 1502 for (int i = 0; i < types.size(); i++) 1503 { 1504 String typeName = types.get(i).toString(); 1505 extensionList.addAll(getExtensions(typeName)); 1506 } 1507 if (extensionList.size() > 0) 1508 { 1509 JPopupMenu popMenu = new JPopupMenu (); 1510 for (int i = 0; i < extensionList.size(); i++) 1511 { 1512 MenuExtensionDescr descr = (MenuExtensionDescr) extensionList.get(i); 1513 1514 DjObject[] selectedObjects = getSelectedObjects(descr.getTypeName()); 1515 1516 if (selectedObjects.length > 0) 1517 { 1518 JMenuItem itm = new JMenuItem (descr.getTitle()); 1519 itm.addActionListener(new MenuExtensionAction(selectedObjects, descr.getExtension(), this)); 1520 popMenu.add(itm); 1521 } 1522 } 1523 popMenu.show(_tree, x, y); 1524 } 1525 } 1526 catch (Exception ex) 1527 { 1528 setStatusMessage(ex); 1529 } 1530 1531 } 1532 1533 private ArrayList getExtensions(String typeName) throws IOException , InstantiationException , IllegalAccessException , 1534 ClassNotFoundException , DjenericException 1535 { 1536 ArrayList result = (ArrayList ) _cachedExtentions.get(typeName); 1537 if (result == null) 1538 { 1539 MenuFactory mf = new MenuFactory(this); 1540 result = mf.getExtensions(typeName); 1541 _cachedExtentions.put(typeName, result); 1542 } 1543 return result; 1544 } 1545 1546 public void setTabsVisible(boolean visible) 1547 { 1548 _tabs.setVisible(visible); 1549 if (visible) 1550 { 1551 _desktopPanel.setBackground(new Color (212, 208, 200)); 1552 } 1553 else 1554 { 1555 _desktopPanel.setBackground(SystemColor.desktop); 1556 } 1557 } 1558 1559 void mnuReloadNavigatorAndEditorDefinition(ActionEvent e) 1560 { 1561 try 1562 { 1563 reloadViewDefinition(); 1564 } 1565 catch (DjenericException e1) 1566 { 1567 DjLogger.log(e1); 1568 } 1569 } 1570 1571 void mnuAbout(ActionEvent e) 1572 { 1573 new AboutDialog(this); 1574 } 1575 1576 public EditorDefinition getEditorDefinition(String editorName) 1577 { 1578 for (int i = 0; i < _editors.length; i++) 1579 { 1580 if (_editors[i].getName().equals(editorName)) 1581 { 1582 return _editors[i]; 1583 } 1584 } 1585 return null; 1586 } 1587 1588 void filterFolder(ActionEvent e) 1589 { 1590 DjenericTreeNode[] sels = getSelectedNodes(); 1591 try 1592 { 1593 for (int i = 0; i < sels.length; i++) 1594 if (sels[i].canFilter()) sels[i].filter(); 1595 } 1596 catch (Throwable x) 1597 { 1598 setStatusMessage(x); 1599 } 1600 } 1601 1602 public static void main(String [] args) 1603 { 1604 try 1605 { 1606 DjenericTool.setLookAndFeel(); 1607 if (args.length < 1) 1608 { 1609 System.out.println(Messages.getString("Specifier.Usage", Specifier.class.getName()) + " <repositories.xml>"); 1610 return; 1611 } 1612 String banner = Messages.getString("global.Version", Specifier.class.getName(), DjVersion.getVersion()); 1613 DjLogPrintStream.logAll(banner, LOG_FILE_NAME); 1614 System.out.println(Messages.getString("global.Reading", args[0])); 1615 new Specifier(args[0]).startApp(); 1616 } 1617 catch (Exception e) 1618 { 1619 DjLogger.log(e); 1620 } 1621 } 1622 1623 public ResourceDefinition getResourceDefinition(String name) 1624 { 1625 return (ResourceDefinition) _resources.get(name); 1626 } 1627 1628 public InputStream getResourceAsStream(String name) 1629 { 1630 ResourceDefinition def = getResourceDefinition(name); 1631 if (def != null) return def.asStream(); 1632 1633 return Thread.currentThread().getContextClassLoader().getResourceAsStream(name); 1634 } 1635 1636 public Class loadClass(String name) throws ClassNotFoundException 1637 { 1638 DjResourceClassLoader ldr = new DjResourceClassLoader(_resources); 1639 Class clz; 1640 try 1641 { 1642 clz = ldr.loadClass(name); 1643 } 1644 catch (ClassNotFoundException cnf) 1645 { 1646 clz = Thread.currentThread().getContextClassLoader().loadClass(name); 1647 } 1648 return clz; 1649 } 1650 1651 public Component getBaseComponent() 1652 { 1653 return this; 1654 } 1655 1656 public boolean resolveReferences(DjSession session, DjUid[] uids, HashMap objectidToExternalObject) 1657 { 1658 UidResolverDialog dlg = new UidResolverDialog(this, session, uids); 1659 1660 if (!dlg.wasCancelled()) 1661 { 1662 HashMap mappings = dlg.getMappings(); 1663 Iterator it = mappings.keySet().iterator(); 1664 while (it.hasNext()) 1665 { 1666 DjUid uid = (DjUid) it.next(); 1667 DjObject object = (DjObject) mappings.get(uid); 1668 objectidToExternalObject.put(new Long (uid.getAssociatedObjectid()), object); 1669 } 1670 } 1671 1672 return !dlg.wasCancelled(); 1673 } 1674 1675 class ScriptAction implements ActionListener 1676 { 1677 String _code; 1678 Specifier _specifier; 1679 1680 public ScriptAction(Specifier specifier, String scriptCode) 1681 { 1682 _code = scriptCode; 1683 _specifier = specifier; 1684 } 1685 1686 public void actionPerformed(ActionEvent e) 1687 { 1688 ScriptRunnerPanel runner = new ScriptRunnerPanel(_specifier); 1689 try 1690 { 1691 runner.setScript(_code); 1692 String title = runner.getTitle(); 1693 int idx = title.lastIndexOf('|'); 1694 if (idx != -1) title = title.substring(idx + 1); 1695 _specifier.addTab(runner, title, null); 1696 runner.runScript(); 1697 } 1698 catch (Throwable x) 1699 { 1700 _specifier.setStatusMessage(x); 1701 } 1702 } 1703 } 1704 1705} 1706 | Popular Tags |