Office: added option to change word field-ID in preferences

Change-Id: I6cb8a752b7d0415cb904dcf779bec7bbea5fee3a
Signed-off-by: Dusan Kalanj <kalanj@chalmers.se>
diff --git a/org.eclipse.capra.ui.office/plugin.properties b/org.eclipse.capra.ui.office/plugin.properties
deleted file mode 100644
index d34f04c..0000000
--- a/org.eclipse.capra.ui.office/plugin.properties
+++ /dev/null
@@ -1,12 +0,0 @@
-###############################################################################
-# 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
-###############################################################################
-
-req_fieldName=REQ
\ No newline at end of file
diff --git a/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/objects/CapraWordRequirement.java b/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/objects/CapraWordRequirement.java
index eab10a7..0c46b6f 100644
--- a/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/objects/CapraWordRequirement.java
+++ b/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/objects/CapraWordRequirement.java
@@ -19,7 +19,6 @@
 import javax.xml.parsers.DocumentBuilderFactory;
 
 import org.apache.poi.xwpf.usermodel.XWPFParagraph;
-import org.eclipse.capra.ui.office.utils.OfficeProperties;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
 import org.w3c.dom.Document;
 import org.w3c.dom.NodeList;
@@ -49,19 +48,23 @@
 	/**
 	 * Start and end XML tags of MS Word field commands
 	 */
-	private static final String REQ_FIELD_TAG = "w:instrText";
-
-	/**
-	 * The name of the requirement field as defined in Word.
-	 */
-	private static final String REQ_FIELD_NAME = OfficeProperties.getInstance().getProperty("req_fieldName");
+	private static final String FIELD_TAG = "w:instrText";
 
 	/**
 	 * A constructor that generates a new instance of CapraWordRequirement where
-	 * the parent properties are extracted from the provided Word paragraph and
-	 * File object that contains containing the paragraph.
+	 * the parent properties are extracted from the provided paragraph, the file
+	 * that contains the paragraph and the id (name) of the field that denotes
+	 * the data that is to be extracted.
+	 * 
+	 * @param officeFile
+	 *            the file that contains the paragraph
+	 * @param paragraph
+	 *            a Word paragraph
+	 * @param fieldName
+	 *            the name of the field that denotes the data that is to be
+	 *            extracted from the paragraph
 	 */
