From eaf713978a7115e5f97100a5e941d72908809c1d Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Thu, 28 Aug 2014 13:00:04 +0000
Subject: [PATCH] - Introduce AnnotationEditingSupportBase class - Ensure and
 document that all output of TextEditingSupport.getValue() is converted to a
 non-null String. This is required because this value will be consumed in
 TextCellEditor.doSetValue() which expects such input

---
 .../AnnotationEditingSupportBase.java         | 114 ++++++++++++++++++
 .../ComboBoxEditingSupport.java               |  71 +----------
 ...iInstanceAnnotationTextEditingSupport.java |  52 +-------
 .../editingsupport/TextEditingSupport.java    |  75 ++----------
 4 files changed, 131 insertions(+), 181 deletions(-)
 create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/AnnotationEditingSupportBase.java

diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/AnnotationEditingSupportBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/AnnotationEditingSupportBase.java
new file mode 100644
index 000000000..3360ba97b
--- /dev/null
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/AnnotationEditingSupportBase.java
@@ -0,0 +1,114 @@
+/*--------------------------------------------------------------------------+
+$Id$
+|                                                                          |
+| Copyright 2014 ForTISS GmbH                     |
+|                                                                          |
+| Licensed under the Apache License, Version 2.0 (the "License");          |
+| you may not use this file except in compliance with the License.         |
+| You may obtain a copy of the License at                                  |
+|                                                                          |
+|    http://www.apache.org/licenses/LICENSE-2.0                            |
+|                                                                          |
+| Unless required by applicable law or agreed to in writing, software      |
+| distributed under the License is distributed on an "AS IS" BASIS,        |
+| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
+| See the License for the specific language governing permissions and      |
+| limitations under the License.                                           |
++--------------------------------------------------------------------------*/
+package org.fortiss.tooling.base.ui.annotation.editingsupport;
+
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.ColumnViewer;
+import org.eclipse.jface.viewers.EditingSupport;
+import org.eclipse.swt.widgets.Shell;
+import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
+import org.fortiss.tooling.base.ui.annotation.AnnotationEntry;
+import org.fortiss.tooling.base.ui.annotation.view.GenericAnnotationView;
+import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
+import org.fortiss.tooling.kernel.service.IPersistencyService;
+
+/**
+ * Base class for {@link EditingSupport}s used in the {@link GenericAnnotationView}.
+ * 
+ * @author barner
+ * @author $Author$
+ * @version $Rev$
+ * @ConQAT.Rating RED
+ */
+public abstract class AnnotationEditingSupportBase extends EditingSupport {
+
+	/** Specification class of this column */
+	protected Class<? extends IAnnotatedSpecification> specClass;
+
+	/** Constructor. */
+	public AnnotationEditingSupportBase(ColumnViewer viewer,
+			Class<? extends IAnnotatedSpecification> specClass) {
+		super(viewer);
+		this.specClass = specClass;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	protected boolean canEdit(Object element) {
+		if(element instanceof AnnotationEntry) {
+			AnnotationEntry data = (AnnotationEntry)element;
+			return data.getSpecificationValue(specClass) != AnnotationEntry.NOVAL;
+		}
+
+		return true;
+	}
+
+	/** Actually gets the value from the {@link IAnnotatedSpecification}. */
+	protected Object doGetValue(AnnotationEntry annotationEntry) {
+		return annotationEntry.getSpecificationValue(specClass);
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	protected Object getValue(Object element) {
+		if(element instanceof AnnotationEntry) {
+			return doGetValue((AnnotationEntry)element);
+		}
+		return null;
+	}
+
+	/** Actually sets a {@link String} value into a {@link IAnnotatedSpecification}. */
+	protected void doSetValue(AnnotationEntry annotationEntry, String value) throws Exception {
+		annotationEntry.setSpecificationValue(value, specClass);
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	protected void setValue(Object element, final Object value) {
+		if(element instanceof AnnotationEntry && value instanceof String) {
+			final AnnotationEntry annotationEntry = (AnnotationEntry)element;
+
+			if(!value.equals(getValue(element))) {
+				ITopLevelElement modelContext =
+						IPersistencyService.INSTANCE.getTopLevelElementFor(annotationEntry
+								.getModelElement());
+				modelContext.runAsCommand(new Runnable() {
+
+					@Override
+					public void run() {
+
+						try {
+							doSetValue(annotationEntry, (String)value);
+						} catch(IllegalArgumentException e) {
+							MessageDialog
+									.openError(
+											new Shell(),
+											"ERROR",
+											"The value you entered does not have the desired type.\nDetailed message:\n" +
+													e.getMessage());
+						} catch(Exception e) {
+							MessageDialog.openError(new Shell(), "ERROR", e.getMessage());
+						}
+					}
+				});
+				this.getViewer().refresh(true);
+			}
+
+		}
+	}
+}
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/ComboBoxEditingSupport.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/ComboBoxEditingSupport.java
index 379d6bcb5..2ffc31f8c 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/ComboBoxEditingSupport.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/ComboBoxEditingSupport.java
@@ -19,19 +19,13 @@ package org.fortiss.tooling.base.ui.annotation.editingsupport;
 
 import java.util.List;
 
-import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.ArrayContentProvider;
 import org.eclipse.jface.viewers.CellEditor;
 import org.eclipse.jface.viewers.ColumnViewer;
 import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
-import org.eclipse.jface.viewers.EditingSupport;
 import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Shell;
 import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
-import org.fortiss.tooling.base.ui.annotation.AnnotationEntry;
-import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
-import org.fortiss.tooling.kernel.service.IPersistencyService;
 
 /**
  * This class constructs and manages an editing element for the annotation view. It provides a combo
@@ -42,13 +36,10 @@ import org.fortiss.tooling.kernel.service.IPersistencyService;
  * @version $Rev$
  * @ConQAT.Rating YELLOW Hash: AC8E4395DB075EC3FA9BF290373BBD9B
  */
-public class ComboBoxEditingSupport extends EditingSupport {
+public class ComboBoxEditingSupport extends AnnotationEditingSupportBase {
 	/** Combo box cell editor */
 	private ComboBoxViewerCellEditor cellEditor = null;
 
-	/** Specification class of this column */
-	private Class<? extends IAnnotatedSpecification> specClass;
-
 	/**
 	 * Constructor.
 	 * 
@@ -61,13 +52,12 @@ public class ComboBoxEditingSupport extends EditingSupport {
 	 */
 	public ComboBoxEditingSupport(ColumnViewer viewer,
 			Class<? extends IAnnotatedSpecification> clazz, List<String> values) {
-		super(viewer);
+		super(viewer, clazz);
 		cellEditor = new ComboBoxViewerCellEditor((Composite)getViewer().getControl());
 		cellEditor.setLabelProvider(new LabelProvider());
 		cellEditor.setContentProvider(new ArrayContentProvider());
 		cellEditor.setInput(values);
 		cellEditor.getViewer().getCCombo().setEditable(false);
-		specClass = clazz;
 	}
 
 	/** {@inheritDoc} */
@@ -75,61 +65,4 @@ public class ComboBoxEditingSupport extends EditingSupport {
 	protected CellEditor getCellEditor(Object element) {
 		return cellEditor;
 	}
-
-	/** {@inheritDoc} */
-	@Override
-	protected boolean canEdit(Object element) {
-		if(element instanceof AnnotationEntry) {
-			AnnotationEntry data = (AnnotationEntry)element;
-			return data.getSpecificationValue(specClass) != AnnotationEntry.NOVAL;
-		}
-
-		return true;
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	protected Object getValue(Object element) {
-		if(element instanceof AnnotationEntry) {
-			AnnotationEntry data = (AnnotationEntry)element;
-			return data.getSpecificationValue(specClass);
-		}
-		return null;
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	protected void setValue(Object element, final Object value) {
-		if(element instanceof AnnotationEntry && value instanceof String) {
-			final AnnotationEntry data = (AnnotationEntry)element;
-
-			if(!value.equals(data.getSpecificationValue(specClass))) {
-				ITopLevelElement modelContext =
-						IPersistencyService.INSTANCE.getTopLevelElementFor(data.getModelElement());
-				modelContext.runAsCommand(new Runnable() {
-
-					@Override
-					public void run() {
-
-						try {
-							data.setSpecificationValue((String)value, specClass);
-						} catch(IllegalArgumentException e) {
-							// should not happen with combo boxes and given values
-							MessageDialog
-									.openError(
-											new Shell(),
-											"ERROR",
-											"The value you entered does not have the desired type.\nDetailed message:\n" +
-													e.getMessage());
-						} catch(Exception e) {
-							// e.printStackTrace();
-							MessageDialog.openError(new Shell(), "ERROR", e.getMessage());
-						}
-					}
-				});
-				this.getViewer().refresh(true);
-			}
-
-		}
-	}
 }
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/MultiInstanceAnnotationTextEditingSupport.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/MultiInstanceAnnotationTextEditingSupport.java
index 1b9ddcfa8..cc44d6195 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/MultiInstanceAnnotationTextEditingSupport.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/MultiInstanceAnnotationTextEditingSupport.java
@@ -17,15 +17,10 @@ $Id$
 +--------------------------------------------------------------------------*/
 package org.fortiss.tooling.base.ui.annotation.editingsupport;
 
-import org.eclipse.emf.common.util.EMap;
-import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.ColumnViewer;
-import org.eclipse.swt.widgets.Shell;
 import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
 import org.fortiss.tooling.base.ui.annotation.AnnotationEntry;
 import org.fortiss.tooling.base.ui.annotation.valueprovider.MultiInstanceAnnotationValueProviderBase;
-import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
-import org.fortiss.tooling.kernel.service.IPersistencyService;
 
 /**
  * This class extends {@link TextEditingSupport} to support text annotations for which multiple
@@ -52,53 +47,14 @@ public class MultiInstanceAnnotationTextEditingSupport extends TextEditingSuppor
 	}
 
 	/** {@inheritDoc} */
-	@SuppressWarnings("unchecked")
 	@Override
-	protected Object getValue(Object element) {
-		String rval = null;
-		if(element instanceof AnnotationEntry) {
-			AnnotationEntry data = (AnnotationEntry)element;
-			if(data.getSpecificationValue(specClass) != null) {
-				rval =
-						((EMap<String, String>)data.getSpecificationValue(specClass))
-								.get(instanceKey);
-			}
-		}
-		return rval != null ? rval : "";
+	protected Object doGetValue(AnnotationEntry annotationEntry) {
+		return annotationEntry.getSpecificationValue(specClass, instanceKey);
 	}
 
 	/** {@inheritDoc} */
 	@Override
-	protected void setValue(Object element, final Object value) {
-		if(element instanceof AnnotationEntry && value instanceof String) {
-			final AnnotationEntry entry = (AnnotationEntry)element;
-
-			if(!value.equals(entry.getSpecificationValue(specClass, instanceKey))) {
-
-				ITopLevelElement modelContext =
-						IPersistencyService.INSTANCE.getTopLevelElementFor(entry.getModelElement());
-				modelContext.runAsCommand(new Runnable() {
-
-					@Override
-					public void run() {
-						try {
-							entry.setSpecificationValue(value, specClass, instanceKey);
-						} catch(IllegalArgumentException e) {
-							// e.printStackTrace();
-							MessageDialog
-									.openError(
-											new Shell(),
-											"ERROR",
-											"The value you entered does not have the desired type.\nDetailed message:\n" +
-													e.getMessage());
-						} catch(Exception e) {
-							MessageDialog.openError(new Shell(), "ERROR", e.getMessage());
-						}
-					}
-				});
-				this.getViewer().refresh(true);
-			}
-
-		}
+	protected void doSetValue(AnnotationEntry annotationEntry, String value) throws Exception {
+		annotationEntry.setSpecificationValue(value, specClass, instanceKey);
 	}
 }
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/TextEditingSupport.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/TextEditingSupport.java
index 09179fdbc..659506ea7 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/TextEditingSupport.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/TextEditingSupport.java
@@ -17,18 +17,12 @@ $Id$
 +--------------------------------------------------------------------------*/
 package org.fortiss.tooling.base.ui.annotation.editingsupport;
 
-import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.CellEditor;
 import org.eclipse.jface.viewers.ColumnViewer;
-import org.eclipse.jface.viewers.EditingSupport;
 import org.eclipse.jface.viewers.TextCellEditor;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Shell;
 import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
-import org.fortiss.tooling.base.ui.annotation.AnnotationEntry;
-import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
-import org.fortiss.tooling.kernel.service.IPersistencyService;
 
 /**
  * This class constructs and manages an editing element for the annotation view. It provides a
@@ -40,19 +34,17 @@ import org.fortiss.tooling.kernel.service.IPersistencyService;
  * @version $Rev$
  * @ConQAT.Rating YELLOW Hash: 09C91FA1AB55052CD74ECF69E121DD7C
  */
-public class TextEditingSupport extends EditingSupport {
+public class TextEditingSupport extends AnnotationEditingSupportBase {
 
 	/** Text cell editor */
 	protected TextCellEditor cellEditor = null;
-	/** Specification class of this column */
-	protected Class<? extends IAnnotatedSpecification> specClass;
 
 	/** Constructor. */
-	public TextEditingSupport(ColumnViewer viewer, Class<? extends IAnnotatedSpecification> class1) {
-		super(viewer);
+	public TextEditingSupport(ColumnViewer viewer,
+			Class<? extends IAnnotatedSpecification> specClass) {
+		super(viewer, specClass);
 
 		cellEditor = new TextCellEditor((Composite)getViewer().getControl(), SWT.NONE);
-		specClass = class1;
 	}
 
 	/** {@inheritDoc} */
@@ -61,62 +53,17 @@ public class TextEditingSupport extends EditingSupport {
 		return cellEditor;
 	}
 
-	/** {@inheritDoc} */
-	@Override
-	protected boolean canEdit(Object element) {
-		if(element instanceof AnnotationEntry) {
-			AnnotationEntry data = (AnnotationEntry)element;
-			return data.getSpecificationValue(specClass) != AnnotationEntry.NOVAL;
-		}
-
-		return true;
-	}
-
 	/** {@inheritDoc} */
 	@Override
 	protected Object getValue(Object element) {
-		if(element instanceof AnnotationEntry) {
-			AnnotationEntry annotationEntry = (AnnotationEntry)element;
-			Object value = annotationEntry.getSpecificationValue(specClass);
-			if(value != null) {
-				return value.toString();
-			}
+		// This method is triggered from several methods in EditingSupport, which - for this
+		// TextEditingSupport - passes its result to TextCellEditor.doSetValue() which expects a
+		// non-null String.
+		Object rval = super.getValue(element);
+		if(rval != null) {
+			return rval.toString();
 		}
-		return "";
-	}
 
-	/** {@inheritDoc} */
-	@Override
-	protected void setValue(Object element, final Object value) {
-		if(element instanceof AnnotationEntry && value instanceof String) {
-			final AnnotationEntry entry = (AnnotationEntry)element;
-
-			if(!value.equals(entry.getSpecificationValue(specClass))) {
-				ITopLevelElement modelContext =
-						IPersistencyService.INSTANCE.getTopLevelElementFor(entry.getModelElement());
-				modelContext.runAsCommand(new Runnable() {
-
-					@Override
-					public void run() {
-						try {
-							entry.setSpecificationValue((String)value, specClass);
-						} catch(IllegalArgumentException e) {
-							// e.printStackTrace();
-							MessageDialog
-									.openError(
-											new Shell(),
-											"ERROR",
-											"The value you entered does not have the desired type.\nDetailed message:\n" +
-													e.getMessage());
-						} catch(Exception e) {
-							MessageDialog.openError(new Shell(), "ERROR", e.getMessage());
-						}
-					}
-				});
-				this.getViewer().refresh(true);
-			}
-
-		}
+		return "";
 	}
-
 }
-- 
GitLab