[CDO] Pointless omnipresent menu contributions to create and edit CDO
users

- Added conditions to gate visibility of the menu

Change-Id: Iff4585d8abc521cd2f53f6242f2149b370c88c0a
diff --git a/cdo/bundles/org.eclipse.papyrus.cdo.security/META-INF/MANIFEST.MF b/cdo/bundles/org.eclipse.papyrus.cdo.security/META-INF/MANIFEST.MF
index a1d54eb..51828a7 100755
--- a/cdo/bundles/org.eclipse.papyrus.cdo.security/META-INF/MANIFEST.MF
+++ b/cdo/bundles/org.eclipse.papyrus.cdo.security/META-INF/MANIFEST.MF
@@ -11,5 +11,6 @@
  org.eclipse.emf.cdo.security;bundle-version="[4.4.0,5.0.0)";visibility:=reexport,
  org.eclipse.papyrus.infra.properties.ui;bundle-version="[4.0.0,5.0.0)",
  org.eclipse.papyrus.infra.constraints;bundle-version="[3.0.0,4.0.0)",
- org.eclipse.papyrus.infra.widgets;bundle-version="[4.0.0,5.0.0)"
-Export-Package: org.eclipse.papyrus.cdo.security.internal.handlers
+ org.eclipse.papyrus.infra.widgets;bundle-version="[4.0.0,5.0.0)",
+ org.eclipse.core.expressions;bundle-version="[3.0.0,4.0.0)"
+Export-Package: org.eclipse.papyrus.cdo.security.handlers
diff --git a/cdo/bundles/org.eclipse.papyrus.cdo.security/plugin.xml b/cdo/bundles/org.eclipse.papyrus.cdo.security/plugin.xml
index 347ffbc..0edbc99 100755
--- a/cdo/bundles/org.eclipse.papyrus.cdo.security/plugin.xml
+++ b/cdo/bundles/org.eclipse.papyrus.cdo.security/plugin.xml
@@ -4,12 +4,12 @@
   <extension point="org.eclipse.ui.commands">
 
     <command
-          defaultHandler="org.eclipse.papyrus.cdo.security.internal.handlers.CreateUserHandler"
+          defaultHandler="org.eclipse.papyrus.cdo.security.handlers.CreateUserHandler"
           id="org.eclipse.papyrus.cdo.security.ui.createuser"
           name="Create User">
     </command>
     <command
-          defaultHandler="org.eclipse.papyrus.cdo.security.internal.handlers.EditUserHandler"
+          defaultHandler="org.eclipse.papyrus.cdo.security.handlers.EditUserHandler"
           id="org.eclipse.papyrus.cdo.security.ui.edituser"
           name="Edit User">
     </command>
@@ -25,22 +25,56 @@
               icon="icons/User.gif"
               label="Create User"
               style="push">
-           <visibleWhen
-                 checkEnabled="true">
-           </visibleWhen>
+              <visibleWhen
+                     checkEnabled="false">
+                  <with
+                        variable="selection">
+                     <test
+                           forcePluginActivation="true"
+                           property="org.eclipse.papyrus.cdo.security.CreateUserCondition"
+                           value="true">
+                     </test>
+                  </with>
+               </visibleWhen>
         </command>
         <command
               commandId="org.eclipse.papyrus.cdo.security.ui.edituser"
               icon="icons/User.gif"
               label="Edit User"
               style="push">
-           <visibleWhen
-                 checkEnabled="true">
-           </visibleWhen>
+                            <visibleWhen
+                     checkEnabled="false">
+                  <with
+                        variable="selection">
+                     <test
+                           forcePluginActivation="true"
+                           property="org.eclipse.papyrus.cdo.security.EditUserCondition"
+                           value="true">
+                     </test>
+                  </with>
+               </visibleWhen>
         </command>
         <!--command id="com.cea.qualitysuite.ui.createProjectMenu" commandId="com.cea.qualitysuite.ui.createProjectCommand" label="Create Project" style="push" /-->
       </menu>
     </menuContribution>
   </extension>
+  
+  <extension
+         point="org.eclipse.core.expressions.propertyTesters">
+      <propertyTester
+            class="org.eclipse.papyrus.cdo.security.internal.conditions.CreateUserCondition"
+            id="org.eclipse.papyrus.cdo.security.CreateUserCondition"
+            namespace="org.eclipse.papyrus.cdo.security"
+            properties="CreateUserCondition"
+            type="org.eclipse.jface.viewers.IStructuredSelection">
+      </propertyTester>
+      <propertyTester
+            class="org.eclipse.papyrus.cdo.security.internal.conditions.EditUserCondition"
+            id="org.eclipse.papyrus.cdo.security.EditUserCondition"
+            namespace="org.eclipse.papyrus.cdo.security"
+            properties="EditUserCondition"
+            type="org.eclipse.jface.viewers.IStructuredSelection">
+      </propertyTester>
+   </extension>
     
 </plugin>
