506898: [Feature] Add source code annotations if traceability link to
source file is created

add cdt handler preferences

Change-Id: I9edec2e3f42779e26e6fe5a900906a6659cdecb3
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=506898
diff --git a/org.eclipse.capra.handler.cdt.tests/src/org/eclipse/capra/handler/cdt/CDTAnnotateTest.java b/org.eclipse.capra.handler.cdt.tests/src/org/eclipse/capra/handler/cdt/CDTAnnotateTest.java
index 0a48452..8b8d235 100644
--- a/org.eclipse.capra.handler.cdt.tests/src/org/eclipse/capra/handler/cdt/CDTAnnotateTest.java
+++ b/org.eclipse.capra.handler.cdt.tests/src/org/eclipse/capra/handler/cdt/CDTAnnotateTest.java
@@ -12,6 +12,7 @@
 
 import static org.junit.Assert.*;
 
+import org.eclipse.capra.handler.cdt.preferences.CDTPreferences;
 import org.eclipse.cdt.core.model.ITranslationUnit;
 import org.eclipse.cdt.managedbuilder.core.BuildException;
 import org.eclipse.core.resources.IProject;
@@ -30,6 +31,9 @@
 	@Before
 	public void setupTestProject() throws CoreException, BuildException {
 		project = TestUtil.setupTestProject("cdt");
+		CDTPreferences.getPreferences().putBoolean(CDTPreferences.ANNOTATE_CDT, true);
+		CDTPreferences.getPreferences().put(CDTPreferences.ANNOTATE_CDT_TAG, "req");
+		CDTPreferences.getPreferences().put(CDTPreferences.ANNOTATE_CDT_TAG_PREFIX, "@");
 	}
 
 	@After
diff --git a/org.eclipse.capra.handler.cdt/META-INF/MANIFEST.MF b/org.eclipse.capra.handler.cdt/META-INF/MANIFEST.MF
index 6ea390b..70b276a 100644
--- a/org.eclipse.capra.handler.cdt/META-INF/MANIFEST.MF
+++ b/org.eclipse.capra.handler.cdt/META-INF/MANIFEST.MF
@@ -10,4 +10,7 @@
  org.eclipse.core.runtime,
  org.eclipse.text,
  org.eclipse.ltk.core.refactoring
-Export-Package: org.eclipse.capra.handler.cdt
+Export-Package: org.eclipse.capra.handler.cdt,
+ org.eclipse.capra.handler.cdt.preferences
+Bundle-Activator: org.eclipse.capra.handler.cdt.Activator
+Bundle-ActivationPolicy: lazy
diff --git a/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/Activator.java b/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/Activator.java
new file mode 100644
index 0000000..15b398a
--- /dev/null
+++ b/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/Activator.java
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * 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.cdt;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+public class Activator implements BundleActivator {
+
+	// The plug-in ID
+	public static String PLUGIN_ID = "org.eclipse.capra.handler.cdt";
+
+	// The shared instance
+	private static Activator plugin;
+
+	/* (non-Javadoc)
+	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+	 */
+	@Override
+	public void start(BundleContext context) throws Exception {
+		plugin = this;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+	 */
+	@Override
+	public void stop(BundleContext context) throws Exception {
+		plugin = null;
+	}
+
+	/**
+	 * Returns the shared instance
+	 *
+	 * @return the shared instance
+	 */
+	public static Activator getDefault() {
+		return plugin;
+	}
+
+}
diff --git a/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/CDTAnnotate.java b/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/CDTAnnotate.java
index 255698d..b7d377c 100644
--- a/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/CDTAnnotate.java
+++ b/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/CDTAnnotate.java
@@ -13,6 +13,7 @@
 import java.util.List;
 
 import org.eclipse.capra.core.handlers.AnnotationException;
+import org.eclipse.capra.handler.cdt.preferences.CDTPreferences;
 import org.eclipse.cdt.core.dom.ast.IASTComment;
 import org.eclipse.cdt.core.dom.ast.IASTNode;
 import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
@@ -26,6 +27,7 @@
 import org.eclipse.cdt.core.model.ITranslationUnit;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
 import org.eclipse.ltk.core.refactoring.Change;
 
 public class CDTAnnotate {
@@ -61,18 +63,20 @@
 		}
 	}
 
