001: package org.enhydra.jawe.components.graph.actions;
002:
003: import java.awt.Point;
004: import java.awt.event.ActionEvent;
005: import java.util.ArrayList;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.Map;
010: import java.util.Set;
011:
012: import org.enhydra.jawe.ActionBase;
013: import org.enhydra.jawe.JaWEComponent;
014: import org.enhydra.jawe.JaWEManager;
015: import org.enhydra.jawe.base.controller.JaWEController;
016: import org.enhydra.jawe.components.graph.Graph;
017: import org.enhydra.jawe.components.graph.GraphController;
018: import org.enhydra.jawe.components.graph.GraphEAConstants;
019: import org.enhydra.jawe.components.graph.GraphUtilities;
020: import org.enhydra.jawe.components.graph.JaWEGraphModel;
021: import org.enhydra.shark.utilities.SequencedHashMap;
022: import org.enhydra.shark.xpdl.XMLCollectionElement;
023: import org.enhydra.shark.xpdl.XMLUtil;
024: import org.enhydra.shark.xpdl.elements.Activity;
025: import org.enhydra.shark.xpdl.elements.ActivitySet;
026: import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
027: import org.enhydra.shark.xpdl.elements.ExtendedAttributes;
028: import org.enhydra.shark.xpdl.elements.Package;
029: import org.enhydra.shark.xpdl.elements.Transition;
030: import org.enhydra.shark.xpdl.elements.WorkflowProcess;
031: import org.jgraph.graph.DefaultGraphCell;
032:
033: /**
034: * @author Sasa Bojanic
035: */
036: public class SimpleGraphLayout extends ActionBase {
037:
038: public SimpleGraphLayout(JaWEComponent jawecomponent) {
039: super (jawecomponent);
040: }
041:
042: public void enableDisableAction() {
043: JaWEController jc = JaWEManager.getInstance()
044: .getJaWEController();
045: GraphController gc = (GraphController) jawecomponent;
046:
047: if (gc.getSelectedGraph() != null)
048: if (XMLUtil.getPackage(gc.getSelectedGraph()
049: .getXPDLObject()) == jc.getMainPackage()) {
050: setEnabled(true);
051: return;
052: }
053:
054: setEnabled(false);
055: }
056:
057: public void actionPerformed(ActionEvent e) {
058: GraphController gcon = (GraphController) jawecomponent;
059: Graph selectedGraph = gcon.getSelectedGraph();
060: if (selectedGraph == null)
061: return;
062:
063: gcon.setUpdateInProgress(true);
064: JaWEManager.getInstance().getJaWEController()
065: .startUndouableChange();
066:
067: SimpleGraphLayout.layoutGraph(gcon, selectedGraph);
068:
069: selectedGraph.clearSelection();
070:
071: List toSelect = new ArrayList();
072: toSelect.add(selectedGraph.getXPDLObject());
073: JaWEManager.getInstance().getJaWEController()
074: .endUndouableChange(toSelect);
075: gcon.setUpdateInProgress(false);
076: }
077:
078: public static void layoutAllGraphs() {
079: Graph selectedGraph = null;
080: GraphController gc = GraphUtilities.getGraphController();
081: gc.setUpdateInProgress(true);
082: JaWEManager.getInstance().getJaWEController()
083: .startUndouableChange();
084: Iterator pkgs = JaWEManager.getInstance().getXPDLHandler()
085: .getAllPackages().iterator();
086: while (pkgs.hasNext()) {
087: Iterator wps = ((Package) pkgs.next())
088: .getWorkflowProcesses().toElements().iterator();
089: while (wps.hasNext()) {
090: WorkflowProcess wp = (WorkflowProcess) wps.next();
091: if (selectedGraph == null) {
092: selectedGraph = gc.getGraph(wp);
093: }
094: SimpleGraphLayout.layoutGraph(gc, gc.getGraph(wp));
095: Iterator ass = wp.getActivitySets().toElements()
096: .iterator();
097: while (ass.hasNext()) {
098: ActivitySet as = (ActivitySet) ass.next();
099: SimpleGraphLayout.layoutGraph(gc, gc.getGraph(as));
100: }
101: }
102: }
103: List toSelect = new ArrayList();
104: toSelect.add(selectedGraph.getXPDLObject());
105: JaWEManager.getInstance().getJaWEController()
106: .endUndouableChange(toSelect);
107: gc.setUpdateInProgress(false);
108: }
109:
110: public static void layoutGraph(GraphController gcon,
111: Graph selectedGraph) {
112: boolean isHorizontal = GraphUtilities
113: .getGraphParticipantOrientation(
114: selectedGraph.getWorkflowProcess(),
115: selectedGraph.getXPDLObject())
116: .equals(
117: GraphEAConstants.EA_JAWE_GRAPH_PARTICIPANT_ORIENTATION_VALUE_HORIZONTAL);
118:
119: Object[] elem = JaWEGraphModel.getAll(selectedGraph.getModel());
120: selectedGraph.getModel().remove(elem);
121:
122: for (int i = 0; i < elem.length; i++) {
123: if (elem[i] instanceof DefaultGraphCell) {
124: if (((DefaultGraphCell) elem[i]).getUserObject() instanceof Transition) {
125: Transition tr = (Transition) ((DefaultGraphCell) elem[i])
126: .getUserObject();
127: if (!tr.getTo().equals(tr.getFrom())) {
128: GraphUtilities.setBreakpoints(tr,
129: new ArrayList());
130: }
131: } else if (((DefaultGraphCell) elem[i]).getUserObject() instanceof ExtendedAttribute) {
132: ExtendedAttribute ea = (ExtendedAttribute) ((DefaultGraphCell) elem[i])
133: .getUserObject();
134: ((ExtendedAttributes) ea.getParent()).remove(ea);
135: }
136: }
137: }
138:
139: SimpleGraphLayout.sortItOut(selectedGraph.getXPDLObject(),
140: isHorizontal);
141:
142: selectedGraph.getGraphManager().createWorkflowGraph(
143: selectedGraph.getXPDLObject());
144: if (gcon.getGraphSettings().shouldUseBubbles()) {
145: List easToAdd = selectedGraph.getGraphManager()
146: .insertMissingStartEndBubbles();
147: XMLUtil.getWorkflowProcess(selectedGraph.getXPDLObject())
148: .getExtendedAttributes().addAll(easToAdd);
149: }
150: }
151:
152: protected static void sortItOut(XMLCollectionElement wpOrAs,
153: boolean isHorizontal) {
154: List starts = new ArrayList(XMLUtil
155: .getStartingActivities(wpOrAs));
156: List inserted = new ArrayList();
157:
158: Map posX = new HashMap();
159: Map posY = new HashMap();
160: Map actX = new HashMap();
161: Map actY = new HashMap();
162:
163: for (int i = 0; i < starts.size(); i++) {
164: Activity act = (Activity) starts.get(i);
165: Point p = SimpleGraphLayout.getYPos(posX, posY, actX, actY,
166: act, null, isHorizontal);
167: GraphUtilities.setOffsetPoint(act, p);
168: inserted.add(act);
169: }
170:
171: SequencedHashMap toInsert = new SequencedHashMap();
172: for (int i = 0; i < starts.size(); i++) {
173: toInsert.put(starts.get(i), null);
174: }
175: while (toInsert.size() > 0) {
176: Activity act = (Activity) toInsert.keySet().toArray()[0];
177: List l = SimpleGraphLayout.insertActivity(posX, actX, act,
178: (Activity) toInsert.get(act), inserted,
179: isHorizontal);
180: toInsert.remove(act);
181: // System.out.println("Inserted act "+act+", TOINS="+toInsert.keySet());
182: if (l.size() == 0) {
183: String pid = GraphUtilities.getParticipantId(act);
184: Point prevP = (Point) posX.get(pid);
185: Point p = null;
186: if (isHorizontal) {
187: p = new Point(prevP.x
188: + (int) (1.5 * GraphUtilities
189: .getGraphController()
190: .getGraphSettings()
191: .getActivityWidth()), prevP.y);
192: } else {
193: p = new Point(prevP.x, prevP.y
194: + (int) (1.5 * GraphUtilities
195: .getGraphController()
196: .getGraphSettings()
197: .getActivityHeight()));
198: }
199: posX.put(pid, p);
200: } else {
201: // System.out.println("LINVOKED, lsize="+l.size());
202: for (int i = 0; i < l.size(); i++) {
203: Object an = l.get(i);
204: if (!toInsert.containsKey(an)) {
205: toInsert.put(an, act);
206: }
207: }
208: }
209: // System.out.println("Inserted act "+act+", TOINS2="+toInsert.keySet());
210: }
211: }
212:
213: protected static List insertActivity(Map posX, Map actX,
214: Activity act, Activity prev, List inserted,
215: boolean isHorizontal) {
216: List ret = new ArrayList();
217: if (inserted.contains(act) && prev != null)
218: return ret;
219: List ordOGT = XMLUtil.getOrderedOutgoingTransitions(act,
220: XMLUtil.getOutgoingTransitions(act));
221: for (int i = 0; i < ordOGT.size(); i++) {
222: Transition t = (Transition) ordOGT.get(i);
223: Activity a = XMLUtil.getWorkflowProcess(act).getActivity(
224: t.getTo());
225: if (!inserted.contains(a)) {
226: ret.add(a);
227: }
228: }
229: if (prev != null) {
230: Point p = SimpleGraphLayout.getXPos(posX, actX, act, prev,
231: isHorizontal);
232: GraphUtilities.setOffsetPoint(act, p);
233: inserted.add(act);
234: }
235:
236: return ret;
237: }
238:
239: protected static Point getYPos(Map posX, Map posY, Map actX,
240: Map actY, Activity act, Activity prev, boolean isHorizontal) {
241: int xdiff = GraphUtilities.getGraphController()
242: .getGraphSettings().getActivityWidth();
243: int ydiff = GraphUtilities.getGraphController()
244: .getGraphSettings().getActivityHeight();
245:
246: int xoff = GraphUtilities.getGraphController()
247: .getGraphSettings().getParticipantNameWidth();
248:
249: if (isHorizontal) {
250: xoff += (xdiff * 1.5);
251: } else {
252: xoff += (ydiff * 1.5);
253: }
254: int yoff = 15;
255:
256: Point p = null;
257: String pid = GraphUtilities.getParticipantId(act);
258: Point prevP = (Point) posY.get(pid);
259: Point prevActP = (prev != null) ? GraphUtilities
260: .getOffsetPoint(prev) : null;
261: if (prevP == null) {
262: prevP = prevActP;
263: if (prevP == null) {
264: if (isHorizontal) {
265: p = new Point(xoff, yoff);
266: } else {
267: p = new Point(yoff, xoff);
268: }
269: }
270: posX.put(pid, p);
271: actX.put(pid, act);
272: } else {
273: if (prevActP != null) {
274: if (isHorizontal) {
275: prevP = new Point(prevP.x, Math.max(prevP.y,
276: prevActP.y));
277: } else {
278: prevP = new Point(Math.max(prevP.x, prevActP.x),
279: prevP.y);
280: }
281: }
282: }
283: if (p == null) {
284: if (isHorizontal) {
285: p = new Point(prevP.x, prevP.y + (int) (1.5 * ydiff));
286: } else {
287: p = new Point(prevP.x + (int) (1.5 * xdiff), prevP.y);
288: }
289: }
290: posY.put(pid, p);
291: actY.put(pid, act);
292: return p;
293: }
294:
295: protected static Point getXPos(Map posX, Map actX, Activity act,
296: Activity prev, boolean isHorizontal) {
297: int xdiff = GraphUtilities.getGraphController()
298: .getGraphSettings().getActivityWidth();
299: int ydiff = GraphUtilities.getGraphController()
300: .getGraphSettings().getActivityHeight();
301:
302: int yoff = 15;
303:
304: Point p = null;
305: String pid = GraphUtilities.getParticipantId(act);
306: Point prevP = (Point) posX.get(pid);
307: Point prevActP = GraphUtilities.getOffsetPoint(prev);
308: boolean changeMap = true;
309: if (prevP == null) {
310: prevP = prevActP;
311: if (isHorizontal) {
312: prevP = new Point(prevActP.x, yoff);
313: } else {
314: prevP = new Point(yoff, prevActP.y);
315: }
316: } else {
317: if (isHorizontal) {
318: prevP = new Point(Math.max(prevP.x, prevActP.x),
319: prevP.y);
320: } else {
321: prevP = new Point(prevP.x, Math
322: .max(prevP.y, prevActP.y));
323: }
324: }
325: Activity pointAct = (Activity) actX.get(pid);
326: if (pointAct != null) {
327: Set s = XMLUtil.getIncomingTransitions(pointAct);
328: Iterator it = s.iterator();
329: while (it.hasNext()) {
330: Transition t = (Transition) it.next();
331: if (t.getFrom().equals(prev.getId())) {
332: changeMap = false;
333: break;
334: }
335: }
336: }
337: if (isHorizontal) {
338: if (changeMap) {
339: p = new Point(prevP.x + (int) (1.5 * xdiff), prevP.y);
340: } else {
341: p = GraphUtilities.getOffsetPoint(pointAct);
342: p.y += (int) (1.5 * ydiff);
343: }
344: } else {
345: if (changeMap) {
346: p = new Point(prevP.x, prevP.y + (int) (1.5 * ydiff));
347: } else {
348: p = GraphUtilities.getOffsetPoint(pointAct);
349: p.x += (int) (1.5 * xdiff);
350: }
351: }
352: if (changeMap) {
353: posX.put(pid, p);
354: }
355: actX.put(pid, act);
356: return p;
357: }
358:
359: }
|