KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* PEGraphUI.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  * xxp xxp1978@163.com
10  *
11  */

12
13 package org.enhydra.jawe;
14
15 import org.enhydra.jawe.graph.*;
16
17 import org.jgraph.JGraph;
18 import org.jgraph.graph.*;
19 import org.jgraph.plaf.basic.*;
20
21 import java.awt.datatransfer.*;
22
23 import java.awt.*;
24 import java.awt.geom.*;
25 import java.awt.event.*;
26 import java.util.*;
27 import javax.swing.*;
28 import javax.swing.tree.*;
29 import javax.swing.undo.UndoableEdit JavaDoc;
30 import org.enhydra.jawe.PackageEditor;
31
32 /**
33  * This class and it's inner classes controls mouse actions and clipboard.
34  * It is addapted to get wanted editing cell behaviour, selection behaviour
35  * , to implement cell overlaping, to implement right participant adjustment
36  * after cell (or group of cells) is moved, and to implement proper copying
37  * and pasting/cloning of cells, as well as pasting at wanted location (along
38  * with right participant adjustment).
39  */

40 public class JaWEGraphUI extends BasicGraphUI {
41
42    /** Variable that correct selection on mouse release. */
43    protected boolean canMouseReleaseSelectCell=true;
44
45    /**
46     * Point that represents paste location. It is set from editor
47     * when PasteAt action was triggered by popup menu.
48     */

49    protected Point insertionPoint=null;
50
51    /**
52     * Returns graph.
53     */

54    public AbstractGraph getGraph() {
55       return (AbstractGraph)graph;
56    }
57
58    /**
59     * Sets location for pasting cells.
60     */

61    public void setInsertionPoint(Point p) {
62       insertionPoint=p;
63    }
64
65    /**
66     * Paint the background of this graph. Calls paintGrid.
67     */

68    protected void paintBackground(Graphics g) {
69       Rectangle pageBounds = new Rectangle(0,0,graph.getWidth(),graph.getHeight());
70
71       if (graph.isGridVisible()) {
72          paintGrid(graph.getGridSize(),g,pageBounds);
73       }
74    }
75
76    /**
77     * This method is called by EditAction class, as well as by
78     * pressing F2 or clicking a mouse on a cell.
79     */

80    protected boolean startEditing(Object JavaDoc cell, MouseEvent event) {
81       if (cell instanceof WorkflowElement) {
82          Window parentW=getGraph().getEditor().getWindow();
83          ((WorkflowElement)cell).showPropertyDialog(parentW,getGraph());
84          return true;
85       }
86       return false;
87    }
88
89    // FIXED by xxp - there was a problem when zooming-out activity
90
public void startEditingAtCell(JGraph graph,Object JavaDoc cell) {
91       if(cell!=null) startEditing(cell,null);
92    }
93
94    /**
95     * Creates the listener responsible for updating the selection based on
96     * mouse events.
97     */

98    protected MouseListener createMouseListener() {
99       return new PEMouseHandler();
100    }
101
102    /**
103     * Handles selection in a way that we expect.
104     */

105    public class PEMouseHandler extends MouseHandler {
106
107       public void mousePressed(MouseEvent e) {
108          handler = null;
109          if (!e.isConsumed() && graph.isEnabled()) {
110             graph.requestFocus();
111             int s = graph.getTolerance();
112             //Rectangle r = graph.fromScreen(new Rectangle(e.getX()-s, e.getY()-s, 2*s, 2*s));
113
Rectangle2D r = graph.fromScreen(//HM, JGraph3.4.1
114
GraphConstants.createRect(e.getX()-s, e.getY()-s, 2*s, 2*s));
115             focus = (focus != null && focus.intersects(graph.getGraphics(), r))
116                ? focus : null;
117             Point2D point = graph.fromScreen(GraphConstants.createPoint(e.getPoint()));//HM, JGraph3.4.1
118
cell = graph.getNextViewAt(focus, point.getX(), point.getY());//HM, JGraph3.4.1
119

120             // changed from original because of overlapping
121
if (focus == null) {
122                cell = graph.getNextViewAt(focus, point.getX(), point.getY());//HM, JGraph3.4.1
123
focus = cell;
124             }
125             else {
126                cell = focus;
127             }
128
129             // added - if this is activity, set it to be the last child
130
// of it's parent because of possible overlaping
131
if (focus != null) {
132
133                Object JavaDoc fcell=focus.getCell();
134                if (fcell instanceof Activity){
135                   DefaultMutableTreeNode child=(DefaultMutableTreeNode)fcell;
136                   DefaultMutableTreeNode parent=(DefaultMutableTreeNode)
137                      child.getParent();
138                   // this is security check (when we set up all things
139
// this if statement could be removed because every
140
// Activity should have it's parent
141
if (parent != null) {
142                      parent.insert(child,parent.getChildCount()-1);
143                      // must refresh child views to get it in a right order
144
// which affect painting and determining which cell is
145
// under mouse cursor
146
((ParticipantView)focus.getParentView()).refreshChildViews();
147                   }
148                }
149
150                // graphView.toFront(new CellView[] {focus});
151
}
152
153             cancelEditing(graph);
154
155             // selecting cell on mouse press
156
if (cell != null && !graph.isCellSelected(cell.getCell())) {
157                selectCellForEvent(cell.getCell(),e);
158                canMouseReleaseSelectCell=false;
159             }
160
161             if (e.getClickCount()==graph.getEditClickCount() && focus!=null &&
162                 !SwingUtilities.isRightMouseButton(e)) {
163                //&& focus.isLeaf()) {
164
// Start Editing
165
graph.startEditingAtCell(focus.getCell());
166                e.consume();
167                cell = null;
168             }
169             else if (!isToggleSelectionEvent(e) && !isForceMarqueeEvent(e) &&
170                      focus != null) {
171                // Immediate Selection
172
if (handle != null) {
173                   handle.mousePressed(e);
174                   handler = handle;
175                }
176             }
177                //Marquee Selection
178
else if (!isToggleSelectionEvent(e) || focus == null) {
179                if (marquee != null) {
180                   marquee.mousePressed(e);
181                   // imeddiately clears selection when clicked on empty area
182
if (focus==null) {
183                      Object JavaDoc[] cells=(new ArrayList()).toArray();
184                      selectCellsForEvent(cells, e);
185                   }
186                   handler = marquee;
187                }
188             }
189
190          }
191       }
192
193       public void mouseDragged(MouseEvent e) {
194          // added - if one of selected cell is Participant there must be no dragging
195
Object JavaDoc[] sc=graph.getSelectionCells();
196          if (sc != null) {
197             for (int i=0; i<sc.length; i++) {
198                if (sc[i] instanceof Participant) {
199                   e.consume();
200                   return;
201                }
202             }
203          }
204
205          try {
206             autoscroll(graph, e.getPoint());
207             if (handler == marquee) {
208                marquee.mouseDragged(e);
209             }
210             else if (handler == null && !isEditing(graph) && focus != null) {
211                if (!graph.isCellSelected(focus.getCell())) {
212                   // removed from original
213
//selectCellForEvent(focus.getCell(), e);
214
cell = null;
215                }
216                if (handle != null) {
217                   handle.mousePressed(e);
218                }
219                handler = handle;
220             }
221             if (handle != null && handler == handle) {
222                handle.mouseDragged(e);
223             }
224          } finally {
225             //cell = null;
226
}
227       }
228
229       public void mouseReleased(MouseEvent e) {
230          try {
231             if (e!=null && !e.isConsumed()) {
232                if (handler == marquee) {
233                   marquee.mouseReleased(e);
234                }
235                else if (handler == handle && handle != null) {
236                   handle.mouseReleased(e);
237                }
238
239                // something added and something removed to suite our needs
240
if (!e.isConsumed() && focus != null &&
241                    !SwingUtilities.isRightMouseButton(e)) {
242                   if (canMouseReleaseSelectCell) {
243                      selectCellForEvent(focus.getCell(),e);
244                   }
245                }
246                canMouseReleaseSelectCell=true;
247             }
248          }
249          finally {
250             insertionLocation = null;
251             handler = null;
252             cell = null;
253          }
254       }
255
256       /**
257        * Introduced to patch JGraph2.0 - it didn't set approprate cursor.
258        */

259       public void mouseMoved(MouseEvent e) {
260          if (graph != null && graph.isEnabled()) {
261             if (marquee != null)
262                marquee.mouseMoved(e);
263             if (handle != null)
264                handle.mouseMoved(e);
265             if (!e.isConsumed())
266                graph.setCursor(Cursor.getDefaultCursor());
267          }
268       }
269    }
270
271    /**
272     * Constructs the "root handle" for <code>context</code>.
273     *
274     * @param context reference to the context of the current selection.
275     */

276    public CellHandle createHandle(GraphContext context) {
277       if (context != null && !context.isEmpty() && graph.isEnabled())
278          return new PERootHandle(context);
279       return null;
280    }
281
282    /**
283     * Manages selection movement. It is adapted to suport proper
284     * undo in coordination with WorkflowManager class.
285     */

286    public class PERootHandle extends RootHandle {
287       /**
288        * Creates a root handle which contains handles for the given
289        * cells. The root handle and all its childs point to the
290        * specified JGraph instance. The root handle is responsible
291        * for dragging the selection.
292        */

293       public PERootHandle(GraphContext ctx) {
294          super(ctx);
295       }
296
297       protected Point2D getInitialLocation(Object JavaDoc[] cells) {
298          try {
299             return super.getInitialLocation(cells);
300          } catch (Throwable JavaDoc thr){
301             return null;
302          }
303       }
304
305       public void mouseReleased(MouseEvent event) {
306          if (event != null && !event.isConsumed()) {
307             if (activeHandle != null) {
308                activeHandle.mouseReleased(event);
309                activeHandle = null;
310             } else if (isMoving&& !event.getPoint().equals(start)) {
311                if (cachedBounds != null) {
312                   int dx = event.getX()-(int)start.getX();//HM, JGraph3.4.1
313
int dy = event.getY()-(int)start.getY();//HM, JGraph3.4.1
314
Point2D tmp = graph.fromScreen(GraphConstants.createPoint(dx, dy));//HM, JGraph3.4.1
315
GraphLayoutCache.translateViews(views, tmp.getX(), tmp.getY());//HM, JGraph3.4.1
316
}
317
318
319                // Harald Meister: snap activities to grid if grid is enabled
320
if (JaWEConfig.getInstance().getGridStatus() &&
321                    views[0] instanceof ActivityView ){
322                   ActivityView view = (ActivityView)views[0];
323                   Rectangle2D rect = view.getBounds();//HM, JGraph3.4.1
324
int dx = 0;
325                   int dy = 0;
326
327                   int gridsize = JaWEConfig.getInstance().getGridSize();
328                   int deltax = (int)rect.getX() % gridsize;
329                   int deltay = (int)rect.getY() % gridsize;
330                   int halfgrid = gridsize / 2;
331                   if (deltax > halfgrid){
332                      dx += (gridsize - deltax);
333                   } else {
334                      dx -= deltax;
335                   }
336                   if (deltay > halfgrid){
337                      dy += (gridsize - deltay);
338                   } else {
339                      dy -= deltay;
340                   }
341                   Point2D tmp = graph.fromScreen(GraphConstants.createPoint(dx, dy));//HM, JGraph3.4.1
342
GraphLayoutCache.translateViews(views, tmp.getX(), tmp.getY());//HM, JGraph3.4.1
343
}
344                // Harald Meister
345

346
347                CellView[] all = graphLayoutCache.getAllDescendants(views);
348
349                if (event.isControlDown() && graph.isCloneable()) { // Clone Cells
350
Object JavaDoc[] cells = graph.getDescendants(context.getCells());
351                   ConnectionSet cs = ConnectionSet.create(graphModel,cells,false);
352                   cs.addConnections(all);
353                   Map propertyMap = GraphConstants.createAttributes(all, null);
354                   insertCells(context.getCells(), propertyMap, cs, true, 0, 0);
355                   // for now I can't allow distinction in case if it is attributeStore or not
356
} else if (graph.isMoveable()){ // Move Cells
357
//if (!graphModel.isAttributeStore()) {
358
//Map propertyMap = GraphConstants.createPropertyMap(all,null);
359
Map propertyMap = GraphConstants.createAttributes(all,null);
360                   WorkflowManager dm=getGraph().getWorkflowManager();
361                   dm.moveCellsAndArrangeParticipants(propertyMap);
362                   /*} else {
363                    Map propertyMap = GraphConstants.createPropertyMap(all,null);
364                    WorkflowManager dm=getGraph().getWorkflowManager();
365                    dm.moveCellsAndArrangeParticipants(propertyMap);
366                    }*/

367                }
368                event.consume();
369             }
370          }
371          start = null;
372       }
373
374    }
375
376    /**
377     * Returns a listener that can update the graph when the model changes.
378     */

379    protected Observer createGraphViewObserver() {
380       return new PEGraphViewObserver();
381    }
382
383    /**
384     * This class observes view changes and is adapted to disallow
385     * deselection of cells after dragging.
386     */

387    public class PEGraphViewObserver extends GraphViewObserver {
388
389       public void update(Observable o, Object JavaDoc arg) {
390          super.update(o,arg);
391          // do not allow "deselection" on mouse release because there was dragging
392
canMouseReleaseSelectCell=false;
393       }
394    }
395
396
397    /**
398     * Creates an instance of TransferHandler. Used for subclassers
399     * to provide different TransferHandler.
400     */

401    protected TransferHandler createTransferHandler() {
402       return new PETransferHandler();
403    }
404
405    /**
406     * PETransferHandler that supports pasteAt action and properly cut
407     * action. The new class must have been written on the basis of class
408     * GraphTransferableHandler because of it's final method createTransferable
409     * which couldn't be overriden. This method had to be modified to forbid
410     * copying of Participants, Subflows and Transitions.
411     */

412    public class PETransferHandler extends TransferHandler {
413       protected boolean isCut = false;
414
415       /* Pointer to the last inserted array of cells. */
416       protected Object JavaDoc out, in;
417
418       /* How many times the last transferable was inserted. */
419       protected int inCount = 0;
420
421       public boolean canImport(JComponent comp, DataFlavor[] flavors) {
422          return true;
423       }
424
425       // ORIGINAL METHOD CHANGED
426
protected final Transferable createTransferable(JComponent c) {
427          if (c instanceof JGraph) {
428             JGraph graph = (JGraph) c;
429
430             Object JavaDoc[] cells = graphLayoutCache.order(graph.getSelectionCells());
431             if (cells != null && cells.length > 0) {
432                // removing everything but generic, subflow and route activity -
433
// only they can be copied
434
boolean dispMessage=false;
435                Set cellsToCopy=new HashSet();
436                for (int i=0; i<cells.length; i++) {
437                   if (!(cells[i] instanceof Participant) &&
438                       // !(cells[i] instanceof Transition) && //HM: enable Transition-copy/paste
439
!(cells[i] instanceof Start) &&
440                       !(cells[i] instanceof End) &&
441                       !(cells[i] instanceof BlockActivity)) {
442                      cellsToCopy.add(cells[i]);
443                   } else {
444                      if (!(cells[i] instanceof Transition)) dispMessage=true;
445                   }
446                }
447                //HM: remove transitions which source and/or target are not copied too
448
HashSet cellsToRemove = new HashSet();
449                for (Iterator it = cellsToCopy.iterator(); it.hasNext();) {
450                   Object JavaDoc obj = (Object JavaDoc) it.next();
451                   if (obj instanceof Transition){
452                      boolean sourceOK = false, targetOK = false;
453
454                      for (Iterator it2 = cellsToCopy.iterator(); it2.hasNext();) {
455                         Object JavaDoc obj2 = (Object JavaDoc) it2.next();
456                         if (obj2 instanceof Activity){
457                            if ( ((Transition)obj).getSourceActivity() == obj2 ){
458                               sourceOK = true;
459                            }
460                            if ( ((Transition)obj).getTargetActivity() == obj2 ){
461                               targetOK = true;
462                            }
463                         }
464                      }
465
466                      if (!sourceOK || !targetOK){
467                         cellsToRemove.add(obj);
468                      }
469                   }
470                }
471                cellsToCopy.removeAll(cellsToRemove);
472
473                if (cellsToCopy.size() != cells.length) {
474                   // warning message is not displayed in a case of copying transitions
475
if (dispMessage) {
476                      String JavaDoc appTitle=JaWE.getAppTitle();
477                      JOptionPane.showMessageDialog(getGraph().
478                                                       getEditor().getWindow(),
479                                                    ResourceManager.getLanguageDependentString(
480                                                       "WarningOnlyGenericSubflowAndRouteActivitiesCanBeCopied"),
481                                                    appTitle,JOptionPane.WARNING_MESSAGE);
482                   }
483                   cells=cellsToCopy.toArray();
484                   if (cells.length == 0) return null;
485                }
486                out = cells;
487                ParentMap pm = ParentMap.create(graphModel, cells, false, true);
488                // I wan't get bother with connections here, but will
489
// stop it from inserting into a model in insertCells method
490
ConnectionSet cs = ConnectionSet.create(graphModel, cells, false);
491                //Map viewAttributes = GraphConstants.createPropertyMap(flat,graphView);
492
Map viewAttributes = GraphConstants.createAttributes(cells,graphLayoutCache);
493                Rectangle2D bounds = graph.getCellBounds((Object JavaDoc[])out);//HM, JGraph3.4.1
494
return create(graph, cells, viewAttributes, bounds, cs, pm);
495
496             }
497          }
498          return null;
499       }
500
501       protected GraphTransferable create(
502          JGraph graph,
503          Object JavaDoc[] cells,
504          Map viewAttributes,
505          Rectangle2D bounds,//HM, JGraph3.4.1
506
ConnectionSet cs,
507          ParentMap pm) {
508          return new GraphTransferable(cells, viewAttributes, bounds, cs, pm);
509       }
510
511       protected void exportDone(
512          JComponent comp,
513          Transferable data,
514          int action) {
515          if (comp instanceof JGraph && data instanceof GraphTransferable) {
516             if (action == TransferHandler.MOVE) {
517                JGraph graph = (JGraph) comp;
518                Object JavaDoc[] cells = ((GraphTransferable) data).getCells();
519                //graph.getGraphLayoutCache().remove(cells);
520
removeCells(graph,cells); //ORIGINAL METHOD CHANGED
521
}
522          }
523          updateHandle();
524          setInsertionLocation(null);
525       }
526
527       public void exportToClipboard(JComponent compo, Clipboard clip, int action) {
528          isCut = (action == TransferHandler.MOVE);
529          super.exportToClipboard(compo, clip, action);
530       }
531
532       public int getSourceActions(JComponent c) {
533          return COPY_OR_MOVE;
534       }
535
536       //This method was changed to support pasteAt action.
537
public boolean importData(JComponent comp, Transferable t) {
538          try {
539             if (comp instanceof JGraph) {
540                JGraph graph = (JGraph)comp;
541                if (t.isDataFlavorSupported(GraphTransferable.dataFlavor)) {
542                   Object JavaDoc obj = t.getTransferData (GraphTransferable.dataFlavor);
543                   GraphTransferable gt = (GraphTransferable)obj;
544                   if (out != gt.getCells() || insertionLocation == null)
545                      return handleInsert(graph, gt);
546                   return true;
547                }
548             }
549          } catch (Exception JavaDoc exception) {
550          } finally {
551             isCut = false;
552          }
553          return false;
554       }
555
556       /**
557        * This method was changed to support pasteAt action.
558        */

559       protected boolean handleInsert(JGraph graph, GraphTransferable t) {
560          Object JavaDoc[] cells = t.getCells();
561          boolean c = out != cells || !isCut;
562          if (in != cells) {
563             inCount = (c) ? 1 : 0;
564          }
565          in = cells;
566          int dx = inCount * (int)graph.getGridSize();//HM, JGraph3.4.1
567
int dy = inCount * (int)graph.getGridSize();//HM, JGraph3.4.1
568

569          Map attributeMap=null;
570
571          // changed to enable pasteAt action
572
if ((insertionLocation != null) || (insertionPoint != null)) {
573             Point p = t.getBounds().getBounds().getLocation();//HM, JGraph3.4.1
574
Point insPoint=insertionLocation;
575             if (insPoint == null) {
576                insPoint=insertionPoint;
577             }
578
579             // Harald Meister: snap to grid if grid is enabled
580
if (JaWEConfig.getInstance().getGridStatus()){
581                int gridsize = JaWEConfig.getInstance().getGridSize();
582                int deltax = insPoint.x % gridsize;
583                int deltay = insPoint.y % gridsize;
584                int halfgrid = gridsize / 2;
585                if (deltax > halfgrid){
586                   insPoint.x += (gridsize - deltax);
587                } else {
588                   insPoint.x -= deltax;
589                }
590                if (deltay > halfgrid){
591                   insPoint.y += (gridsize - deltay);
592                } else {
593                   insPoint.y -= deltay;
594                }
595             }
596             // Harald Meister
597

598             dx = insPoint.x - p.x;
599             dy = insPoint.y - p.y;
600
601             if (insertionPoint != null) {
602                // translating cells at clipboard to new location
603
attributeMap=t.getAttributeMap();
604                GraphConstants.translate(attributeMap.values(),dx,dy);
605
606                // also translating the bounding rectangle of cells at clipboard
607
Rectangle rect = t.getBounds().getBounds();//HM, JGraph3.4.1
608
rect.translate(dx,dy);//HM, JGraph3.4.1
609
t.getBounds().setRect(rect);//HM, JGraph3.4.1
610

611                // there is no need for further translation in insertCells method
612
dx=0; dy=0;
613                // reseting counter
614
inCount=0;
615             }
616          }
617          inCount++;
618          if (cells == out || graph.isDropEnabled()) {
619             if (attributeMap==null) attributeMap=t.getAttributeMap();
620             insertCells(cells,attributeMap,t.getConnectionSet(),c,dx,dy);
621             return true;
622          }
623          return false;
624       }
625
626       protected void removeCells(JGraph graph, Object JavaDoc[] cells) {
627          WorkflowManager dm=getGraph().getWorkflowManager();
628          dm.removeCellsAndArrangeParticipants(cells);
629       }
630    }
631
632    /**
633     * Insert the specified cells into the model. If clone is true,
634     * the cells are cloned an all datastructures are updated to
635     * reference these clones. Clones are created using the
636     * GraphModel.clone(Object)-method.
637     * If cells are cloned, they are translated using the given amount.
638     * This method is modified from original to support pasteAt action,
639     * and to forbid pasting of Participants, Subflows and Transitions as well
640     * as to fix a bug which didn't clone cell's userObject properly.
641     */

642    public void insertCells(Object JavaDoc[] cells, Map viewAttributeMap,
643                            ConnectionSet cs, boolean clone, int dx, int dy) {
644
645       Object JavaDoc[] insert;
646       // retreive workflow manager
647
WorkflowManager wm=getGraph().getWorkflowManager();
648
649       // retreive property object
650
Object JavaDoc wp=getGraph().getPropertyObject();
651
652       String JavaDoc appTitle=JaWE.getAppTitle();
653
654       // added to avoid cloning/pasting participants and transitions
655
Set cellsToCloneOrPaste = new HashSet();
656       boolean dispMessage=false;
657       for (int i=0; i<cells.length; i++) {
658          if (!(cells[i] instanceof Participant) &&
659              // !(cells[i] instanceof Transition) && //HM: enable Transition-copy/paste
660
!(cells[i] instanceof Start) &&
661              !(cells[i] instanceof End) &&
662              !(cells[i] instanceof BlockActivity)) {
663             // if this is not an activity from the same process, forbid copying
664
//(can't do cross-graph copy of activities)
665
if ( cells[i] instanceof Activity && //HM: enable Transition-copy/paste
666
((org.enhydra.jawe.xml.elements.Activity)
667                        ((Activity)cells[i]).getUserObject()).getOwnerProcess()!=wp) {
668                JOptionPane.showMessageDialog(getGraph().getEditor().getWindow(),
669                                              ResourceManager.getLanguageDependentString("ErrorCrossGraphCopyIsForbidden"),
670                                              appTitle,JOptionPane.ERROR_MESSAGE);
671                return;
672             } else {
673                cellsToCloneOrPaste.add(cells[i]);
674             }
675
676             //Harald Meister: hack to add DefaultPort after Cut/Paste
677
if (cells[i] instanceof Activity &&
678                    (((Activity)cells[i]).getChildren() == null ||
679                        ((Activity)cells[i]).getPort() == null)){
680                DefaultPort port = new DefaultPort("Center");
681                ((Activity)cells[i]).add(port);
682             }
683             //Harald Meister
684

685          } else {
686             if (!(cells[i] instanceof Transition)) dispMessage=true;
687          }
688       }
689       if (cellsToCloneOrPaste.size() != cells.length) {
690          // warning message is displayed in case of copying participants but
691
// not in a case of copying transitions
692
if (dispMessage){
693             JOptionPane.showMessageDialog(getGraph().getEditor().getWindow(),
694                                           ResourceManager.getLanguageDependentString(
695                                              "WarningOnlyGenericSubflowAndRouteActivitiesCanBePastedOrCloned"),
696                                           appTitle,JOptionPane.WARNING_MESSAGE);
697          }
698          cells=cellsToCloneOrPaste.toArray();
699       }
700
701       if (cellsToCloneOrPaste.size()>0) {
702          // I don't care if it is clonning or cell was cutted, I always
703
// clone cells because otherwise, I wouldn't have correct undo
704
Map cellMap = graph.cloneCells(cells);
705
706          viewAttributeMap = GraphConstants.replaceKeys(cellMap, viewAttributeMap);
707
708          cs = new ConnectionSet(); //HM: enable Transition-copy/paste
709

710          // if there was a cloning and model is attribute store,
711
// must fix value attribute
712
//if (clone && graphModel.isAttributeStore()) {
713
if (clone) {
714             Iterator it=viewAttributeMap.entrySet().iterator();
715             while (it.hasNext()) {
716                Map.Entry entry = (Map.Entry)it.next();
717                Object JavaDoc userObject=((DefaultMutableTreeNode)entry.getKey()).
718                   getUserObject();
719                Map map=(Map)entry.getValue();
720                GraphConstants.setValue(map,userObject);
721
722                //HM: change the from/to in transitions to the proper objects
723
if (entry.getKey() instanceof org.enhydra.jawe.graph.Transition){
724                   Transition tr = (Transition)entry.getKey();
725                   org.enhydra.jawe.xml.elements.Transition trUo =
726                      (org.enhydra.jawe.xml.elements.Transition)tr.getUserObject();
727                   Activity source = null, target = null;
728                   for (Iterator it1 = cellMap.entrySet().iterator(); it1.hasNext();) {
729                      Map.Entry en1 = (Map.Entry)it1.next();
730                      if (en1.getKey() instanceof Activity){
731                         org.enhydra.jawe.xml.elements.Activity actUo =
732                            (org.enhydra.jawe.xml.elements.Activity)((Activity)en1.getKey()).getUserObject();
733                         if (trUo.getFrom() == actUo){
734                            source = (Activity)en1.getValue();
735                            tr.setSource(null);
736                            trUo.setFrom(
737                                           (org.enhydra.jawe.xml.elements.Activity)source.getUserObject() );
738                         }
739                         if (trUo.getTo() == actUo){
740                            target = (Activity)en1.getValue();
741                            tr.setTarget(null);
742                            trUo.setTo(
743                                         (org.enhydra.jawe.xml.elements.Activity)target.getUserObject());
744                         }
745                      }
746                   }
747                   if (source != null && target != null){
748                      cs.connect(tr,source.getPort(),target.getPort());
749                   }
750                }
751             }
752          }
753
754          // cs = cs.clone(cellMap); //HM: enable Transition-copy/paste
755
insert = new Object JavaDoc[cells.length];
756
757          for (int i = 0; i < cells.length; i++) {
758             insert[i] = cellMap.get(cells[i]);
759          }
760
761          // moving pasted/cloned cells to new location if needed
762
if (dx!=0 || dy!=0) {
763             GraphConstants.translate(viewAttributeMap.values(), dx, dy);
764          }
765
766          // inserting pasted/cloned cells into model (finding it's parents
767
// and arranging participants) this is the place where I avoided to
768
// insert connection sets
769
wm.insertCellsAndArrangeParticipants(insert,viewAttributeMap, cs);
770       }
771    }
772
773 }
774
775
Popular Tags