-	// TODO: Get tag from somewhere else
 	private static String createNewCommentString(IASTComment comment, String annotation) {
+		IEclipsePreferences preferences = CDTPreferences.getPreferences();
+		String tag = preferences.get(CDTPreferences.ANNOTATE_CDT_TAG_PREFIX, CDTPreferences.ANNOTATE_CDT_TAG_PREFIX_DEFAULT).trim() +
+				preferences.get(CDTPreferences.ANNOTATE_CDT_TAG, CDTPreferences.ANNOTATE_CDT_TAG_DEFAULT).trim();
 		String text;
 		if (comment != null) {
 			text = comment.getRawSignature();
-			if (text.contains("@req")) {
-				text = text.replaceAll("@req .*", "@req " + annotation);
+			if (text.contains(tag)) {
+				text = text.replaceAll(tag + " .*", tag + " " + annotation);
 			} else {
-				text = text.replaceAll("\\*/", "* @req " + annotation + System.lineSeparator() + " */");
+				text = text.replaceAll("\\*/", "* " + tag + " " + annotation + System.lineSeparator() + " */");
 			}
 		} else {
-			text = "/**" + System.lineSeparator() + " * @req " + annotation + System.lineSeparator() + " */";
+			text = "/**" + System.lineSeparator() + " * " + tag + " " + annotation + System.lineSeparator() + " */";
 		}
 
 		return text;
diff --git a/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/CDTHandler.java b/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/CDTHandler.java
index 6300f2b..47bf0e8 100644
--- a/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/CDTHandler.java
+++ b/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/CDTHandler.java
@@ -15,8 +15,10 @@
 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.cdt.preferences.CDTPreferences;
 import org.eclipse.cdt.core.model.CoreModel;
 import org.eclipse.cdt.core.model.ICElement;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
 import org.eclipse.emf.ecore.EObject;
 
 /**
@@ -54,8 +56,11 @@
 
 	@Override
 	public void annotateArtifact(EObject artifact, String annotation) throws AnnotationException {
-		ICElement handle = resolveArtifact(artifact);
-		CDTAnnotate.annotateArtifact(handle, annotation);
+		IEclipsePreferences preferences = CDTPreferences.getPreferences();
+		if (preferences.getBoolean(CDTPreferences.ANNOTATE_CDT, CDTPreferences.ANNOTATE_CDT_DEFAULT)) {
+			ICElement handle = resolveArtifact(artifact);
+			CDTAnnotate.annotateArtifact(handle, annotation);
+		}
 	}
 
 }
diff --git a/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/preferences/CDTPreferences.java b/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/preferences/CDTPreferences.java
new file mode 100644
index 0000000..903b127
--- /dev/null
+++ b/org.eclipse.capra.handler.cdt/src/org/eclipse/capra/handler/cdt/preferences/CDTPreferences.java
@@ -0,0 +1,39 @@
+/*******************************************************************************
+ * 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.cdt.preferences;
+
+import org.eclipse.capra.handler.cdt.Activator;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.eclipse.core.runtime.preferences.IScopeContext;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+
+public class CDTPreferences {
+
+    public static final IScopeContext SCOPE_CONTEXT = InstanceScope.INSTANCE;
+    public static final String PREFERENCE_NODE = Activator.PLUGIN_ID;
+
+	// Should annotate C source code?
+	public static final String ANNOTATE_CDT = "ANNOTATE_CDT";
+	public static final boolean ANNOTATE_CDT_DEFAULT = false;
+
+	// Doxygen tag to use for annotation
+	public static final String ANNOTATE_CDT_TAG = "ANNOTATE_CDT_TAG";
+	public static final String ANNOTATE_CDT_TAG_DEFAULT = "parent";
+
+	// Doxygen tag prefix to use for annotation
+	public static final String ANNOTATE_CDT_TAG_PREFIX = "ANNOTATE_CDT_TAG_PREFIX";
+	public static final String ANNOTATE_CDT_TAG_PREFIX_DEFAULT = "@";
+
+    public static IEclipsePreferences getPreferences() {
+        return SCOPE_CONTEXT.getNode(PREFERENCE_NODE);
+    }
+
+}