diff --git a/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/internal/handlers/CreateUserHandler.java b/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/handlers/CreateUserHandler.java
similarity index 94%
rename from cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/internal/handlers/CreateUserHandler.java
rename to cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/handlers/CreateUserHandler.java
index cb607d7..0dca6c0 100755
--- a/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/internal/handlers/CreateUserHandler.java
+++ b/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/handlers/CreateUserHandler.java
@@ -15,7 +15,7 @@
  *****************************************************************************/
 
 
-package org.eclipse.papyrus.cdo.security.internal.handlers;
+package org.eclipse.papyrus.cdo.security.handlers;
 
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
@@ -141,9 +141,9 @@
 	/**
 	 *
 	 * @return
-	 * 		the realm (which is the root of the security file)
+	 *         the realm (which is the root of the security file)
 	 */
-	protected final Realm getRealm() {
+	protected static final Realm getRealm() {
 		final IStructuredSelection service = SelectionHelper.getCurrentStructuredSelection();
 		Object first = service.getFirstElement();
 		if (first instanceof Realm) {
@@ -172,9 +172,9 @@
 	/**
 	 *
 	 * @return
-	 * 		<code>true</code>if all required elements are available for a user creation
+	 *         <code>true</code>if all required elements are available for a user creation
 	 */
-	protected boolean allowCreation() {
+	public static boolean allowCreation() {
 		final Realm realm = getRealm();
 		if (null == realm) {
 			return false;
diff --git a/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/internal/handlers/EditUserHandler.java b/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/handlers/EditUserHandler.java
similarity index 87%
rename from cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/internal/handlers/EditUserHandler.java
rename to cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/handlers/EditUserHandler.java
index ceba1ee..f24d29b 100755
--- a/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/internal/handlers/EditUserHandler.java
+++ b/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/handlers/EditUserHandler.java
@@ -13,7 +13,7 @@
  *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
  *
  *****************************************************************************/
-package org.eclipse.papyrus.cdo.security.internal.handlers;
+package org.eclipse.papyrus.cdo.security.handlers;
 
 import java.util.Collection;
 import java.util.HashSet;
@@ -43,9 +43,9 @@
 	/**
 	 *
 	 * @return
-	 * 		the list of selected user
+	 *         the list of selected user
 	 */
-	private Collection<User> getSelectedUsers() {
+	private static Collection<User> getSelectedUsers() {
 		final Collection<User> users = new HashSet<>();
 		final IStructuredSelection selection = SelectionHelper.getCurrentStructuredSelection();
 		final Iterator<?> iter = selection.iterator();
@@ -67,7 +67,11 @@
 	 */
 	@Override
 	public void setEnabled(Object evaluationContext) {
-		setBaseEnabled(getSelectedUsers().size() > 0);
+		setBaseEnabled(isValidSelection());
+	}
+
+	public static boolean isValidSelection() {
+		return getSelectedUsers().size() > 0;
 	}
 
 
diff --git a/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/internal/conditions/CreateUserCondition.java b/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/internal/conditions/CreateUserCondition.java
new file mode 100644
index 0000000..606f991
--- /dev/null
+++ b/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/internal/conditions/CreateUserCondition.java
@@ -0,0 +1,46 @@
+/*****************************************************************************
+ * Copyright (c) 2020 CEA LIST.
+ *
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ *  CEA LIST - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.cdo.security.internal.conditions;
+
+import org.eclipse.core.expressions.PropertyTester;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.papyrus.cdo.security.handlers.CreateUserHandler;
+
+public class CreateUserCondition extends PropertyTester {
+
+	private final static String propertyValue = "CreateUserCondition";
+
+	/**
+	 * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
+	 *
+	 * @param receiver
+	 * @param property
+	 * @param args
+	 * @param expectedValue
+	 * @return
+	 */
+	@Override
+	public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
+		if (!(expectedValue instanceof Boolean)
+				|| !(propertyValue.equals(property))
+				|| !(receiver instanceof IStructuredSelection)) {
+			return false;// worst case, so we result false
+		}
+		return CreateUserHandler.allowCreation();
+	}
+
+}
diff --git a/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/internal/conditions/EditUserCondition.java b/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/internal/conditions/EditUserCondition.java
new file mode 100644
index 0000000..c39664b
--- /dev/null
+++ b/cdo/bundles/org.eclipse.papyrus.cdo.security/src/org/eclipse/papyrus/cdo/security/internal/conditions/EditUserCondition.java
@@ -0,0 +1,46 @@
+/*****************************************************************************
+ * Copyright (c) 2020 CEA LIST.
+ *
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ *  CEA LIST - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.cdo.security.internal.conditions;
+
+import org.eclipse.core.expressions.PropertyTester;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.papyrus.cdo.security.handlers.EditUserHandler;
+
+public class EditUserCondition extends PropertyTester {
+
+	private final static String propertyValue = "EditUserCondition";
+
+	/**
+	 * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
+	 *
+	 * @param receiver
+	 * @param property
+	 * @param args
+	 * @param expectedValue
+	 * @return
+	 */
+	@Override
+	public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
+		if (!(expectedValue instanceof Boolean)
+				|| !(propertyValue.equals(property))
+				|| !(receiver instanceof IStructuredSelection)) {
+			return false;// worst case, so we result false
+		}
+		return EditUserHandler.isValidSelection();
+	}
+
+}