ChartControl.java

  1. package de.slothsoft.charts.swt;

  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.events.PaintEvent;
  4. import org.eclipse.swt.graphics.Rectangle;
  5. import org.eclipse.swt.widgets.Canvas;
  6. import org.eclipse.swt.widgets.Composite;

  7. import de.slothsoft.charts.Chart;
  8. import de.slothsoft.charts.PaintInstructions;
  9. import de.slothsoft.charts.RefreshListener;

  10. /**
  11.  * A SWT control displaying a {@link Chart} and hooking it to the SWT framework.
  12.  *
  13.  * @author Stef Schulz
  14.  * @since 0.1.0
  15.  */

  16. public class ChartControl extends Canvas {

  17.     private Chart model;
  18.     private SwtGraphicContext graphicContext;

  19.     private final RefreshListener refreshListener = e -> redraw();

  20.     /**
  21.      * Constructs a new instance of this class given its parentand a style value
  22.      * describing its behavior and appearance.
  23.      *
  24.      * @param parent a composite control which will be the parent of the new instance
  25.      *            (cannot be null)
  26.      * @param style the style of control to construct
  27.      */

  28.     public ChartControl(Composite parent, int style) {
  29.         super(parent, style | SWT.DOUBLE_BUFFERED);
  30.         addPaintListener(this::paintControl);
  31.         addDisposeListener(e -> dispose());
  32.     }

  33.     private void paintControl(PaintEvent e) {
  34.         final Rectangle rect = getClientArea();
  35.         e.gc.setAntialias(SWT.ON);

  36.         if (this.model != null) {
  37.             if (this.graphicContext == null) {
  38.                 this.graphicContext = new SwtGraphicContext(e.gc);
  39.             } else {
  40.                 this.graphicContext.delegate(e.gc);
  41.             }

  42.             final PaintInstructions instructions = new PaintInstructions(
  43.                     new de.slothsoft.charts.Area(rect.x, rect.y, rect.width, rect.height));
  44.             this.model.paintOn(this.graphicContext, instructions);
  45.         }
  46.     }

  47.     /**
  48.      * Returns the {@link Chart} that gets displayed by this control.
  49.      *
  50.      * @return the chart
  51.      */

  52.     public Chart getModel() {
  53.         return this.model;
  54.     }

  55.     /**
  56.      * Sets the {@link Chart} that gets displayed by this control.
  57.      *
  58.      * @param newModel the chart
  59.      * @return this instance
  60.      */

  61.     public ChartControl model(Chart newModel) {
  62.         setModel(newModel);
  63.         return this;
  64.     }

  65.     /**
  66.      * Sets the {@link Chart} that gets displayed by this control.
  67.      *
  68.      * @param model the chart
  69.      */

  70.     public void setModel(Chart model) {
  71.         if (this.model != null) {
  72.             this.model.removeRefreshListener(this.refreshListener);
  73.         }
  74.         this.model = model;
  75.         if (this.model != null) {
  76.             this.model.addRefreshListener(this.refreshListener);
  77.         }
  78.         redraw();
  79.     }

  80.     @Override
  81.     public void dispose() {
  82.         if (this.graphicContext != null) {
  83.             this.graphicContext.dispose();
  84.         }
  85.         if (this.model != null) {
  86.             this.model.removeRefreshListener(this.refreshListener);
  87.         }
  88.         super.dispose();
  89.     }

  90. }