001: /**
002: *
003: */package newprocess.diagram.cust.tool;
004:
005: import java.util.Collection;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import newprocess.ConditionProxy;
010: import newprocess.OperatorTerm;
011: import newprocess.Root;
012: import newprocess.diagram.edit.parts.ConditionProxyEditPart;
013: import newprocess.diagram.edit.parts.ConditionTermConditionProxyEditPart;
014: import newprocess.diagram.edit.parts.ConditionTermEditPart;
015: import newprocess.diagram.edit.parts.OperatorTermSubTermEditPart;
016: import newprocess.diagram.edit.parts.RootEditPart;
017: import newprocess.diagram.providers.New_processElementTypes;
018:
019: import org.eclipse.core.runtime.IAdaptable;
020: import org.eclipse.emf.ecore.EObject;
021: import org.eclipse.gef.EditPart;
022: import org.eclipse.gef.EditPartViewer;
023: import org.eclipse.gef.Request;
024: import org.eclipse.gef.commands.Command;
025: import org.eclipse.gef.requests.CreateConnectionRequest;
026: import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
027: import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeEditPart;
028: import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramCommandStack;
029: import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequestFactory;
030: import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants;
031: import org.eclipse.gmf.runtime.diagram.ui.tools.UnspecifiedTypeConnectionTool;
032: import org.eclipse.gmf.runtime.emf.type.core.IElementType;
033: import org.eclipse.gmf.runtime.notation.Node;
034: import org.eclipse.gmf.runtime.notation.View;
035:
036: /**
037: * @author sh
038: *
039: */
040: public class ProcessLinkTool extends UnspecifiedTypeConnectionTool {
041: // temporarily disable the autoexpose helper since it interferes with menu
042: // selection.
043: private boolean antiScroll = false;
044:
045: /**
046: * Constructor
047: * @param connectionTypes
048: */
049: public ProcessLinkTool(List connectionTypes) {
050: super (connectionTypes);
051: }
052:
053: /**
054: * This method creates a complex link chain by
055: * one drag and drob operation from Operator to Proxy.
056: * It means this link creation tool builds a chain between
057: * "EClass:Operator--EReference-->EClass:Port--EReference-->EClass:Proxy"
058: * and creates the Node "Port" and two Edges "OperatorHas" and "PortProxy"
059: */
060: @Override
061: protected boolean handleCreateConnection() {
062: //setAvoidDeactivation(true);
063: eraseSourceFeedback();
064:
065: // get edit parts & model objects
066: EditPart sourceEP = getSourceEditPart();
067: EditPart targetEP = getTargetEditPart();
068: if (sourceEP == null || targetEP == null)
069: return false;
070: if (sourceEP instanceof ShapeEditPart == false
071: || targetEP instanceof ShapeEditPart == false)
072: return false;
073: EObject sourceEO = (EObject) ((Node) sourceEP.getModel())
074: .getElement();
075: EObject targetEO = (EObject) ((Node) targetEP.getModel())
076: .getElement();
077:
078: // switch inversed link requests
079: if (sourceEO instanceof ConditionProxy) {
080: EditPart tempEP = sourceEP;
081: EObject tempEO = sourceEO;
082: sourceEP = targetEP;
083: sourceEO = targetEO;
084: targetEP = tempEP;
085: targetEO = tempEO;
086: }
087:
088: if (sourceEO instanceof OperatorTerm
089: && targetEO instanceof Root) {
090: EditPart tempEP = sourceEP;
091: EObject tempEO = sourceEO;
092: sourceEP = targetEP;
093: sourceEO = targetEO;
094: targetEP = tempEP;
095: targetEO = tempEO;
096: }
097:
098: /*
099: if (sourceEO instanceof AbstractCondition)
100: createLink(sourceEP, targetEP);
101: */
102:
103: if (sourceEO instanceof Root) {
104: if (targetEO instanceof ConditionProxy
105: && ((Root) sourceEO).acceptOutgoingLink(Root.PORT))
106: createChain();
107: if (targetEO instanceof OperatorTerm
108: && ((Root) sourceEO)
109: .acceptOutgoingLink(Root.OPERATOR)
110: && ((OperatorTerm) targetEO).acceptIncomingLink())
111: createLink(sourceEP, targetEP);
112: }
113:
114: if (sourceEO instanceof OperatorTerm) {
115: if (targetEO instanceof ConditionProxy)
116: createChain();
117: if (targetEO instanceof OperatorTerm
118: && ((OperatorTerm) targetEO).acceptIncomingLink()
119: && sourceEO.eContainer() == targetEO.eContainer())
120: createLink(sourceEP, targetEP);
121: }
122:
123: return true;
124: }
125:
126: /**
127: *
128: * @return
129: */
130: private EditPart getSourceEditPart() {
131: return ((CreateConnectionRequest) getSourceRequest())
132: .getSourceEditPart();
133: }
134:
135: /**
136: * @author ts & sh
137: * The creation of the Root / AbstractOperator - Term - Proxy chain
138: */
139: private void createChain() {
140: EditPart termEP = null;
141: ConditionProxyEditPart proxyEP = null;
142:
143: if (getSourceEditPart() instanceof ConditionProxyEditPart) {
144: termEP = getTargetEditPart();
145: proxyEP = (ConditionProxyEditPart) getSourceEditPart();
146: } else {
147: termEP = getSourceEditPart();
148: proxyEP = (ConditionProxyEditPart) getTargetEditPart();
149: }
150:
151: EditPart conditionEP = null;
152: if (termEP instanceof RootEditPart)
153: conditionEP = termEP.getParent();
154: else
155: // navigate behind the compartment
156: conditionEP = termEP.getParent().getParent();
157:
158: // create the PortEditPart
159: ConditionTermEditPart portEP = createPort(conditionEP);
160:
161: // create Operator || RootPort - Port Link
162: createLink(termEP, portEP);
163:
164: // create Port - Proxy Link
165: createLink(portEP, proxyEP);
166: }
167:
168: /**
169: * Creates the Term
170: * @param container
171: * @return created TermEditPart
172: */
173: private ConditionTermEditPart createPort(EditPart container) {
174: if (container == null)
175: return null;
176:
177: // create a Request for fetching the term creation command
178: IElementType termType = New_processElementTypes.ConditionTerm_3005;
179: PreferencesHint hint = getPreferencesHint();
180: Request req = CreateViewRequestFactory.getCreateShapeRequest(
181: termType, hint);
182: setTargetRequest(req);
183: Command com = container.getCommand(req);
184:
185: // execute the term creation command
186: if (com == null)
187: return null;
188: setCurrentCommand(com);
189: antiScroll = true;
190: executeCurrentCommand();
191: antiScroll = false;
192:
193: // search for the created Term EditPart
194: EditPart editPart = findEditPart(com, termType);
195: if (editPart instanceof ConditionTermEditPart)
196: return (ConditionTermEditPart) editPart;
197:
198: return null;
199: }
200:
201: /**
202: * Creates the link from the given source Edit Part to the given
203: * Target Edit Part
204: * @param sourceEP
205: * @param targetEP
206: */
207: private void createLink(EditPart sourceEP, EditPart targetEP) {
208: if (sourceEP == null || targetEP == null)
209: return;
210:
211: // create the Request for the demanded link
212: CreateConnectionRequest connectionRequest = (CreateConnectionRequest) createTargetRequest();
213: connectionRequest.setTargetEditPart(sourceEP);
214: connectionRequest
215: .setType(RequestConstants.REQ_CONNECTION_START);
216:
217: // only if the connection is supported will we get a non null
218: // command from the sourceEditPart
219: if (sourceEP.getCommand(connectionRequest) == null)
220: return;
221: connectionRequest.setSourceEditPart(sourceEP);
222: connectionRequest.setTargetEditPart(targetEP);
223: connectionRequest.setType(RequestConstants.REQ_CONNECTION_END);
224:
225: // get the command of the target to create the link
226: Command command = targetEP.getCommand(connectionRequest);
227: if (command == null)
228: return;
229:
230: // execute the link creation command
231: setCurrentCommand(command);
232: antiScroll = true;
233: executeCurrentCommand();
234: antiScroll = false;
235:
236: // if no Viewer is set we borrow one from the source EP
237: if (getCurrentViewer() == null)
238: setViewer(sourceEP.getViewer());
239:
240: selectAddedObject(getCurrentViewer(), DiagramCommandStack
241: .getReturnValues(command));
242:
243: deactivate();
244: }
245:
246: /**
247: * After executing a CreateElementCommand or a CreateRelationshipCommand
248: * this method searchs for the created EditPart by the given command
249: * and EdigPart type
250: *
251: * @param command
252: * @param targetEditPart
253: * @return
254: */
255: private EditPart findEditPart(Command command,
256: IElementType editPartType) {
257: // there is the need to search for the concerning EditPart
258: // by this silly complex algorithm
259: Collection objects = DiagramCommandStack
260: .getReturnValues(command);
261: EditPartViewer viewer = getCurrentViewer();
262:
263: for (Iterator i = objects.iterator(); i.hasNext();) {
264: Object object = i.next();
265: if (object instanceof IAdaptable) {
266: Object editPart = viewer.getEditPartRegistry().get(
267: ((IAdaptable) object).getAdapter(View.class));
268:
269: // check for what kind of EditPart we are searching for
270: if (editPartType
271: .equals(New_processElementTypes.ConditionTerm_3005)) {
272: if (editPart instanceof ConditionTermEditPart)
273: return (ConditionTermEditPart) editPart;
274: } else if (editPartType
275: .equals(New_processElementTypes.ConditionTermConditionProxy_4004)) {
276: if (editPart instanceof ConditionTermConditionProxyEditPart)
277: return (ConditionTermConditionProxyEditPart) editPart;
278: } else if (editPartType
279: .equals(New_processElementTypes.OperatorTermSubTerm_4003)) {
280: if (editPart instanceof OperatorTermSubTermEditPart) {
281: return (OperatorTermSubTermEditPart) editPart;
282: }
283: }
284: }
285: }
286: return null;
287: }
288: }
|