Bug 376101 - [sfs] Implement option to disable of SFS team actions
diff --git a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/actions/ActionBase.java b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/actions/ActionBase.java
index 6524249..4ca6c60 100644
--- a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/actions/ActionBase.java
+++ b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/actions/ActionBase.java
@@ -17,12 +17,14 @@
 import org.eclipse.core.filesystem.EFS;
 import org.eclipse.core.internal.resources.semantic.ui.SemanticResourcesUIPlugin;
 import org.eclipse.core.internal.resources.semantic.util.ISemanticFileSystemLog;
+import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.semantic.ISemanticFile;
 import org.eclipse.core.resources.semantic.ISemanticFileSystem;
 import org.eclipse.core.resources.semantic.ISemanticResource;
 import org.eclipse.core.resources.semantic.ISemanticResourceInfo;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.QualifiedName;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.dialogs.MessageDialog;
@@ -37,6 +39,8 @@
 
 abstract class ActionBase implements IObjectActionDelegate {
 
+	static final QualifiedName DISABLE_ALL_SFS_ACTIONS = new QualifiedName(SemanticResourcesUIPlugin.PLUGIN_ID, "DisableAllSFSActions"); //$NON-NLS-1$
+
 	private IStructuredSelection mySelection;
 	private IWorkbenchPart myActivePart;
 	private ISemanticFileSystemLog myLog;
@@ -61,8 +65,54 @@
 		return this.mySelection;
 	}
 
-	@SuppressWarnings( {"rawtypes"})
+	protected boolean checkSelectionEnabled() {
+		boolean shouldEnable = true;
+
+		// we check for all object that they are semantic resources and there is
+		// no veto property set
+
+		for (Iterator<?> it = getSelection().iterator(); it.hasNext();) {
+
+			Object nextObject = it.next();
+
+			if (!(nextObject instanceof ISemanticResource)) {
+				shouldEnable = false;
+				break;
+			}
+			ISemanticResource sResource = (ISemanticResource) nextObject;
+			IResource resource = sResource.getAdaptedResource();
+
+			while (resource != null) {
+				ISemanticResource sRes = (ISemanticResource) resource.getAdapter(ISemanticResource.class);
+
+				if (sRes != null) {
+					try {
+						if (sRes.getPersistentProperty(DISABLE_ALL_SFS_ACTIONS) != null) {
+							shouldEnable = false;
+							break;
+						}
+					} catch (CoreException e) {
+						shouldEnable = false;
+						break;
+					}
+				} else {
+					break;
+				}
+				resource = resource.getParent();
+			}
+			if (!shouldEnable) {
+				break;
+			}
+		}
+
+		return shouldEnable;
+	}
+
+	@SuppressWarnings({"rawtypes"})
 	protected boolean checkSelectionNonLocalOnly() {
+		if (!checkSelectionEnabled()) {
+			return false;
+		}
 
 		boolean shouldEnable = true;
 
@@ -100,14 +150,16 @@
 		return shouldEnable;
 	}
 
