public void testCreateMenu() {
    final MWindow window = createWindowWithOneViewAndMenu();
    wb = new E4Workbench(window, appContext);

    Widget topWidget = (Widget) window.getWidget();
    assertTrue(topWidget instanceof Shell);
    Shell shell = (Shell) topWidget;
    final Menu menuBar = shell.getMenuBar();
    assertNotNull(menuBar);
    assertEquals(1, menuBar.getItemCount());
    final MenuItem fileItem = menuBar.getItem(0);
    assertEquals("File", fileItem.getText());
    final Menu fileMenu = fileItem.getMenu();
    fileMenu.notifyListeners(SWT.Show, null);
    assertEquals(2, fileMenu.getItemCount());
    fileMenu.notifyListeners(SWT.Hide, null);

    MMenu mainMenu = window.getMainMenu();
    MMenu modelFileMenu = (MMenu) mainMenu.getChildren().get(0);
    final MMenuItem item2Model = (MMenuItem) modelFileMenu.getChildren().get(0);
    item2Model.setToBeRendered(false);
    fileMenu.notifyListeners(SWT.Show, null);
    assertEquals(1, fileMenu.getItemCount());
    fileMenu.notifyListeners(SWT.Hide, null);

    item2Model.setToBeRendered(true);
    fileMenu.notifyListeners(SWT.Show, null);
    assertEquals(2, fileMenu.getItemCount());
    fileMenu.notifyListeners(SWT.Hide, null);
  }
 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;
 }
 /**
  * Unlink all contribution items from the given model menu.
  *
  * @param renderer the renderer that is holding the links
  * @param menu the model menu whose children should have its items unlinked from their
  *     corresponding contribution items
  */
 private void unlink(MenuManagerRenderer renderer, MMenu menu) {
   for (MMenuElement menuElement : menu.getChildren()) {
     if (menuElement instanceof MOpaqueMenuItem) {
       MOpaqueMenuItem opaqueMenuItem = (MOpaqueMenuItem) menuElement;
       Object item = opaqueMenuItem.getOpaqueItem();
       if (item instanceof IContributionItem) {
         renderer.clearModelToContribution(opaqueMenuItem, (IContributionItem) item);
         opaqueMenuItem.setOpaqueItem(null);
       }
     } else if (menuElement instanceof MOpaqueMenuSeparator) {
       MOpaqueMenuSeparator opaqueMenuItem = (MOpaqueMenuSeparator) menuElement;
       Object item = opaqueMenuItem.getOpaqueItem();
       if (item instanceof IContributionItem) {
         renderer.clearModelToContribution(opaqueMenuItem, (IContributionItem) item);
         opaqueMenuItem.setOpaqueItem(null);
       }
     } else if (menuElement instanceof MMenu) {
       MMenu subMenu = (MMenu) menuElement;
       unlink(renderer, subMenu);
       MenuManager manager = renderer.getManager(subMenu);
       if (manager != null) {
         renderer.clearModelToManager(subMenu, manager);
       }
     } else {
       IContributionItem contribution = renderer.getContribution(menuElement);
       if (contribution != null) {
         renderer.clearModelToContribution(menuElement, contribution);
       }
     }
   }
 }
 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;
 }
 void cleanUp(MMenu menuModel) {
   ArrayList<ArrayList<MMenuElement>> lists = pendingCleanup.remove(menuModel);
   if (lists == null) {
     return;
   }
   for (ArrayList<MMenuElement> list : lists) {
     for (MMenuElement item : list) {
       menuModel.getChildren().remove(item);
     }
   }
 }
  private MWindow createWindowWithOneViewAndMenu() {
    final MWindow window = createWindowWithOneView();
    final MMenu menuBar = MenuFactoryImpl.eINSTANCE.createMenu();
    window.setMainMenu(menuBar);
    final MMenu fileMenu = MenuFactoryImpl.eINSTANCE.createMenu();
    fileMenu.setLabel("File");
    fileMenu.setElementId("file");
    menuBar.getChildren().add(fileMenu);

    final MMenuItem item1 = MenuFactoryImpl.eINSTANCE.createDirectMenuItem();
    item1.setElementId("item1");
    item1.setLabel("item1");
    fileMenu.getChildren().add(item1);
    final MMenuItem item2 = MenuFactoryImpl.eINSTANCE.createDirectMenuItem();
    item2.setElementId("item2");
    item2.setLabel("item2");
    fileMenu.getChildren().add(item2);

    return window;
  }
 private void redirectHandledMenuItems(MMenu menu) {
   if (menu == null) {
     return;
   }
   for (MMenuElement elmt : menu.getChildren()) {
     if (elmt instanceof MMenu) {
       redirectHandledMenuItems((MMenu) elmt);
     } else if (elmt instanceof MMenuItem) {
       redirectHandledMenuItem((MMenuItem) elmt);
     }
   }
 }
  @SuppressWarnings("restriction")
  @Execute
  public void execute(
      MApplication app, EModelService service, IExtensionRegistry registery, IThemeManager mgr) {

    // sanity check
    if (menu == null) {
      return;
    }

    List<String> tags = app.getTags();
    for (String tag : tags) {
      if (PROCESSOR_ID.equals(tag)) {
        return; // already processed
      }
    }

    tags.add(PROCESSOR_ID);

    IThemeEngine engine = mgr.getEngineForDisplay(Display.getCurrent());

    List<ITheme> themes = engine.getThemes();

    MCommand switchThemeCommand = ThemeHelper.findCommand(app);

    // no themes or command, stop processing
    if (themes.size() <= 0 || switchThemeCommand == null) {
      return;
    }

    themesMenu = service.createModelElement(MMenu.class);
    themesMenu.setLabel("%switchThemeMenu"); // $NON-NLS-1$
    themesMenu.setContributorURI(BUNDLE_ID);

    for (ITheme theme : themes) {
      if (!theme.getId().startsWith("org.eclipse.e4.demo.contacts.")) {
        return;
      }
      MParameter parameter = service.createModelElement(MParameter.class);
      parameter.setName("contacts.commands.switchtheme.themeid"); // $NON-NLS-1$
      parameter.setValue(theme.getId());
      String iconURI = ThemeHelper.getCSSUri(theme.getId(), registery);
      if (iconURI != null) {
        iconURI = iconURI.replace(".css", ".png");
      }
      processTheme(theme.getLabel(), switchThemeCommand, parameter, iconURI, service);
    }
    menu.getChildren().add(themesMenu);
  }
 protected void processTheme(
     String name,
     MCommand switchCommand,
     MParameter themeId,
     String iconURI,
     EModelService service) {
   MHandledMenuItem menuItem = service.createModelElement(MHandledMenuItem.class);
   menuItem.setLabel(name);
   menuItem.setCommand(switchCommand);
   menuItem.getParameters().add(themeId);
   menuItem.setContributorURI(BUNDLE_ID);
   if (iconURI != null) {
     menuItem.setIconURI(iconURI);
   }
   themesMenu.getChildren().add(menuItem);
 }
 @PostConstruct
 public void init() {
   itemMap = new HashMap<>();
   MMenu menu =
       (MMenu)
           modelService
               .findElements(
                   application,
                   "at.bestsolution.dart.app.menu.editorfeatures",
                   null,
                   null,
                   EModelService.ANYWHERE | EModelService.IN_MAIN_MENU)
               .get(0);
   menu.getChildren().clear();
   menu.getChildren()
       .addAll(Stream.of(Feature.values()).map(this::createFeature).collect(Collectors.toList()));
   updateFeatureSet();
 }
 /**
  * @param menuModel
  * @param isMenuBar
  * @param isPopup
  */
 public void processContributions(MMenu menuModel, boolean isMenuBar, boolean isPopup) {
   if (menuModel.getElementId() == null) {
     return;
   }
   final ArrayList<MMenuContribution> toContribute = new ArrayList<MMenuContribution>();
   ContributionsAnalyzer.XXXgatherMenuContributions(
       menuModel,
       application.getMenuContributions(),
       menuModel.getElementId(),
       toContribute,
       null,
       isPopup);
   generateContributions(menuModel, toContribute, isMenuBar);
   for (MMenuElement element : menuModel.getChildren()) {
     if (element instanceof MMenu) {
       processContributions((MMenu) element, false, isPopup);
     }
   }
 }
