public static boolean processAddition(
     final MToolBar toolBarModel,
     MToolBarContribution toolBarContribution,
     List<MToolBarElement> contributions,
     HashSet<String> existingSeparatorNames) {
   int idx = getIndex(toolBarModel, toolBarContribution.getPositionInParent());
   if (idx == -1) {
     return false;
   }
   for (MToolBarElement item : toolBarContribution.getChildren()) {
     if (item instanceof MToolBarSeparator
         && existingSeparatorNames.contains(item.getElementId())) {
       // skip this, it's already there
       continue;
     }
     MToolBarElement copy = (MToolBarElement) EcoreUtil.copy((EObject) item);
     if (DEBUG) {
       trace(
           "addToolBarContribution " + copy,
           toolBarModel.getWidget(),
           toolBarModel); //$NON-NLS-1$
     }
     toolBarModel.getChildren().add(idx++, copy);
     contributions.add(copy);
     if (copy instanceof MToolBarSeparator && copy.getElementId() != null) {
       existingSeparatorNames.add(copy.getElementId());
     }
   }
   return true;
 }
  public static void addToolBarContributions(
      final MToolBar toolbarModel,
      ArrayList<MToolBarContribution> toContribute,
      IEclipseContext ctx,
      final ExpressionContext eContext,
      HashMap<MToolBar, ArrayList<ArrayList<MToolBarElement>>> pendingCleanup) {
    HashSet<String> existingSeparatorNames = new HashSet<String>();
    for (MToolBarElement child : toolbarModel.getChildren()) {
      String elementId = child.getElementId();
      if (child instanceof MToolBarSeparator && elementId != null) {
        existingSeparatorNames.add(elementId);
      }
    }
    boolean done = toContribute.size() == 0;
    while (!done) {
      ArrayList<MToolBarContribution> curList = new ArrayList<MToolBarContribution>(toContribute);
      int retryCount = toContribute.size();
      toContribute.clear();

      for (final MToolBarContribution contribution : curList) {
        final ArrayList<MToolBarElement> toRemove = new ArrayList<MToolBarElement>();
        if (!ContributionsAnalyzer.processAddition(
            toolbarModel, contribution, toRemove, existingSeparatorNames)) {
          toContribute.add(contribution);
        } else {
          if (contribution.getVisibleWhen() != null) {
            ctx.runAndTrack(
                new RunAndTrack() {
                  @Override
                  public boolean changed(IEclipseContext context) {
                    if (!toolbarModel.isToBeRendered()
                        || !toolbarModel.isVisible()
                        || toolbarModel.getWidget() == null) {
                      return false;
                    }
                    boolean rc = ContributionsAnalyzer.isVisible(contribution, eContext);
                    for (MToolBarElement child : toRemove) {
                      child.setToBeRendered(rc);
                    }
                    return true;
                  }
                });
          }
          ArrayList<ArrayList<MToolBarElement>> lists = pendingCleanup.get(toolbarModel);
          if (lists == null) {
            lists = new ArrayList<ArrayList<MToolBarElement>>();
            pendingCleanup.put(toolbarModel, lists);
          }
          lists.add(toRemove);
        }
      }
      // We're done if the retryList is now empty (everything done) or
      // if the list hasn't changed at all (no hope)
      done = (toContribute.size() == 0) || (toContribute.size() == retryCount);
    }
  }
 private static boolean containsMatching(List<MToolBarElement> children, MToolBarElement me) {
   for (MToolBarElement element : children) {
     if (Util.equals(me.getElementId(), element.getElementId())
         && element.getClass().isInstance(me)
         && (element instanceof MToolBarSeparator || element instanceof MToolBar)) {
       return true;
     }
   }
   return false;
 }
 public boolean anyVisibleWhen() {
   if (toolbarContribution.getVisibleWhen() != null) {
     return true;
   }
   for (MToolBarElement child : toolbarContribution.getChildren()) {
     if (child.getVisibleWhen() != null) {
       return true;
     }
   }
   return false;
 }
 /** @param context */
 public void updateVisibility(IEclipseContext context) {
   ExpressionContext exprContext = new ExpressionContext(context);
   updateIsVisible(exprContext);
   HashSet<ToolBarContributionRecord> recentlyUpdated = new HashSet<ToolBarContributionRecord>();
   recentlyUpdated.add(this);
   for (MToolBarElement item : generatedElements) {
     boolean currentVisibility = computeVisibility(recentlyUpdated, item, exprContext);
     if (item.isVisible() != currentVisibility) {
       item.setVisible(currentVisibility);
     }
   }
 }
        public void handleEvent(Event event) {
          // Ensure that this event is for a MMenuItem
          if (!(event.getProperty(UIEvents.EventTags.ELEMENT) instanceof MToolBarElement)) return;

          MToolBarElement itemModel =
              (MToolBarElement) event.getProperty(UIEvents.EventTags.ELEMENT);
          String attName = (String) event.getProperty(UIEvents.EventTags.ATTNAME);
          if (UIEvents.UIElement.TOBERENDERED.equals(attName)) {
            Object obj = itemModel.getParent();
            if (!(obj instanceof MToolBar)) {
              return;
            }
            ToolBarManager parent = getManager((MToolBar) obj);
            if (itemModel.isToBeRendered()) {
              if (parent != null) {
                modelProcessSwitch(parent, itemModel);
                parent.update(true);
                ToolBar tb = parent.getControl();
                if (tb != null && !tb.isDisposed()) {
                  tb.pack(true);
                  tb.getShell().layout(new Control[] {tb}, SWT.DEFER);
                }
              }
            } else {
              IContributionItem ici = modelToContribution.remove(itemModel);
              if (ici != null && parent != null) {
                parent.remove(ici);
              }
              if (ici != null) {
                ici.dispose();
              }
            }
          } else if (UIEvents.UIElement.VISIBLE.equals(attName)) {
            IContributionItem ici = getContribution(itemModel);
            if (ici == null) {
              return;
            }
            ici.setVisible(itemModel.isVisible());
            ToolBarManager parent = (ToolBarManager) ((ContributionItem) ici).getParent();
            if (parent != null) {
              parent.markDirty();
              parent.update(true);
              // MUIElement tbModel = itemModel.getParent();
              // disposeToolbarIfNecessary((MToolBar) tbModel);
              ToolBar tb = parent.getControl();
              if (tb != null && !tb.isDisposed()) {
                tb.pack(true);
                tb.getShell().layout(new Control[] {tb}, SWT.DEFER);
              }
            }
          }
        }
  public boolean computeVisibility(
      HashSet<ToolBarContributionRecord> recentlyUpdated,
      MToolBarElement item,
      ExpressionContext exprContext) {
    boolean currentVisibility = isVisible;

    if (currentVisibility && item.getVisibleWhen() instanceof MCoreExpression) {
      boolean val =
          ContributionsAnalyzer.isVisible((MCoreExpression) item.getVisibleWhen(), exprContext);
      currentVisibility = val;
    }
    return currentVisibility;
  }
