001: package org.enhydra.jawe.components.graph;
002:
003: import java.awt.Graphics;
004: import java.awt.Point;
005: import java.awt.Rectangle;
006: import java.awt.event.MouseEvent;
007: import java.awt.geom.Line2D;
008: import java.awt.geom.Point2D;
009: import java.util.ArrayList;
010: import java.util.HashMap;
011: import java.util.List;
012: import java.util.Map;
013:
014: import org.enhydra.jawe.JaWEManager;
015: import org.enhydra.jawe.base.controller.JaWEController;
016: import org.enhydra.shark.xpdl.elements.Activity;
017: import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
018: import org.enhydra.shark.xpdl.elements.Transition;
019: import org.jgraph.graph.AttributeMap;
020: import org.jgraph.graph.CellHandle;
021: import org.jgraph.graph.CellView;
022: import org.jgraph.graph.CellViewRenderer;
023: import org.jgraph.graph.ConnectionSet;
024: import org.jgraph.graph.EdgeView;
025: import org.jgraph.graph.GraphCell;
026: import org.jgraph.graph.GraphConstants;
027: import org.jgraph.graph.GraphContext;
028:
029: /**
030: * Represents a view for a model's Transition object.
031: * @author Sasa Bojanic
032: */
033: public class DefaultGraphTransitionView extends
034: GraphTransitionViewInterface {
035:
036: protected static Map renderers = new HashMap();
037:
038: /**
039: * Constructs an edge view for the specified model object.
040: *
041: * @param cell reference to the model object
042: */
043: public DefaultGraphTransitionView(Object cell) {
044: super (cell);
045: int x = (GraphConstants.PERMILLE / 2);
046: int y = (GraphConstants.PERMILLE / 100);
047: Point center = new Point(x, y);
048: AttributeMap map = new AttributeMap();
049: GraphConstants.setLabelPosition(map, center);
050: GraphConstants.setAutoSize(map, true);
051: this .setAttributes(map);
052: }
053:
054: public CellViewRenderer getRenderer() {
055: String type = ((GraphTransitionInterface) super .getCell())
056: .getType();
057: GraphTransitionRendererInterface gtrenderer = (GraphTransitionRendererInterface) renderers
058: .get(type);
059: if (gtrenderer == null) {
060: gtrenderer = createRenderer((Transition) ((GraphTransitionInterface) super
061: .getCell()).getUserObject());
062: renderers.put(type, gtrenderer);
063: }
064: return gtrenderer;
065: }
066:
067: /**
068: * Returns a cell handle for the view.
069: */
070: public CellHandle getHandle(GraphContext context) {
071: return new TransitionHandle(this , context);
072: }
073:
074: /**
075: * Inserts a "break point" at transition object at the point where
076: * popup menu appeared.
077: */
078: public void addPoint(Graph graph, Point popupPoint) {
079: boolean bendable = graph.isBendable()
080: && GraphConstants.isBendable(getAttributes());
081: if (bendable) {
082: int index = -1;
083: int s = graph.getHandleSize();
084: // Rectangle rect = (Rectangle)graph.fromScreen(new Rectangle(popupPoint.x-s,popupPoint.y-s,2*s,2*s));
085: Rectangle rect = new Rectangle(popupPoint.x - s,
086: popupPoint.y - s, 2 * s, 2 * s);
087: System.err
088: .println("Rect=" + rect + ", popup=" + popupPoint);
089: if (intersects(graph, rect)) {
090: Point point = new Point(popupPoint); //(Point) graph.snap(new Point(popupPoint));//HM, JGraph3.4.1
091: double min = Double.MAX_VALUE, dist = 0;
092: for (int i = 0; i < getPointCount() - 1; i++) {
093: Point p = new Point((int) getPoint(i).getX(),
094: (int) getPoint(i).getY());//HM, JGraph3.4.1
095: Point p1 = new Point((int) getPoint(i + 1).getX(),
096: (int) getPoint(i + 1).getY());//HM, JGraph3.4.1
097:
098: // Point p = (Point) graph.snap(new Point((int) getPoint(i).getX(), (int) getPoint(i).getY()));//HM, JGraph3.4.1
099: // Point p1 = new Point((int) getPoint(i + 1).getX(), (int) getPoint(i + 1).getY());//HM, JGraph3.4.1
100: dist = new Line2D.Double(p, p1).ptLineDistSq(point);
101: System.err.println("P=" + p + ", P1=" + p1
102: + ", popup=" + point + ", dist=" + dist
103: + ", min=" + min + ", index=" + index);
104: if (dist < min) {
105: min = dist;
106: index = i + 1;
107: }
108: }
109: if (index != -1) {
110: addPoint(index, point);
111: Map propertyMap = new HashMap();
112: AttributeMap edgeMap = new AttributeMap(
113: ((GraphCell) cell).getAttributes());
114: GraphConstants.setPoints(edgeMap, points);
115: propertyMap.put(cell, edgeMap);
116: String undoMsg = graph
117: .getGraphController()
118: .getSettings()
119: .getLanguageDependentString(
120: "MessageAddingBreakPointAtTransition");
121: ((JaWEGraphModel) graph.getModel()).insertAndEdit(
122: null, propertyMap, null, null, null,
123: undoMsg);
124: }
125: }
126: }
127: }
128:
129: /**
130: * Removes a "break point" from transition at the point where
131: * popup menu appeared.
132: */
133: public void removePoint(Graph graph, Point popupPoint) {
134: boolean bendable = graph.isBendable()
135: && GraphConstants.isBendable(getAttributes());
136: if (bendable) {
137: int index = -1;
138: int s = graph.getHandleSize();
139: //Rectangle rect = graph.fromScreen(new Rectangle(popupPoint.x-s,popupPoint.y-s,2*s,2*s));
140: Rectangle rect = new Rectangle(popupPoint.x - s,
141: popupPoint.y - s, 2 * s, 2 * s);
142: System.err
143: .println("Rect=" + rect + ", popup=" + popupPoint);
144: if (intersects(graph, rect)) {
145: Point point = new Point(popupPoint);//(Point) graph.snap(new Point(popupPoint));//HM, JGraph3.4.1
146: double min = Double.MAX_VALUE, dist = 0;
147: for (int i = 0; i < getPointCount(); i++) {
148: Point p = new Point((int) getPoint(i).getX(),
149: (int) getPoint(i).getY());//HM, JGraph3.4.1
150: dist = Math.sqrt(((Point2D) point).distanceSq(p));
151: System.err.println("P=" + p + ", popup=" + point
152: + ", dist=" + dist + ", min=" + min
153: + ", index=" + index);
154:
155: if (dist < min) {
156: min = dist;
157: index = i;
158: }
159: }
160: if (index != -1 && min <= s + 2 && index != 0
161: && index != getPointCount() - 1) {
162: removePoint(index);
163: Map propertyMap = new HashMap();
164: AttributeMap edgeMap = new AttributeMap(
165: ((GraphCell) cell).getAttributes());
166: GraphConstants.setPoints(edgeMap, points);
167: propertyMap.put(cell, edgeMap);
168: String undoMsg = graph
169: .getGraphController()
170: .getSettings()
171: .getLanguageDependentString(
172: "MessageRemovingBreakPointFromTransition");
173: ((JaWEGraphModel) graph.getModel()).insertAndEdit(
174: null, propertyMap, null, null, null,
175: undoMsg);
176: }
177: }
178: }
179: }
180:
181: public static class TransitionHandle extends EdgeHandle {
182:
183: public TransitionHandle(EdgeView edge, GraphContext ctx) {
184: super (edge, ctx);
185: }
186:
187: public void mouseDragged(MouseEvent event) {
188: super .mouseDragged(event);
189: }
190:
191: public void mouseReleased(MouseEvent e) {
192: try {
193: if (source || target) {
194: GraphTransitionInterface tr = (GraphTransitionInterface) edge
195: .getCell();
196: // GraphPortViewInterface ss = (GraphPortViewInterface) graph.getGraphLayoutCache().getMapping(
197: // tr.getSource(), false);
198: // Object tt = (PortView) graph.getGraphLayoutCache().getMapping(tr.getTarget(), false);
199: GraphPortViewInterface pvs = (GraphPortViewInterface) edge
200: .getSource();
201: GraphPortViewInterface pvt = (GraphPortViewInterface) edge
202: .getTarget();
203: GraphActivityInterface s1 = tr.getSourceActivity();
204: GraphActivityInterface t1 = tr.getTargetActivity();
205: if (s1 instanceof GraphBubbleActivityInterface
206: || t1 instanceof GraphBubbleActivityInterface) {
207: clean(tr);
208: return;
209: }
210: GraphActivityInterface s2 = null;
211: try {
212: s2 = (GraphActivityInterface) ((GraphPortInterface) pvs
213: .getCell()).getParent();
214: } catch (Exception ex) {
215: }
216: GraphActivityInterface t2 = null;
217: try {
218: t2 = (GraphActivityInterface) ((GraphPortInterface) pvt
219: .getCell()).getParent();
220: } catch (Exception ex) {
221: }
222:
223: if (s2 == null
224: || t2 == null
225: || s2 instanceof GraphBubbleActivityInterface
226: || t2 instanceof GraphBubbleActivityInterface) {
227: clean(tr);
228: return;
229: }
230: // System.out.println("source="+source+", target="+target);
231: // System.out.println("S1="+s1+", T1="+t1);
232: // System.out.println("S2="+s2+", T2="+t2);
233: // System.out.println("ss="+ss.hashCode()+", tt="+tt.hashCode());
234: // System.out.println("ss="+ss.getClass().getName()+", tt="+tt.getClass().getName());
235: // System.out.println("pvs="+pvs.hashCode()+", pvt="+pvt.hashCode());
236: // System.out.println("pvs="+pvs.getGraphActivity()+", pvt="+pvt.getGraphActivity());
237:
238: if (s1 != s2 || t1 != t2) {
239: GraphMarqueeHandler jmh = (GraphMarqueeHandler) graph
240: .getMarqueeHandler();
241: Transition uo = (Transition) tr
242: .getPropertyObject();
243: boolean accept = jmh.validateConnection(pvs,
244: pvt, uo);
245: if (!accept) {
246: clean(tr);
247: return;
248: }
249: JaWEController jc = JaWEManager.getInstance()
250: .getJaWEController();
251: ((Graph) graph).getGraphController()
252: .setUpdateInProgress(true);
253: jc.startUndouableChange();
254: setChanges();
255:
256: // must set source and target activity objects after inserting into model
257: uo.setFrom(((Activity) s2.getPropertyObject())
258: .getId());
259: uo.setTo(((Activity) t2.getPropertyObject())
260: .getId());
261: if (uo.getFrom().equals(uo.getTo())) {
262: ExtendedAttribute bpea = GraphUtilities
263: .getBreakpointsEA(uo);
264: if (bpea == null) {
265: GraphManager gmgr = ((Graph) graph)
266: .getGraphManager();
267: GraphActivityInterface gact = gmgr
268: .getGraphActivity(uo.getFrom());
269: Point realP = new Point(50, 50);
270: if (gact != null) {
271: realP = gmgr.getCenter(gact);
272: }
273: List breakpoints = new ArrayList();
274: int rp50x1 = realP.x - 50;
275: int rp50x2 = realP.x + 50;
276: if (rp50x1 < 0) {
277: rp50x2 = rp50x2 - rp50x1;
278: rp50x1 = 0;
279: }
280: int rp50y = realP.y - 50;
281: if (rp50y < 0)
282: rp50y = realP.y + 50;
283:
284: Point p1 = new Point(Math.abs(rp50x1),
285: Math.abs(rp50y));
286: Point p2 = new Point(Math.abs(rp50x2),
287: Math.abs(rp50y));
288: breakpoints.add(p1);
289: breakpoints.add(p2);
290:
291: GraphUtilities
292: .createBreakpointsEA(
293: uo,
294: GraphUtilities
295: .createBreakpointsEAVal(breakpoints),
296: true);
297: Map propertyMap = new HashMap();
298: AttributeMap map = new AttributeMap(
299: edge.getAttributes());
300: propertyMap.put(edge.getCell(), map);
301: Point2D ps = edge.getPoint(0);
302: Point2D pt = edge.getPoint(1);
303: List points = new ArrayList();
304: points.add(ps);
305: points.addAll(breakpoints);
306: points.add(pt);
307: // JaWEManager.getInstance().getLoggingManager().debug("Updating breakpoints for transition: "+points);
308: GraphConstants.setPoints(map, points);
309: ((JaWEGraphModel) graph.getModel())
310: .insertAndEdit(null,
311: propertyMap, null,
312: null, null, "");
313: }
314:
315: }
316:
317: List toSelect = new ArrayList();
318: toSelect.add(uo);
319: jc.endUndouableChange(toSelect);
320: ((Graph) graph).getGraphController()
321: .setUpdateInProgress(false);
322: return;
323: }
324: }
325: setChanges();
326: } finally {
327: e.consume();
328: }
329: }
330:
331: protected void setChanges() {
332: JaWEController jc = JaWEManager.getInstance()
333: .getJaWEController();
334: boolean isucinprogress = jc.isUndoableChangeInProgress();
335: if (!isucinprogress) {
336: ((Graph) graph).getGraphController()
337: .setUpdateInProgress(true);
338: jc.startUndouableChange();
339: }
340: if (edgeModified) {
341: int noOfPoints = edge.getPointCount();
342: List pnts = new ArrayList();
343: for (int i = 1; i < noOfPoints - 1; i++) {
344: pnts.add(new Point((int) edge.getPoint(i).getX(),
345: (int) edge.getPoint(i).getY()));//HM, JGraph3.4.1
346: }
347: GraphUtilities.setBreakpoints(
348: (Transition) ((GraphTransitionInterface) edge
349: .getCell()).getPropertyObject(), pnts);
350: }
351: ConnectionSet cs = createConnectionSet(edge,
352: edge.getCell(), false);
353: Map nested = GraphConstants.createAttributes(
354: new CellView[] { edge }, null);
355: graph.getGraphLayoutCache().edit(nested, cs, null, null);
356: if (!isucinprogress) {
357: GraphTransitionInterface tr = (GraphTransitionInterface) edge
358: .getCell();
359: List toSelect = new ArrayList();
360: toSelect.add(tr.getPropertyObject());
361: jc.endUndouableChange(toSelect);
362: ((Graph) graph).getGraphController()
363: .setUpdateInProgress(false);
364: }
365: }
366:
367: protected void clean(GraphTransitionInterface tr) {
368: Graphics g = graph.getGraphics();
369: overlay(g);
370: firstOverlayCall = true;
371: graph.removeSelectionCell(tr);
372: return;
373: }
374:
375: }
376:
377: protected GraphTransitionRendererInterface createRenderer(
378: Transition tra) {
379: return GraphUtilities.getGraphController()
380: .getGraphObjectRendererFactory()
381: .createTransitionRenderer(tra);
382: }
383: }
|