private void handleWidgetSelection(Event e, ToolItem item) {

    boolean selection = item.getSelection();

    int style = item.getStyle();
    IAction action = (IAction) actionMap.get(item);

    if ((style & (SWT.TOGGLE | SWT.CHECK)) != 0) {
      if (action.getStyle() == IAction.AS_CHECK_BOX) {
        action.setChecked(selection);
      }
    } else if ((style & SWT.RADIO) != 0) {
      if (action.getStyle() == IAction.AS_RADIO_BUTTON) {
        action.setChecked(selection);
      }
    } else if ((style & SWT.DROP_DOWN) != 0) {
      if (e.detail == 4) { // on drop-down button
        if (action.getStyle() == IAction.AS_DROP_DOWN_MENU) {
          IMenuCreator mc = action.getMenuCreator();
          ToolItem ti = (ToolItem) item;
          if (mc != null) {
            Menu m = mc.getMenu(ti.getParent());
            if (m != null) {
              Point point = ti.getParent().toDisplay(new Point(e.x, e.y));
              m.setLocation(point.x, point.y); // waiting
              m.setVisible(true);
              return; // we don't fire the action
            }
          }
        }
      }
    }

    action.runWithEvent(e);
  }
 @Override
 public Node item(int index) {
   ToolItem item = getToolItem();
   if ((item.getStyle() & SWT.SEPARATOR) == SWT.SEPARATOR) {
     Object control = item.getControl();
     if (control != null) {
       return getElement(control);
     }
   }
   return null;
 }
 boolean hasOnlySeparators(ToolBar toolbar) {
   ToolItem[] children = toolbar.getItems();
   for (ToolItem toolItem : children) {
     if ((toolItem.getStyle() & SWT.SEPARATOR) == 0) {
       return false;
     } else if (toolItem.getControl() != null
         && toolItem.getControl().getData(OWNING_ME) instanceof MToolControl) {
       return false;
     }
   }
   return true;
 }
Example #4
0
 boolean isTabGroup() {
   ToolItem[] tabList = parent._getTabItemList();
   if (tabList != null) {
     for (int i = 0; i < tabList.length; i++) {
       if (tabList[i] == this) return true;
     }
   }
   if ((style & SWT.SEPARATOR) != 0) return true;
   int index = parent.indexOf(this);
   if (index == 0) return true;
   ToolItem previous = parent.getItem(index - 1);
   return (previous.getStyle() & SWT.SEPARATOR) != 0;
 }
  /**
   * 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;
  }
 @Override
 public int getLength() {
   ToolItem item = getToolItem();
   return (item.getStyle() & SWT.SEPARATOR) == SWT.SEPARATOR && item.getControl() != null ? 1 : 0;
 }