<commit_before>package info.limpet.stackedcharts.ui.editor.parts;

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

import org.eclipse.draw2d.ActionEvent;
import org.eclipse.draw2d.ActionListener;
import org.eclipse.draw2d.Button;
import org.eclipse.draw2d.GridData;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
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.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.editpolicies.ComponentEditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.requests.GroupRequest;

import info.limpet.stackedcharts.model.Chart;
import info.limpet.stackedcharts.model.ChartSet;
import info.limpet.stackedcharts.model.Dataset;
import info.limpet.stackedcharts.model.DependentAxis;
import info.limpet.stackedcharts.model.Orientation;
import info.limpet.stackedcharts.model.StackedchartsPackage;
import info.limpet.stackedcharts.model.Styling;
import info.limpet.stackedcharts.ui.editor.StackedchartsImages;
import info.limpet.stackedcharts.ui.editor.commands.DeleteDatasetsFromAxisCommand;
import info.limpet.stackedcharts.ui.editor.figures.DatasetFigure;
import info.limpet.stackedcharts.ui.editor.figures.DirectionalShape;

/**
 * An {@link GraphicalEditPart} to represent datasets
 */
public class DatasetEditPart extends AbstractGraphicalEditPart implements
    ActionListener
{

  private DatasetFigure contentPane;

  private DatasetAdapter adapter = new DatasetAdapter();

  @Override
  protected IFigure createFigure()
  {
    DirectionalShape figure = new DirectionalShape();

    contentPane = new DatasetFigure();
    figure.add(contentPane);

    Button button =
        new Button(StackedchartsImages
            .getImage(StackedchartsImages.DESC_DELETE));
    button.setToolTip(new Label("Remove the dataset from this axis"));
    button.addActionListener(this);
    figure.add(button);
    return figure;
  }

  @Override
  protected void addChildVisual(EditPart childEditPart, int index)
  {
    super.addChildVisual(childEditPart, getContentPane().getChildren().size());
  }

  @Override
  public IFigure getContentPane()
  {
    return contentPane;
  }

  @Override
  protected void createEditPolicies()
  {
    installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE,
        new NonResizableEditPolicy());
    installEditPolicy(EditPolicy.COMPONENT_ROLE, new ComponentEditPolicy()
    {
      protected Command createDeleteCommand(GroupRequest deleteRequest)
      {
        Dataset dataset = (Dataset) getHost().getModel();
        DependentAxis parent = (DependentAxis) getHost().getParent().getModel();
        DeleteDatasetsFromAxisCommand cmd =
            new DeleteDatasetsFromAxisCommand(parent, dataset);
        return cmd;
      }
    });
  }

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

  @Override
  public void deactivate()
  {
    getDataset().eAdapters().remove(adapter);
    super.deactivate();
  }

  @Override
  protected void refreshVisuals()
  {
    contentPane.setName(getDataset().getName());

    ChartSet parent =
        ((Chart) getParent().getParent().getParent().getModel()).getParent();

    boolean horizontal = parent.getOrientation() == Orientation.HORIZONTAL;
    ((DirectionalShape) getFigure()).setVertical(!horizontal);

    if (horizontal)
    {
      contentPane.setVertical(false);
      setLayoutConstraint(this, getFigure(), new GridData(GridData.FILL,
          GridData.FILL, true, false));

    }
    else
    {
      contentPane.setVertical(true);
      setLayoutConstraint(this, getFigure(), new GridData(GridData.CENTER,
          GridData.FILL, false, true));

    }
  }

  protected Dataset getDataset()
  {
    return (Dataset) getModel();
  }

  @Override
  public void actionPerformed(ActionEvent event)
  {
    Command deleteCommand = getCommand(new GroupRequest(REQ_DELETE));
    if (deleteCommand != null)
    {
      CommandStack commandStack = getViewer().getEditDomain().getCommandStack();
      commandStack.execute(deleteCommand);
    }
  }

  @SuppressWarnings("rawtypes")
  @Override
  protected List getModelChildren()
  {
    List<Styling> result = new ArrayList<>();
    Styling styling = getDataset().getStyling();
    if (styling != null)
    {
      result.add(styling);
    }
    return result;
  }

  public class DatasetAdapter implements Adapter
  {

    @Override
    public void notifyChanged(Notification notification)
    {
      int featureId = notification.getFeatureID(StackedchartsPackage.class);
      switch (featureId)<commit_msg>Provide dataset & styling elements together<commit_after>package info.limpet.stackedcharts.ui.editor.parts;

import info.limpet.stackedcharts.model.Chart;
import info.limpet.stackedcharts.model.ChartSet;
import info.limpet.stackedcharts.model.Dataset;
import info.limpet.stackedcharts.model.DependentAxis;
import info.limpet.stackedcharts.model.Orientation;
import info.limpet.stackedcharts.model.StackedchartsPackage;
import info.limpet.stackedcharts.model.Styling;
import info.limpet.stackedcharts.ui.editor.StackedchartsImages;
import info.limpet.stackedcharts.ui.editor.commands.DeleteDatasetsFromAxisCommand;
import info.limpet.stackedcharts.ui.editor.figures.DatasetFigure;
import info.limpet.stackedcharts.ui.editor.figures.DirectionalShape;

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

import org.eclipse.draw2d.ActionEvent;
import org.eclipse.draw2d.ActionListener;
import org.eclipse.draw2d.Button;
import org.eclipse.draw2d.GridData;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
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.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.editpolicies.ComponentEditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.requests.GroupRequest;
import org.eclipse.ui.views.properties.IPropertySource;

/**
 * An {@link GraphicalEditPart} to represent datasets
 */
public class DatasetEditPart extends AbstractGraphicalEditPart implements
    ActionListener, IPropertySourceProvider
{

  private DatasetFigure contentPane;

  private DatasetAdapter adapter = new DatasetAdapter();

  @Override
  protected IFigure createFigure()
  {
    DirectionalShape figure = new DirectionalShape();

    contentPane = new DatasetFigure();
    figure.add(contentPane);

    Button button =
        new Button(StackedchartsImages
            .getImage(StackedchartsImages.DESC_DELETE));
    button.setToolTip(new Label("Remove the dataset from this axis"));
    button.addActionListener(this);
    figure.add(button);
    return figure;
  }

  @Override
  protected void addChildVisual(EditPart childEditPart, int index)
  {
    super.addChildVisual(childEditPart, getContentPane().getChildren().size());
  }

  @Override
  public IFigure getContentPane()
  {
    return contentPane;
  }
  

  @Override
  public IPropertySource getPropertySource()
  {
    final Dataset axis = getDataset();
    final Styling axisType = getDataset().getStyling();

    // Proxy two objects in to one
    return new CombinedProperty(axis, axisType, "Styling");
  }

  @Override
  protected void createEditPolicies()
  {
    installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE,
        new NonResizableEditPolicy());
    installEditPolicy(EditPolicy.COMPONENT_ROLE, new ComponentEditPolicy()
    {
      protected Command createDeleteCommand(GroupRequest deleteRequest)
      {
        Dataset dataset = (Dataset) getHost().getModel();
        DependentAxis parent = (DependentAxis) getHost().getParent().getModel();
        DeleteDatasetsFromAxisCommand cmd =
            new DeleteDatasetsFromAxisCommand(parent, dataset);
        return cmd;
      }
    });
  }

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

  @Override
  public void deactivate()
  {
    getDataset().eAdapters().remove(adapter);
    super.deactivate();
  }

  @Override
  protected void refreshVisuals()
  {
    contentPane.setName(getDataset().getName());

    ChartSet parent =
        ((Chart) getParent().getParent().getParent().getModel()).getParent();

    boolean horizontal = parent.getOrientation() == Orientation.HORIZONTAL;
    ((DirectionalShape) getFigure()).setVertical(!horizontal);

    if (horizontal)
    {
      contentPane.setVertical(false);
      setLayoutConstraint(this, getFigure(), new GridData(GridData.FILL,
          GridData.FILL, true, false));

    }
    else
    {
      contentPane.setVertical(true);
      setLayoutConstraint(this, getFigure(), new GridData(GridData.CENTER,
          GridData.FILL, false, true));

    }
  }

  protected Dataset getDataset()
  {
    return (Dataset) getModel();
  }

  @Override
  public void actionPerformed(ActionEvent event)
  {
    Command deleteCommand = getCommand(new GroupRequest(REQ_DELETE));
    if (deleteCommand != null)
    {
      CommandStack commandStack = getViewer().getEditDomain().getCommandStack();
      commandStack.execute(deleteCommand);
    }
  }

  public class DatasetAdapter implements Adapter
  {

    @Override
    public void notifyChanged(Notification notification)
    {
      int featureId = notification.getFeatureID(StackedchartsPackage.class);
      switch (featureId)
{'fraction_non_alphanumeric': 0.10346564577876284, 'fraction_numerical': 0.0012089462018940156, 'mean_word_length': 4.257944915254237, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2502811', 'n_tokens_mistral': 2966, 'n_tokens_neox': 2889, 'n_words': 527}