Пример #1
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;
 }
 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;
 }
 /**
  * @param menuManager
  * @param element
  * @param evalContext
  */
 public static void updateVisibility(
     MenuManager menuManager, MMenuElement element, ExpressionContext evalContext) {
   if (!(element.getVisibleWhen() instanceof MCoreExpression)) {
     return;
   }
   boolean val =
       ContributionsAnalyzer.isVisible((MCoreExpression) element.getVisibleWhen(), evalContext);
   if (val != element.isVisible()) {
     element.setVisible(val);
     menuManager.markDirty();
   }
 }
 public void handleEvent(Event event) {
   Object element = event.getProperty(UIEvents.EventTags.ELEMENT);
   String attName = (String) event.getProperty(UIEvents.EventTags.ATTNAME);
   if (element instanceof MMenuItem) {
     MMenuItem itemModel = (MMenuItem) element;
     if (UIEvents.UIElement.TOBERENDERED.equals(attName)) {
       Object obj = itemModel.getParent();
       if (!(obj instanceof MMenu)) {
         return;
       }
       MenuManager parent = getManager((MMenu) obj);
       if (itemModel.isToBeRendered()) {
         if (parent != null) {
           modelProcessSwitch(parent, itemModel);
         }
       } else {
         IContributionItem ici = getContribution(itemModel);
         clearModelToContribution(itemModel, ici);
         if (ici != null && parent != null) {
           parent.remove(ici);
         }
         if (ici != null) {
           ici.dispose();
         }
       }
     }
   }
   if (UIEvents.UIElement.VISIBLE.equals(attName)) {
     if (element instanceof MMenu) {
       MMenu menuModel = (MMenu) element;
       MenuManager manager = getManager(menuModel);
       if (manager == null) {
         return;
       }
       manager.setVisible(menuModel.isVisible());
       if (manager.getParent() != null) {
         manager.getParent().markDirty();
       }
     } else if (element instanceof MMenuElement) {
       MMenuElement itemModel = (MMenuElement) element;
       Object obj = getContribution(itemModel);
       if (!(obj instanceof ContributionItem)) {
         return;
       }
       ContributionItem item = (ContributionItem) obj;
       item.setVisible(itemModel.isVisible());
       if (item.getParent() != null) {
         item.getParent().markDirty();
       }
     }
   }
 }
 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 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;
 }
Пример #7
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;
            }
          }
        }
      }
    }
  }
 private static void printElement(int level, MMenuElement element) {
   StringBuilder buf = new StringBuilder();
   for (int i = 0; i < level; i++) {
     buf.append('\t');
   }
   buf.append(element.toString());
   trace(buf.toString(), null);
   if (element instanceof MMenu) {
     for (MMenuElement item : ((MMenu) element).getChildren()) {
       printElement(level + 1, item);
     }
   }
 }
  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);
    }
  }
 private void addToManager(
     MenuManager parentManager, MMenuElement model, IContributionItem menuManager) {
   MElementContainer<MUIElement> parent = model.getParent();
   // technically this shouldn't happen
   if (parent == null) {
     parentManager.add(menuManager);
   } else {
     int index = parent.getChildren().indexOf(model);
     // shouldn't be -1, but better safe than sorry
     if (index > parentManager.getSize() || index == -1) {
       parentManager.add(menuManager);
     } else {
       parentManager.insert(index, menuManager);
     }
   }
 }
 /**
  * @param menuManager
  * @param childME
  */
 void modelProcessSwitch(MenuManager menuManager, MMenuElement childME) {
   if (!childME.isToBeRendered()) {
     return;
   }
   if (childME instanceof MRenderedMenuItem) {
     MRenderedMenuItem itemModel = (MRenderedMenuItem) childME;
     processRenderedItem(menuManager, itemModel);
   } else if (childME instanceof MHandledMenuItem) {
     MHandledMenuItem itemModel = (MHandledMenuItem) childME;
     processHandledItem(menuManager, itemModel);
   } else if (childME instanceof MDirectMenuItem) {
     MDirectMenuItem itemModel = (MDirectMenuItem) childME;
     processDirectItem(menuManager, itemModel, null);
   } else if (childME instanceof MMenuSeparator) {
     MMenuSeparator sep = (MMenuSeparator) childME;
     processSeparator(menuManager, sep);
     // } else if (childME instanceof MOpaqueMenu) {
     // I'm not sure what to do here
     // so I'll just take it out of the running
   } else if (childME instanceof MMenu) {
     MMenu itemModel = (MMenu) childME;
     processMenu(menuManager, itemModel);
   }
 }
Пример #13
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);
    }
  }