/OntoUML.diagram/src/OntoUML/diagram/edit/commands/MaterialAssociationReorientCommand.java

http://ontouml.googlecode.com/ · Java · 172 lines · 107 code · 17 blank · 48 comment · 21 complexity · 11a6dd989f127fce276620d3a0b68f8d MD5 · raw file

  1. package OntoUML.diagram.edit.commands;
  2. import org.eclipse.core.commands.ExecutionException;
  3. import org.eclipse.core.runtime.IAdaptable;
  4. import org.eclipse.core.runtime.IProgressMonitor;
  5. import org.eclipse.emf.ecore.EObject;
  6. import org.eclipse.gmf.runtime.common.core.command.CommandResult;
  7. import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
  8. import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest;
  9. /**
  10. * @generated
  11. */
  12. public class MaterialAssociationReorientCommand extends EditElementCommand {
  13. /**
  14. * @generated
  15. */
  16. private final int reorientDirection;
  17. /**
  18. * @generated
  19. */
  20. private final EObject oldEnd;
  21. /**
  22. * @generated
  23. */
  24. private final EObject newEnd;
  25. /**
  26. * @generated
  27. */
  28. public MaterialAssociationReorientCommand(
  29. ReorientRelationshipRequest request) {
  30. super(request.getLabel(), request.getRelationship(), request);
  31. reorientDirection = request.getDirection();
  32. oldEnd = request.getOldRelationshipEnd();
  33. newEnd = request.getNewRelationshipEnd();
  34. }
  35. /**
  36. * @generated
  37. */
  38. public boolean canExecute() {
  39. if (false == getElementToEdit() instanceof OntoUML.MaterialAssociation) {
  40. return false;
  41. }
  42. if (reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) {
  43. return canReorientSource();
  44. }
  45. if (reorientDirection == ReorientRelationshipRequest.REORIENT_TARGET) {
  46. return canReorientTarget();
  47. }
  48. return false;
  49. }
  50. /**
  51. * @generated
  52. */
  53. protected boolean canReorientSource() {
  54. if (!(oldEnd instanceof OntoUML.Type && newEnd instanceof OntoUML.Type)) {
  55. return false;
  56. }
  57. if (getLink().getAssociationEndAux2().size() != 1) {
  58. return false;
  59. }
  60. OntoUML.Type target = (OntoUML.Type) getLink().getAssociationEndAux2()
  61. .get(0);
  62. if (!(getLink().eContainer() instanceof OntoUML.Container)) {
  63. return false;
  64. }
  65. OntoUML.Container container = (OntoUML.Container) getLink()
  66. .eContainer();
  67. return OntoUML.diagram.edit.policies.OntoUMLBaseItemSemanticEditPolicy.LinkConstraints
  68. .canExistMaterialAssociation_4007(container, getNewSource(),
  69. target);
  70. }
  71. /**
  72. * @generated
  73. */
  74. protected boolean canReorientTarget() {
  75. if (!(oldEnd instanceof OntoUML.Type && newEnd instanceof OntoUML.Type)) {
  76. return false;
  77. }
  78. if (getLink().getAssociationEndAux1().size() != 1) {
  79. return false;
  80. }
  81. OntoUML.Type source = (OntoUML.Type) getLink().getAssociationEndAux1()
  82. .get(0);
  83. if (!(getLink().eContainer() instanceof OntoUML.Container)) {
  84. return false;
  85. }
  86. OntoUML.Container container = (OntoUML.Container) getLink()
  87. .eContainer();
  88. return OntoUML.diagram.edit.policies.OntoUMLBaseItemSemanticEditPolicy.LinkConstraints
  89. .canExistMaterialAssociation_4007(container, source,
  90. getNewTarget());
  91. }
  92. /**
  93. * @generated
  94. */
  95. protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
  96. IAdaptable info) throws ExecutionException {
  97. if (!canExecute()) {
  98. throw new ExecutionException(
  99. "Invalid arguments in reorient link command"); //$NON-NLS-1$
  100. }
  101. if (reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) {
  102. return reorientSource();
  103. }
  104. if (reorientDirection == ReorientRelationshipRequest.REORIENT_TARGET) {
  105. return reorientTarget();
  106. }
  107. throw new IllegalStateException();
  108. }
  109. /**
  110. * @generated
  111. */
  112. protected CommandResult reorientSource() throws ExecutionException {
  113. getLink().getAssociationEndAux1().remove(getOldSource());
  114. getLink().getAssociationEndAux1().add(getNewSource());
  115. return CommandResult.newOKCommandResult(getLink());
  116. }
  117. /**
  118. * @generated
  119. */
  120. protected CommandResult reorientTarget() throws ExecutionException {
  121. getLink().getAssociationEndAux2().remove(getOldTarget());
  122. getLink().getAssociationEndAux2().add(getNewTarget());
  123. return CommandResult.newOKCommandResult(getLink());
  124. }
  125. /**
  126. * @generated
  127. */
  128. protected OntoUML.MaterialAssociation getLink() {
  129. return (OntoUML.MaterialAssociation) getElementToEdit();
  130. }
  131. /**
  132. * @generated
  133. */
  134. protected OntoUML.Type getOldSource() {
  135. return (OntoUML.Type) oldEnd;
  136. }
  137. /**
  138. * @generated
  139. */
  140. protected OntoUML.Type getNewSource() {
  141. return (OntoUML.Type) newEnd;
  142. }
  143. /**
  144. * @generated
  145. */
  146. protected OntoUML.Type getOldTarget() {
  147. return (OntoUML.Type) oldEnd;
  148. }
  149. /**
  150. * @generated
  151. */
  152. protected OntoUML.Type getNewTarget() {
  153. return (OntoUML.Type) newEnd;
  154. }
  155. }