-	public CapraWordRequirement(XWPFParagraph paragraph, File officeFile) {
+	public CapraWordRequirement(File officeFile, XWPFParagraph paragraph, String fieldName) {
 		// TODO This solution assumes that there is only one requirement per
 		// paragraph. Should it be different?
 		super();
@@ -78,14 +81,14 @@
 			return;
 		}
 
-		NodeList nodeList = doc.getElementsByTagName(REQ_FIELD_TAG);
+		NodeList nodeList = doc.getElementsByTagName(FIELD_TAG);
 		if (nodeList.getLength() > 0) {
 			// TODO Use a for loop if the solution needs to parse multiple
 			// requirements in a single paragraph. In that case,
 			// paragraph.getText() should be replaced with something from the
 			// org.w3c.dom.Document class.
 			String[] parts = nodeList.item(0).getTextContent().split(WORD_FIELD_SPLIT_DELIMITERS);
-			if (Arrays.asList(parts).contains(REQ_FIELD_NAME) && parts.length > 2) {
+			if (Arrays.asList(parts).contains(fieldName) && parts.length > 2) {
 				rText = paragraph.getText();
 				rId = parts[2].trim();
 			}
diff --git a/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/preferences/OfficePreferences.java b/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/preferences/OfficePreferences.java
index cad9a1a..76df8bd 100644
--- a/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/preferences/OfficePreferences.java
+++ b/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/preferences/OfficePreferences.java
@@ -43,10 +43,11 @@
 	/**
 	 * IDs of preferences
 	 */
-	public static final String CHAR_COUNT = "org.eclipse.capra.ui.office.preferences.charcount";
-	public static final String EXCEL_COLUMN_RADIO_CHOICE = "org.eclipse.capra.ui.office.preferences.excelcolumnradiochoice";
-	public static final String EXCEL_CUSTOM_COLUMN = "org.eclipse.capra.ui.office.preferences.excelcustomcolumn";
-	public static final String EXCEL_COLUMN_VALUE = "org.eclipse.capra.ui.office.preferences.excelcolumnvalue";
+	public static final String CHAR_COUNT = "org.eclipse.capra.ui.office.preferences.charCount";
+	public static final String EXCEL_COLUMN_RADIO_CHOICE = "org.eclipse.capra.ui.office.preferences.excelColumnRadioChoice";
+	public static final String EXCEL_CUSTOM_COLUMN = "org.eclipse.capra.ui.office.preferences.excelCustomColumn";
+	public static final String EXCEL_COLUMN_VALUE = "org.eclipse.capra.ui.office.preferences.excelColumnValue";
+	public static final String WORD_FIELD_NAME = "org.eclipse.capra.ui.office.preferences.wordFieldId";
 
 	/**
 	 * Default preference values
@@ -55,6 +56,7 @@
 	public static final boolean EXCEL_COLUMN_RADIO_ID_IS_LINE_NUMBER = true;
 	public static final String EXCEL_CUSTOM_COLUMN_DEFAULT = "A";
 	public static final String EXCEL_COLUMN_VALUE_DEFAULT = "0";
+	public static final String WORD_FIELD_NAME_DEFAULT = "REQ";
 
 	/**
 	 * Description of controls
@@ -64,16 +66,18 @@
 	private static final String EXCEL_COLUMN_IS_LINE_NUMBER_OPTION_DESC = "Line number is used as ID";
 	private static final String EXCEL_COLUMN_IS_CUSTOM_OPTION_DESC = "Custom ID column: ";
 	private static final String EXCEL_COLUMN_IS_CUSTOM_OPTION_HINT = "(e.g. \"A\", \"BC\"...)";
+	private static final String WORD_FIELD_NAME_DESC = "ID of Word fields to display: ";
 
 	private static final int FIXED_TEXT_FIELD_WIDTH = 35;
 
 	/**
 	 * Controls
 	 */
-	private Button idIsRowNumberRadio;
-	private Button idIsCustomRadio;
 	private Text charCount;
-	private Text customIdColumn;
+	private Button excel_rowIdIsRowNumber;
+	private Button excel_rowIdIsCustomColumn;
+	private Text excel_customIdColumnName;
+	private Text word_fieldName;
 
 	private Composite createComposite(Composite parent, int numColumns) {
 		Composite composite = new Composite(parent, SWT.NULL);
@@ -100,21 +104,22 @@
 
 		boolean idIsRowNumber;
 		String idColumn;
-		if (idIsRowNumberRadio.getSelection()) {
+		if (excel_rowIdIsRowNumber.getSelection()) {
 			idColumn = EXCEL_COLUMN_VALUE_DEFAULT;
-			customIdColumn.setText("");
+			excel_customIdColumnName.setText("");
 			idIsRowNumber = true;
 		} else {
-			if (customIdColumn.getText().isEmpty())
-				customIdColumn.setText(EXCEL_CUSTOM_COLUMN_DEFAULT);
-			idColumn = customIdColumn.getText();
+			if (excel_customIdColumnName.getText().isEmpty())
+				excel_customIdColumnName.setText(EXCEL_CUSTOM_COLUMN_DEFAULT);
+			idColumn = excel_customIdColumnName.getText();
 			idIsRowNumber = false;
 		}
 
 		store.setValue(CHAR_COUNT, charCount.getText());
 		store.setValue(EXCEL_COLUMN_RADIO_CHOICE, idIsRowNumber);
-		store.setValue(EXCEL_CUSTOM_COLUMN, customIdColumn.getText());
+		store.setValue(EXCEL_CUSTOM_COLUMN, excel_customIdColumnName.getText());
 		store.setValue(EXCEL_COLUMN_VALUE, idColumn);
+		store.setValue(WORD_FIELD_NAME, word_fieldName.getText());
 	}
 
 	private void initializeValues() {
@@ -122,26 +127,29 @@
 
 		boolean idIsRowNumber = store.getBoolean(EXCEL_COLUMN_RADIO_CHOICE);
 		if (idIsRowNumber) {
-			idIsRowNumberRadio.setSelection(true);
-			customIdColumn.setText("");
-			customIdColumn.setEnabled(false);
+			excel_rowIdIsRowNumber.setSelection(true);
+			excel_customIdColumnName.setText("");
+			excel_customIdColumnName.setEnabled(false);
 		} else {
-			idIsCustomRadio.setSelection(true);
-			customIdColumn.setText(store.getString(EXCEL_CUSTOM_COLUMN));
+			excel_rowIdIsCustomColumn.setSelection(true);
+			excel_customIdColumnName.setText(store.getString(EXCEL_CUSTOM_COLUMN));
 		}
 
 		charCount.setText(store.getString(CHAR_COUNT));
+		word_fieldName.setText(store.getString(WORD_FIELD_NAME));
 	}
 
 	private void initializeDefaults() {
-		idIsRowNumberRadio.setSelection(EXCEL_COLUMN_RADIO_ID_IS_LINE_NUMBER);
-		idIsCustomRadio.setSelection(!EXCEL_COLUMN_RADIO_ID_IS_LINE_NUMBER);
+		excel_rowIdIsRowNumber.setSelection(EXCEL_COLUMN_RADIO_ID_IS_LINE_NUMBER);
+		excel_rowIdIsCustomColumn.setSelection(!EXCEL_COLUMN_RADIO_ID_IS_LINE_NUMBER);
 
-		if (idIsRowNumberRadio.getSelection()) {
-			customIdColumn.setText("");
-			customIdColumn.setEnabled(false);
+		if (excel_rowIdIsRowNumber.getSelection()) {
+			excel_customIdColumnName.setText("");
+			excel_customIdColumnName.setEnabled(false);
 		}
 		charCount.setText(CHAR_COUNT_DEFAULT);
+
+		word_fieldName.setText(WORD_FIELD_NAME_DEFAULT);
 	}
 
 	@Override
@@ -155,17 +163,17 @@
 		createLabel(compositeExcelIdColumn, EXCEL_COLUMN_RADIO_CHOICE_DESC, 2);
 
 		Composite compositeRadioButtons = createComposite(compositeExcelIdColumn, 3);
-		idIsRowNumberRadio = createRadioButton(compositeRadioButtons, EXCEL_COLUMN_IS_LINE_NUMBER_OPTION_DESC, 3);
-		idIsCustomRadio = createRadioButton(compositeRadioButtons, EXCEL_COLUMN_IS_CUSTOM_OPTION_DESC, 1);
-		customIdColumn = createTextField(compositeRadioButtons, 1, FIXED_TEXT_FIELD_WIDTH);
+		excel_rowIdIsRowNumber = createRadioButton(compositeRadioButtons, EXCEL_COLUMN_IS_LINE_NUMBER_OPTION_DESC, 3);
+		excel_rowIdIsCustomColumn = createRadioButton(compositeRadioButtons, EXCEL_COLUMN_IS_CUSTOM_OPTION_DESC, 1);
+		excel_customIdColumnName = createTextField(compositeRadioButtons, 1, FIXED_TEXT_FIELD_WIDTH);
 		createLabel(compositeRadioButtons, EXCEL_COLUMN_IS_CUSTOM_OPTION_HINT, 1);
 
-		idIsRowNumberRadio.addSelectionListener(new SelectionListener() {
+		excel_rowIdIsRowNumber.addSelectionListener(new SelectionListener() {
 
 			@Override
 			public void widgetSelected(SelectionEvent e) {
-				customIdColumn.setText("");
-				customIdColumn.setEnabled(false);
+				excel_customIdColumnName.setText("");
+				excel_customIdColumnName.setEnabled(false);
 			}
 
 			@Override
@@ -173,12 +181,12 @@
 			}
 		});
 
-		idIsCustomRadio.addSelectionListener(new SelectionListener() {
+		excel_rowIdIsCustomColumn.addSelectionListener(new SelectionListener() {
 
 			@Override
 			public void widgetSelected(SelectionEvent e) {
-				customIdColumn.setEnabled(true);
-				customIdColumn.setText(EXCEL_CUSTOM_COLUMN_DEFAULT);
+				excel_customIdColumnName.setEnabled(true);
+				excel_customIdColumnName.setText(EXCEL_CUSTOM_COLUMN_DEFAULT);
 			}
 
 			@Override
@@ -186,6 +194,10 @@
 			}
 		});
 
+		Composite compositeWordRequirementFieldName = createComposite(parent, 2);
+		createLabel(compositeWordRequirementFieldName, WORD_FIELD_NAME_DESC, 1);
+		word_fieldName = createTextField(compositeWordRequirementFieldName, 1, FIXED_TEXT_FIELD_WIDTH);
+
 		initializeValues();
 
 		return new Composite(parent, SWT.NULL);
diff --git a/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/preferences/PreferenceInitializer.java b/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/preferences/PreferenceInitializer.java
index f96f7a9..082bae5 100644
--- a/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/preferences/PreferenceInitializer.java
+++ b/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/preferences/PreferenceInitializer.java
@@ -35,5 +35,6 @@
 				OfficePreferences.EXCEL_COLUMN_RADIO_ID_IS_LINE_NUMBER);
 		store.setDefault(OfficePreferences.EXCEL_CUSTOM_COLUMN, OfficePreferences.EXCEL_CUSTOM_COLUMN_DEFAULT);
 		store.setDefault(OfficePreferences.EXCEL_COLUMN_VALUE, OfficePreferences.EXCEL_COLUMN_VALUE_DEFAULT);
+		store.setDefault(OfficePreferences.WORD_FIELD_NAME, OfficePreferences.WORD_FIELD_NAME_DEFAULT);
 	}
 }
\ No newline at end of file
diff --git a/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/OfficeProperties.java b/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/OfficeProperties.java
deleted file mode 100644
index 481977e..0000000
--- a/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/OfficeProperties.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*******************************************************************************
- * 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.ui.office.utils;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Properties;
-
-/**
- * This singleton class provides access to the plugin.properties file.
- *
- * @author Dusan Kalanj
- *
- */
-public class OfficeProperties {
-
-	private static OfficeProperties officeProperties = new OfficeProperties();
-
-	private Properties properties;
-
-	private OfficeProperties() {
-		this.properties = new Properties();
-
-		try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("plugin.properties")){
-			properties.load(is);
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-	}
-
-	/**
-	 * Provides an instance of OfficeProperties class.
-	 * 
-	 * @return instance of OfficeProperties
-	 */
-	public static OfficeProperties getInstance() {
-		return officeProperties;
-	}
-
-	/**
-	 * Returns the property String that corresponds to the provided key.
-	 * 
-	 * @param key
-	 *            key of the property to be accessed
-	 * @return value of the property that is defined by the provided key
-	 */
-	public String getProperty(String key) {
-		return properties.getProperty(key);
-	}
-}
diff --git a/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/views/OfficeView.java b/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/views/OfficeView.java
index 51d8af4..ded9e9b 100644
--- a/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/views/OfficeView.java
+++ b/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/views/OfficeView.java
@@ -444,10 +444,11 @@
 		clearSelection();
 
 		// Populate the view with Word requirements
+		String fieldName = Activator.getDefault().getPreferenceStore().getString(OfficePreferences.WORD_FIELD_NAME);
 		for (int i = 0; i < paragraphs.size(); i++) {
 			XWPFParagraph paragraph = paragraphs.get(i);
 			if (paragraph != null) {
-				CapraWordRequirement cRequirement = new CapraWordRequirement(paragraph, officeFile);
+				CapraWordRequirement cRequirement = new CapraWordRequirement(officeFile, paragraph, fieldName);
 				if (!cRequirement.getData().isEmpty())
 					selection.add(cRequirement);
 			}