package info.limpet.stackedcharts.ui.editor.commands;

import info.limpet.stackedcharts.model.Chart;
import info.limpet.stackedcharts.model.ChartSet;
import info.limpet.stackedcharts.model.ScatterSet;
import info.limpet.stackedcharts.model.SelectiveAnnotation;
import info.limpet.stackedcharts.model.StackedchartsFactory;

import org.eclipse.emf.common.util.EList;
import org.eclipse.gef.commands.Command;

public class AddScatterSetsToChartCommand extends Command
{
  private final ScatterSet[] scatterSets;
  private final Chart parent;

  public AddScatterSetsToChartCommand(Chart parent, ScatterSet... scatterSets)
  {
    this.scatterSets = scatterSets;
    this.parent = parent;
  }

  @Override
  public void execute()
  {
    for (ScatterSet ds : scatterSets)
    {
      // ok, we may have to add it to the chartset first
      ChartSet charts = parent.getParent();
      EList<SelectiveAnnotation> annots =
          charts.getSharedAxis().getAnnotations();
      SelectiveAnnotation host = null;
      for (SelectiveAnnotation annot : annots)
      {
        if (annot.getAnnotation().getName() != null
            && annot.getAnnotation().getName().equals(ds.getName()))
        {
          host = annot;
          break;
        }
      }

      if (host == null)
      {
        host = StackedchartsFactory.eINSTANCE.createSelectiveAnnotation();
        host.setAnnotation(ds);
        annots.add(host);
      }

      // check we're not already in that chart
      EList<Chart> appearsIn = host.getAppearsIn();
      if (!appearsIn.contains(parent))
      {
        appearsIn.add(parent);
      }
    }
  }

  @Override
  public void undo()
  {
    for (@SuppressWarnings("unused")
    ScatterSet ds : scatterSets)
    {
      // parent.getDatasets().remove(ds);
      System.err.println("UNDO ADDING SCATTERSET NOT IMPLEMENTED");
    }
  }
}

@@ -48,16 +48,152 @@
 mands;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport org.eclipse.emf.common.util.EList;%0Aimport org.eclipse.gef.commands.Command;%0A%0A
 import i
@@ -456,92 +456,8 @@
 y;%0A%0A
-import org.eclipse.emf.common.util.EList;%0Aimport org.eclipse.gef.commands.Command;%0A%0A
 publ
@@ -585,16 +585,422 @@
 arent;%0A%0A
+  /**%0A   * Contains all newly created annotations during %7B@link #execute()%7D that need to be removed during%0A   * %7B@link #undo()%7D%0A   */%0A  private List%3CSelectiveAnnotation%3E createdAnnotations;%0A%0A  /**%0A   * Contains annotations which have been added to appear in the parent. Again the parent needs to%0A   * be removed from those during %7B@link #undo()%7D.%0A   */%0A  private List%3CSelectiveAnnotation%3E appearInParent;%0A%0A
   public
@@ -1177,24 +1177,148 @@
 ecute()%0A  %7B%0A
+%0A    createdAnnotations = new ArrayList%3CSelectiveAnnotation%3E();%0A    appearInParent = new ArrayList%3CSelectiveAnnotation%3E();%0A%0A
     for (Sca
@@ -1491,26 +1491,16 @@
 annots =
-%0A         
  charts.
@@ -1514,16 +1514,27 @@
 edAxis()
+%0A          
 .getAnno
@@ -1693,20 +1693,8 @@
 null
-%0A           
  && 
@@ -1706,32 +1706,45 @@
 .getAnnotation()
+%0A            
 .getName().equal
@@ -1991,24 +1991,62 @@
 .add(host);%0A
+        createdAnnotations.add(host);%0A
       %7D%0A%0A   
@@ -2215,24 +2215,58 @@
 dd(parent);%0A
+        appearInParent.add(host);%0A
       %7D%0A    
@@ -2322,182 +2322,358 @@
 or (
-@SuppressWarnings(%22unused%22)%0A    ScatterSet ds : scatterSets
+SelectiveAnnotation annotation : appearInParent)%0A    %7B%0A      annotation.getAppearsIn().remove(parent);%0A    %7D%0A%0A    ChartSet charts = parent.getParent();%0A    EList%3CSelectiveAnnotation%3E annotations = charts.getSharedAxis(
 )%0A    
-%7B%0A
     
-  // parent.getDatasets().remove(ds);%0A      System.err.println(%22UNDO ADDING SCATTERSET NOT IMPLEMENTED%22
+.getAnnotations();%0A    for (SelectiveAnnotation annotation : createdAnnotations)%0A    %7B%0A      annotations.remove(annotation
 );%0A 
@@ -2682,9 +2682,8 @@
  %7D%0A  %7D%0A%7D
-%0A

handle undo when adding scatter set #358