@Override
        public void widgetSelected(SelectionEvent event) {
          MenuItem selected = null;

          if (event.widget instanceof MenuItem) {
            selected = (MenuItem) event.widget;
          } else return;

          try {
            if (selected.equals(spinOn)) {
              spin.on();
              logger.info("Switch ON beam monitor.");
            } else if (selected.equals(spinOff)) {
              spin.off();
              logger.info("Switch OFF beam monitor.");
            }
          } catch (DeviceException e) {
            logger.error("Failed to control spin", e);
          }
        }
  public SpinStatusComposite(
      Composite parent, int style, final Display display, String label, ISpin spin) {
    super(parent, style);

    GridDataFactory.fillDefaults().applyTo(this);
    GridLayoutFactory.swtDefaults().numColumns(1).applyTo(this);

    Group grp = new Group(this, style);
    GridDataFactory.fillDefaults().applyTo(grp);
    GridLayoutFactory.swtDefaults().numColumns(1).applyTo(grp);
    grp.setText(label);

    this.display = display;
    GridLayoutFactory.swtDefaults().numColumns(1).applyTo(this);
    GridDataFactory.fillDefaults().applyTo(this);

    this.spin = spin;

    currentColor = SPIN_OFF_COLOR;

    try {
      if (spin.getState().equalsIgnoreCase("enabled")) {
        currentColor = SPIN_ON_COLOR;
      } else {
        currentColor = SPIN_OFF_COLOR;
      }
    } catch (DeviceException e1) {
      logger.error("Failed to get spin status", e1);
    }

    canvas = new Canvas(grp, SWT.NONE);
    GridData gridData = new GridData(GridData.VERTICAL_ALIGN_FILL);
    gridData.widthHint = 40;
    gridData.heightHint = 40;
    canvas.setLayoutData(gridData);
    canvas.addPaintListener(
        new PaintListener() {
          @Override
          public void paintControl(PaintEvent e) {
            GC gc = e.gc;
            gc.setAntialias(SWT.ON);
            gc.setBackground(currentColor);
            gc.setLineWidth(1);
            Rectangle clientArea = canvas.getClientArea();
            final int margin = 4;
            final Point topLeft = new Point(margin, margin);
            final Point size =
                new Point(clientArea.width - margin * 2, clientArea.height - margin * 2);
            gc.fillOval(topLeft.x, topLeft.y, size.x, size.y);
            gc.drawOval(topLeft.x, topLeft.y, size.x, size.y);
          }
        });
    canvas.setMenu(createPopup(this));
    // initialize tooltip
    try {
      if (spin.getState().equalsIgnoreCase("enabled")) {
        canvas.setToolTipText(SPIN_ON_TOOL_TIP);
        spinOn.setSelection(true);
      } else {
        canvas.setToolTipText(SPIN_OFF_TOOL_TIP);
        spinOff.setSelection(true);
      }
    } catch (DeviceException e1) {
      logger.error("Failed to get spin status", e1);
    }

    final IObserver spinObserver =
        new IObserver() {
          @Override
          public void update(final Object theObserved, final Object changeCode) {
            Display.getDefault()
                .asyncExec(
                    new Runnable() {
                      @Override
                      public void run() {
                        String value = "";
                        if (theObserved instanceof ISpin) {
                          if (changeCode instanceof String) {
                            value = ((String) changeCode);
                            if (value.equalsIgnoreCase("Enabled")) {
                              currentColor = SPIN_ON_COLOR;
                              canvas.setToolTipText(SPIN_ON_TOOL_TIP);
                              spinOn.setSelection(true);
                            } else {
                              currentColor = SPIN_OFF_COLOR;
                              canvas.setToolTipText(SPIN_OFF_TOOL_TIP);
                              spinOff.setSelection(true);
                            }
                          }
                        }
                        updateBatonCanvas();
                      }
                    });
          }
        };
    spin.addIObserver(spinObserver);
  }