Example #1
0
  private static class ToolbarSeparator extends Figure {

    private final Color GRAY_COLOR =
        XYGraphMediaFactory.getInstance().getColor(new RGB(130, 130, 130));

    @Override
    protected void paintClientArea(Graphics graphics) {
      super.paintClientArea(graphics);
      graphics.setForegroundColor(GRAY_COLOR);
      graphics.setLineWidth(1);
      graphics.drawLine(
          bounds.x + bounds.width / 2,
          bounds.y,
          bounds.x + bounds.width / 2,
          bounds.y + bounds.height);
    }
  }
Example #2
0
 private void addSnapshotButton() {
   Button snapShotButton =
       new Button(XYGraphMediaFactory.getInstance().getImage("images/camera.png"));
   snapShotButton.setToolTip(new Label("Save Snapshot to PNG file"));
   addButton(snapShotButton);
   snapShotButton.addActionListener(
       new ActionListener() {
         public void actionPerformed(ActionEvent event) {
           // Have valid name, so get image
           ImageLoader loader = new ImageLoader();
           Image image = xyGraph.getImage();
           loader.data = new ImageData[] {image.getImageData()};
           image.dispose();
           // Prompt for file name
           String path = SingleSourceHelper2.getImageSavePath();
           if (path == null || path.length() <= 0) return;
           // Assert *.png at end of file name
           if (!path.toLowerCase().endsWith(".png")) path = path + ".png";
           // Save
           loader.save(path, SWT.IMAGE_PNG);
         }
       });
 }
Example #3
0
  /**
   * Use {@link #XYGraphToolbar(IXYGraph, int)} instead<br>
   * Initialize
   *
   * @param xyGraph XYGraph on which this toolbar operates
   * @param flags Bitwise 'or' of flags
   * @see XYGraphFlags#COMBINED_ZOOM
   * @see XYGraphFlags#SEPARATE_ZOOM
   */
  @Deprecated
  public XYGraphToolbar(final XYGraph xyGraph, final int flags) {
    this.xyGraph = xyGraph;
    setLayoutManager(new WrappableToolbarLayout());

    final Button configButton =
        new Button(XYGraphMediaFactory.getInstance().getImage("images/Configure.png"));
    configButton.setToolTip(new Label("Configure Settings..."));
    addButton(configButton);
    configButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            XYGraphConfigDialog dialog =
                new XYGraphConfigDialog(Display.getCurrent().getActiveShell(), xyGraph);
            dialog.open();
          }
        });

    final Button addAnnotationButton =
        new Button(XYGraphMediaFactory.getInstance().getImage("images/Add_Annotation.png"));
    addAnnotationButton.setToolTip(new Label("Add Annotation..."));
    addButton(addAnnotationButton);
    addAnnotationButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            AddAnnotationDialog dialog =
                new AddAnnotationDialog(Display.getCurrent().getActiveShell(), xyGraph);
            if (dialog.open() == Window.OK) {
              xyGraph.addAnnotation(dialog.getAnnotation());
              xyGraph
                  .getOperationsManager()
                  .addCommand(new AddAnnotationCommand(xyGraph, dialog.getAnnotation()));
            }
          }
        });

    final Button delAnnotationButton =
        new Button(XYGraphMediaFactory.getInstance().getImage("images/Del_Annotation.png"));
    delAnnotationButton.setToolTip(new Label("Remove Annotation..."));
    addButton(delAnnotationButton);
    delAnnotationButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            RemoveAnnotationDialog dialog =
                new RemoveAnnotationDialog(Display.getCurrent().getActiveShell(), xyGraph);
            if (dialog.open() == Window.OK && dialog.getAnnotation() != null) {
              xyGraph.removeAnnotation(dialog.getAnnotation());
              xyGraph
                  .getOperationsManager()
                  .addCommand(new RemoveAnnotationCommand(xyGraph, dialog.getAnnotation()));
            }
          }
        });

    addSeparator();
    if ((flags & XYGraphFlags.STAGGER) > 0) { // stagger axes button
      final Button staggerButton =
          new Button(XYGraphMediaFactory.getInstance().getImage("images/stagger.png"));
      staggerButton.setToolTip(new Label("Stagger axes so they don't overlap"));
      addButton(staggerButton);
      staggerButton.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent event) {
              xyGraph.performStagger();
            }
          });
    } else { // auto scale button
      final Button autoScaleButton =
          new Button(XYGraphMediaFactory.getInstance().getImage("images/AutoScale.png"));
      autoScaleButton.setToolTip(new Label("Perform Auto Scale"));
      addButton(autoScaleButton);
      autoScaleButton.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent event) {
              xyGraph.performAutoScale();
            }
          });
    }

    // zoom buttons
    zoomGroup = new ButtonGroup();
    createZoomButtons(flags);

    addSeparator();
    addUndoRedoButtons();

    addSeparator();
    if (!SWT.getPlatform().startsWith("rap")) // $NON-NLS-1$
    addSnapshotButton();
  }
