1 11 12 package org.enhydra.jawe; 13 14 import java.awt.*; 15 import java.awt.event.*; 16 import java.io.*; 17 import java.util.*; 18 19 import javax.swing.*; 20 import javax.swing.tree.*; 21 import javax.swing.event.*; 22 23 import org.jgraph.*; 24 import org.jgraph.event.*; 25 import org.jgraph.graph.*; 26 import org.w3c.dom.*; 27 import org.enhydra.jawe.actions.*; 28 import org.enhydra.jawe.actions.Applications; 29 import org.enhydra.jawe.actions.ExternalPackages; 30 import org.enhydra.jawe.actions.Participants; 31 import org.enhydra.jawe.actions.TypeDeclarations; 32 33 import org.enhydra.jawe.graph.Activity; 34 import org.enhydra.jawe.graph.BlockActivity; 35 import org.enhydra.jawe.graph.Participant; 36 import org.enhydra.jawe.graph.Transition; 37 import org.enhydra.jawe.xml.*; 38 import org.enhydra.jawe.xml.elements.*; 39 import org.enhydra.jawe.xml.panels.*; 40 41 42 public abstract class AbstractEditor extends JPanel implements GraphModelListener, 43 GraphSelectionListener, Observer { 44 45 48 protected BarFactory barFactory = new BarFactory(this); 49 50 protected AbstractGraph graph; 52 protected Hashtable commands; 53 protected Hashtable menuItems; 54 protected Hashtable specialItems; 55 protected Hashtable toolbarComponents; 56 protected Hashtable specialButtons; 57 protected JMenuBar menubar; 58 protected JToolBar toolbar; 59 protected JTabbedPane tabbedPane; 60 protected JScrollPane graphScrollPane; 61 protected JaWEStatusBar statusBar; 62 protected GraphUndoManager undo; 63 protected UndoHandler undoHandler; 64 65 protected JTabbedPane contentTabbedPane; 66 protected XPDLPreview xpdlPreview; 67 protected TextPreview textPreview; 68 69 protected Undo undoAction = new Undo(this); 71 protected Redo redoAction = new Redo(this); 72 73 76 protected Action[] defaultActions; 77 78 protected AbstractEditor parentEditor; 79 80 public AbstractEditor(XMLComplexElement pkgOrProcess) { 81 super(true); 82 setBorder(BorderFactory.createEtchedBorder()); 83 setLayout(new BorderLayout()); 84 85 undoHandler = new UndoHandler(this,true); 87 undo = new GraphUndoManager(); 88 graph=createGraph(); 90 91 ToolTipManager.sharedInstance().registerComponent(graph); 93 94 registerListeners(graph); 95 96 graph.getModel().addUndoableEditListener(undoHandler); 98 commands = new Hashtable(); 100 createActions(); 102 Action[] actions = getActions(); 103 for (int i = 0; i < actions.length; i++) { 104 Action a = actions[i]; 105 commands.put(a.getValue(Action.NAME), a); 106 } 107 graph.setAdditionalKeyboardShortcuts(); 108 109 JPanel panel=new JPanel(); 111 panel.setLayout(new BorderLayout()); 112 specialItems = createSpecialItems(); 114 JaWEMarqueeHandler pemh = (JaWEMarqueeHandler)graph.getMarqueeHandler(); 116 specialButtons = pemh.getSpecialButtons(); 117 toolbarComponents = new Hashtable(); 119 tabbedPane=new JTabbedPane(); 120 Component toolBarComponent = barFactory.createToolBars(tabbedPane); 121 122 menuItems = new Hashtable(); 124 menubar = barFactory.createMenubar(); 125 panel.add(toolBarComponent,BorderLayout.NORTH); 126 add(menubar,BorderLayout.NORTH); 127 graph.setPropertyObject(pkgOrProcess); 129 panel.add(createCenterComponent(),BorderLayout.CENTER); 131 JPanel mainPanel=new JPanel(); 132 mainPanel.setLayout(new BorderLayout()); 133 mainPanel.add(menubar,BorderLayout.NORTH); 135 mainPanel.add(panel,BorderLayout.CENTER); 137 mainPanel.add(createStatusBar(),BorderLayout.SOUTH); 139 add(createContentTabbedPane(mainPanel),BorderLayout.CENTER); 141 valueChanged(null); 142 } 143 144 149 protected Component createCenterComponent() { 150 graphScrollPane = new JScrollPane(graph); 152 JViewport port = graphScrollPane.getViewport(); 153 port.setScrollMode(JViewport.BLIT_SCROLL_MODE); 154 155 graphScrollPane.getVerticalScrollBar().setUnitIncrement(20); 158 graphScrollPane.getHorizontalScrollBar().setUnitIncrement(40); 159 160 return graphScrollPane; 161 } 162 163 public TextPreview getTextPreview() { 164 return this.textPreview; 165 } 166 167 public XPDLPreview getXPDLPreview() { 168 return this.xpdlPreview; 169 } 170 171 protected Component createContentTabbedPane (Component graphPanel) { 172 xpdlPreview=new XPDLPreview(this); 174 textPreview = new TextPreview(this); 175 176 contentTabbedPane=new JTabbedPane(JTabbedPane.BOTTOM); 178 contentTabbedPane.addTab(ResourceManager.getLanguageDependentString("GraphViewKey"),graphPanel); 179 contentTabbedPane.addTab(ResourceManager.getLanguageDependentString("TextViewKey"),textPreview); 180 contentTabbedPane.addTab(ResourceManager.getLanguageDependentString("XPDLViewKey"),xpdlPreview); 181 182 contentTabbedPane.addChangeListener(new ChangeListener() { 183 public void stateChanged (ChangeEvent ce) { 184 if (contentTabbedPane.getSelectedIndex()==1) { 185 textPreview.refreshView(); 186 } 187 else if (contentTabbedPane.getSelectedIndex()==2) { 188 xpdlPreview.refreshView(); 189 } 190 } 191 }); 192 193 return contentTabbedPane; 194 } 195 196 protected Component createStatusBar () { 197 statusBar=new JaWEStatusBar(this); 198 if (!JaWEConfig.getInstance().getStatusBarStatus()) { 199 statusBar.setVisible(false); 200 } 201 return statusBar; 202 } 203 204 public JaWEStatusBar getStatusBar () { 205 return statusBar; 206 } 207 208 211 protected abstract AbstractGraph createGraph(); 212 213 216 public abstract String getTitle (); 217 218 221 public abstract String toolbarToLoad (); 222 223 226 public abstract String menubarToLoad (); 227 228 229 protected abstract void createActions (); 230 231 public AbstractEditor getParentEditor () { 232 return parentEditor; 233 } 234 235 protected void doEditorSpecificStuff() {}; 236 237 public Object getSpecialButton (String key) { 238 return specialButtons.get(key); 239 } 240 241 public Map getSpecialButtons () { 242 return Collections.unmodifiableMap(specialButtons); 243 } 244 245 public Object getSpecialItem (String key) { 246 return specialItems.get(key); 247 } 248 249 public Map getSpecialItems () { 250 return Collections.unmodifiableMap(specialItems); 251 } 252 253 public void putMenuItem (String key,Object item) { 254 menuItems.put(key,item); 255 } 256 257 public void putToolbarComponent(String key,Object item) { 258 toolbarComponents.put(key,item); 259 } 260 261 public Map getToolbarComponents () { 262 return Collections.unmodifiableMap(toolbarComponents); 263 } 264 265 public BarFactory getBarFactory () { 266 return barFactory; 267 } 268 269 public JTabbedPane getContentTabbedPane () { 270 return contentTabbedPane; 271 } 272 273 public JTabbedPane getTabbedPane () { 274 return tabbedPane; 275 } 276 277 public JMenuBar getMenubar () { 278 return menubar; 279 } 280 281 public Map getMenuItems () { 282 return Collections.unmodifiableMap(menuItems); 283 } 284 285 288 public JMenuItem getMenuItem (String cmd) { 289 return (JMenuItem)menuItems.get(cmd); 290 } 291 292 295 public JComponent getToolbarComponent (String cmd) { 296 return (JComponent)toolbarComponents.get(cmd); 297 } 298 299 public Redo getRedo () { 300 return redoAction; 301 } 302 303 public Undo getUndo () { 304 return undoAction; 305 } 306 307 public JScrollPane getGraphScrollPane () { 308 return graphScrollPane; 309 } 310 311 314 protected Hashtable createSpecialItems() { 315 return new Hashtable(); 316 } 317 318 321 public Window getWindow () { 322 for (Container p = getParent(); p != null; p = p.getParent()) { 323 if (p instanceof Window) { 324 return (Window)p; 325 } 326 } 327 return null; 328 } 329 330 333 public void update() { 334 undoAction.update(); 337 redoAction.update(); 338 340 try { 341 setTitle(getWindow(),getTitle()); 342 } catch (Exception ex) {} 343 } 344 345 348 public void showWindow (Window parentWindow,String name) { 349 Window w=getWindow(); 350 if (w!=null) { 351 setBounds(w,parentWindow); 352 setTitle(w,name); 353 tabbedPane.requestFocus(); 354 w.show(); 355 } 356 } 357 358 361 public void setTitle (Window w,String name) { 362 if (w==null) return; 363 if (w instanceof JFrame) { 364 ((JFrame)w).setTitle(name); 365 } 366 else { 367 ((JDialog)w).setTitle(name); 368 } 369 } 370 371 374 protected void setBounds (Window w,Window parentWindow) { 375 Rectangle wBounds=w.getBounds(); 376 if (parentWindow!=null) { 377 wBounds=new Rectangle(parentWindow.getBounds()); 378 wBounds.grow(-20,-20); 379 } 380 w.setBounds(wBounds); 381 } 382 383 386 public void resetUndoManager() { 387 undo.discardAllEdits(); 388 undoAction.update(); 389 redoAction.update(); 390 } 391 392 395 public GraphUndoManager getUndoManager() { 396 return undo; 397 } 398 399 402 public AbstractGraph getGraph() { 403 return graph; 404 } 405 406 407 public void setScale(double scale) { 408 scale = Math.max(Math.min(scale,2),0.1); 409 graph.setScale(scale); 410 } 412 413 protected void setALTCursorKeyboardShortcuts (JComponent jc) { 414 String focusUp="focusUp"; 415 String focusDown="focusDown"; 416 String focusLeft="focusLeft"; 417 String focusRight="focusRight"; 418 419 420 jc.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) 421 .put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,InputEvent.ALT_DOWN_MASK,false),focusUp); 422 jc.getActionMap().put(focusUp,new FocusNearestCell(this,FocusNearestCell.FOCUS_UP)); 423 jc.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) 424 .put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,InputEvent.ALT_DOWN_MASK,false),focusDown); 425 jc.getActionMap().put(focusDown,new FocusNearestCell(this,FocusNearestCell.FOCUS_DOWN)); 426 jc.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) 427 .put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,InputEvent.ALT_DOWN_MASK,false),focusLeft); 428 jc.getActionMap().put(focusLeft,new FocusNearestCell(this,FocusNearestCell.FOCUS_LEFT)); 429 jc.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) 430 .put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,InputEvent.ALT_DOWN_MASK,false),focusRight); 431 jc.getActionMap().put(focusRight,new FocusNearestCell(this,FocusNearestCell.FOCUS_RIGHT)); 432 433 } 434 435 436 440 public void refreshCollections (Set s1,Set s2) { 441 Set toAdd=new HashSet(); 442 Set toRemove=new HashSet(); 443 if (s1!=null) { 444 toRemove.addAll(s1); 445 if (s2!=null) { 446 toRemove.removeAll(s2); 447 } 448 } 449 if (s2!=null) { 450 toAdd.addAll(s2); 451 if (s1!=null) { 452 toAdd.removeAll(s1); 453 } 454 } 455 456 Set transToManage=new HashSet(); 457 if (s1!=null) { 458 transToManage.addAll(s1); 459 if (s2!=null) { 460 transToManage.removeAll(toRemove); 461 transToManage.addAll(toAdd); 462 } 463 Iterator it=transToManage.iterator(); 464 while (it.hasNext()) { 465 Object o=it.next(); 466 if (o instanceof Transition) { 467 graph.getWorkflowManager().correctXMLTransition((Transition)o); 468 } 469 } 470 } 471 472 graph.getWorkflowManager().refreshCollections(toAdd.toArray(),true); 473 graph.getWorkflowManager().refreshCollections(toRemove.toArray(),false); 474 } 475 476 477 481 public Action[] getActions() { 482 return defaultActions; 483 } 484 485 490 public Action getAction(String cmd) { 491 return (Action)commands.get(cmd); 492 } 493 495 496 498 protected void registerListeners(JGraph graph) { 499 graph.getSelectionModel().addGraphSelectionListener(this); 500 graph.getModel().addGraphModelListener(this); 501 graph.getGraphLayoutCache().addObserver(this); 502 } 503 504 505 protected void unregisterListeners(JGraph graph) { 506 graph.getSelectionModel().removeGraphSelectionListener(this); 507 graph.getModel().removeGraphModelListener(this); 508 } 509 511 513 public void valueChanged(GraphSelectionEvent e) {} 514 516 517 public void update(Observable obs, Object arg) { 519 JaWE.getInstance().setModified(true); 520 update(); 521 } 522 524 525 public void graphChanged(GraphModelEvent e) { 527 JaWE.getInstance().setModified(true); 528 update(); 529 } 530 532 533 public void refreshEditorConfiguration () { 534 SwingUtilities.updateComponentTreeUI(this); 535 for (int i=0;i<tabbedPane.getComponentCount(); i++) { 537 try { 538 JPanel p=(JPanel)tabbedPane.getComponent(i); 539 JToolBar toolbar=(JToolBar)p.getComponent(0); 540 toolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE); 541 toolbar.updateUI(); 542 } catch (Exception ex){} 543 } 544 statusBar.setVisible(JaWEConfig.getInstance().getStatusBarStatus()); 545 statusBar.updateMessage(); 546 getGraph().refreshGraphConfiguration(); 547 } 548 } 549 550 551 552 | Popular Tags |