Added ability to lock current diagram to PlantUML view

This commit adds a new entry in the Capra PlantUML View context menu
that allows to lock the current diagram, i.e., preventing the diagram
from updating if the selection changes. This can be helpful, e.g., when
working with change impact analysis where the graph should remain
visible when opening other artifacts.
diff --git a/bundles/org.eclipse.capra.ui.plantuml/OSGI-INF/l10n/bundle.properties b/bundles/org.eclipse.capra.ui.plantuml/OSGI-INF/l10n/bundle.properties
index ac886f9..accf529 100644
--- a/bundles/org.eclipse.capra.ui.plantuml/OSGI-INF/l10n/bundle.properties
+++ b/bundles/org.eclipse.capra.ui.plantuml/OSGI-INF/l10n/bundle.properties
@@ -24,5 +24,7 @@
 command.label.relationshipTypes= Select relationship types
 command.name.depth = Set transitivity depth
 command.label.depth = Set transitivity depth
+command.name.lock = Lock current diagram
+command.label.lock = Lock current diagram
 Bundle-Vendor = Eclipse Capra
 Bundle-Name = Eclipse Capra Trace Visualisation Support (PlantUML)
diff --git a/bundles/org.eclipse.capra.ui.plantuml/plugin.xml b/bundles/org.eclipse.capra.ui.plantuml/plugin.xml
index 34b10b5..ffe6730 100644
--- a/bundles/org.eclipse.capra.ui.plantuml/plugin.xml
+++ b/bundles/org.eclipse.capra.ui.plantuml/plugin.xml
@@ -18,6 +18,7 @@
    <extension
          point="net.sourceforge.plantuml.eclipse.diagramTextProvider">
       <diagramTextProvider
+            id="org.eclipse.capra.ui.plantuml.diagramTextProviderHandler"
             providerClass="org.eclipse.capra.ui.plantuml.DiagramTextProviderHandler">
       </diagramTextProvider>
    </extension>
@@ -68,6 +69,14 @@
             id="org.eclipse.capra.ui.plantuml.transitivtyDepth"
             name="%command.name.depth">
       </command>
+      <command
+            id="org.eclipse.capra.ui.plantuml.lockDiagram"
+            name="%command.name.lock">
+         <state
+               class="org.eclipse.jface.commands.ToggleState"
+               id="org.eclipse.ui.commands.toggleState">
+         </state>
+      </command>
    </extension>
    <extension
          point="org.eclipse.ui.handlers">
@@ -91,6 +100,10 @@
             class="org.eclipse.capra.ui.plantuml.TransitivityDepthHandler"
             commandId="org.eclipse.capra.ui.plantuml.transitivtyDepth">
       </handler>
+      <handler
+            class="org.eclipse.capra.ui.plantuml.ToggleLockDiagramHandler"
+            commandId="org.eclipse.capra.ui.plantuml.lockDiagram">
+      </handler>
    </extension>
 	<extension
          point="org.eclipse.ui.menus">
@@ -121,6 +134,11 @@
                label="%command.label.depth"
                style="push">
          </command>
+         <command
+               commandId="org.eclipse.capra.ui.plantuml.lockDiagram"
+               label="%command.name.lock"
+               style="toggle">
+         </command>
       </menuContribution>
    </extension>
    
diff --git a/bundles/org.eclipse.capra.ui.plantuml/src/org/eclipse/capra/ui/plantuml/DiagramTextProviderHandler.java b/bundles/org.eclipse.capra.ui.plantuml/src/org/eclipse/capra/ui/plantuml/DiagramTextProviderHandler.java
index 0528493..1dea201 100644
--- a/bundles/org.eclipse.capra.ui.plantuml/src/org/eclipse/capra/ui/plantuml/DiagramTextProviderHandler.java
+++ b/bundles/org.eclipse.capra.ui.plantuml/src/org/eclipse/capra/ui/plantuml/DiagramTextProviderHandler.java
@@ -27,6 +27,7 @@
 import org.eclipse.capra.core.helpers.EMFHelper;
 import org.eclipse.capra.core.helpers.ExtensionPointHelper;
 import org.eclipse.capra.ui.helpers.SelectionSupportHelper;
+import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.ecore.resource.ResourceSet;
 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
@@ -34,6 +35,7 @@
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.IViewPart;
 import org.eclipse.ui.IWorkbenchPart;
+import org.osgi.service.prefs.Preferences;
 
 import net.sourceforge.plantuml.eclipse.utils.DiagramTextProvider;
 import net.sourceforge.plantuml.eclipse.views.PlantUmlView;