Example #4
0
  private void addUndoRedoButtons() {
    // undo button
    final GrayableButton undoButton =
        new GrayableButton(
            XYGraphMediaFactory.getInstance().getImage("images/Undo.png"), // $NON-NLS-1$
            XYGraphMediaFactory.getInstance().getImage("images/Undo_Gray.png")); // $NON-NLS-1$
    undoButton.setToolTip(new Label("Undo"));
    undoButton.setEnabled(false);
    addButton(undoButton);
    undoButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            xyGraph.getOperationsManager().undo();
          }
        });
    xyGraph
        .getOperationsManager()
        .addListener(
            new IOperationsManagerListener() {
              public void operationsHistoryChanged(OperationsManager manager) {
                if (manager.getUndoCommandsSize() > 0) {
                  undoButton.setEnabled(true);
                  final String cmd_name =
                      manager.getUndoCommands()[manager.getUndoCommandsSize() - 1].toString();
                  undoButton.setToolTip(new Label("Undo" + cmd_name));
                } else {
                  undoButton.setEnabled(false);
                  undoButton.setToolTip(new Label("Undo"));
                }
              }
            });

    // redo button
    final GrayableButton redoButton =
        new GrayableButton(
            XYGraphMediaFactory.getInstance().getImage("images/Redo.png"), // $NON-NLS-1$
            XYGraphMediaFactory.getInstance().getImage("images/Redo_Gray.png")); // $NON-NLS-1$
    redoButton.setToolTip(new Label("Redo"));
    redoButton.setEnabled(false);
    addButton(redoButton);
    redoButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            xyGraph.getOperationsManager().redo();
          }
        });
    xyGraph
        .getOperationsManager()
        .addListener(
            new IOperationsManagerListener() {
              public void operationsHistoryChanged(OperationsManager manager) {
                if (manager.getRedoCommandsSize() > 0) {
                  redoButton.setEnabled(true);
                  final String cmd_name =
                      manager.getRedoCommands()[manager.getRedoCommandsSize() - 1].toString();
                  redoButton.setToolTip(new Label("Redo" + cmd_name));
                } else {
                  redoButton.setEnabled(false);
                  redoButton.setToolTip(new Label("Redo"));
                }
              }
            });
  }
Example #5
0
/**
 * The figure of gauge
 *
 * @author Xihui Chen
 */
public class GaugeFigure extends AbstractRoundRampedFigure {

  private final Color WHITE_COLOR =
      XYGraphMediaFactory.getInstance().getColor(XYGraphMediaFactory.COLOR_WHITE);
  private final Color BORDER_COLOR =
      XYGraphMediaFactory.getInstance().getColor(new RGB(100, 100, 100));
  private final Color GRAY_COLOR =
      XYGraphMediaFactory.getInstance().getColor(XYGraphMediaFactory.COLOR_GRAY);
  private final Color DEFAULT_NEEDLE_COLOR =
      XYGraphMediaFactory.getInstance().getColor(XYGraphMediaFactory.COLOR_RED);
  private final Font DEFAULT_LABEL_FONT =
      XYGraphMediaFactory.getInstance().getFont(new FontData("Arial", 12, SWT.BOLD));
  private static final int BORDER_WIDTH = 2;

  private boolean effect3D = true;

  private NeedleCenter needleCenter;

  private Needle needle;

  private Label valueLabel;

  public GaugeFigure() {
    super();
    transparent = true;
    scale.setScaleLineVisible(false);
    scale.setTickLabelSide(LabelSide.Secondary);
    ramp.setRampWidth(10);

    valueLabel = new Label();
    valueLabel.setFont(DEFAULT_LABEL_FONT);

    needle = new Needle();
    needle.setFill(true);
    needle.setOutline(false);

    needleCenter = new NeedleCenter();
    needleCenter.setOutline(false);

    setLayoutManager(new GaugeLayout());
    add(ramp, GaugeLayout.RAMP);
    add(scale, GaugeLayout.SCALE);
    add(valueLabel, GaugeLayout.VALUE_LABEL);
    add(needle, GaugeLayout.NEEDLE);
    add(needleCenter, GaugeLayout.NEEDLE_CENTER);
    addFigureListener(
        new FigureListener() {
          public void figureMoved(IFigure source) {
            ramp.setDirty(true);
            revalidate();
          }
        });
  }

  @Override
  public void setBounds(Rectangle rect) {

    super.setBounds(rect);
  }

  @Override
  public void setValue(double value) {
    super.setValue(value);
    valueLabel.setText(getValueText());
  }

