KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > graph > TransitionView


1 /* TransitionView.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  *
9  */

10
11 package org.enhydra.jawe.graph;
12
13 import org.enhydra.jawe.*;
14
15 import org.jgraph.graph.*;
16 import org.jgraph.JGraph;
17
18 import java.util.*;
19 import java.awt.*;
20 import java.awt.geom.*;
21 import java.awt.event.*;
22 import javax.swing.undo.UndoableEdit JavaDoc;
23
24 /**
25  * Represents a view for a model's Transition object.
26  */

27 public class TransitionView extends EdgeView {
28
29
30    /**
31     * Constructs an edge view for the specified model object.
32     *
33     * @param cell reference to the model object
34     */

35    public TransitionView(Object JavaDoc cell, JGraph graph, CellMapper mapper) {
36       super(cell,graph,mapper);
37       renderer=new TransitionRenderer();
38       /*int x = (int) (GraphConstants.PERMILLE / 2);
39       int y = (int) (GraphConstants.PERMILLE / 100);
40       Point center = new Point(x, y);
41       Map map = GraphConstants.createMap();
42       GraphConstants.setLabelPosition(map, center);
43       GraphConstants.setAutoSize(map, true);
44        this.setAttributes(map);*/

45    }
46
47    /**
48     * Returns a cell handle for the view.
49     */

50    public CellHandle getHandle(GraphContext context) {
51       return new TransitionHandle(this, context);
52    }
53
54    /**
55     * Inserts a "break point" at transition object at the point where
56     * popup menu appeared.
57     */

58    public void addPoint (Point popupPoint) {
59       boolean bendable = graph.isBendable() &&
60          GraphConstants.isBendable(getAttributes());
61       if (bendable) {
62          int index=-1;
63          int s = graph.getHandleSize();
64          //Rectangle rect = graph.fromScreen(new Rectangle(popupPoint.x-s,popupPoint.y-s,2*s,2*s));
65
Rectangle rect = new Rectangle(popupPoint.x-s,popupPoint.y-s,2*s,2*s);
66          if (intersects(graph.getGraphics(),rect)) {
67             Point point = (Point)graph.snap(new Point(popupPoint));//HM, JGraph3.4.1
68
double min = Double.MAX_VALUE, dist = 0;
69             for (int i = 0; i < getPointCount()-1; i++) {
70                Point p = new Point((int)getPoint(i).getX(),(int)getPoint(i).getY());//HM, JGraph3.4.1
71
Point p1 = new Point((int)getPoint(i+1).getX(),(int)getPoint(i+1).getY());//HM, JGraph3.4.1
72
dist = new Line2D.Double(p, p1).ptLineDistSq(point);
73                if (dist < min) {
74                   min = dist;
75                   index = i+1;
76                }
77             }
78             if (index != -1) {
79                addPoint(index,point);
80                Map propertyMap = new HashMap();
81                Map edgeMap=GraphConstants.cloneMap(((GraphCell)cell).getAttributes());
82                GraphConstants.setPoints(edgeMap,points);
83                propertyMap.put(cell,edgeMap);
84                String JavaDoc undoMsg=ResourceManager.getLanguageDependentString("MessageAddingBreakPointAtTransition");
85                ((JaWEGraphModel)getModel()).insertAndEdit(null,propertyMap,null,null,null,undoMsg);
86             }
87          }
88       }
89    }
90
91    /**
92     * Inserts a "break point" programatically.
93     */

94    public void addPointProgramatically (Point popupPoint,int index) {
95       if (popupPoint==null) return;
96       int s = graph.getHandleSize();
97       if (index<1) index=1;
98       if (index>getPointCount()-1) index=getPointCount()-1;
99       try {
100          addPoint(index,new Point(popupPoint));
101          Map propertyMap = new HashMap();
102          Map edgeMap=GraphConstants.cloneMap(((GraphCell)cell).getAttributes());
103          GraphConstants.setPoints(edgeMap,points);
104          propertyMap.put(cell,edgeMap);
105          String JavaDoc undoMsg=ResourceManager.getLanguageDependentString("MessageAddingBreakPointAtTransition");
106          ((JaWEGraphModel)getModel()).insertAndEdit(null,propertyMap,null,null,null,undoMsg);
107       } catch (Exception JavaDoc ex) {}
108
109    }
110
111
112    /**
113     * Removes a "break point" from transition at the point where
114     * popup menu appeared.
115     */

116    public void removePoint (Point popupPoint) {
117       boolean bendable = graph.isBendable() &&
118          GraphConstants.isBendable(getAttributes());
119       if (bendable) {
120          int index=-1;
121          int s = graph.getHandleSize();
122          //Rectangle rect = graph.fromScreen(new Rectangle(popupPoint.x-s,popupPoint.y-s,2*s,2*s));
123
Rectangle rect = new Rectangle(popupPoint.x-s,popupPoint.y-s,2*s,2*s);
124          if (intersects(graph.getGraphics(),rect)) {
125             Point point = (Point)graph.snap(new Point(popupPoint));//HM, JGraph3.4.1
126
double min = Double.MAX_VALUE, dist = 0;
127             for (int i = 0; i < getPointCount(); i++) {
128                Point p = new Point((int)getPoint(i).getX(),(int)getPoint(i).getY());//HM, JGraph3.4.1
129
dist = Math.sqrt(((Point2D)point).distanceSq(p));
130                if (dist < min) {
131                   min = dist;
132                   index = i;
133                }
134             }
135             if (index != -1 && min<=s+2 && index!=0 && index!=getPointCount()-1) {
136                removePoint(index);
137                Map propertyMap = new HashMap();
138                Map edgeMap=GraphConstants.cloneMap(((GraphCell)cell).getAttributes());
139                GraphConstants.setPoints(edgeMap,points);
140                propertyMap.put(cell,edgeMap);
141                String JavaDoc undoMsg=ResourceManager.getLanguageDependentString("MessageRemovingBreakPointFromTransition");
142                ((JaWEGraphModel)getModel()).insertAndEdit(null,propertyMap,null,null,null,undoMsg);
143             }
144          }
145       }
146    }
147
148    /**
149     * Override Superclass Method so that points could
150     * be added only by addPoint method.
151     */

152    public boolean isAddPointEvent(MouseEvent event) {
153       return false;
154    }
155
156    /**
157     * Override Superclass Method so that points could
158     * be removed only by removePoint method.
159     */

160    public boolean isRemovePointEvent(MouseEvent event) {
161       return false;
162    }
163
164    public static class TransitionHandle extends EdgeHandle {
165
166       public TransitionHandle(EdgeView edge, GraphContext ctx) {
167          super(edge,ctx);
168       }
169
170       public void mouseReleased(MouseEvent e) {
171          try {
172             if (source || target) {
173                Transition tr=(Transition)edge.getCell();
174                PortView ss=(PortView)graph.getGraphLayoutCache().getMapping(tr.getSource(),false);
175                Object JavaDoc tt=(PortView)graph.getGraphLayoutCache().getMapping(tr.getTarget(),false);
176                PortView pvs=(PortView)edge.getSource();
177                PortView pvt=(PortView)edge.getTarget();
178                Activity s1=tr.getSourceActivity();
179                Activity t1=tr.getTargetActivity();
180                Activity s2=null;
181                try {
182                   s2=(Activity)((DefaultPort)pvs.getCell()).getParent();
183                } catch (Exception JavaDoc ex) {
184                }
185                Activity t2=null;
186                try {
187                   t2=(Activity)((DefaultPort)pvt.getCell()).getParent();
188                } catch (Exception JavaDoc ex) {
189                }
190
191                if (s2==null || t2==null) {
192                   clean(tr);
193                   return;
194                }
195                /*System.out.println("source="+source+", target="+target);
196                 System.out.println("S1="+s1+", T1="+t1);
197                 System.out.println("S2="+s2+", T2="+t2);
198
199                 System.out.println("ss="+ss.hashCode()+", tt="+tt.hashCode());
200                 System.out.println("ss="+ss.getClass().getName()+", tt="+tt.getClass().getName());
201                 System.out.println("pvs="+pvs.hashCode()+", pvt="+pvt.hashCode());*/

202
203                if (ss!=pvs || tt!=pvt) {
204                   JaWEMarqueeHandler jmh=(JaWEMarqueeHandler)graph.getMarqueeHandler();
205                   org.enhydra.jawe.xml.elements.Transition uo=
206                      (org.enhydra.jawe.xml.elements.Transition)tr.getPropertyObject();
207                   if (ss!=pvs) {
208                      boolean accept=
209                         !(s1 instanceof Start) &&
210                         !(s2 instanceof Start) &&
211                         !(t1 instanceof End) &&
212                         jmh.acceptsSourceOrTarget(pvs,true,pvt,false);
213                      System.out.println("accept-"+accept);
214                      if (!accept) {
215                         clean(tr);
216                         return;
217                      }
218                      setChanges();
219
220                      // must set source and target activity objects after inserting into model
221
uo.setFrom(s2.getPropertyObject());
222                      ((AbstractGraph)graph).getWorkflowManager().updateSplit(s2);
223                      ((AbstractGraph)graph).getWorkflowManager().updateSplit(s1);
224                   } else {
225                      boolean accept=
226                         !(t1 instanceof End) &&
227                         !(t2 instanceof End) &&
228                         !(s1 instanceof Start) &&
229                         jmh.acceptsSourceOrTarget(pvt,false,pvs,false);
230                      if (!accept) {
231                         clean(tr);
232                         return;
233                      }
234                      setChanges();
235
236                      // must set source and target activity objects after inserting into model
237
uo.setTo(t2.getPropertyObject());
238                      ((AbstractGraph)graph).getWorkflowManager().updateJoin(t2);
239                      ((AbstractGraph)graph).getWorkflowManager().updateJoin(t1);
240                   }
241                   return;
242                }
243             }
244             setChanges();
245          } finally {
246             e.consume();
247          }
248       }
249
250       private void setChanges () {
251          ConnectionSet cs = createConnectionSet(edge, edge.getCell(), false);
252          Map nested =
253             GraphConstants.createAttributes(new CellView[] { edge }, null);
254          graph.getGraphLayoutCache().edit(nested, cs, null, null);
255       }
256
257       private void clean (Transition tr) {
258          Graphics g = graph.getGraphics();
259          overlay(g);
260          firstOverlayCall=true;
261          graph.removeSelectionCell(tr);
262          return;
263       }
264
265    }
266
267 }
268
269 /* End of TransitionView.java */
270
Popular Tags