Merge "506898: [Feature] Add source code annotations if traceability link to source file is created" into develop
diff --git a/org.eclipse.capra.handler.jdt.tests/src/org/eclipse/capra/handler/jdt/JDTAnnotateTest.java b/org.eclipse.capra.handler.jdt.tests/src/org/eclipse/capra/handler/jdt/JDTAnnotateTest.java
index 3c24c19..cf89766 100644
--- a/org.eclipse.capra.handler.jdt.tests/src/org/eclipse/capra/handler/jdt/JDTAnnotateTest.java
+++ b/org.eclipse.capra.handler.jdt.tests/src/org/eclipse/capra/handler/jdt/JDTAnnotateTest.java
@@ -12,6 +12,7 @@
 
 import static org.junit.Assert.*;
 
+import org.eclipse.capra.handler.jdt.preferences.JDTPreferences;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.emf.ecore.EObject;
@@ -29,6 +30,8 @@
 	@Before
 	public void createTestProject() throws CoreException {
 		project = TestUtil.createTestProject("jdt");
+		JDTPreferences.getPreferences().putBoolean(JDTPreferences.ANNOTATE_JDT, true);
+		JDTPreferences.getPreferences().put(JDTPreferences.ANNOTATE_JDT_TAG, "req");
 	}
 
 	@After
diff --git a/org.eclipse.capra.handler.jdt/META-INF/MANIFEST.MF b/org.eclipse.capra.handler.jdt/META-INF/MANIFEST.MF
index 21dd030..ae14fb6 100644
--- a/org.eclipse.capra.handler.jdt/META-INF/MANIFEST.MF
+++ b/org.eclipse.capra.handler.jdt/META-INF/MANIFEST.MF
@@ -9,4 +9,6 @@
  org.eclipse.jface.text,
  org.eclipse.core.runtime
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
-Export-Package: org.eclipse.capra.handler.jdt
+Export-Package: org.eclipse.capra.handler.jdt,
+ org.eclipse.capra.handler.jdt.preferences
+Bundle-ActivationPolicy: lazy
diff --git a/org.eclipse.capra.handler.jdt/src/org/eclipse/capra/handler/jdt/JDTAnnotate.java b/org.eclipse.capra.handler.jdt/src/org/eclipse/capra/handler/jdt/JDTAnnotate.java
index 86ecdd7..ab8a89b 100644
--- a/org.eclipse.capra.handler.jdt/src/org/eclipse/capra/handler/jdt/JDTAnnotate.java
+++ b/org.eclipse.capra.handler.jdt/src/org/eclipse/capra/handler/jdt/JDTAnnotate.java
@@ -13,7 +13,9 @@
 import java.util.List;
 
 import org.eclipse.capra.core.handlers.AnnotationException;
+import org.eclipse.capra.handler.jdt.preferences.JDTPreferences;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
 import org.eclipse.jdt.core.ICompilationUnit;
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.IMethod;
@@ -41,7 +43,7 @@
 
 public class JDTAnnotate {
 
-	private static final String TAG_NAME = "@req";
+	private static final String TAG_PREFIX = "@";
 	private static final String ANNOTATION_FAILED = "Annotation failed";
 
 	@SuppressWarnings("unchecked")
@@ -69,12 +71,15 @@
 
 				ListRewrite tagsRewriter = rewrite.getListRewrite(javaDoc, Javadoc.TAGS_PROPERTY);
 
-				// TODO: Get tag name from somewhere else
+				// Get preferred tag name
+				IEclipsePreferences preferences = JDTPreferences.getPreferences();
+				String tagName = TAG_PREFIX + preferences.get(JDTPreferences.ANNOTATE_JDT_TAG,
+						JDTPreferences.ANNOTATE_JDT_TAG_DEFAULT).trim();
 
 				// Remove existing tags
 				((List<TagElement>)javaDoc.tags())
 					.stream()
-					.filter(t -> t.getTagName() != null && t.getTagName().equals(TAG_NAME))
+					.filter(t -> t.getTagName() != null && t.getTagName().equals(tagName))
 					.forEach(t -> tagsRewriter.remove(t, null));
 
 				// Create new tag
@@ -82,7 +87,7 @@
 				TextElement text = ast.newTextElement();
 				text.setText(annotation);
 				tag.fragments().add(text);
-				tag.setTagName(TAG_NAME);
+				tag.setTagName(tagName);
 
 				tagsRewriter.insertLast(tag, null);
 
diff --git a/org.eclipse.capra.handler.jdt/src/org/eclipse/capra/handler/jdt/JavaElementHandler.java b/org.eclipse.capra.handler.jdt/src/org/eclipse/capra/handler/jdt/JavaElementHandler.java
index 86aacbb..7824b2d 100644
--- a/org.eclipse.capra.handler.jdt/src/org/eclipse/capra/handler/jdt/JavaElementHandler.java
+++ b/org.eclipse.capra.handler.jdt/src/org/eclipse/capra/handler/jdt/JavaElementHandler.java
@@ -15,6 +15,8 @@
 import org.eclipse.capra.core.handlers.ArtifactHandler;
 import org.eclipse.capra.core.handlers.IAnnotateArtifact;
 import org.eclipse.capra.core.helpers.ExtensionPointHelper;
+import org.eclipse.capra.handler.jdt.preferences.JDTPreferences;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.JavaCore;
@@ -58,7 +60,10 @@
 
 	@Override
 	public void annotateArtifact(EObject artifact, String annotation) throws AnnotationException {
-		IJavaElement handle = resolveArtifact(artifact);
-		JDTAnnotate.annotateArtifact(handle, annotation);
+		IEclipsePreferences preferences = JDTPreferences.getPreferences();
+		if (preferences.getBoolean(JDTPreferences.ANNOTATE_JDT, JDTPreferences.ANNOTATE_JDT_DEFAULT)) {
+			IJavaElement handle = resolveArtifact(artifact);
+			JDTAnnotate.annotateArtifact(handle, annotation);
+		}
 	}
 }
diff --git a/org.eclipse.capra.handler.jdt/src/org/eclipse/capra/handler/jdt/preferences/JDTPreferences.java b/org.eclipse.capra.handler.jdt/src/org/eclipse/capra/handler/jdt/preferences/JDTPreferences.java
new file mode 100644
index 0000000..84a2e60
--- /dev/null
+++ b/org.eclipse.capra.handler.jdt/src/org/eclipse/capra/handler/jdt/preferences/JDTPreferences.java
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * Copyright (c) 2016 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 v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *    Chalmers | University of Gothenburg and rt-labs - initial API and implementation and/or initial documentation
+ *******************************************************************************/
+package org.eclipse.capra.handler.jdt.preferences;
+
+import org.eclipse.capra.handler.jdt.Activator;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.eclipse.core.runtime.preferences.IScopeContext;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+
+public class JDTPreferences {
+
+    public static final IScopeContext SCOPE_CONTEXT = InstanceScope.INSTANCE;
+    public static final String PREFERENCE_NODE = Activator.PLUGIN_ID;
+
+	// Should annotate Java source code?
+	public static final String ANNOTATE_JDT = "ANNOTATE_JDT";
+	public static final boolean ANNOTATE_JDT_DEFAULT = false;
+
+	// Doxygen tag to use for annotation
+	public static final String ANNOTATE_JDT_TAG = "ANNOTATE_JDT_TAG";
+	public static final String ANNOTATE_JDT_TAG_DEFAULT = "parent";
+
+    public static IEclipsePreferences getPreferences() {
+        return SCOPE_CONTEXT.getNode(PREFERENCE_NODE);
+    }
+
+}