  @Override
  protected void paintClientArea(Graphics graphics) {
    graphics.setAntialias(SWT.ON);
    Rectangle area = getClientArea();
    area.width = Math.min(area.width, area.height);
    area.height = area.width;
    Pattern pattern = null;
    graphics.pushState();
    graphics.setBackgroundColor(GRAY_COLOR);
    boolean support3D = GraphicsUtil.testPatternSupported(graphics);
    if (effect3D && support3D) {
      // add this to eliminate the repaint bug on Mac
      graphics.fillOval(new Rectangle());
      pattern =
          new Pattern(
              Display.getCurrent(),
              area.x,
              area.y,
              area.x + area.width,
              area.y + area.height,
              BORDER_COLOR,
              WHITE_COLOR);
      graphics.setBackgroundPattern(pattern);
    }
    graphics.fillOval(area);
    graphics.popState();

    if (effect3D && support3D) {
      pattern.dispose();
      area.shrink(BORDER_WIDTH, BORDER_WIDTH);
    } else area.shrink(1, 1);

    graphics.fillOval(area);

    super.paintClientArea(graphics);

    // glossy effect
    if (effect3D && support3D) {
      graphics.pushState();
      graphics.setAntialias(SWT.ON);
      final double R = area.width / 2.0d;
      final double UD_FILL_PART = 9.5d / 10d;
      final double UP_DOWN_RATIO = 1d / 2d;
      final double LR_FILL_PART = 8.5d / 10d;
      final double UP_ANGLE = 0d * Math.PI / 180d;
      final double DOWN_ANGLE = 35d * Math.PI / 180d;
      // add this to eliminate the repaint bug on Mac

      graphics.fillOval(new Rectangle());

      Pattern glossyPattern =
          new Pattern(
              Display.getCurrent(),
              area.x + area.width / 2,
              (float) (area.y + area.height / 2 - R * UD_FILL_PART),
              area.x + area.width / 2,
              (float) (area.y + area.height / 2 + R * UP_DOWN_RATIO),
              WHITE_COLOR,
              90,
              WHITE_COLOR,
              0);
      graphics.setBackgroundPattern(glossyPattern);
      Rectangle rectangle =
          new Rectangle(
              (int) (area.x + area.width / 2 - R * LR_FILL_PART * Math.cos(UP_ANGLE)),
              (int) (area.y + area.height / 2 - R * UD_FILL_PART),
              (int) (2 * R * LR_FILL_PART * Math.cos(UP_ANGLE)),
              (int) (R * UD_FILL_PART + R * UP_DOWN_RATIO));
      graphics.fillOval(rectangle);
      glossyPattern.dispose();

      glossyPattern =
          new Pattern(
              Display.getCurrent(),
              area.x + area.width / 2,
              (float) (area.y + area.height / 2 + R * UP_DOWN_RATIO - 1),
              area.x + area.width / 2,
              (float) (area.y + area.height / 2 + R * UD_FILL_PART + 1),
              WHITE_COLOR,
              0,
              WHITE_COLOR,
              40);
      graphics.setBackgroundPattern(glossyPattern);
      rectangle =
          new Rectangle(
              (int) (area.x + area.width / 2 - R * LR_FILL_PART * Math.sin(DOWN_ANGLE)),
              (int) Math.ceil(area.y + area.height / 2 + R * UP_DOWN_RATIO),
              (int) (2 * R * LR_FILL_PART * Math.sin(DOWN_ANGLE)),
              (int) Math.ceil(R * UD_FILL_PART - R * UP_DOWN_RATIO));
      graphics.fillOval(rectangle);
      glossyPattern.dispose();
      graphics.popState();
    }
  }

  /** @param needleColor the needleColor to set */
  public void setNeedleColor(Color needleColor) {
    needle.setBackgroundColor(needleColor);
  }

  public Color getNeedleColor() {
    return needle.getBackgroundColor();
  }

  /** @param effect3D the effect3D to set */
  public void setEffect3D(boolean effect3D) {
    if (this.effect3D == effect3D) return;
    this.effect3D = effect3D;
    repaint();
  }

  /** @return the effect3D */
  public boolean isEffect3D() {
    return effect3D;
  }

  class Needle extends Polygon {
    public Needle() {
      setBackgroundColor(DEFAULT_NEEDLE_COLOR);
    }

    @Override
    protected void fillShape(Graphics g) {
      g.setAntialias(SWT.ON);
      super.fillShape(g);
    }
  }

  class NeedleCenter extends Ellipse {

    public static final int DIAMETER = 16;