Exemple #12
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;
            }
          }
        }
      }
    }
  }
 /**
  * @param parentManager
  * @param menuModel
  */
 private void processMenu(MenuManager parentManager, MMenu menuModel) {
   MenuManager menuManager = getManager(menuModel);
   if (menuManager == null) {
     String menuText = getText(menuModel);
     ImageDescriptor desc = getImageDescriptor(menuModel);
     menuManager = new MenuManager(menuText, desc, menuModel.getElementId());
     linkModelToManager(menuModel, menuManager);
     menuManager.setVisible(menuModel.isVisible());
     addToManager(parentManager, menuModel, menuManager);
   }
   // processContributions(menuModel, false);
   List<MMenuElement> parts = menuModel.getChildren();
   if (parts != null) {
     MMenuElement[] plist = parts.toArray(new MMenuElement[parts.size()]);
     for (int i = 0; i < plist.length; i++) {
       MMenuElement childME = plist[i];
       modelProcessSwitch(menuManager, childME);
     }
   }
 }
 /**
  * Find the action with the given ID by recursively crawling the provided menu manager. If the
  * action cannot be found <code>null</code> is returned.
  *
  * @param actionId the id to search for
  * @param menu the menu to search
  * @return the action or <code>null</code>
  */
 private MMenuItem findAction(String actionId, MMenu menu) {
   if (menu == null) {
     return null;
   }
   for (MMenuElement item : menu.getChildren()) {
     if (item instanceof MMenuItem) {
       MMenuItem mmi = (MMenuItem) item;
       if (mmi.getElementId() != null && mmi.getElementId().equals(actionId)) return mmi;
       if (mmi instanceof MHandledMenuItem) {
         MHandledMenuItem mhmi = (MHandledMenuItem) mmi;
         if (mhmi.getCommand() != null && actionId.equals(mhmi.getCommand().getElementId())) {
           return mmi;
         }
       }
     } else if (item instanceof MMenu) {
       MMenuItem found = findAction(actionId, (MMenu) item);
       if (found != null) return found;
     }
   }
   return null;
 }
  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);
    }
  }
  // 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);
    }
  }
  /**
   * @param menuManager
   * @param menuModel
   */
  public void reconcileManagerToModel(MenuManager menuManager, MMenu menuModel) {
    List<MMenuElement> modelChildren = menuModel.getChildren();

    HashSet<MOpaqueMenuItem> oldModelItems = new HashSet<MOpaqueMenuItem>();
    HashSet<MOpaqueMenu> oldMenus = new HashSet<MOpaqueMenu>();
    HashSet<MOpaqueMenuSeparator> oldSeps = new HashSet<MOpaqueMenuSeparator>();
    for (MMenuElement itemModel : modelChildren) {
      if (itemModel instanceof MOpaqueMenuSeparator) {
        oldSeps.add((MOpaqueMenuSeparator) itemModel);
      } else if (itemModel instanceof MOpaqueMenuItem) {
        oldModelItems.add((MOpaqueMenuItem) itemModel);
      } else if (itemModel instanceof MOpaqueMenu) {
        oldMenus.add((MOpaqueMenu) itemModel);
      }
    }

    IContributionItem[] items = menuManager.getItems();
    for (int src = 0, dest = 0; src < items.length; src++, dest++) {
      IContributionItem item = items[src];
      if (item instanceof MenuManager) {
        MenuManager childManager = (MenuManager) item;
        MMenu childModel = getMenuModel(childManager);
        if (childModel == null) {
          MMenu legacyModel = MenuFactoryImpl.eINSTANCE.createOpaqueMenu();
          legacyModel.setElementId(childManager.getId());
          legacyModel.setVisible(childManager.isVisible());
          linkModelToManager(legacyModel, childManager);
          modelChildren.add(dest, legacyModel);
          reconcileManagerToModel(childManager, legacyModel);
        } else {
          if (childModel instanceof MOpaqueMenu) {
            oldMenus.remove(childModel);
          }
          if (modelChildren.size() > dest) {
            if (modelChildren.get(dest) != childModel) {
              modelChildren.remove(childModel);
              modelChildren.add(dest, childModel);
            }
          } else {
            modelChildren.add(childModel);
          }
        }
      } else if (item.isSeparator() || item.isGroupMarker()) {
        MMenuElement menuElement = getMenuElement(item);
        if (menuElement == null) {
          MOpaqueMenuSeparator legacySep = MenuFactoryImpl.eINSTANCE.createOpaqueMenuSeparator();
          legacySep.setElementId(item.getId());
          legacySep.setVisible(item.isVisible());
          legacySep.setOpaqueItem(item);
          linkModelToContribution(legacySep, item);
          modelChildren.add(dest, legacySep);
        } else if (menuElement instanceof MOpaqueMenuSeparator) {
          MOpaqueMenuSeparator legacySep = (MOpaqueMenuSeparator) menuElement;
          oldSeps.remove(legacySep);
          if (modelChildren.size() > dest) {
            if (modelChildren.get(dest) != legacySep) {
              modelChildren.remove(legacySep);
              modelChildren.add(dest, legacySep);
            }
          } else {
            modelChildren.add(legacySep);
          }
        }
      } else {
        MMenuElement menuElement = getMenuElement(item);
        if (menuElement == null) {
          MOpaqueMenuItem legacyItem = MenuFactoryImpl.eINSTANCE.createOpaqueMenuItem();
          legacyItem.setElementId(item.getId());
          legacyItem.setVisible(item.isVisible());
          legacyItem.setOpaqueItem(item);
          linkModelToContribution(legacyItem, item);
          modelChildren.add(dest, legacyItem);
        } else if (menuElement instanceof MOpaqueMenuItem) {
          MOpaqueMenuItem legacyItem = (MOpaqueMenuItem) menuElement;
          oldModelItems.remove(legacyItem);
          if (modelChildren.size() > dest) {
            if (modelChildren.get(dest) != legacyItem) {
              modelChildren.remove(legacyItem);
              modelChildren.add(dest, legacyItem);
            }
          } else {
            modelChildren.add(legacyItem);
          }
        }
      }
    }
    if (!oldModelItems.isEmpty()) {
      modelChildren.removeAll(oldModelItems);
      for (MOpaqueMenuItem model : oldModelItems) {
        clearModelToContribution(model, (IContributionItem) model.getOpaqueItem());
      }
    }
    if (!oldMenus.isEmpty()) {
      modelChildren.removeAll(oldMenus);
      for (MOpaqueMenu oldMenu : oldMenus) {
        MenuManager oldManager = getManager(oldMenu);
        clearModelToManager(oldMenu, oldManager);
      }
    }
    if (!oldSeps.isEmpty()) {
      modelChildren.removeAll(oldSeps);
      for (MOpaqueMenuSeparator model : oldSeps) {
        clearModelToContribution(model, (IContributionItem) model.getOpaqueItem());
      }
    }
  }
 void removeMenuContributions(
     final MMenu menuModel, final ArrayList<MMenuElement> menuContributionsToRemove) {
   for (MMenuElement item : menuContributionsToRemove) {
     menuModel.getChildren().remove(item);
   }
 }