KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > AbstractEditor


1 /* AbstractEditor.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  * Harald Meister harald.meister@abacus.ch
9  *
10  */

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    /**
46     * A factory for the menu, tool and popup bars
47     */

48    protected BarFactory barFactory = new BarFactory(this);
49
50    // various things needed for initializing and further work
51
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    // --- action implementations -----------------------------------
70
protected Undo undoAction = new Undo(this);
71    protected Redo redoAction = new Redo(this);
72
73    /**
74     * Actions defined by the Editor class
75     */

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       // creating undo manager
86
undoHandler = new UndoHandler(this,true);
87       undo = new GraphUndoManager();
88       // create the graph
89
graph=createGraph();
90
91       // registers graph's tooltip listener
92
ToolTipManager.sharedInstance().registerComponent(graph);
93
94       registerListeners(graph);
95
96       // Add this as a listener for undoable edits.
97
graph.getModel().addUndoableEditListener(undoHandler);
98       // install the command table
99
commands = new Hashtable();
100       // Actions defined by the concrete editor class
101
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       // creating panel
110
JPanel panel=new JPanel();
111       panel.setLayout(new BorderLayout());
112       // creating special items and butons
113
specialItems = createSpecialItems();
114       //specialButtons = createSpecialButtons();
115
JaWEMarqueeHandler pemh = (JaWEMarqueeHandler)graph.getMarqueeHandler();
116       specialButtons = pemh.getSpecialButtons();
117       // creating toolbars
118
toolbarComponents = new Hashtable();
119       tabbedPane=new JTabbedPane();
120       Component toolBarComponent = barFactory.createToolBars(tabbedPane);
121
122       // creating menubar
123
menuItems = new Hashtable();
124       menubar = barFactory.createMenubar();
125       panel.add(toolBarComponent,BorderLayout.NORTH);
126       add(menubar,BorderLayout.NORTH);
127       // seting property object of graph
128
graph.setPropertyObject(pkgOrProcess);
129       // creates the central component of editor
130
panel.add(createCenterComponent(),BorderLayout.CENTER);
131       JPanel mainPanel=new JPanel();
132       mainPanel.setLayout(new BorderLayout());
133       // adding toolbar and menubar to the main panel
134
mainPanel.add(menubar,BorderLayout.NORTH);
135       // adding center component to the main panel
136
mainPanel.add(panel,BorderLayout.CENTER);
137       // adding status bar to the south
138
mainPanel.add(createStatusBar(),BorderLayout.SOUTH);
139       // adding tabbed pane to the main main panel
140
add(createContentTabbedPane(mainPanel),BorderLayout.CENTER);
141       valueChanged(null);
142    }
143
144    /**
145     * Create the center component of this panel. This creates a scroll-
146     * pane for the current graph variable and stores the scrollpane
147     * in the scrollPane variable.
148     */