    @Override
    protected void fillShape(Graphics graphics) {
      graphics.setAntialias(SWT.ON);
      Pattern pattern = null;
      graphics.setBackgroundColor(GRAY_COLOR);
      boolean support3D = GraphicsUtil.testPatternSupported(graphics);
      if (effect3D && support3D) {
        // add this to eliminate the repaint bug on Mac
        graphics.fillOval(new Rectangle());

        pattern =
            new Pattern(
                Display.getCurrent(),
                bounds.x,
                bounds.y,
                bounds.x + bounds.width,
                bounds.y + bounds.height,
                WHITE_COLOR,
                BORDER_COLOR);
        graphics.setBackgroundPattern(pattern);
      }
      super.fillShape(graphics);
      if (effect3D && support3D) pattern.dispose();
    }
  }

  class GaugeLayout extends AbstractLayout {

    private static final int GAP_BTW_NEEDLE_SCALE = -1;

    /** Used as a constraint for the scale. */
    public static final String SCALE = "scale"; // $NON-NLS-1$
    /** Used as a constraint for the Needle. */
    public static final String NEEDLE = "needle"; // $NON-NLS-1$
    /** Used as a constraint for the Ramp */
    public static final String RAMP = "ramp"; // $NON-NLS-1$
    /** Used as a constraint for the needleCenter */
    public static final String NEEDLE_CENTER = "needleCenter"; // $NON-NLS-1$
    /** Used as a constraint for the value label */
    public static final String VALUE_LABEL = "valueLabel"; // $NON-NLS-1$

    private RoundScale scale;
    private RoundScaledRamp ramp;
    private Polygon needle;
    private NeedleCenter needleCenter;
    private Label valueLabel;
    private PointList needlePoints = new PointList(new int[] {0, 0, 0, 0, 0, 0});

    @Override
    public void setConstraint(IFigure child, Object constraint) {
      if (constraint.equals(SCALE)) scale = (RoundScale) child;
      else if (constraint.equals(RAMP)) ramp = (RoundScaledRamp) child;
      else if (constraint.equals(NEEDLE)) needle = (Polygon) child;
      else if (constraint.equals(NEEDLE_CENTER)) needleCenter = (NeedleCenter) child;
      else if (constraint.equals(VALUE_LABEL)) valueLabel = (Label) child;
    }

    @Override
    protected Dimension calculatePreferredSize(IFigure container, int w, int h) {
      Insets insets = container.getInsets();
      Dimension d = new Dimension(256, 256);
      d.expand(insets.getWidth(), insets.getHeight());
      return d;
    }

    public void layout(IFigure container) {
      Rectangle area = container.getClientArea();

      area.width = Math.min(area.width, area.height);
      area.height = area.width;
      area.shrink(BORDER_WIDTH, BORDER_WIDTH);

      Point center = area.getCenter();

      if (scale != null) {
        scale.setBounds(area);
      }

      if (ramp != null && ramp.isVisible()) {
        Rectangle rampBounds = area.getCopy();
        ramp.setBounds(rampBounds.shrink(area.width / 4, area.height / 4));
      }

      if (valueLabel != null) {
        Dimension labelSize = valueLabel.getPreferredSize();
        valueLabel.setBounds(
            new Rectangle(
                area.x + area.width / 2 - labelSize.width / 2,
                area.y + area.height * 7 / 8 - labelSize.height / 2,
                labelSize.width,
                labelSize.height));
      }

      if (needle != null && scale != null) {
        needlePoints.setPoint(new Point(center.x, center.y - NeedleCenter.DIAMETER / 2 + 3), 0);
        scale.getScaleTickMarks();
        needlePoints.setPoint(
            new Point(
                center.x
                    + area.width / 2
                    - RoundScaleTickMarks.MAJOR_TICK_LENGTH
                    - GAP_BTW_NEEDLE_SCALE,
                center.y),
            1);
        needlePoints.setPoint(new Point(center.x, center.y + NeedleCenter.DIAMETER / 2 - 3), 2);

        double valuePosition = 360 - scale.getValuePosition(getCoercedValue(), false);
        if (maximum > minimum) {
          if (value > maximum) valuePosition += 10;
          else if (value < minimum) valuePosition -= 10;
        } else {
          if (value > minimum) valuePosition -= 10;
          else if (value < maximum) valuePosition += 10;
        }
        needlePoints.setPoint(
            PointsUtil.rotate(needlePoints.getPoint(0), valuePosition, center), 0);
        needlePoints.setPoint(
            PointsUtil.rotate(needlePoints.getPoint(1), valuePosition, center), 1);
        needlePoints.setPoint(
            PointsUtil.rotate(needlePoints.getPoint(2), valuePosition, center), 2);
        needle.setPoints(needlePoints);
      }

      if (needleCenter != null) {
        needleCenter.setBounds(
            new Rectangle(
                center.x - NeedleCenter.DIAMETER / 2,
                center.y - NeedleCenter.DIAMETER / 2,
                NeedleCenter.DIAMETER,
                NeedleCenter.DIAMETER));
      }
    }
  }
}