@@ -47,6 +49,11 @@
 public class DiagramTextProviderHandler implements DiagramTextProvider {
 	private EObject artifactModel = null;
 
+	private boolean isLockDiagram() {
+		Preferences preferences = InstanceScope.INSTANCE.getNode("org.eclipse.capra.ui.plantuml.lockDiagram");
+		return preferences.node("lockDiagram").getBoolean("option", false);
+	}
+
 	@Override
 	public String getDiagramText(IEditorPart editor, ISelection input) {
 		return (getDiagramText((IWorkbenchPart) editor, input));
@@ -231,11 +238,19 @@
 
 	@Override
 	public boolean supportsEditor(IEditorPart editor) {
+		// This is a work around to disable update of the diagram if the view is locked.
+		if (isLockDiagram()) {
+			return false;
+		}
 		return true;
 	}
 
 	@Override
 	public boolean supportsView(IViewPart part) {
+		// This is a work around to disable update of the diagram if the view is locked.
+		if (isLockDiagram()) {
+			return false;
+		}
 		if (part instanceof PlantUmlView) {
 			return false;
 		}
@@ -244,6 +259,10 @@
 
 	@Override
 	public boolean supportsSelection(ISelection selection) {
+		// This is a work around to disable update of the diagram if the view is locked.
+		if (isLockDiagram()) {
+			return false;
+		}
 		return true;
 	}
 
diff --git a/bundles/org.eclipse.capra.ui.plantuml/src/org/eclipse/capra/ui/plantuml/ToggleLockDiagramHandler.java b/bundles/org.eclipse.capra.ui.plantuml/src/org/eclipse/capra/ui/plantuml/ToggleLockDiagramHandler.java
new file mode 100644
index 0000000..23f7bd4
--- /dev/null
+++ b/bundles/org.eclipse.capra.ui.plantuml/src/org/eclipse/capra/ui/plantuml/ToggleLockDiagramHandler.java
@@ -0,0 +1,74 @@
+/*******************************************************************************
+ * Copyright (c) 2016, 2020 Chalmers | University of Gothenburg, rt-labs and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v20.html
+ *  
+ * SPDX-License-Identifier: EPL-2.0
+ *  
+ * Contributors:
+ *      Chalmers | University of Gothenburg and rt-labs - initial API and implementation and/or initial documentation
+ *      Chalmers | University of Gothenburg - additional features, updated API
+ *******************************************************************************/
+package org.eclipse.capra.ui.plantuml;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.Command;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.osgi.service.prefs.BackingStoreException;
+import org.osgi.service.prefs.Preferences;
+
+/**
+ * Toggles between locking the view and updating it when new elements are
+ * selected.
+ * 
+ * @author Jan-Philipp Steghöfer
+ */
+public class ToggleLockDiagramHandler extends AbstractHandler {
+
+	@Override
+	public Object execute(ExecutionEvent event) throws ExecutionException {
+		Command command = event.getCommand();
+		boolean oldValue = HandlerUtil.toggleCommandState(command);
+		setlockDiagram(!oldValue);
+		return null;
+	}
+
+	/**
+	 * Checks whether the view should be locked and thus not refreshed.
+	 * 
+	 * @return {@code true} if the lock is enabled, {@code false} otherwise
+	 */
+	public static boolean isLockDiagram() {
+		Preferences lockDiagram = getPreference();
+		return lockDiagram.getBoolean("option", false);
+	}
+
+	private static Preferences getPreference() {
+		Preferences preferences = InstanceScope.INSTANCE.getNode("org.eclipse.capra.ui.plantuml.lockDiagram");
+		return preferences.node("lockDiagram");
+	}
+
+	/**
+	 * Sets whether the trace view is locked and should thus not be refreshed when
+	 * new elements are selected.
+	 * 
+	 * @param value {@code true} if the view is locked, {@code false} otherwise
+	 * 
+	 */
+	public static void setlockDiagram(boolean value) {
+		Preferences transitivity = getPreference();
+		transitivity.putBoolean("option", value);
+
+		try {
+			// forces the application to save the preferences
+			transitivity.flush();
+		} catch (BackingStoreException e) {
+			e.printStackTrace();
+		}
+	}
+}
\ No newline at end of file
diff --git a/bundles/org.eclipse.capra.ui.plantuml/src/org/eclipse/capra/ui/plantuml/views/CapraPlantUmlView.java b/bundles/org.eclipse.capra.ui.plantuml/src/org/eclipse/capra/ui/plantuml/views/CapraPlantUmlView.java
index 0df9b3e..6cf2bb9 100644
--- a/bundles/org.eclipse.capra.ui.plantuml/src/org/eclipse/capra/ui/plantuml/views/CapraPlantUmlView.java
+++ b/bundles/org.eclipse.capra.ui.plantuml/src/org/eclipse/capra/ui/plantuml/views/CapraPlantUmlView.java
@@ -14,6 +14,7 @@
 package org.eclipse.capra.ui.plantuml.views;
 
 import org.eclipse.capra.ui.plantuml.ToggleDisplayGraphHandler;
+import org.eclipse.capra.ui.plantuml.ToggleLockDiagramHandler;
 import org.eclipse.capra.ui.plantuml.ToggleTransitivityHandler;
 import org.eclipse.core.commands.Command;
 import org.eclipse.swt.widgets.Composite;
@@ -41,5 +42,11 @@
 			displayGraph.getState("org.eclipse.ui.commands.toggleState")
 					.setValue(ToggleDisplayGraphHandler.isDisplayGraph());
 		}
+		Command lockDiagram = cmdService.getCommand("org.eclipse.capra.ui.plantuml.lockDiagram");
+		if (lockDiagram != null) {
+			lockDiagram.getState("org.eclipse.ui.commands.toggleState")
+					.setValue(ToggleLockDiagramHandler.isLockDiagram());
+		}
+
 	}
 }