From 5675118e4b27a734a7c103ab43a765b87c8e98b6 Mon Sep 17 00:00:00 2001
From: Vincent Aravantinos <aravantinos@fortiss.org>
Date: Thu, 14 Apr 2016 14:22:00 +0000
Subject: [PATCH] context menu for constraints refs 2553

---
 .../ui/internal/views/ConstraintMenu.java     | 210 +++++++++++++++---
 1 file changed, 180 insertions(+), 30 deletions(-)

diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/ConstraintMenu.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/ConstraintMenu.java
index 40460c433..d91df48db 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/ConstraintMenu.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/ConstraintMenu.java
@@ -14,6 +14,8 @@
  +-----------------------------------------------------------------------*/
 package org.fortiss.tooling.kernel.ui.internal.views;
 
+import static org.eclipse.jface.resource.ImageDescriptor.createFromImage;
+
 import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -21,29 +23,41 @@ import java.util.stream.Collectors;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.ActionContributionItem;
+import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.action.IContributionItem;
+import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.viewers.DecorationOverlayIcon;
 import org.eclipse.jface.viewers.IDecoration;
 import org.eclipse.swt.graphics.Image;
+import org.fortiss.tooling.kernel.model.constraints.ConstrainedWithChecksum;
+import org.fortiss.tooling.kernel.model.constraints.ErrorVerificationStatus;
+import org.fortiss.tooling.kernel.model.constraints.FailVerificationStatus;
 import org.fortiss.tooling.kernel.model.constraints.IConstrained;
 import org.fortiss.tooling.kernel.model.constraints.IConstraint;
+import org.fortiss.tooling.kernel.model.constraints.IConstraintVerificationStatus;
 import org.fortiss.tooling.kernel.model.constraints.OutdatedVerificationStatus;
+import org.fortiss.tooling.kernel.model.constraints.SuccessVerificationStatus;
+import org.fortiss.tooling.kernel.service.ICommandStackService;
+import org.fortiss.tooling.kernel.service.IConstraintVerificationService;
+import org.fortiss.tooling.kernel.service.IConstraintVerificationService.IFix;
 import org.fortiss.tooling.kernel.ui.ESharedImages;
+import org.fortiss.tooling.kernel.ui.ToolingKernelUIActivator;
 import org.fortiss.tooling.kernel.ui.extension.IContextMenuContributor;
 import org.fortiss.tooling.kernel.ui.extension.IModelElementHandler;
 import org.fortiss.tooling.kernel.ui.extension.data.ContextMenuContextProvider;
+import org.fortiss.tooling.kernel.ui.service.IConstraintVerificationUIService;
 import org.fortiss.tooling.kernel.ui.service.IContextMenuService;
+import org.fortiss.tooling.kernel.ui.service.IModelEditorBindingService;
 import org.fortiss.tooling.kernel.ui.service.IModelElementHandlerService;
 
 /**
- * The contributor for the dynamic new menu of the navigator view based on model
- * element composition.
+ * Context menu for constraints.
  * 
  * @author hoelzl
  * @author $Author$
  * @version $Rev$
- * @ConQAT.Rating GREEN Hash: 49C26BBDB13EC81195004EDDE740E2C3
+ * @ConQAT.Rating YELLOW Hash: 480E689337B14B883F1AA3967CA73AEC
  */
 public class ConstraintMenu implements IContextMenuContributor {
 
@@ -55,60 +69,196 @@ public class ConstraintMenu implements IContextMenuContributor {
 			return Collections.emptyList();
 		}
 		return ((IConstrained)selectedObject).getConstraints().stream()
-				.filter(c -> c.getVerificationStatus() instanceof OutdatedVerificationStatus)
-				.map(c -> new ActionContributionItem(new CheckOutdatedConstraintAction(c)))
+				.filter(c -> !(c.getVerificationStatus() instanceof SuccessVerificationStatus))
+				.map(c -> constraintToAction(c, (IConstrained)selectedObject))
 				.collect(Collectors.toList());
 	}
 
