protected void pauseJob() {

    final StatusBean bean = getSelection();
    if (bean == null) return;

    if (bean.getStatus().isFinal()) {
      MessageDialog.openInformation(
          getViewSite().getShell(),
          "Run '" + bean.getName() + "' inactive",
          "Run '" + bean.getName() + "' is inactive and cannot be paused.");
      return;
    }

    try {
      if (bean.getStatus().isPaused()) {
        bean.setStatus(org.eclipse.scanning.api.event.status.Status.REQUEST_RESUME);
        bean.setMessage("Resume of " + bean.getName());
      } else {
        bean.setStatus(org.eclipse.scanning.api.event.status.Status.REQUEST_PAUSE);
        bean.setMessage("Pause of " + bean.getName());
      }

      IPublisher<StatusBean> terminate = service.createPublisher(getUri(), getTopicName());
      terminate.broadcast(bean);

    } catch (Exception e) {
      ErrorDialog.openError(
          getViewSite().getShell(),
          "Cannot pause " + bean.getName(),
          "Cannot pause " + bean.getName() + "\n\nPlease contact your support representative.",
          new Status(IStatus.ERROR, "org.eclipse.scanning.event.ui", e.getMessage()));
    }
  }
  protected void togglePausedConsumer(IAction pauseConsumer) {

    // The button can get out of sync if two clients are used.
    final boolean currentState = queueConnection.isQueuePaused(getSubmissionQueueName());
    try {
      pauseConsumer.setChecked(!currentState); // We are toggling it.

      IPublisher<PauseBean> pauser = service.createPublisher(getUri(), IEventService.CMD_TOPIC);
      pauser.setStatusSetName(IEventService.CMD_SET); // The set that other clients may check
      pauser.setStatusSetAddRequired(true);

      PauseBean pbean = new PauseBean();
      pbean.setQueueName(getSubmissionQueueName()); // The queue we are pausing
      pbean.setPause(pauseConsumer.isChecked());
      pauser.broadcast(pbean);

    } catch (Exception e) {
      ErrorDialog.openError(
          getViewSite().getShell(),
          "Cannot pause queue " + getSubmissionQueueName(),
          "Cannot pause queue "
              + getSubmissionQueueName()
              + "\n\nPlease contact your support representative.",
          new Status(IStatus.ERROR, "org.eclipse.scanning.event.ui", e.getMessage()));
    }
    pauseConsumer.setChecked(queueConnection.isQueuePaused(getSubmissionQueueName()));
  }
  protected void stopJob() {

    final StatusBean bean = getSelection();
    if (bean == null) return;

    if (!bean.getStatus().isActive()) {

      String queueName = null;

      if (bean.getStatus() != org.eclipse.scanning.api.event.status.Status.SUBMITTED) {
        queueName = getQueueName();
        boolean ok =
            MessageDialog.openQuestion(
                getSite().getShell(),
                "Confirm Remove '" + bean.getName() + "'",
                "Are you sure you would like to remove '" + bean.getName() + "'?");
        if (!ok) return;
      } else {
        // Submitted delete it right away without asking or the consumer will run it!
        queueName = getSubmissionQueueName();
      }

      // It is submitted and not running. We can probably delete it.
      try {
        queueConnection.remove(bean, queueName);
        refresh();
      } catch (EventException e) {
        ErrorDialog.openError(
            getViewSite().getShell(),
            "Cannot delete " + bean.getName(),
            "Cannot delete "
                + bean.getName()
                + "\n\nIt might have changed state at the same time and being remoted.",
            new Status(IStatus.ERROR, "org.eclipse.scanning.event.ui", e.getMessage()));
      }
      return;
    }

    try {

      final DateFormat format = DateFormat.getDateTimeInstance();
      boolean ok =
          MessageDialog.openQuestion(
              getViewSite().getShell(),
              "Confirm terminate " + bean.getName(),
              "Are you sure you want to terminate "
                  + bean.getName()
                  + " submitted on "
                  + format.format(new Date(bean.getSubmissionTime()))
                  + "?");

      if (!ok) return;

      bean.setStatus(org.eclipse.scanning.api.event.status.Status.REQUEST_TERMINATE);
      bean.setMessage("Termination of " + bean.getName());

      IPublisher<StatusBean> terminate = service.createPublisher(getUri(), getTopicName());
      terminate.broadcast(bean);

    } catch (Exception e) {
      ErrorDialog.openError(
          getViewSite().getShell(),
          "Cannot terminate " + bean.getName(),
          "Cannot terminate " + bean.getName() + "\n\nPlease contact your support representative.",
          new Status(IStatus.ERROR, "org.eclipse.scanning.event.ui", e.getMessage()));
    }
  }