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

import info.limpet.stackedcharts.model.Chart;
import info.limpet.stackedcharts.model.ChartSet;
import info.limpet.stackedcharts.model.StackedchartsPackage;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.GridLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;

public class ChartSetEditPart extends AbstractGraphicalEditPart
{
  /**
   * Wraps the charts, so that they are displayed in a separate container and not together with the
   * shared axis.
   */
  public static class ChartsWrapper
  {
    private final List<Chart> charts;

    public ChartsWrapper(List<Chart> charts)
    {
      this.charts = charts;
    }

    public List<Chart> getCharts()
    {
      return charts;
    }
  }

  public static class ChartSetWrapper
  {
    private final ChartSet charts;

    public ChartSetWrapper(ChartSet charts)
    {
      this.charts = charts;
    }

    public ChartSet getcChartSet()
    {
      return charts;
    }
  }

  
  private ChartSetAdapter adapter = new ChartSetAdapter();

  @Override
  public void activate()
  {
    super.activate();
    getChartSet().eAdapters().add(adapter);
  }

  @Override
  public void deactivate()
  {
    getChartSet().eAdapters().remove(adapter);
    super.deactivate();
  }
  
  ChartSet getChartSet()
  {
    return (ChartSet) getModel();
  }
  
  @Override
  protected IFigure createFigure()
  {
    RectangleFigure rectangle = new RectangleFigure();
    rectangle.setOutline(false);
    GridLayout gridLayout = new GridLayout();
    gridLayout.marginHeight = 10;
    gridLayout.marginWidth = 10;
    rectangle.setLayoutManager(gridLayout);
    rectangle.setBackgroundColor(Display.getDefault().getSystemColor(
        SWT.COLOR_WIDGET_BACKGROUND));

    return rectangle;
  }

  @Override
  protected void createEditPolicies()
  {
  }

  @Override
  public ChartSet getModel()
  {
    return (ChartSet )super.getModel();
  }
  
  public class ChartSetAdapter implements Adapter
  {

    @Override
    public void notifyChanged(Notification notification)
    {
      int featureId = notification.getFeatureID(StackedchartsPackage.class);
      switch (featureId)
      {
      case StackedchartsPackage.CHART_SET__CHARTS:
        refreshChildren();
        break;
      }
    }

    @Override
    public Notifier getTarget()
    {
      return  getModel();
    }

    @Override
    public void setTarget(Notifier newTarget)
    {
      // Do nothing.
    }

    @Override
    public boolean isAdapterForType(Object type)
    {
      return type.equals(ChartSet.class);
    }
  }

  @SuppressWarnings(
  {"rawtypes", "unchecked"})
  @Override
  protected List getModelChildren()
  {
    // 2 model children - the charts, displayed in a separate container and the shared (independent
    // axis) shown on the bottom
    List modelChildren = new ArrayList<>();
    ChartSet chartSet = getModel();
    modelChildren.add(new ChartSetWrapper(chartSet));
    modelChildren.add(new ChartsWrapper(chartSet.getCharts()));
    modelChildren.add(chartSet.getSharedAxis());
    return modelChildren;
  }
}

@@ -140,16 +140,68 @@
 artSet;%0A
+import info.limpet.stackedcharts.model.Orientation;%0A
 import i
@@ -253,16 +253,16 @@
 ackage;%0A
-
 %0Aimport 
@@ -1367,16 +1367,317 @@
 %0A  %7D%0A%0A  
+@Override%0A  protected void refreshVisuals()%0A  %7B%0A    GridLayout layoutManager = (GridLayout) getFigure().getLayoutManager();%0A    layoutManager.numColumns =%0A        getChartSet().getOrientation() == Orientation.HORIZONTAL%0A            ? getModelChildren().size() : 1;%0A    layoutManager.invalidate();%0A  %7D%0A
 %0A  priva
@@ -1959,19 +1959,17 @@
 ();%0A  %7D%0A
-  
 %0A
+
   ChartS
@@ -2027,19 +2027,17 @@
 ();%0A  %7D%0A
-  
 %0A
+
   @Overr
@@ -2589,10 +2589,10 @@
 tSet
- 
 )
+ 
 supe
@@ -2613,11 +2613,9 @@
   %7D%0A
-  
 %0A
+
   pu
@@ -2945,16 +2945,106 @@
  break;%0A
+      case StackedchartsPackage.CHART_SET__ORIENTATION:%0A        refresh();%0A        break;%0A
       %7D%0A
@@ -3094,32 +3094,32 @@
 tTarget()%0A    %7B%0A
+
       return  ge
@@ -3115,17 +3115,16 @@
  return 
- 
 getModel

Support ChartOrientation in Editor #294