-	@SuppressWarnings( {"rawtypes"})
 	protected boolean checkSelectionSemanticResource() {
+		if (!checkSelectionEnabled()) {
+			return false;
+		}
 
 		boolean shouldEnable = true;
 
 		// we check for all object that they are semantic resources
 
-		for (Iterator it = getSelection().iterator(); it.hasNext();) {
+		for (Iterator<?> it = getSelection().iterator(); it.hasNext();) {
 
 			Object nextObject = it.next();
 
@@ -120,14 +172,16 @@
 		return shouldEnable;
 	}
 
-	@SuppressWarnings( {"rawtypes"})
 	protected boolean checkFilesWithReadOnlyFlagOnly(boolean readOnly) {
+		if (!checkSelectionEnabled()) {
+			return false;
+		}
 
 		boolean shouldEnable = true;
 
 		// we check for all object that they are read-only semantic resources
 
-		for (Iterator it = getSelection().iterator(); it.hasNext();) {
+		for (Iterator<?> it = getSelection().iterator(); it.hasNext();) {
 
 			Object nextObject = it.next();
 
@@ -156,15 +210,17 @@
 		return shouldEnable;
 	}
 
-	@SuppressWarnings( {"rawtypes"})
 	protected boolean checkSelectionLockingSupportedOnly() {
+		if (!checkSelectionEnabled()) {
+			return false;
+		}
 
 		boolean shouldEnable = true;
 
 		// we check for all object that they are semantic resources and support
 		// locking
 		// we don't check the lock state intentionally
-		for (Iterator it = getSelection().iterator(); it.hasNext();) {
+		for (Iterator<?> it = getSelection().iterator(); it.hasNext();) {
 
 			Object nextObject = it.next();
 
diff --git a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/actions/UnmapAction.java b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/actions/UnmapAction.java
index 9834c98..a37d6a5 100644
--- a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/actions/UnmapAction.java
+++ b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/actions/UnmapAction.java
@@ -46,7 +46,15 @@
 
 		if (resource != null && resource instanceof ISemanticProject) {
 			IProject project = (IProject) resource.getAdaptedResource();
-			action.setEnabled(RepositoryProvider.getProvider(project, ISemanticFileSystem.SFS_REPOSITORY_PROVIDER) != null);
+			try {
+				if (resource.getPersistentProperty(DISABLE_ALL_SFS_ACTIONS) != null) {
+					action.setEnabled(false);
+				} else {
+					action.setEnabled(RepositoryProvider.getProvider(project, ISemanticFileSystem.SFS_REPOSITORY_PROVIDER) != null);
+				}
+			} catch (CoreException e) {
+				action.setEnabled(false);
+			}
 		} else {
 			action.setEnabled(false);
 		}
diff --git a/tests/org.eclipse.ui.resources.semantic.examples.test/src/org/eclipse/core/resources/semantic/test/TestsSFSUi.java b/tests/org.eclipse.ui.resources.semantic.examples.test/src/org/eclipse/core/resources/semantic/test/TestsSFSUi.java
index 83a03c1..e80b133 100644
--- a/tests/org.eclipse.ui.resources.semantic.examples.test/src/org/eclipse/core/resources/semantic/test/TestsSFSUi.java
+++ b/tests/org.eclipse.ui.resources.semantic.examples.test/src/org/eclipse/core/resources/semantic/test/TestsSFSUi.java
@@ -24,6 +24,7 @@
 import org.eclipse.compare.CompareEditorInput;
 import org.eclipse.core.filesystem.EFS;
 import org.eclipse.core.internal.resources.semantic.Util;
+import org.eclipse.core.internal.resources.semantic.ui.SemanticResourcesUIPlugin;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
@@ -755,8 +756,28 @@
 
 	}
 
+	private void setDisableStateOfSFSActions(final ISemanticResource resource, final boolean disabled) throws Exception {
+		IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
+			public void run(IProgressMonitor monitor) throws CoreException {
+
+				if (disabled) {
+					resource.setPersistentProperty(new QualifiedName(SemanticResourcesUIPlugin.PLUGIN_ID, "DisableAllSFSActions"), "true");
+				} else {
+					resource.setPersistentProperty(new QualifiedName(SemanticResourcesUIPlugin.PLUGIN_ID, "DisableAllSFSActions"), null);
+				}
+			}
+		};
+		ResourcesPlugin.getWorkspace().run(runnable, new NullProgressMonitor());
+	}
+
 	private void runCommandByAction(final String actionName, final ISemanticResource resource) throws Exception {
 
+		setDisableStateOfSFSActions(resource, true);
+		Assert.assertFalse(retrieveCommandEnablementByAction(actionName, resource));
+
+		setDisableStateOfSFSActions(resource, false);
+		Assert.assertTrue(retrieveCommandEnablementByAction(actionName, resource));
+
 		// make sure project explorer is selected
 		// Project Explorer view id is hard-coded for 3.4 compatibility
 		final IViewPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
@@ -775,6 +796,36 @@
 		return;
 	}
 
+	private boolean retrieveCommandEnablementByAction(final String actionName, final ISemanticResource resource) throws Exception {
+
+		// make sure project explorer is selected
+		// Project Explorer view id is hard-coded for 3.4 compatibility
+		final IViewPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
+				.showView("org.eclipse.ui.navigator.ProjectExplorer");
+		final boolean[] isEnabled = new boolean[] {false};
+		final boolean[] isEnabledCalled = new boolean[] {false};
+
+		final String id = "org.eclipse.core.resources.semantic.ui." + actionName;
+		final IObjectActionDelegate delegate = findDelegate(id);
+		final IAction action = new Action() {
+			//
+			@Override
+			public void setEnabled(boolean enabled) {
+				super.setEnabled(enabled);
+				isEnabled[0] = enabled;
+				isEnabledCalled[0] = true;
+			}
+		};
+
+		delegate.setActivePart(action, part);
+		delegate.selectionChanged(action, new StructuredSelection(resource));
+
+		if (!isEnabledCalled[0]) {
+			throw new IllegalStateException("setEnabled not called for action " + actionName);
+		}
+		return isEnabled[0];
+	}
+
 	IObjectActionDelegate findDelegate(String delegateID) throws CoreException {
 		List<IConfigurationElement> configElements = new ArrayList<IConfigurationElement>();
 		IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(PlatformUI.PLUGIN_ID, "popupMenus"); //$NON-NLS-1$