149    protected Component createCenterComponent() {
150       // creating graph pane
151
graphScrollPane = new JScrollPane(graph);
152       JViewport port = graphScrollPane.getViewport();
153       port.setScrollMode(JViewport.BLIT_SCROLL_MODE);
154
155       // Harald Meister: set bigger scroll-amounts, especially useful for
156
// mouse-wheel-scolling in large workflows
157
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       // creating text area pane for xpdl preview
173
xpdlPreview=new XPDLPreview(this);
174       textPreview = new TextPreview(this);
175
176       // creating tabbed pane
177
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    /**
209     * Create an editor to represent the given document.
210     */

211    protected abstract AbstractGraph createGraph();
212
213    /**
214     * Gets a title.
215     */

216    public abstract String JavaDoc getTitle ();
217
218    /**
219     * Returns string of toolbars for load.
220     */

221    public abstract String JavaDoc toolbarToLoad ();
222
223    /**
224     * Returns string of menubar for load.
225     */

226    public abstract String JavaDoc menubarToLoad ();
227
228    /** Creates actions used by editor. */
229    protected abstract void createActions ();
230
231    public AbstractEditor getParentEditor () {
232       return parentEditor;
233    }
234
235    protected void doEditorSpecificStuff() {};
236
237    public Object JavaDoc getSpecialButton (String JavaDoc key) {
238       return specialButtons.get(key);
239    }
240
241    public Map getSpecialButtons () {
242       return Collections.unmodifiableMap(specialButtons);
243    }
244
245    public Object JavaDoc getSpecialItem (String JavaDoc key) {
246       return specialItems.get(key);
247    }
248
249    public Map getSpecialItems () {
250       return Collections.unmodifiableMap(specialItems);
251    }
252
253    public void putMenuItem (String JavaDoc key,Object JavaDoc item) {
254       menuItems.put(key,item);
255    }
256
257    public void putToolbarComponent(String JavaDoc key,Object JavaDoc 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    /**
286     * Returns menu item if exist.
287     */

288    public JMenuItem getMenuItem (String JavaDoc cmd) {
289       return (JMenuItem)menuItems.get(cmd);
290    }
291
292    /**
293     * Returns toolbar button if exist.
294     */

295    public JComponent getToolbarComponent (String JavaDoc 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    /** Create special menu items - have no functionality at the moment.
312     * This is a place to add special things.
313     */

314    protected Hashtable createSpecialItems() {
315       return new Hashtable();
316    }
317
318    /**
319     * Find the hosting window.
320     */

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    /**
331     * Invoked after the selection or the model has changed.
332     */

333    public void update() {
334       // FIX: Test Multi-View Undo
335
// Remove View-Observer (cannot listen to other views)
336
undoAction.update();
337       redoAction.update();
338       // FIX: End of Test
339

340       try {
341          setTitle(getWindow(),getTitle());
342       } catch (Exception JavaDoc ex) {}
343    }
344
345    /**
346     * Shows a window.
347     */

348    public void showWindow (Window parentWindow,String JavaDoc 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    /**
359     * Sets the title to the window.
360     */

361    public void setTitle (Window w,String JavaDoc 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    /**
372     * Determines the loop bounds depending on it's parent bounds.
373     */

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    /**
384     * Resets UndoManager.
385     */

386    public void resetUndoManager() {
387       undo.discardAllEdits();
388       undoAction.update();
389       redoAction.update();
390    }
391
392    /**
393     *
394     */

395    public GraphUndoManager getUndoManager() {
396       return undo;
397    }
398
399    /**
400     *
401     */

402    public AbstractGraph getGraph() {
403       return graph;
404    }
405
406    /** Sets the value of scale for zooming. */
407    public void setScale(double scale) {
408       scale = Math.max(Math.min(scale,2),0.1);
409       graph.setScale(scale);
410       //componentResized(null);
411
}
412
413    protected void setALTCursorKeyboardShortcuts (JComponent jc) {
414       String JavaDoc focusUp="focusUp";
415       String JavaDoc focusDown="focusDown";
416       String JavaDoc focusLeft="focusLeft";
417       String JavaDoc 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    /**
437     * Refreshes all collections within WorkflowProcess tag (thus means
438     * the WorkflowProcesses collection for this instance of JaWE).
439     */

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 JavaDoc 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    // ************************* ACTION STUFF *****************************
478
/**
479     * Fetch the list of actions supported by this editor.
480     */

481    public Action[] getActions() {
482       return defaultActions;
483    }
484
485    /**
486     * Method to get editor's action corresponding to the given string.
487     * @param cmd String representation of editor's action.
488     * @return action specified by the string cmd.
489     **/

490    public Action getAction(String JavaDoc cmd) {
491       return (Action)commands.get(cmd);
492    }
493    // ********************* END OF ACTION STUFF **************************
494

495
496    //********************* REGISTRATION AND REMOVAL OF LISTENERS *****************
497
/* Add this documents listeners to the specified graph. */
498    protected void registerListeners(JGraph graph) {
499       graph.getSelectionModel().addGraphSelectionListener(this);
500       graph.getModel().addGraphModelListener(this);
501       graph.getGraphLayoutCache().addObserver(this);
502    }
503
504    /* Remove this documents listeners from the specified graph. */
505    protected void unregisterListeners(JGraph graph) {
506       graph.getSelectionModel().removeGraphSelectionListener(this);
507       graph.getModel().removeGraphModelListener(this);
508    }
509    //***************** END OF REGISTRATION AND REMOVAL OF LISTENERS **************
510

511    //*********************** GRAPH SELECTION LISTENER INTERFACE ******************
512
/** Enables and disables various actions depending of cell selection */
513    public void valueChanged(GraphSelectionEvent e) {}
514    //******************* END OF GRAPH SELECTION LISTENER INTERFACE ***************
515

516
517    //****************************** OBSERVER INTERFACE ***************************
518
public void update(Observable obs, Object JavaDoc arg) {
519       JaWE.getInstance().setModified(true);
520       update();
521    }
522    //************************** END OF OBSERVER INTERFACE ************************
523

524
525    //***************************** GRAPH MODEL LISTENER INTERFACE ****************
526
public void graphChanged(GraphModelEvent e) {
527       JaWE.getInstance().setModified(true);
528       update();
529    }
530    //************************* END OF GRAPH MODEL LISTENER INTERFACE *************
531

532
533    public void refreshEditorConfiguration () {
534       SwingUtilities.updateComponentTreeUI(this);
535       // BUG WITH Java 1.4.2 -> must set back Rollover property of toolbars
536
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 JavaDoc ex){}
543       }
544       statusBar.setVisible(JaWEConfig.getInstance().getStatusBarStatus());
545       statusBar.updateMessage();
546       getGraph().refreshGraphConfiguration();
547    }
548 }
549
550
551 /* End of AbstractEditor.java */
552
Popular Tags