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

import info.limpet.stackedcharts.model.Dataset;
import info.limpet.stackedcharts.model.DependentAxis;
import info.limpet.stackedcharts.model.impl.StackedchartsFactoryImpl;
import info.limpet.stackedcharts.ui.editor.commands.AddAxisToChartCommand;
import info.limpet.stackedcharts.ui.editor.commands.AddDatasetsToAxisCommand;
import info.limpet.stackedcharts.ui.editor.parts.AxisLandingPadEditPart;
import info.limpet.stackedcharts.ui.editor.parts.ChartEditPart.ChartPanePosition;
import info.limpet.stackedcharts.ui.editor.parts.ChartPaneEditPart;

import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.swt.dnd.DropTargetEvent;

public class DatasetToAxisLandingDropTargetListener extends
    DatasetDropTargetListener
{
  public DatasetToAxisLandingDropTargetListener(GraphicalViewer viewer)
  {
    super(viewer);
  }

  @Override
  public boolean appliesTo(DropTargetEvent event)
  {
    EditPart findObjectAt = findPart(event);
    return findObjectAt instanceof AxisLandingPadEditPart;
  }

  protected Command createCommand(AbstractGraphicalEditPart axis,
      List<Dataset> datasets)
  {
    CompoundCommand compoundCommand = new CompoundCommand();

    StackedchartsFactoryImpl factory = new StackedchartsFactoryImpl();
    DependentAxis newAxis = factory.createDependentAxis();
    newAxis.setName("[dimensionless]");
    newAxis.setAxisType(factory.createNumberAxis());

    final ChartPaneEditPart.AxisLandingPad pad =
        (ChartPaneEditPart.AxisLandingPad) axis.getModel();
    // find out which list (min/max) this axis is currently on
    final EList<DependentAxis> destination =
        pad.getPos() == ChartPanePosition.MIN ? pad.getChart().getMinAxes()
            : pad.getChart().getMaxAxes();

    compoundCommand.add(new AddAxisToChartCommand(destination, newAxis));

    AddDatasetsToAxisCommand addDatasetsToAxisCommand =
        new AddDatasetsToAxisCommand(newAxis, datasets
            .toArray(new Dataset[datasets.size()]));
    compoundCommand.add(addDatasetsToAxisCommand);
    return compoundCommand;
  }

}

@@ -1454,16 +1454,326 @@
 mmand();
+%0A    %0A    // get the dimensions of the first dataset%0A    final String units;%0A    if (datasets != null && datasets.size() %3E 0 && datasets.get(0).getUnits() != null)%0A    %7B%0A      final Dataset dataset = datasets.get(0);%0A      units = dataset.getUnits();%0A    %7D%0A    else%0A    %7B%0A      units = %22%5Bdimensionless%5D%22;%0A    %7D
 %0A%0A    St
@@ -1916,33 +1916,21 @@
 setName(
-%22%5Bdimensionless%5D%22
+units
 );%0A    n

use the data units, if known