public AbstractSelectionRegion<?> getEditingRegion() {

    final String txt = nameText.getText();
    try {
      xyGraph.renameRegion(editingRegion, txt);
    } catch (Exception e) {
      MessageDialog.openError(
          Display.getDefault().getActiveShell(),
          "Region Exists",
          "The region '"
              + txt
              + "' already exists.\n\n"
              + "Please choose a unique name for regions.");
    }
    final AspectAxis x = getAxis(xyGraph.getXAxisList(), xCombo.getSelectionIndex());
    final AspectAxis y = getAxis(xyGraph.getYAxisList(), yCombo.getSelectionIndex());
    RegionCoordinateSystem sys = new RegionCoordinateSystem(getImageTrace(), x, y);
    editingRegion.setCoordinateSystem(sys);
    editingRegion.setShowPosition(showPoints.getSelection());
    editingRegion.setRegionColor(new Color(getDisplay(), colorSelector.getColorValue()));
    editingRegion.setAlpha(alpha.getSelection());
    editingRegion.setMobile(mobile.getSelection());
    editingRegion.setVisible(visible.getSelection());
    editingRegion.setShowLabel(showLabel.getSelection());
    editingRegion.setFill(fillRegion.getSelection());

    return editingRegion;
  }
  public void setEditingRegion(final AbstractSelectionRegion<?> region) {

    this.editingRegion = region;
    this.roiViewer.setRegion(region.getROI(), region.getRegionType(), region.getCoordinateSystem());
    this.roiListener =
        new IROIListener.Stub() {
          @Override
          public void roiChanged(ROIEvent evt) {
            region.setROI(evt.getROI());
          }
        };
    roiViewer.addROIListener(roiListener);

    Range range = xyGraph.primaryXAxis.getRange();
    roiViewer.setXLowerBound(Math.min(range.getUpper(), range.getLower()));
    roiViewer.setXUpperBound(Math.max(range.getUpper(), range.getLower()));

    range = xyGraph.primaryYAxis.getRange();
    roiViewer.setYLowerBound(Math.min(range.getUpper(), range.getLower()));
    roiViewer.setYUpperBound(Math.max(range.getUpper(), range.getLower()));

    nameText.setText(region.getName());
    regionType.select(region.getRegionType().getIndex());
    regionType.setEnabled(false);
    regionType.setEditable(false);

    int index = xyGraph.getXAxisList().indexOf(region.getCoordinateSystem().getX());
    xCombo.select(index);

    index = xyGraph.getYAxisList().indexOf(region.getCoordinateSystem().getY());
    yCombo.select(index);

    colorSelector.setColorValue(region.getRegionColor().getRGB());
    alpha.setSelection(region.getAlpha());
    mobile.setSelection(region.isMobile());
    showPoints.setSelection(region.isShowPosition());
    visible.setSelection(region.isVisible());
    showLabel.setSelection(region.isShowLabel());
    fillRegion.setSelection(region.isFill());
  }
  public AbstractSelectionRegion<?> createRegion() throws Exception {

    final AspectAxis xAxis = getAxis(xyGraph.getXAxisList(), xCombo.getSelectionIndex());
    final AspectAxis yAxis = getAxis(xyGraph.getYAxisList(), yCombo.getSelectionIndex());

    AbstractSelectionRegion<?> region = null;

    final String txt = nameText.getText();
    final Pattern pattern = Pattern.compile(".* (\\d+)");
    final Matcher matcher = pattern.matcher(txt);
    if (matcher.matches()) {
      final int count = Integer.parseInt(matcher.group(1));
      countMap.put(regionType.getSelectionIndex(), count);
    }

    region =
        xyGraph.createRegion(
            txt, xAxis, yAxis, RegionType.getRegion(regionType.getSelectionIndex()), true);
    region.setPlotType(plottingSystem.getPlotType());
    this.editingRegion = region;
    getEditingRegion();

    return region;
  }
 public void disposeRegion(AbstractSelectionRegion<?> region) {
   xyGraph.disposeRegion(region);
 }
 public IImageTrace getImageTrace() {
   return ((RegionArea) xyGraph.getPlotArea()).getImageTrace();
 }
  /**
   * Used internally
   *
   * @param parent
   * @param plottingSystem
   * @param style
   * @param xyGraph
   * @param defaultRegion
   * @param isImplicit Flag to tell whether the RegionComposite is used in a specific window with an
   *     apply button or part of a view where the changes are implicit
   */
  public RegionEditComposite(
      final Composite parent,
      final IPlottingSystem<?> plottingSystem,
      final int style,
      final XYRegionGraph xyGraph,
      final RegionType defaultRegion,
      final boolean isImplicit) {

    super(parent, SWT.NONE);

    this.setImplicit(isImplicit);

    this.xyGraph = xyGraph;
    this.plottingSystem = plottingSystem;

    setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    setLayout(new org.eclipse.swt.layout.GridLayout(2, false));

    final Label nameLabel = new Label(this, SWT.NONE);
    nameLabel.setText("Name   ");
    nameLabel.setLayoutData(new GridData());

    nameText = new Text(this, SWT.BORDER | SWT.SINGLE);
    nameText.setToolTipText("Region name");
    nameText.setLayoutData(new GridData(SWT.FILL, 0, true, false));
    if (isImplicit()) {
      nameText.addSelectionListener(
          new SelectionAdapter() {
            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
              AbstractSelectionRegion<?> region = getEditingRegion();
              region.repaint();
            }
          });
    }

    final Label horiz = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
    horiz.setLayoutData(new GridData(SWT.FILL, 0, true, false, 2, 1));

    final Label typeLabel = new Label(this, SWT.NONE);
    typeLabel.setText("Type");
    typeLabel.setLayoutData(new GridData());

    regionType = new CCombo(this, SWT.BORDER | SWT.NONE);
    regionType.setEditable(false);
    regionType.setToolTipText("Region type");
    regionType.setLayoutData(new GridData(SWT.FILL, 0, true, false));
    for (RegionType type : RegionType.ALL_TYPES) {
      regionType.add(type.getName());
    }
    regionType.select(defaultRegion.getIndex());

    xCombo = createAxisChooser(this, "X Axis", xyGraph.getXAxisList());
    yCombo = createAxisChooser(this, "Y Axis", xyGraph.getYAxisList());

    Label colorLabel = new Label(this, 0);
    colorLabel.setText("Selection Color");
    colorLabel.setLayoutData(new GridData());

    colorSelector = new ColorSelector(this);
    colorSelector.getButton().setLayoutData(new GridData());
    colorSelector.setColorValue(defaultRegion.getDefaultColor().getRGB());
    if (isImplicit()) {
      colorSelector.addListener(
          new IPropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent event) {
              AbstractSelectionRegion<?> region = getEditingRegion();
              region.repaint();
            }
          });
    }

    final Label horiz2 = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
    horiz2.setLayoutData(new GridData(SWT.FILL, 0, true, false, 2, 1));

    Label alphaLabel = new Label(this, 0);
    alphaLabel.setText("Alpha level");
    alphaLabel.setLayoutData(new GridData());

    alpha = new Spinner(this, SWT.NONE);
    alpha.setToolTipText("Alpha transparency level of the shaded shape");
    alpha.setLayoutData(new GridData());
    alpha.setMinimum(0);
    alpha.setMaximum(255);
    alpha.setSelection(80);
    if (isImplicit()) {
      alpha.addSelectionListener(
          new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
              AbstractSelectionRegion<?> region = getEditingRegion();
              region.repaint();
            }
          });
    }

    this.mobile = new Button(this, SWT.CHECK);
    mobile.setText("   Mobile   ");
    mobile.setToolTipText("When true, this selection can be resized and moved around the graph.");
    mobile.setLayoutData(new GridData(0, 0, false, false, 2, 1));
    mobile.setSelection(true);
    if (isImplicit()) {
      mobile.addSelectionListener(
          new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
              AbstractSelectionRegion<?> region = getEditingRegion();
              region.repaint();
            }
          });
    }

    this.showPoints = new Button(this, SWT.CHECK);
    showPoints.setText("   Show vertex values");
    showPoints.setToolTipText(
        "When on this will show the actual value of the point in the axes it was added.");
    showPoints.setLayoutData(new GridData(0, 0, false, false, 2, 1));
    if (isImplicit()) {
      showPoints.addSelectionListener(
          new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
              AbstractSelectionRegion<?> region = getEditingRegion();
              region.repaint();
            }
          });
    }

    this.visible = new Button(this, SWT.CHECK);
    visible.setText("   Show region");
    visible.setToolTipText("You may turn off the visibility of this region.");
    visible.setLayoutData(new GridData(0, 0, false, false, 2, 1));
    visible.setSelection(true);
    if (isImplicit()) {
      visible.addSelectionListener(
          new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
              AbstractSelectionRegion<?> region = getEditingRegion();
              region.repaint();
            }
          });
    }

    this.showLabel = new Button(this, SWT.CHECK);
    showLabel.setText("   Show name");
    showLabel.setToolTipText("Turn on to show the name of a selection region.");
    showLabel.setLayoutData(new GridData(0, 0, false, false, 2, 1));
    showLabel.setSelection(true);
    if (isImplicit()) {
      showLabel.addSelectionListener(
          new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
              AbstractSelectionRegion<?> region = getEditingRegion();
              region.repaint();
            }
          });
    }

    this.fillRegion = new Button(this, SWT.CHECK);
    fillRegion.setText("   Fill region");
    fillRegion.setToolTipText("Fill the body of an area region");
    fillRegion.setLayoutData(new GridData(0, 0, false, false, 2, 1));
    fillRegion.setSelection(true);
    if (isImplicit()) {
      fillRegion.addSelectionListener(
          new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
              AbstractSelectionRegion<?> region = getEditingRegion();
              region.repaint();
            }
          });
    }

    final Label spacer = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
    spacer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

    // We add a composite for setting the region location programmatically.
    final Label location = new Label(this, SWT.NONE);
    location.setText("Region Location");
    location.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

    this.roiViewer = new ROIEditTable();
    roiViewer.createPartControl(this);

    // Should be last
    nameText.setText(getDefaultName(defaultRegion.getIndex()));
  }
 public void undo() {
   for (AbstractSelectionRegion<?> region : regions) xyGraph.addRegion(region);
 }
 public void redo() {
   for (AbstractSelectionRegion<?> region : regions) xyGraph.removeRegion(region);
 }