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.parts;
025:
026: import java.util.ArrayList;
027: import java.util.Collections;
028: import java.util.HashMap;
029: import java.util.List;
030:
031: import org.eclipse.draw2d.BendpointConnectionRouter;
032: import org.eclipse.draw2d.ColorConstants;
033: import org.eclipse.draw2d.IFigure;
034: import org.eclipse.draw2d.PolygonDecoration;
035: import org.eclipse.draw2d.PolylineConnection;
036: import org.eclipse.draw2d.RotatableDecoration;
037: import org.eclipse.draw2d.geometry.Dimension;
038: import org.eclipse.draw2d.geometry.PointList;
039: import org.eclipse.emf.common.command.AbstractCommand;
040: import org.eclipse.emf.common.command.CompoundCommand;
041: import org.eclipse.emf.common.notify.Notification;
042: import org.eclipse.emf.common.notify.impl.AdapterImpl;
043: import org.eclipse.emf.ecore.EStructuralFeature;
044: import org.eclipse.emf.edit.command.RemoveCommand;
045: import org.eclipse.emf.edit.command.SetCommand;
046: import org.eclipse.emf.transaction.TransactionalEditingDomain;
047: import org.eclipse.emf.transaction.util.TransactionUtil;
048: import org.eclipse.gef.EditPolicy;
049: import org.eclipse.gef.commands.Command;
050: import org.eclipse.gef.editparts.AbstractConnectionEditPart;
051: import org.eclipse.gef.editpolicies.ComponentEditPolicy;
052: import org.eclipse.gef.editpolicies.ConnectionEndpointEditPolicy;
053: import org.eclipse.gef.requests.GroupRequest;
054: import org.eclipse.gmf.runtime.notation.Edge;
055: import org.eclipse.gmf.runtime.notation.NotationPackage;
056: import org.eclipse.gmf.runtime.notation.RelativeBendpoints;
057: import org.eclipse.gmf.runtime.notation.View;
058: import org.eclipse.gmf.runtime.notation.datatype.RelativeBendpoint;
059:
060: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.BaseElement;
061: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.DecorativeModelElement;
062: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.DefaultMepType;
063: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.EndPoint;
064: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.MessageExchange;
065: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.Sequencer;
066: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.componentflowPackage;
067: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.edit.policies.BendpointEditPolicy;
068: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.rules.RuleStrategy;
069: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.wizards.WizardUtil;
070:
071: /**
072: * @generated NOT
073: */
074: public class MessageExchangeEditPart extends AbstractConnectionEditPart
075: implements IUpdatableEditPart {
076:
077: PolylineConnection connection = null;
078:
079: View model = null;
080:
081: BaseElement target_component = null;
082:
083: BaseElement so_component = null;
084:
085: public boolean loop = false;
086:
087: RuleStrategy ruleStrategy;
088:
089: public static final int VISUAL_ID = 4001;
090:
091: protected int null_count = 0;
092:
093: protected boolean delete = false;
094:
095: public void wizardOut(View view, boolean newCom) {
096: // NOTHING to do here
097: }
098:
099: public MessageExchangeEditPart(View view) {
100: assert view instanceof Edge;
101: setModel(view);
102: ruleStrategy = new RuleStrategy(this );
103: }
104:
105: public Edge getDiagramEdge() {
106: return (Edge) getModel();
107: }
108:
109: protected void createEditPolicies() {
110: // XXX: install correct edit policies!
111: installEditPolicy(EditPolicy.COMPONENT_ROLE,
112: new ComponentEditPolicy() {
113: protected Command createDeleteCommand(
114: GroupRequest deleteRequest) {
115: final Edge edgeToRemove = getDiagramEdge();
116: final View source = edgeToRemove.getSource();
117: final View target = edgeToRemove.getTarget();
118: TransactionalEditingDomain editingDomain = TransactionUtil
119: .getEditingDomain(getDiagramEdge()
120: .getDiagram().getElement());
121: CompoundCommand cc = new CompoundCommand();
122: cc
123: .append(createDomainModelRemoveCommand(editingDomain));
124: cc.append(new AbstractCommand() {
125: public boolean canExecute() {
126: return source != null
127: && edgeToRemove != null;
128: }
129:
130: public boolean canUndo() {
131: return source != null
132: && edgeToRemove != null;
133: }
134:
135: public void undo() {
136: source.getDiagram().insertEdge(
137: edgeToRemove);
138: edgeToRemove.setSource(source);
139: edgeToRemove.setTarget(target);
140: }
141:
142: public void redo() {
143: execute();
144: }
145:
146: public void execute() {
147: source.getDiagram().removeEdge(
148: edgeToRemove);
149: edgeToRemove.setSource(null);
150: edgeToRemove.setTarget(null);
151: }
152: });
153: return new WrappingCommand(editingDomain, cc);
154: }
155:
156: private org.eclipse.emf.common.command.Command createDomainModelRemoveCommand(
157: TransactionalEditingDomain editingDomain) {
158: CompoundCommand result = new CompoundCommand();
159: result.append(RemoveCommand.create(
160: editingDomain, getDiagramEdge()
161: .getElement().eContainer(),
162: getDiagramEdge().getElement()
163: .eContainmentFeature(),
164: getDiagramEdge().getElement()));
165: result
166: .append(SetCommand
167: .create(
168: editingDomain,
169: getDiagramEdge()
170: .getElement(),
171: componentflowPackage.eINSTANCE
172: .getMessageExchange_SourceElement(),
173: SetCommand.UNSET_VALUE));
174: result
175: .append(SetCommand
176: .create(
177: editingDomain,
178: getDiagramEdge()
179: .getElement(),
180: componentflowPackage.eINSTANCE
181: .getMessageExchange_TargetElement(),
182: SetCommand.UNSET_VALUE));
183: return result;
184: }
185: });
186: installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE,
187: new ConnectionEndpointEditPolicy());
188: installEditPolicy(EditPolicy.CONNECTION_BENDPOINTS_ROLE,
189: new BendpointEditPolicy());
190: }
191:
192: protected List getModelChildren() {
193: return ((Edge) getModel()).getChildren();
194: }
195:
196: private HashMap structuralFeatures2Refresher;
197:
198: public Refresher getRefresher(EStructuralFeature feature,
199: Notification msg) {
200: if (structuralFeatures2Refresher == null) {
201: createRefreshers();
202: }
203: return (Refresher) structuralFeatures2Refresher.get(feature);
204: }
205:
206: public void activate() {
207:
208: super .activate();
209: ruleStrategy.addAdapters();
210:
211: }
212:
213: public void deactivate() {
214:
215: // If this connection is going to be deleted, we need to refresh its
216: // targets and the sources.
217: if (!loop) {
218: ruleStrategy.refreshTargetDel(this );
219:
220: // keep this alerted for later use
221: ruleStrategy.refreshSourceDel();
222: }
223:
224: ruleStrategy.removeAdapters();
225: super .deactivate();
226: }
227:
228: public DomainModelRefresher getDomainModelRefresher() {
229: return domainModelRefresher;
230: }
231:
232: private DomainModelRefresher domainModelRefresher = new DomainModelRefresher();
233:
234: private class DomainModelRefresher extends AdapterImpl {
235:
236: public void notifyChanged(Notification msg) {
237: super .notifyChanged(msg);
238: if (msg.isTouch()) {
239: return;
240: }
241:
242: Refresher refresher = getRefresher((EStructuralFeature) msg
243: .getFeature(), msg);
244: if (refresher != null) {
245: refresher.refresh();
246: }
247: int feature = -1;
248: feature = msg.getFeatureID(EndPoint.class);
249:
250: if ((feature == componentflowPackage.MESSAGE_EXCHANGE__EXPRESSION)
251: || (feature == componentflowPackage.MESSAGE_EXCHANGE__LABEL)) {
252: modifyExpression();
253: }
254: try {
255: refreshVisuals();
256: } catch (Exception e) {
257: e.printStackTrace();
258:
259: }
260:
261: }
262: }
263:
264: public void addRefresher(EStructuralFeature feature,
265: Refresher refresher) {
266: CompositeRefresher compositeRefresher = getCompositeRefresher(feature);
267: compositeRefresher.addRefresher(refresher);
268: }
269:
270: public void removeRefresher(EStructuralFeature feature,
271: Refresher refresher) {
272: CompositeRefresher compositeRefresher = getCompositeRefresher(feature);
273: compositeRefresher.removeRefresher(refresher);
274: }
275:
276: private CompositeRefresher getCompositeRefresher(
277: EStructuralFeature feature) {
278: if (structuralFeatures2Refresher == null) {
279: createRefreshers();
280: }
281: Refresher refresher = (Refresher) structuralFeatures2Refresher
282: .get(feature);
283: if (refresher instanceof CompositeRefresher) {
284: return (CompositeRefresher) refresher;
285: }
286: CompositeRefresher result = new CompositeRefresher();
287: if (refresher != null) {
288: result.addRefresher(refresher);
289: }
290: structuralFeatures2Refresher.put(feature, result);
291: return result;
292: }
293:
294: private void createRefreshers() {
295: structuralFeatures2Refresher = new HashMap();
296: Refresher childrenRefresher = new Refresher() {
297: public void refresh() {
298: refreshChildren();
299: }
300: };
301: structuralFeatures2Refresher.put(NotationPackage.eINSTANCE
302: .getView_PersistedChildren(), childrenRefresher);
303: structuralFeatures2Refresher.put(NotationPackage.eINSTANCE
304: .getView_TransientChildren(), childrenRefresher);
305: Refresher visibilityRefresher = new Refresher() {
306: public void refresh() {
307: refreshVisibility();
308: }
309: };
310: structuralFeatures2Refresher.put(NotationPackage.eINSTANCE
311: .getView_Visible(), visibilityRefresher);
312: Refresher bendpointsRefresher = new Refresher() {
313: public void refresh() {
314: refreshBendpoints();
315: }
316: };
317: structuralFeatures2Refresher.put(NotationPackage.eINSTANCE
318: .getEdge_Bendpoints(), bendpointsRefresher);
319: structuralFeatures2Refresher.put(NotationPackage.eINSTANCE
320: .getRelativeBendpoints_Points(), bendpointsRefresher);
321: }
322:
323: public void refreshVisuals() {
324:
325: // When we change the default mep of the source, we need to modify the
326: // decoration of the connection.
327:
328: if (!loop)
329: modifyDoc();
330: super .refreshVisuals();
331: refreshVisibility();
332: refreshBendpoints();
333: if (!loop) {
334: ruleStrategy.refreshTarget();
335: ruleStrategy.refreshSource();
336: }
337: // Each time when we refresh the connection, sometimes we need to
338: // refresh all the connections related to both the source and the target
339: // too.
340:
341: /*if(delete)
342: {
343: removeView();
344: }
345: else
346: {
347:
348: }*/
349:
350: }
351:
352: private void removeView() {
353:
354: /*TransactionalEditingDomain editingDomain = TransactionUtil
355: .getEditingDomain(getDiagramEdge().getDiagram().getElement());
356:
357:
358: editingDomain.getCommandStack().execute(createDeleteCommand());*/
359: //deactivate();
360: createDeleteCommand().execute();
361:
362: }
363:
364: protected Command createDeleteCommand() {
365: final Edge edgeToRemove = getDiagramEdge();
366: final View source = edgeToRemove.getSource();
367: final View target = edgeToRemove.getTarget();
368: TransactionalEditingDomain editingDomain = TransactionUtil
369: .getEditingDomain(getDiagramEdge().getDiagram()
370: .getElement());
371: CompoundCommand cc = new CompoundCommand();
372: cc.append(createDomainModelRemoveCommand(editingDomain));
373: cc.append(new AbstractCommand() {
374: public boolean canExecute() {
375: return source != null && edgeToRemove != null;
376: }
377:
378: public boolean canUndo() {
379: return source != null && edgeToRemove != null;
380: }
381:
382: public void undo() {
383: source.getDiagram().insertEdge(edgeToRemove);
384: edgeToRemove.setSource(source);
385: edgeToRemove.setTarget(target);
386: }
387:
388: public void redo() {
389: execute();
390: }
391:
392: public void execute() {
393: source.getDiagram().removeEdge(edgeToRemove);
394: edgeToRemove.setSource(null);
395: edgeToRemove.setTarget(null);
396: }
397: });
398: return new WrappingCommand(editingDomain, cc);
399: }
400:
401: private org.eclipse.emf.common.command.Command createDomainModelRemoveCommand(
402: TransactionalEditingDomain editingDomain) {
403: CompoundCommand result = new CompoundCommand();
404: result.append(RemoveCommand.create(editingDomain,
405: getDiagramEdge().getElement().eContainer(),
406: getDiagramEdge().getElement().eContainmentFeature(),
407: getDiagramEdge().getElement()));
408: result.append(SetCommand.create(editingDomain, getDiagramEdge()
409: .getElement(), componentflowPackage.eINSTANCE
410: .getMessageExchange_SourceElement(),
411: SetCommand.UNSET_VALUE));
412: result.append(SetCommand.create(editingDomain, getDiagramEdge()
413: .getElement(), componentflowPackage.eINSTANCE
414: .getMessageExchange_TargetElement(),
415: SetCommand.UNSET_VALUE));
416: return result;
417: }
418:
419: public void setLoop(boolean loop) {
420: this .loop = loop;
421: }
422:
423: public boolean getLoop() {
424: return this .loop;
425: }
426:
427: public void modifyExpression() {
428:
429: ruleStrategy.modifyExpression();
430: }
431:
432: // This method is used to change the Target Decoration directly by the
433: // default mep.
434: public void changeConnection(DefaultMepType mep) {
435: PolylineConnection connection = this .getConnection();
436: if (connection != null) {
437: try {
438:
439: ((MessageExchangeFigure) connection)
440: .setTargetDecoration(mep);
441:
442: } catch (Exception e) {
443: e.printStackTrace();
444: }
445: }
446: super .refreshVisuals();
447: refreshVisibility();
448: refreshBendpoints();
449: }
450:
451: public void modifyDoc() {
452: PolylineConnection connection = this .getConnection();
453: if (connection != null) {
454: try {
455: View model = (View) this .getModel();
456: MessageExchange object = (MessageExchange) model
457: .getElement();
458:
459: DefaultMepType mep = ruleStrategy.getDefault_MEP();
460:
461: ((MessageExchangeFigure) connection)
462: .setTargetDecoration(mep);
463:
464: } catch (Exception e) {
465: e.printStackTrace();
466: }
467: }
468: }
469:
470: protected void refreshBendpoints() {
471: RelativeBendpoints bendpoints = (RelativeBendpoints) getDiagramEdge()
472: .getBendpoints();
473: if (bendpoints == null) {
474: getConnectionFigure().setRoutingConstraint(
475: Collections.EMPTY_LIST);
476: return;
477: }
478: List modelConstraint = bendpoints.getPoints();
479: List figureConstraint = new ArrayList();
480: for (int i = 0; i < modelConstraint.size(); i++) {
481: RelativeBendpoint wbp = (RelativeBendpoint) modelConstraint
482: .get(i);
483: org.eclipse.draw2d.RelativeBendpoint rbp = new org.eclipse.draw2d.RelativeBendpoint(
484: getConnectionFigure());
485: rbp.setRelativeDimensions(new Dimension(wbp.getSourceX(),
486: wbp.getSourceY()), new Dimension(wbp.getTargetX(),
487: wbp.getTargetY()));
488: rbp.setWeight((i + 1)
489: / ((float) modelConstraint.size() + 1));
490: figureConstraint.add(rbp);
491: }
492: getConnectionFigure().setRoutingConstraint(figureConstraint);
493: }
494:
495: protected void refreshVisibility() {
496: boolean isVisible = ((View) getModel()).isVisible();
497: boolean wasVisible = getFigure().isVisible();
498: if (isVisible == wasVisible) {
499: return;
500: }
501: if (!isVisible && (getSelected() != SELECTED_NONE)) {
502: getViewer().deselect(this );
503: }
504:
505: getFigure().setVisible(isVisible);
506: getFigure().revalidate();
507: }
508:
509: public void addNotify() {
510: super .addNotify();
511: getConnectionFigure().setConnectionRouter(
512: new BendpointConnectionRouter());
513: }
514:
515: public RuleStrategy getRuleStrategy() {
516: return ruleStrategy;
517: }
518:
519: protected IFigure createFigure() {
520:
521: View model = (View) this .getModel();
522:
523: if (delete) {
524: delete = true;
525: return null;
526: }
527: if (ruleStrategy.sourceTargetIsConnection()) {
528: connection = new MessageExchangeFigure(model);
529: delete = false;
530: return connection;
531: } else {
532:
533: if (WizardUtil.isCurrentEditorActive(model.getElement())) {
534: delete = true;
535: null_count = 1;
536: return null;
537: } else {
538: connection = new MessageExchangeFigure(model);
539: delete = false;
540: return connection;
541:
542: }
543: }
544:
545: }
546:
547: public MessageExchangeFigure getConnection() {
548: return (MessageExchangeFigure) connection;
549: }
550:
551: public class MessageExchangeFigure extends
552: org.eclipse.draw2d.PolylineConnection {
553:
554: public MessageExchangeFigure(View model) {
555:
556: setSourceDecoration(createSourceDecoration());
557:
558: MessageExchange object = (MessageExchange) model
559: .getElement();
560: so_component = object.getSourceElement();
561: DefaultMepType mep = ruleStrategy.getDefault_MEP();
562:
563: setTargetDecoration(mep);
564: setColor(object);
565:
566: setConnectionRouter(new BendpointConnectionRouter());
567:
568: }
569:
570: public org.eclipse.draw2d.PolylineDecoration createTargetDecoration() {
571: org.eclipse.draw2d.PolylineDecoration df = new org.eclipse.draw2d.PolylineDecoration();
572: // dispatchNext?
573:
574: org.eclipse.draw2d.geometry.PointList pl = new org.eclipse.draw2d.geometry.PointList();
575: pl.addPoint(-2, 2);
576: pl.addPoint(0, 0);
577: pl.addPoint(-2, -2);
578: df.setTemplate(pl);
579: df.setScale(7, 3);
580:
581: return df;
582: }
583:
584: public org.eclipse.draw2d.PolygonDecoration createTargetInOutDecoration() {
585: PolygonDecoration decoration = new PolygonDecoration();
586:
587: PointList pointList = new PointList();
588: pointList.addPoint(0, 0);
589: pointList.addPoint(-1, 1);
590: pointList.addPoint(-2, 0);
591: pointList.addPoint(-1, -1);
592: decoration.setTemplate(pointList);
593:
594: decoration.setBackgroundColor(ColorConstants.white);
595: decoration.setOpaque(true);
596:
597: return decoration;
598: }
599:
600: public org.eclipse.draw2d.PolylineDecoration createTargetOnewayDecoration() {
601: org.eclipse.draw2d.PolylineDecoration df = new org.eclipse.draw2d.PolylineDecoration();
602: org.eclipse.draw2d.geometry.PointList pl = new org.eclipse.draw2d.geometry.PointList();
603: pl.addPoint(-1, 1);
604: pl.addPoint(0, 0);
605: pl.addPoint(-1, -1);
606: df.setTemplate(pl);
607: df.setScale(7, 3);
608:
609: return df;
610: }
611:
612: private org.eclipse.draw2d.PolygonDecoration createSourceDecoration() {
613: PolygonDecoration decoration = new PolygonDecoration();
614:
615: PointList pointList = new PointList();
616: pointList.addPoint(0, 1);
617: pointList.addPoint(-1, 1);
618: pointList.addPoint(-1, -1);
619: pointList.addPoint(0, -1);
620: decoration.setTemplate(pointList);
621:
622: decoration.setBackgroundColor(ColorConstants.white);
623: decoration.setOpaque(true);
624: return decoration;
625:
626: }
627:
628: private org.eclipse.draw2d.PolygonDecoration createTargetReliableInDecoration() {
629:
630: return new PolygonDecoration();
631:
632: }
633:
634: public void setTargetDecoration(DefaultMepType default_mep) {
635:
636: // set the decoration according to the mep type
637: RotatableDecoration dec = null;
638: if (default_mep.equals(DefaultMepType.INONLY_LITERAL)) {
639:
640: dec = createTargetOnewayDecoration();
641:
642: } else if (default_mep.equals(DefaultMepType.INOUT_LITERAL)) {
643: dec = createTargetInOutDecoration();
644: } else {
645: dec = createTargetReliableInDecoration();
646: }
647: super .setTargetDecoration(dec);
648: }
649:
650: public void setColor(MessageExchange object) {
651:
652: BaseElement target_component = object.getTargetElement();
653:
654: BaseElement source_component = object.getSourceElement();
655: if (target_component instanceof DecorativeModelElement) {
656: setForegroundColor(org.eclipse.draw2d.ColorConstants.blue);
657:
658: } else if (source_component instanceof DecorativeModelElement) {
659: setForegroundColor(org.eclipse.draw2d.ColorConstants.blue);
660:
661: }
662:
663: else if (source_component instanceof Sequencer) {
664: setForegroundColor(org.eclipse.draw2d.ColorConstants.orange);
665:
666: } else
667: this
668: .setForegroundColor(org.eclipse.draw2d.ColorConstants.black);
669: if (loop)
670: setForegroundColor(org.eclipse.draw2d.ColorConstants.red);
671: }
672:
673: }
674:
675: }
|