private void addToggle(Toggle toggle) {
    final ToolItem button = new ToolItem(mLayoutToolBar, SWT.CHECK);

    URL iconUrl = toggle.getIconUrl();
    String title = toggle.getTitle();
    if (iconUrl != null) {
      button.setImage(IconFactory.getInstance().getIcon(iconUrl));
      button.setToolTipText(title);
    } else {
      button.setText(title);
    }
    button.setData(toggle);

    button.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            Toggle toggle = (Toggle) button.getData();
            toggle
                .getCallback()
                .action(toggle, getSelectedNodes(), toggle.getId(), button.getSelection());
            updateSelection();
          }
        });
    if (toggle.isChecked()) {
      button.setSelection(true);
    }
  }
  /**
   * Attempts to update the existing toolbar actions, if the action list is similar to the current
   * list. Returns false if this cannot be done and the contents must be replaced.
   */
  private boolean updateActions(@NonNull List<RuleAction> actions) {
    List<RuleAction> before = mPrevActions;
    List<RuleAction> after = actions;

    if (before == null) {
      return false;
    }

    if (!before.equals(after) || after.size() > mLayoutToolBar.getItemCount()) {
      return false;
    }

    int actionIndex = 0;
    for (int i = 0, max = mLayoutToolBar.getItemCount(); i < max; i++) {
      ToolItem item = mLayoutToolBar.getItem(i);
      int style = item.getStyle();
      Object data = item.getData();
      if (data != null) {
        // One action can result in multiple toolbar items (e.g. a choice action
        // can result in multiple radio buttons), so we've have to replace all of
        // them with the corresponding new action
        RuleAction prevAction = before.get(actionIndex);
        while (prevAction != data) {
          actionIndex++;
          if (actionIndex == before.size()) {
            return false;
          }
          prevAction = before.get(actionIndex);
          if (prevAction == data) {
            break;
          } else if (!(prevAction instanceof RuleAction.Separator)) {
            return false;
          }
        }
        RuleAction newAction = after.get(actionIndex);
        assert newAction.equals(prevAction); // Maybe I can do this lazily instead?

        // Update action binding to the new action
        item.setData(newAction);

        // Sync button states: the checked state is not considered part of
        // RuleAction equality
        if ((style & SWT.CHECK) != 0) {
          assert newAction instanceof Toggle;
          Toggle toggle = (Toggle) newAction;
          item.setSelection(toggle.isChecked());
        } else if ((style & SWT.RADIO) != 0) {
          assert newAction instanceof Choices;
          Choices choices = (Choices) newAction;
          String current = choices.getCurrent();
          String id = (String) item.getData(ATTR_ID);
          boolean selected = Strings.nullToEmpty(current).equals(id);
          item.setSelection(selected);
        }
      } else {
        // Must be a separator, or a label (which we insert for nested widgets)
        assert (style & SWT.SEPARATOR) != 0 || !item.getText().isEmpty() : item;
      }
    }

    return true;
  }