+	/**
+	 * @param c
+	 * @param elt
+	 * @return The submenu corresponding to the status of <code>c</code>. <code>elt</code> is needed
+	 *         to prevent displaying an entry to go to <code>elt</code> (not hurtful, but useless,
+	 *         since the user is already seeing it).
+	 */
+	private IContributionItem constraintToAction(IConstraint c, IConstrained elt) {
+		IConstraintVerificationStatus status = c.getVerificationStatus();
+		if(status instanceof OutdatedVerificationStatus) {
+			return new CheckOutdatedConstraintAction(c, elt);
+		} else if(status instanceof FailVerificationStatus) {
+			return new CheckFailingConstraintAction(c, elt);
+		} else if(status instanceof ErrorVerificationStatus) {
+			return new CheckErrorConstraintAction(c, elt);
+		} else {
+			return null;
+		}
+	}
+
 	/** {@inheritDoc} */
 	@Override
 	public String getMenuSectionID() {
 		return IContextMenuService.TOP_MOST_MENU_SECTION_ID;
 	}
 
-	/** Action for creating a new prototype. */
-	private static class CheckOutdatedConstraintAction extends Action {
+	/** Action to "go to" a given constrained element. */
+	private static class GoToConstrained extends Action {
+
+		/** The constrained element to open. */
+		private IConstrained c;
 
 		/** Constructor. */
-		public CheckOutdatedConstraintAction(IConstraint c) {
-			super(getName(c), getIcon(c));
+		public GoToConstrained(IConstrained c) {
+			super("Go to " + IModelElementHandlerService.INSTANCE.getName(c),
+					createFromImage(IModelElementHandlerService.INSTANCE.getIcon(c)));
+			this.c = c;
 		}
 
-		/** Get the icon of the prototype. */
-		public static String getName(IConstraint c) {
+		/** {@inheritDoc} */
+		@Override
+		public void run() {
 			IModelElementHandler<EObject> handler =
 					IModelElementHandlerService.INSTANCE.getModelElementHandler(c);
+			EObject eltToOpen = handler == null ? c : handler.handleOpenModelElementRequest(c);
+			IModelEditorBindingService.INSTANCE.openInEditor(eltToOpen);
+		}
+	}
+
+	/** Action for creating a new prototype. */
+	private static class ConstraintSubMenuBase extends MenuManager {
+
+		/** The constraint. */
+		protected IConstraint c;
 
-			return handler == null ? "[Outdated constraint]" : handler.getName(c);
+		/**
+		 * The action to get more information about the constraint status.
+		 * The same action allows to update an outdated constraint status (there is no
+		 * "more information" to get out of an outdated constraint); it is stored in a field to
+		 * allow the "update" action to change the icon and the text going with it.
+		 */
+		protected ActionContributionItem moreInfoAction;
+
+		/** Constructor. */
+		public ConstraintSubMenuBase(IConstraint c, IConstrained selectedElt, String prefix,
+				ImageDescriptor overlay) {
+			super(getName(c, prefix), getIcon(c, overlay), null);
+			this.c = c;
+			moreInfoAction = new ActionContributionItem(new OpenStatusAction());
+			this.add(moreInfoAction);
+			for(ConstrainedWithChecksum cwc : c.getConstrainedsWithChecksum()) {
+				if(!selectedElt.equals(cwc.getConstrained())) {
+					this.add(new ActionContributionItem(new GoToConstrained(cwc.getConstrained())));
+				}
+			}
 		}
 
 		/** Get the icon of the prototype. */
-		public static ImageDescriptor getIcon(IConstraint c) {
-			IModelElementHandler<EObject> handler =
-					IModelElementHandlerService.INSTANCE.getModelElementHandler(c);
-			if(handler == null) {
-				return null;
-			}
-			Image img = handler.getIcon(c);
+		public static String getName(IConstraint c, String prefix) {
+			String name = IModelElementHandlerService.INSTANCE.getName(c);
+			return prefix + (name == null ? "Constraint" : name);
+		}
+
+		/** Get the icon of the prototype. */
+		public static ImageDescriptor getIcon(IConstraint c, ImageDescriptor overlay) {
+			Image img = IModelElementHandlerService.INSTANCE.getIcon(c);
 			if(img == null) {
 				return null;
 			}
-			ImageDescriptor overlay = ESharedImages.WARNING_OVERLAY.getImageDescriptor();
 			ImageDescriptor[] descriptors = new ImageDescriptor[5];
 			descriptors[IDecoration.BOTTOM_LEFT] = overlay;
 			return new DecorationOverlayIcon(img, descriptors);
 		}
 
-		/** {@inheritDoc} */
-		@Override
-		public void run() {
-			// ICommandStackService.INSTANCE.runAsCommand(container, new Runnable() {
-			//
-			// @Override
-			// public void run() {
-			//
-			// }
-			// });
+		/** Action to update a constraint. */
+		protected class OpenStatusAction extends Action {
+
+			/** Constructor. */
+			public OpenStatusAction() {
+				super("Get more information", ToolingKernelUIActivator
+						.getImageDescriptor("icons/info.gif"));
+			}
+
+			/** {@inheritDoc} */
+			@Override
+			public void run() {
+				ICommandStackService.INSTANCE.runAsCommand(c, new Runnable() {
+
+					@Override
+					public void run() {
+						IConstraintVerificationUIService.INSTANCE.openStatus(c
+								.getVerificationStatus());
+					}
+				});
+			}
+		}
+	}
+
+	/** Action for creating a new prototype. */
+	private static class CheckOutdatedConstraintAction extends ConstraintSubMenuBase {
+
+		/** Constructor. */
+		public CheckOutdatedConstraintAction(IConstraint c, IConstrained selectedElt) {
+			super(c, selectedElt, "Outdated constraint: ", ESharedImages.WARNING_OVERLAY
+					.getImageDescriptor());
+			IAction action = moreInfoAction.getAction();
+			action.setImageDescriptor(ToolingKernelUIActivator.getImageDescriptor("icons/ok.png"));
+			action.setText("Check");
+		}
+	}
+
+	/** Action for creating a new prototype. */
+	private static class CheckUnsuccessfulConstraintAction extends ConstraintSubMenuBase {
+
+		/** Constructor. */
+		public CheckUnsuccessfulConstraintAction(IConstraint c, IConstrained selectedElt,
+				String prefix) {
+			super(c, selectedElt, prefix, ESharedImages.ERROR_OVERLAY.getImageDescriptor());
+			for(IFix fix : IConstraintVerificationService.INSTANCE.fixes(c.getVerificationStatus())) {
+				this.add(new ActionContributionItem(new FixAction(fix)));
+			}
+		}
+
+		/** Action to update a constraint. */
+		private class FixAction extends Action {
+
+			/** The fix to achieve. */
+			IFix fix;
+
+			/** Constructor. */
+			public FixAction(IFix fix) {
+				super(fix.getDescription(), ToolingKernelUIActivator
+						.getImageDescriptor("icons/fix.png"));
+				this.fix = fix;
+			}
+
+			/** {@inheritDoc} */
+			@Override
+			public void run() {
+				ICommandStackService.INSTANCE.runAsCommand(c, new Runnable() {
+
+					@Override
+					public void run() {
+						fix.runFix(c.getVerificationStatus());
+					}
+				});
+			}
+		}
+	}
+
+	/** Action for creating a new prototype. */
+	private static class CheckFailingConstraintAction extends CheckUnsuccessfulConstraintAction {
+
+		/** Constructor. */
+		public CheckFailingConstraintAction(IConstraint c, IConstrained selectedElt) {
+			super(c, selectedElt, "Unsatisfied constraint: ");
+		}
+	}
+
+	/** Action for creating a new prototype. */
+	private static class CheckErrorConstraintAction extends CheckUnsuccessfulConstraintAction {
+
+		/** Constructor. */
+		public CheckErrorConstraintAction(IConstraint c, IConstrained selectedElt) {
+			super(c, selectedElt, "Error while checking constraint: ");
 		}
 	}
 }
-- 
GitLab