001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.edit.policies;
025:
026: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.edit.parts.WrappingCommand;
027:
028: import java.util.ArrayList;
029: import java.util.List;
030:
031: import org.eclipse.draw2d.Connection;
032:
033: import org.eclipse.draw2d.geometry.Dimension;
034: import org.eclipse.draw2d.geometry.Point;
035:
036: import org.eclipse.emf.common.command.AbstractCommand;
037:
038: import org.eclipse.emf.transaction.TransactionalEditingDomain;
039:
040: import org.eclipse.emf.transaction.util.TransactionUtil;
041:
042: import org.eclipse.gef.commands.Command;
043:
044: import org.eclipse.gef.requests.BendpointRequest;
045:
046: import org.eclipse.gmf.runtime.notation.Edge;
047: import org.eclipse.gmf.runtime.notation.NotationFactory;
048: import org.eclipse.gmf.runtime.notation.RelativeBendpoints;
049:
050: import org.eclipse.gmf.runtime.notation.datatype.RelativeBendpoint;
051:
052: /**
053: * @generated
054: */
055: public class BendpointEditPolicy extends
056: org.eclipse.gef.editpolicies.BendpointEditPolicy {
057: /**
058: * @generated
059: */
060: protected Edge getDiagramEdge() {
061: return (Edge) getHost().getModel();
062: }
063:
064: /**
065: * @generated
066: */
067: protected Command getCreateBendpointCommand(BendpointRequest request) {
068: return getModifyBendpointCommand(new BendpointCreator(request
069: .getIndex(), request.getLocation()));
070: }
071:
072: /**
073: * @generated
074: */
075: protected Command getDeleteBendpointCommand(BendpointRequest request) {
076: return getModifyBendpointCommand(new BendpointDeleter(request
077: .getIndex()));
078: }
079:
080: /**
081: * @generated
082: */
083: protected Command getMoveBendpointCommand(BendpointRequest request) {
084: return getModifyBendpointCommand(new BendpointMover(request
085: .getIndex(), request.getLocation()));
086: }
087:
088: /**
089: * @generated
090: */
091: private Command getModifyBendpointCommand(BendpointModifier modifier) {
092: TransactionalEditingDomain editingDomain = TransactionUtil
093: .getEditingDomain(getDiagramEdge().getDiagram()
094: .getElement());
095: return new WrappingCommand(editingDomain,
096: new BendpointModificationCommand(modifier));
097: }
098:
099: /**
100: * @generated
101: */
102: private class BendpointModificationCommand extends AbstractCommand {
103: /**
104: * @generated
105: */
106: private BendpointModifier bendpointModifier;
107:
108: /**
109: * @generated
110: */
111: private List oldPoints;
112:
113: /**
114: * @generated
115: */
116: public BendpointModificationCommand(
117: BendpointModifier bendpointModifier) {
118: this .bendpointModifier = bendpointModifier;
119: }
120:
121: /**
122: * @generated
123: */
124: public void execute() {
125: Connection connection = getConnection();
126: final Point ptRef1 = connection.getSourceAnchor()
127: .getReferencePoint().getCopy();
128: connection.translateToRelative(ptRef1);
129: final Point ptRef2 = connection.getTargetAnchor()
130: .getReferencePoint().getCopy();
131: connection.translateToRelative(ptRef2);
132: RelativeBendpoints bendpoints = (RelativeBendpoints) getDiagramEdge()
133: .getBendpoints();
134: if (bendpoints == null) {
135: bendpoints = NotationFactory.eINSTANCE
136: .createRelativeBendpoints();
137: getDiagramEdge().setBendpoints(bendpoints);
138: }
139: oldPoints = bendpoints.getPoints();
140: List newPoints = new ArrayList(oldPoints);
141: bendpointModifier.applyModification(newPoints,
142: new BendpointConverter() {
143: public RelativeBendpoint convert(Point point) {
144: Dimension s = point.getDifference(ptRef1);
145: Dimension t = point.getDifference(ptRef2);
146: return new RelativeBendpoint(s.width,
147: s.height, t.width, t.height);
148: }
149: });
150: bendpoints.setPoints(newPoints);
151: }
152:
153: /**
154: * @generated
155: */
156: public void undo() {
157: RelativeBendpoints bendpoints = (RelativeBendpoints) getDiagramEdge()
158: .getBendpoints();
159: bendpoints.setPoints(oldPoints);
160: }
161:
162: /**
163: * @generated
164: */
165: public void redo() {
166: execute();
167: }
168:
169: /**
170: * @generated
171: */
172: public boolean canExecute() {
173: return true;
174: }
175: }
176:
177: /**
178: * @generated
179: */
180: private static interface BendpointConverter {
181: public RelativeBendpoint convert(Point point);
182: }
183:
184: /**
185: * @generated
186: */
187: private static interface BendpointModifier {
188: public void applyModification(
189: List/*<RelativeBendpoint>*/originalBendpoints,
190: BendpointConverter converter);
191: }
192:
193: /**
194: * @generated
195: */
196: private static class BendpointDeleter implements BendpointModifier {
197: /**
198: * @generated
199: */
200: private int index;
201:
202: /**
203: * @generated
204: */
205: public BendpointDeleter(int index) {
206: this .index = index;
207: }
208:
209: /**
210: * @generated
211: */
212: public void applyModification(List originalBendpoints,
213: BendpointConverter converter) {
214: originalBendpoints.remove(index);
215: }
216: }
217:
218: /**
219: * @generated
220: */
221: private static class BendpointMover implements BendpointModifier {
222: /**
223: * @generated
224: */
225: private int index;
226:
227: /**
228: * @generated
229: */
230: private Point point;
231:
232: /**
233: * @generated
234: */
235: public BendpointMover(int index, Point point) {
236: this .index = index;
237: this .point = point;
238: }
239:
240: /**
241: * @generated
242: */
243: public void applyModification(List originalBendpoints,
244: BendpointConverter converter) {
245: originalBendpoints.set(index, converter.convert(point));
246: }
247: }
248:
249: /**
250: * @generated
251: */
252: private static class BendpointCreator implements BendpointModifier {
253: /**
254: * @generated
255: */
256: private int index;
257:
258: /**
259: * @generated
260: */
261: private Point point;
262:
263: /**
264: * @generated
265: */
266: public BendpointCreator(int index, Point point) {
267: this .index = index;
268: this .point = point;
269: }
270:
271: /**
272: * @generated
273: */
274: public void applyModification(List originalBendpoints,
275: BendpointConverter converter) {
276: originalBendpoints.add(index, converter.convert(point));
277: }
278: }
279: }
|