001: package newprocess.diagram.cust.operation;
002:
003: import newprocess.NewprocessPackage;
004: import newprocess.diagram.edit.parts.AndOperatorEditPart;
005: import newprocess.diagram.edit.parts.ConditionTermConditionProxyEditPart;
006: import newprocess.diagram.edit.parts.ConditionTermEditPart;
007: import newprocess.diagram.edit.parts.OperatorTermSubTermEditPart;
008: import newprocess.diagram.edit.parts.OrOperatorEditPart;
009: import newprocess.diagram.edit.parts.RootEditPart;
010: import newprocess.diagram.edit.parts.RootTermEditPart;
011:
012: import org.eclipse.core.commands.ExecutionException;
013: import org.eclipse.core.runtime.IAdaptable;
014: import org.eclipse.core.runtime.IProgressMonitor;
015: import org.eclipse.emf.ecore.EObject;
016: import org.eclipse.emf.ecore.EReference;
017: import org.eclipse.emf.transaction.TransactionalEditingDomain;
018: import org.eclipse.gef.ConnectionEditPart;
019: import org.eclipse.gef.EditPart;
020: import org.eclipse.gef.commands.Command;
021: import org.eclipse.gef.commands.CommandStack;
022: import org.eclipse.gef.requests.GroupRequest;
023: import org.eclipse.gmf.runtime.common.core.command.CommandResult;
024: import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
025: import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
026: import org.eclipse.gmf.runtime.diagram.ui.requests.EditCommandRequestWrapper;
027: import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants;
028: import org.eclipse.gmf.runtime.emf.type.core.commands.DestroyReferenceCommand;
029: import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
030: import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyReferenceRequest;
031: import org.eclipse.gmf.runtime.notation.Node;
032:
033: /**
034: * @author sh
035: *
036: */
037: public class ChainDeleteOperation extends DestroyReferenceCommand {
038: private EditPart hostEP = null;
039:
040: /**
041: * Constructor
042: * @param request
043: */
044: public ChainDeleteOperation(DestroyReferenceRequest request,
045: EditPart host) {
046: super (request);
047: hostEP = host;
048: }
049:
050: @Override
051: protected CommandResult doExecuteWithResult(
052: IProgressMonitor monitor, IAdaptable info)
053: throws ExecutionException {
054:
055: super .doExecuteWithResult(monitor, info);
056:
057: ConditionTermEditPart portEP = null;
058:
059: // if we have the Term/Proxy link
060: if (hostEP instanceof ConditionTermConditionProxyEditPart) {
061: ConditionTermConditionProxyEditPart ppEP = (ConditionTermConditionProxyEditPart) hostEP;
062: if (ppEP.getSource() != null)
063: portEP = (ConditionTermEditPart) ppEP.getSource();
064: }
065:
066: // if we have the Operator/Term link
067: if (hostEP instanceof OperatorTermSubTermEditPart) {
068: OperatorTermSubTermEditPart subtermEP = (OperatorTermSubTermEditPart) hostEP;
069: if (subtermEP.getTarget() != null) {
070: if (subtermEP.getTarget() instanceof AndOperatorEditPart)
071: return deleteLink(subtermEP);
072: else if (subtermEP.getTarget() instanceof OrOperatorEditPart)
073: return deleteLink(subtermEP);
074: else if (subtermEP.getTarget() instanceof ConditionTermEditPart)
075: portEP = (ConditionTermEditPart) subtermEP
076: .getTarget();
077: }
078: if (subtermEP.getTarget() != null)
079: portEP = (ConditionTermEditPart) subtermEP.getTarget();
080: }
081:
082: // if we have the Root/Term link
083: if (hostEP instanceof RootTermEditPart) {
084: RootTermEditPart termEP = (RootTermEditPart) hostEP;
085: if (termEP.getTarget() != null) {
086: if (termEP.getTarget() instanceof ConditionTermEditPart)
087: portEP = (ConditionTermEditPart) termEP.getTarget();
088: else
089: return deleteLink(termEP);
090: }
091: }
092:
093: // delete the term
094: return deletePort(portEP);
095: }
096:
097: private CommandResult deletePort(ConditionTermEditPart portEP) {
098: // there is the need to select an other editpart to avoid exceptions
099: hostEP.getViewer().select(hostEP.getRoot());
100:
101: // build the Request to delete the port
102: CommandStack stack = hostEP.getViewer().getEditDomain()
103: .getCommandStack();
104: GroupRequest req = new GroupRequest(RequestConstants.REQ_DELETE);
105: req.setEditParts(portEP);
106:
107: // wrapp the request into a RequestWrapper
108: TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) hostEP)
109: .getEditingDomain();
110: EditCommandRequestWrapper semReq = new EditCommandRequestWrapper(
111: new DestroyElementRequest(editingDomain, false), req
112: .getExtendedData());
113:
114: // delete the port
115: Command command = portEP.getCommand(semReq);
116: if (command != null && command.canExecute()) {
117: stack.execute(command);
118: }
119:
120: return CommandResult.newOKCommandResult();
121: }
122:
123: private CommandResult deleteLink(ConnectionEditPart conEP) {
124: // there is the need to select an other editpart to avoid exceptions
125: hostEP.getViewer().select(hostEP.getRoot());
126:
127: // build the Request to delete the port
128: CommandStack stack = hostEP.getViewer().getEditDomain()
129: .getCommandStack();
130: GroupRequest req = new GroupRequest(RequestConstants.REQ_DELETE);
131: req.setEditParts(conEP);
132:
133: EObject container = null;
134: EReference ref = null;
135: EObject refObj = null;
136: if (conEP instanceof RootTermEditPart) {
137: container = ((Node) ((RootEditPart) conEP.getSource())
138: .getModel()).getElement();
139: ref = NewprocessPackage.eINSTANCE.getRoot_Term();
140: if (conEP.getTarget() instanceof AndOperatorEditPart)
141: refObj = ((Node) ((AndOperatorEditPart) conEP
142: .getTarget()).getModel()).getElement();
143: else if (conEP.getTarget() instanceof OrOperatorEditPart)
144: refObj = ((Node) ((OrOperatorEditPart) conEP
145: .getTarget()).getModel()).getElement();
146: } else if (conEP instanceof OperatorTermSubTermEditPart) {
147: EditPart part = conEP.getSource();
148: if (part instanceof AndOperatorEditPart) {
149: container = ((Node) ((AndOperatorEditPart) part)
150: .getModel()).getElement();
151: ref = NewprocessPackage.eINSTANCE
152: .getOperatorTerm_SubTerm();
153: if (conEP.getTarget() instanceof AndOperatorEditPart)
154: refObj = ((Node) ((AndOperatorEditPart) conEP
155: .getTarget()).getModel()).getElement();
156: else if (conEP.getTarget() instanceof OrOperatorEditPart)
157: refObj = ((Node) ((OrOperatorEditPart) conEP
158: .getTarget()).getModel()).getElement();
159: } else if (part instanceof OrOperatorEditPart) {
160: container = ((Node) ((OrOperatorEditPart) part)
161: .getModel()).getElement();
162: ref = NewprocessPackage.eINSTANCE
163: .getOperatorTerm_SubTerm();
164: if (conEP.getTarget() instanceof AndOperatorEditPart)
165: refObj = ((Node) ((AndOperatorEditPart) conEP
166: .getTarget()).getModel()).getElement();
167: else if (conEP.getTarget() instanceof OrOperatorEditPart)
168: refObj = ((Node) ((OrOperatorEditPart) conEP
169: .getTarget()).getModel()).getElement();
170: }
171: }
172:
173: // return if parameters aren't specified
174: if (container == null || ref == null || refObj == null)
175: return null;
176:
177: DestroyReferenceRequest destReq = new DestroyReferenceRequest(
178: container, ref, refObj, false);
179: DestroyReferenceCommand com = new DestroyReferenceCommand(
180: destReq);
181:
182: if (com != null && com.canExecute()) {
183: stack.execute(new ICommandProxy(com));
184: }
185: return CommandResult.newOKCommandResult();
186: }
187: }
|