001: /**
002: * Miroslav Popov, Sep 1, 2005
003: */package org.enhydra.jawe.base.controller;
004:
005: import java.util.ArrayList;
006: import java.util.HashMap;
007: import java.util.HashSet;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Map;
011: import java.util.Set;
012:
013: import org.enhydra.jawe.JaWEManager;
014: import org.enhydra.jawe.Utils;
015: import org.enhydra.jawe.XPDLElementChangeInfo;
016: import org.enhydra.jawe.base.xpdlhandler.XPDLHandler;
017: import org.enhydra.shark.xpdl.XMLCollection;
018: import org.enhydra.shark.xpdl.XMLCollectionElement;
019: import org.enhydra.shark.xpdl.XMLComplexElement;
020: import org.enhydra.shark.xpdl.XMLElement;
021: import org.enhydra.shark.xpdl.XMLUtil;
022: import org.enhydra.shark.xpdl.elements.Activities;
023: import org.enhydra.shark.xpdl.elements.Activity;
024: import org.enhydra.shark.xpdl.elements.ActivitySet;
025: import org.enhydra.shark.xpdl.elements.Application;
026: import org.enhydra.shark.xpdl.elements.Package;
027: import org.enhydra.shark.xpdl.elements.Participant;
028: import org.enhydra.shark.xpdl.elements.Tool;
029: import org.enhydra.shark.xpdl.elements.Transition;
030: import org.enhydra.shark.xpdl.elements.Transitions;
031: import org.enhydra.shark.xpdl.elements.WorkflowProcess;
032:
033: /**
034: * @author Miroslav Popov
035: * @author Sasa Bojanic
036: */
037: public class JaWEEdit {
038:
039: protected List clipboard = new ArrayList();
040:
041: protected boolean isPasteInProgress = false;
042:
043: public void copy() {
044: copyOrCut(false);
045: }
046:
047: public void cut() {
048: copyOrCut(true);
049: }
050:
051: protected void copyOrCut(boolean isCut) {
052: clipboard = new ArrayList();
053: JaWEController jc = JaWEManager.getInstance()
054: .getJaWEController();
055: if (jc.getSelectionManager().getSelectedElements() != null) {
056: clipboard.addAll(jc.getSelectionManager()
057: .getSelectedElements());
058:
059: XMLElement firstEl = (XMLElement) clipboard.get(0);
060: XMLElement selOwner = firstEl.getParent();
061: if (selOwner == null) {
062: selOwner = firstEl;
063: }
064: XPDLElementChangeInfo info = jc.createInfo(selOwner,
065: clipboard, isCut ? XPDLElementChangeInfo.CUT
066: : XPDLElementChangeInfo.COPY);
067:
068: jc.sendEvent(info);
069: prepareForPaste();
070: if (isCut) {
071: delete();
072: }
073: }
074: JaWEManager.getInstance().getJaWEController().adjustActions();
075: }
076:
077: public void duplicate() {
078: JaWEController jc = JaWEManager.getInstance()
079: .getJaWEController();
080:
081: XMLElement el = jc.getSelectionManager().getSelectedElement();
082: if (el.getParent() instanceof XMLCollection) {
083: XMLCollection col = (XMLCollection) el.getParent();
084: XMLElement cloned = JaWEManager.getInstance()
085: .getXPDLObjectFactory()
086: .duplicateXPDLObject(col, el);
087: if (cloned instanceof ActivitySet) {
088: duplicateActivitySetContext((ActivitySet) cloned,
089: new HashSet(), new HashSet());
090: }
091: jc.startUndouableChange();
092: col.add(cloned);
093: List toSelect = new ArrayList();
094: toSelect.add(cloned);
095: jc.endUndouableChange(toSelect);
096: }
097: }
098:
099: public void references() {
100: JaWEController jc = JaWEManager.getInstance()
101: .getJaWEController();
102: XMLComplexElement el = (XMLComplexElement) jc
103: .getSelectionManager().getSelectedElement();
104: XMLComplexElement pkgOrWP = XMLUtil.getPackage(el);
105: WorkflowProcess wp = XMLUtil.getWorkflowProcess(el);
106: if (wp != null && wp != el) {
107: pkgOrWP = wp;
108: }
109:
110: XPDLElementChangeInfo info = null;
111: if (el instanceof Activity) {
112: info = jc.createInfo(el, Utils
113: .makeSearchResultList(JaWEManager.getInstance()
114: .getXPDLUtils()
115: .getReferences((Activity) el)),
116: XPDLElementChangeInfo.REFERENCES);
117: } else if (el instanceof Transition) {
118: info = jc.createInfo(el, Utils
119: .makeSearchResultList(JaWEManager.getInstance()
120: .getXPDLUtils().getReferences(
121: (Transition) el)),
122: XPDLElementChangeInfo.REFERENCES);
123: } else if (el instanceof Package) {
124: info = jc.createInfo(el,
125: Utils.makeSearchResultList(JaWEManager
126: .getInstance().getXPDLUtils()
127: .getReferences((Package) el)),
128: XPDLElementChangeInfo.REFERENCES);
129: } else {
130: if (el instanceof WorkflowProcess
131: || el instanceof Application
132: || el instanceof Participant) {
133: if (pkgOrWP instanceof Package) {
134: info = jc.createInfo(el, getAllReferences(el),
135: XPDLElementChangeInfo.REFERENCES);
136: } else {
137: info = jc.createInfo(el, Utils
138: .makeSearchResultList(JaWEManager
139: .getInstance().getXPDLUtils()
140: .getReferences(pkgOrWP, el)),
141: XPDLElementChangeInfo.REFERENCES);
142: }
143: } else {
144: info = jc.createInfo(el, Utils
145: .makeSearchResultList(JaWEManager.getInstance()
146: .getXPDLUtils().getReferences(pkgOrWP,
147: el)),
148: XPDLElementChangeInfo.REFERENCES);
149: }
150: }
151:
152: if (info != null) {
153: jc.sendEvent(info);
154: }
155: }
156:
157: public void paste() {
158: JaWEManager.getInstance().getLoggingManager().debug(
159: "PASTE started");
160: isPasteInProgress = true;
161: JaWEController jc = JaWEManager.getInstance()
162: .getJaWEController();
163: XMLElement firstSelected = jc.getSelectionManager()
164: .getSelectedElement();
165: jc.startUndouableChange();
166:
167: Iterator it = clipboard.iterator();
168: Transitions transCollection = null;
169: Activities actsCollection = null;
170: XMLCollection destCollection = null;
171: List duplicatedElements = new ArrayList();
172: Map actIdMappings = new HashMap();
173: List acts = new ArrayList();
174: List trans = new ArrayList();
175: Set skipIds = new HashSet();
176: Set skipActIds = new HashSet();
177: Set skipTransIds = new HashSet();
178: while (it.hasNext()) {
179: destCollection = null;
180: XMLElement copied = (XMLElement) it.next();
181: XMLCollection parent = (XMLCollection) copied.getParent();
182: // System.err.println("Searching for dest collection for el
183: // "+copied.getClass()+", firstSelected="+firstSelected.getClass());
184: if (parent.getClass() == firstSelected.getClass()) {
185: // System.err.println("Dest coll found -> 1");
186: destCollection = (XMLCollection) firstSelected;
187: } else if (firstSelected.getClass() == copied.getClass()) {
188: destCollection = (XMLCollection) firstSelected
189: .getParent();
190: // System.err.println("Dest coll found -> 2");
191: } else {
192: List subEls = ((XMLComplexElement) firstSelected)
193: .toElements();
194: for (int i = 0; i < subEls.size(); i++) {
195: if (subEls.get(i).getClass() == parent.getClass()) {
196: destCollection = (XMLCollection) subEls.get(i);
197: // System.err.println("Dest coll found -> 3");
198: break;
199: }
200: }
201: if (destCollection == null) {
202: if (firstSelected instanceof Activity) {
203: destCollection = (Transitions) ((XMLComplexElement) firstSelected
204: .getParent().getParent())
205: .get("Transitions");
206: // System.err.println("Dest coll found -> 4");
207: } else if (firstSelected instanceof Transition) {
208: destCollection = (Activities) ((XMLComplexElement) firstSelected
209: .getParent().getParent())
210: .get("Activities");
211: // System.err.println("Dest coll found -> 5");
212: }
213: }
214: }
215: // System.err.println("Dest collection="+destCollection);
216: if (destCollection instanceof Activities) {
217: actsCollection = (Activities) destCollection;
218: } else if (destCollection instanceof Transitions) {
219: transCollection = (Transitions) destCollection;
220: }
221: XMLElement twin = JaWEManager.getInstance()
222: .getXPDLObjectFactory().makeIdenticalXPDLObject(
223: parent, copied);
224: if (twin instanceof Activity) {
225: acts.add(twin);
226: } else if (twin instanceof Transition) {
227: trans.add(twin);
228: }
229: if (twin instanceof XMLCollectionElement
230: && !(twin instanceof Tool)) {
231: XMLCollectionElement cel = (XMLCollectionElement) twin;
232: String oldid = cel.getId();
233: Set sids = null;
234: XMLCollection destCol = destCollection;
235: if (cel instanceof Activity) {
236: sids = skipActIds;
237: destCol = actsCollection;
238: } else if (cel instanceof Transition) {
239: sids = skipTransIds;
240: destCol = transCollection;
241: } else {
242: sids = skipIds;
243: }
244: String id = JaWEManager.getInstance().getIdFactory()
245: .generateSimilarOrIdenticalUniqueId(destCol,
246: sids, oldid);
247: if (!oldid.equals(id)) {
248: if (cel instanceof Activity) {
249: actIdMappings.put(oldid, cel);
250: }
251: cel.setId(id);
252: sids.add(id);
253: }
254: }
255: duplicatedElements.add(twin);
256: }
257:
258: it = duplicatedElements.iterator();
259: while (it.hasNext()) {
260: XMLElement el = (XMLElement) it.next();
261: if (el instanceof ActivitySet) {
262: ActivitySet as = (ActivitySet) el;
263: duplicateActivitySetContext(as, skipActIds,
264: skipTransIds);
265: }
266: }
267:
268: it = trans.iterator();
269: while (it.hasNext()) {
270: Transition t = (Transition) it.next();
271: if (actIdMappings.containsKey(t.getFrom())) {
272: Activity act = (Activity) actIdMappings
273: .get(t.getFrom());
274: t.setFrom(act.getId());
275: }
276: if (actIdMappings.containsKey(t.getTo())) {
277: Activity act = (Activity) actIdMappings.get(t.getTo());
278: t.setTo(act.getId());
279: }
280: }
281:
282: it = duplicatedElements.iterator();
283: while (it.hasNext()) {
284: XMLElement duplicated = (XMLElement) it.next();
285: if (!(duplicated instanceof Transition)) {
286: if (duplicated instanceof Activity) {
287: actsCollection.add(duplicated);
288: } else {
289: destCollection.add(duplicated);
290: }
291: }
292: }
293: it = duplicatedElements.iterator();
294: while (it.hasNext()) {
295: XMLElement duplicated = (XMLElement) it.next();
296: if (duplicated instanceof Transition) {
297: transCollection.add(duplicated);
298: }
299: }
300:
301: it = acts.iterator();
302: while (it.hasNext()) {
303: XMLElement duplicated = (XMLElement) it.next();
304: if (duplicated instanceof Activity) {
305: JaWEManager.getInstance().getXPDLUtils()
306: .correctSplitAndJoin((Activity) duplicated);
307: }
308: }
309:
310: JaWEManager.getInstance().getLoggingManager().debug(
311: "Paste ended");
312: jc.endUndouableChange(new ArrayList(duplicatedElements));
313: isPasteInProgress = false;
314: }
315:
316: public void delete() {
317: JaWEController jc = JaWEManager.getInstance()
318: .getJaWEController();
319: jc.startUndouableChange();
320: XMLCollection toSel = null;
321: int lastElementIndex = 0;
322: List sels = jc.getSelectionManager().getSelectedElements();
323: for (int i = 0; i < sels.size(); i++) {
324: XMLElement el = (XMLElement) sels.get(i);
325: toSel = (XMLCollection) el.getParent();
326: lastElementIndex = toSel.indexOf(el);
327: toSel.remove(el);
328: }
329:
330: List toSelect = new ArrayList();
331: if (toSel.size() == 0)
332: toSelect.add(toSel);
333: else {
334: if (toSel.size() <= lastElementIndex
335: || lastElementIndex < 0) {
336: toSelect.add(toSel.get(toSel.size() - 1));
337: } else {
338: toSelect.add(toSel.get(lastElementIndex));
339: }
340: }
341:
342: jc.endUndouableChange(toSelect);
343: }
344:
345: protected void prepareForPaste() {
346: Iterator it = clipboard.iterator();
347: List twinElements = new ArrayList();
348: while (it.hasNext()) {
349: XMLElement copied = (XMLElement) it.next();
350: XMLCollection parent = (XMLCollection) copied.getParent();
351: XMLElement twin = JaWEManager.getInstance()
352: .getXPDLObjectFactory().makeIdenticalXPDLObject(
353: parent, copied);
354: twin.setReadOnly(false);
355: twinElements.add(twin);
356: }
357: clipboard = new ArrayList(twinElements);
358: }
359:
360: public void clear() {
361: clipboard.clear();
362: }
363:
364: public List getClipboard() {
365: return clipboard;
366: }
367:
368: public boolean isPasteInProgress() {
369: return isPasteInProgress;
370: }
371:
372: public List getAllReferences(
373: XMLComplexElement referencedWpOrParOrApp) {
374: List references = new ArrayList();
375:
376: XPDLHandler xpdlh = JaWEManager.getInstance().getXPDLHandler();
377: Iterator it = xpdlh.getAllPackages().iterator();
378: while (it.hasNext()) {
379: Package pkg = (Package) it.next();
380: references.addAll(Utils.makeSearchResultList(JaWEManager
381: .getInstance().getXPDLUtils().getReferences(pkg,
382: referencedWpOrParOrApp)));
383: }
384:
385: return references;
386: }
387:
388: protected void duplicateActivitySetContext(ActivitySet as,
389: Set skipActIds, Set skipTransIds) {
390: Map actIdMappings = new HashMap();
391: List toIterate = new ArrayList(as.getActivities().toElements());
392: toIterate.addAll(as.getTransitions().toElements());
393: Iterator itat = toIterate.iterator();
394: while (itat.hasNext()) {
395: XMLCollectionElement actOrTrans = (XMLCollectionElement) itat
396: .next();
397: String oldid = actOrTrans.getId();
398: Set sids = null;
399: if (actOrTrans instanceof Activity) {
400: sids = skipActIds;
401: } else {
402: sids = skipTransIds;
403: }
404: String id = JaWEManager.getInstance().getIdFactory()
405: .generateSimilarOrIdenticalUniqueId(
406: (XMLCollection) actOrTrans.getParent(),
407: sids, oldid);
408: if (!oldid.equals(id)) {
409: if (actOrTrans instanceof Activity) {
410: actIdMappings.put(oldid, actOrTrans);
411: }
412: actOrTrans.setId(id);
413: sids.add(id);
414: }
415: }
416: Iterator it = as.getTransitions().toElements().iterator();
417: while (it.hasNext()) {
418: Transition t = (Transition) it.next();
419: if (actIdMappings.containsKey(t.getFrom())) {
420: Activity act = (Activity) actIdMappings
421: .get(t.getFrom());
422: t.setFrom(act.getId());
423: }
424: if (actIdMappings.containsKey(t.getTo())) {
425: Activity act = (Activity) actIdMappings.get(t.getTo());
426: t.setTo(act.getId());
427: }
428: }
429: it = as.getActivities().toElements().iterator();
430: while (it.hasNext()) {
431: JaWEManager.getInstance().getXPDLUtils()
432: .correctSplitAndJoin((Activity) it.next());
433: }
434: }
435: }
|