Bug 532164: [Charting] clear() will leave a chart in an unusable state

  we need to cache user annotations and remove them one by one from the
  chart

Change-Id: I919f9524adde374581b21e9fee7291184b648a5d
diff --git a/plugins/org.eclipse.ease.modules.charting/src/org/eclipse/ease/modules/charting/ChartingModule.java b/plugins/org.eclipse.ease.modules.charting/src/org/eclipse/ease/modules/charting/ChartingModule.java
index 28f019f..73c00f1 100644
--- a/plugins/org.eclipse.ease.modules.charting/src/org/eclipse/ease/modules/charting/ChartingModule.java
+++ b/plugins/org.eclipse.ease.modules.charting/src/org/eclipse/ease/modules/charting/ChartingModule.java
@@ -12,6 +12,8 @@
 package org.eclipse.ease.modules.charting;
 
 import java.io.File;
+import java.util.Collection;
+import java.util.HashSet;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.ease.modules.AbstractScriptModule;
@@ -35,13 +37,15 @@
 public class ChartingModule extends AbstractScriptModule {
 	public static final String MODULE_NAME = "Charting";
 
+	private static int fFigureIterator = 1;
+
 	private Chart fChart = null;
 
 	private XYGraph fXYGraph = null;
 
 	private Trace fCurrentPlot = null;
 
-	private static int fFigureIterator = 1;
+	private final Collection<Annotation> fUserMarkers = new HashSet<>();
 
 	/**
 	 * Opens a new view with an empty figure. <i>figureId</i> is used as a view and chart title. If a view with the same <i>figureId</i> already exists, it will
@@ -428,14 +432,17 @@
 	 */
 	@WrapToScript
 	public void clear() {
+		Display.getDefault().syncExec(() -> {
+			if (fXYGraph != null) {
+				for (final Annotation marker : fUserMarkers)
+					fXYGraph.removeAnnotation(marker);
+				// fXYGraph.removeAll();
+			}
+		});
+
 		if (fChart != null)
 			fChart.clear();
 
-		Display.getDefault().asyncExec(() -> {
-			if (fXYGraph != null)
-				fXYGraph.removeAll();
-		});
-
 		fCurrentPlot = null;
 	}
 
@@ -504,6 +511,8 @@
 				});
 			});
 
+			fUserMarkers.add(annotation);
+
 			return annotation;
 		}