private static boolean containsMatching(List<MMenuElement> children, MMenuElement me) {
   for (MMenuElement element : children) {
     if (Util.equals(me.getElementId(), element.getElementId())
         && element.getClass().isInstance(me)
         && (element instanceof MMenuSeparator || element instanceof MMenu)) {
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 2
0
 private MHandledMenuItem findMenuItem(final String inItemID, final MMenu inMenu) {
   inMenu.getChildren();
   for (final MMenuElement lItem : inMenu.getChildren()) {
     if (inItemID.equals(lItem.getElementId())) {
       return (MHandledMenuItem) lItem;
     }
   }
   return null;
 }
Ejemplo n.º 3
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 static boolean processAddition(
     final MMenu menuModel,
     final ArrayList<MMenuElement> menuContributionsToRemove,
     MMenuContribution menuContribution,
     final HashSet<String> existingMenuIds,
     HashSet<String> existingSeparatorNames) {
   int idx = getIndex(menuModel, menuContribution.getPositionInParent());
   if (idx == -1) {
     return false;
   }
   for (MMenuElement item : menuContribution.getChildren()) {
     if (item instanceof MMenu && existingMenuIds.contains(item.getElementId())) {
       // skip this, it's already there
       continue;
     } else if (item instanceof MMenuSeparator
         && existingSeparatorNames.contains(item.getElementId())) {
       // skip this, it's already there
       continue;
     }
     MMenuElement copy = (MMenuElement) EcoreUtil.copy((EObject) item);
     if (DEBUG) {
       trace("addMenuContribution " + copy, menuModel.getWidget(), menuModel); // $NON-NLS-1$
     }
     menuContributionsToRemove.add(copy);
     menuModel.getChildren().add(idx++, copy);
     if (copy instanceof MMenu && copy.getElementId() != null) {
       existingMenuIds.add(copy.getElementId());
     } else if (copy instanceof MMenuSeparator && copy.getElementId() != null) {
       existingSeparatorNames.add(copy.getElementId());
     }
   }
   return true;
 }
 public static int indexForId(MElementContainer<MMenuElement> parentMenu, String id) {
   if (id == null || id.length() == 0) {
     return -1;
   }
   int i = 0;
   for (MMenuElement item : parentMenu.getChildren()) {
     if (id.equals(item.getElementId())) {
       return i;
     }
     i++;
   }
   return -1;
 }
  public static void addMenuContributions(
      final MMenu menuModel,
      final ArrayList<MMenuContribution> toContribute,
      final ArrayList<MMenuElement> menuContributionsToRemove) {

    HashSet<String> existingMenuIds = new HashSet<String>();
    HashSet<String> existingSeparatorNames = new HashSet<String>();
    for (MMenuElement child : menuModel.getChildren()) {
      String elementId = child.getElementId();
      if (child instanceof MMenu && elementId != null) {
        existingMenuIds.add(elementId);
      } else if (child instanceof MMenuSeparator && elementId != null) {
        existingSeparatorNames.add(elementId);
      }
    }

    boolean done = toContribute.size() == 0;
    while (!done) {
      ArrayList<MMenuContribution> curList = new ArrayList<MMenuContribution>(toContribute);
      int retryCount = toContribute.size();
      toContribute.clear();

      for (MMenuContribution menuContribution : curList) {
        if (!processAddition(
            menuModel,
            menuContributionsToRemove,
            menuContribution,
            existingMenuIds,
            existingSeparatorNames)) {
          toContribute.add(menuContribution);
        }
      }
      // 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);
    }
  }
  /**
   * @param menuModel
   * @param toContribute
   */
  private void generateContributions(
      MMenu menuModel, ArrayList<MMenuContribution> toContribute, boolean menuBar) {
    HashSet<String> existingMenuIds = new HashSet<String>();
    HashSet<String> existingSeparatorNames = new HashSet<String>();
    for (MMenuElement child : menuModel.getChildren()) {
      String elementId = child.getElementId();
      if (child instanceof MMenu && elementId != null) {
        existingMenuIds.add(elementId);
      } else if (child instanceof MMenuSeparator && elementId != null) {
        existingSeparatorNames.add(elementId);
      }
    }

    MenuManager manager = getManager(menuModel);
    boolean done = toContribute.size() == 0;
    while (!done) {
      ArrayList<MMenuContribution> curList = new ArrayList<MMenuContribution>(toContribute);
      int retryCount = toContribute.size();
      toContribute.clear();

      for (MMenuContribution menuContribution : curList) {
        if (!processAddition(
            menuModel,
            manager,
            menuContribution,
            existingMenuIds,
            existingSeparatorNames,
            menuBar)) {
          toContribute.add(menuContribution);
        }
      }

      // 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);
    }
  }
Ejemplo n.º 8
0
  // this is similar in nature to:
  // org.eclipse.e4.ui.internal.workbench.ContributionsAnalyzer.addMenuContributions(MMenu,
  // ArrayList<MMenuContribution>, ArrayList<MMenuElement>)
  // the difference is it needs to add all the contributions and manage their
  // visiblility through a RAT
  private void addMenuBarContributions(
      final MMenu menuModel,
      ArrayList<MMenuContribution> toContribute,
      final IEclipseContext ctx,
      final ExpressionContext eContext) {
    HashSet<String> existingMenuIds = new HashSet<String>();
    HashSet<String> existingSeparatorNames = new HashSet<String>();
    for (MMenuElement child : menuModel.getChildren()) {
      String elementId = child.getElementId();
      if (child instanceof MMenu && elementId != null) {
        existingMenuIds.add(elementId);
      } else if (child instanceof MMenuSeparator && elementId != null) {
        existingSeparatorNames.add(elementId);
      }
    }

    boolean done = toContribute.size() == 0;
    while (!done) {
      ArrayList<MMenuContribution> curList = new ArrayList<MMenuContribution>(toContribute);
      int retryCount = toContribute.size();
      toContribute.clear();

      for (final MMenuContribution menuContribution : curList) {
        final ArrayList<MMenuElement> menuContributionsToRemove = new ArrayList<MMenuElement>();
        if (!ContributionsAnalyzer.processAddition(
            menuModel,
            menuContributionsToRemove,
            menuContribution,
            existingMenuIds,
            existingSeparatorNames)) {
          toContribute.add(menuContribution);
        } else {
          if (menuContribution.getVisibleWhen() != null) {
            ctx.runAndTrack(
                new RunAndTrack() {
                  @Override
                  public boolean changed(IEclipseContext context) {
                    if (!menuModel.isToBeRendered()
                        || !menuModel.isVisible()
                        || menuModel.getWidget() == null) {
                      return false;
                    }
                    boolean rc = ContributionsAnalyzer.isVisible(menuContribution, eContext);
                    for (MMenuElement element : menuContributionsToRemove) {
                      element.setToBeRendered(rc);
                    }
                    return true;
                  }
                });
          }
          ArrayList<ArrayList<MMenuElement>> lists = pendingCleanup.get(menuModel);
          if (lists == null) {
            lists = new ArrayList<ArrayList<MMenuElement>>();
            pendingCleanup.put(menuModel, lists);
          }
          lists.add(menuContributionsToRemove);
        }
      }
      // 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);
    }
  }