Example #8
0
  private void restorePersistedState(MApplication application, MPart part) {

    final Map<String, String> persistedState = application.getPersistedState();
    showSuppressedLines =
        toolbox.parseBoolean(persistedState.get(IPersistenceKey.TERMINAL_SUPPRESS_LINES));
    showGrblStateLines =
        toolbox.parseBoolean(persistedState.get(IPersistenceKey.TERMINAL_GRBL_STATE));
    showGcodeModeLines =
        toolbox.parseBoolean(persistedState.get(IPersistenceKey.TERMINAL_GRBL_MODES));

    // set the state of the direct menu items according to persisted state
    // find the two direct menu items
    final MToolBar toolbar = part.getToolbar();
    List<MToolBarElement> toolBarChildren = toolbar.getChildren();
    for (MToolBarElement child : toolBarChildren) {
      if (child instanceof MHandledToolItem
          && child
              .getElementId()
              .equals("de.jungierek.grblrunner.handledtoolitem.terminal.togglesuppresslines")) {
        LOG.debug(
            "restorePersistedState: child=" + child.getElementId() + " class=" + child.getClass());
        MMenu menu = ((MHandledToolItem) child).getMenu();
        if (menu != null) {
          List<MMenuElement> items = menu.getChildren();
          for (MMenuElement item : items) {
            LOG.debug(
                "restorePersistedState: item=" + item.getElementId() + "class=" + child.getClass());
            switch (item.getElementId()) {
              case "de.jungierek.grblrunner.directmenuitem.togglesuppresslines.grblstate":
                ((MMenuItem) item).setSelected(showGrblStateLines);
                break;
              case "de.jungierek.grblrunner.directmenuitem.togglesuppresslines.gcodestate":
                ((MMenuItem) item).setSelected(showGcodeModeLines);
                break;
              default:
                break;
            }
          }
        }
      }
    }
  }
  public boolean mergeIntoModel() {
    int idx = getIndex(toolbarModel, toolbarContribution.getPositionInParent());
    if (idx == -1) {
      return false;
    }

    final List<MToolBarElement> copyElements = new ArrayList<MToolBarElement>();
    for (MToolBarElement item : toolbarContribution.getChildren()) {
      MToolBarElement copy = (MToolBarElement) EcoreUtil.copy((EObject) item);
      copyElements.add(copy);
    }

    for (MToolBarElement copy : copyElements) {
      // if a visibleWhen clause is defined, the item should not be
      // visible until the clause has been evaluated and returned 'true'
      copy.setVisible(!anyVisibleWhen());
      generatedElements.add(copy);
      toolbarModel.getChildren().add(idx++, copy);
    }
    return true;
  }
 /**
  * @param parentManager
  * @param itemModel
  * @param ci
  */
 private void addToManager(
     ToolBarManager parentManager, MToolBarElement model, IContributionItem ci) {
   MElementContainer<MUIElement> parent = model.getParent();
   // technically this shouldn't happen
   if (parent == null) {
     parentManager.add(ci);
   } else {
     int index = parent.getChildren().indexOf(model);
     // shouldn't be -1, but better safe than sorry
     if (index > parentManager.getSize() || index == -1) {
       parentManager.add(ci);
     } else {
       parentManager.insert(index, ci);
     }
   }
 }