📄 menumanager.java
字号:
/** * Initializes the menu control. */ private void initializeMenu() { menu.addMenuListener(new MenuAdapter() { public void menuHidden(MenuEvent e) { // ApplicationWindow.resetDescription(e.widget); handleAboutToHide(); } public void menuShown(MenuEvent e) { handleAboutToShow(); } }); // Don't do an update(true) here, in case menu is never opened. // Always do it lazily in handleAboutToShow(). } /* (non-Javadoc) * @see org.eclipse.jface.action.IContributionItem#isDynamic() */ public boolean isDynamic() { return false; } /** * Returns whether this menu should be enabled or not. * Used to enable the menu item containing this menu when it is realized as a sub-menu. * <p> * The default implementation of this framework method * returns <code>true</code>. Subclasses may reimplement. * </p> * * @return <code>true</code> if enabled, and * <code>false</code> if disabled */ public boolean isEnabled() { return true; } /* (non-Javadoc) * @see org.eclipse.jface.action.IContributionItem#isGroupMarker() */ public boolean isGroupMarker() { return false; } /* (non-Javadoc) * @see org.eclipse.jface.action.IContributionItem#isSeparator() */ public boolean isSeparator() { return false; } /** * @deprecated this method is no longer a part of the * {@link org.eclipse.jface.action.IContributionItem} API. */ public boolean isSubstituteFor(IContributionItem item) { return this.equals(item); } /* (non-Javadoc) * @see org.eclipse.jface.action.IContributionItem#isVisible() */ public boolean isVisible() { if (!visible) { return false; // short circut calculations in this case } // menus arent visible if all of its children are invisible (or only contains visible separators). IContributionItem[] childItems = getItems(); boolean visibleChildren = false; for (int j = 0; j < childItems.length; j++) { if (childItems[j].isVisible() && !childItems[j].isSeparator()) { visibleChildren = true; break; } } return visibleChildren; } /** * The <code>MenuManager</code> implementation of this <code>ContributionManager</code> method * also propagates the dirty flag up the parent chain. * * @since 3.1 */ public void markDirty() { super.markDirty(); // Can't optimize by short-circuiting when the first dirty manager is encountered, // since non-visible children are not even processed. // That is, it's possible to have a dirty sub-menu under a non-dirty parent menu // even after the parent menu has been updated. // If items are added/removed in the sub-menu, we still need to propagate the dirty flag up, // even if the sub-menu is already dirty, since the result of isVisible() may change // due to the added/removed items. IContributionManager parent = getParent(); if (parent != null) { parent.markDirty(); } } /** * Returns whether the menu control is created * and not disposed. * * @return <code>true</code> if the control is created * and not disposed, <code>false</code> otherwise */ private boolean menuExist() { return menu != null && !menu.isDisposed(); } /* (non-Javadoc) * @see org.eclipse.jface.action.IMenuManager#removeMenuListener(org.eclipse.jface.action.IMenuListener) */ public void removeMenuListener(IMenuListener listener) { listeners.remove(listener); } /* (non-Javadoc) * @see org.eclipse.jface.action.IContributionItem#saveWidgetState() */ public void saveWidgetState() { } /** * Sets the overrides for this contribution manager * * @param newOverrides the overrides for the items of this manager * @since 2.0 */ public void setOverrides(IContributionManagerOverrides newOverrides) { overrides = newOverrides; super.setOverrides(overrides); } /* (non-Javadoc) * @see org.eclipse.jface.action.IContributionItem#setParent(org.eclipse.jface.action.IContributionManager) */ public void setParent(IContributionManager manager) { parent = manager; } /* (non-Javadoc) * @see org.eclipse.jface.action.IMenuManager#setRemoveAllWhenShown(boolean) */ public void setRemoveAllWhenShown(boolean removeAll) { this.removeAllWhenShown = removeAll; } /* (non-Javadoc) * @see org.eclipse.jface.action.IContributionItem#setVisible(boolean) */ public void setVisible(boolean visible) { this.visible = visible; } /* (non-Javadoc) * @see org.eclipse.jface.action.IContributionItem#update() */ public void update() { updateMenuItem(); } /** * The <code>MenuManager</code> implementation of this <code>IContributionManager</code> * updates this menu, but not any of its submenus. * * @see #updateAll */ public void update(boolean force) { update(force, false); } /** * Incrementally builds the menu from the contribution items. * This method leaves out double separators and separators in the first * or last position. * * @param force <code>true</code> means update even if not dirty, * and <code>false</code> for normal incremental updating * @param recursive <code>true</code> means recursively update * all submenus, and <code>false</code> means just this menu */ protected void update(boolean force, boolean recursive) { if (isDirty() || force) { if (menuExist()) { // clean contains all active items without double separators IContributionItem[] items = getItems(); List clean = new ArrayList(items.length); IContributionItem separator = null; for (int i = 0; i < items.length; ++i) { IContributionItem ci = items[i]; if (!ci.isVisible()) { continue; } if (ci.isSeparator()) { // delay creation until necessary // (handles both adjacent separators, and separator at end) separator = ci; } else { if (separator != null) { if (clean.size() > 0) { clean.add(separator); } separator = null; } clean.add(ci); } } // remove obsolete (removed or non active) MenuItem[] mi = menu.getItems(); for (int i = 0; i < mi.length; i++) { Object data = mi[i].getData(); if (data == null || !clean.contains(data)) { mi[i].dispose(); } else if (data instanceof IContributionItem && ((IContributionItem) data).isDynamic() && ((IContributionItem) data).isDirty()) { mi[i].dispose(); } } // add new mi = menu.getItems(); int srcIx = 0; int destIx = 0; for (Iterator e = clean.iterator(); e.hasNext();) { IContributionItem src = (IContributionItem) e.next(); IContributionItem dest; // get corresponding item in SWT widget if (srcIx < mi.length) { dest = (IContributionItem) mi[srcIx].getData(); } else { dest = null; } if (dest != null && src.equals(dest)) { srcIx++; destIx++; } else if (dest != null && dest.isSeparator() && src.isSeparator()) { mi[srcIx].setData(src); srcIx++; destIx++; } else { int start = menu.getItemCount(); src.fill(menu, destIx); int newItems = menu.getItemCount() - start; for (int i = 0; i < newItems; i++) { MenuItem item = menu.getItem(destIx++); item.setData(src); } } // May be we can optimize this call. If the menu has just // been created via the call src.fill(fMenuBar, destIx) then // the menu has already been updated with update(true) // (see MenuManager). So if force is true we do it again. But // we can't set force to false since then information for the // sub sub menus is lost. if (recursive) { IContributionItem item = src; if (item instanceof SubContributionItem) { item = ((SubContributionItem) item).getInnerItem(); } if (item instanceof IMenuManager) { ((IMenuManager) item).updateAll(force); } } } // remove any old menu items not accounted for for (; srcIx < mi.length; srcIx++) { mi[srcIx].dispose(); } setDirty(false); } } else { // I am not dirty. Check if I must recursivly walk down the hierarchy. if (recursive) { IContributionItem[] items = getItems(); for (int i = 0; i < items.length; ++i) { IContributionItem ci = items[i]; if (ci instanceof IMenuManager) { IMenuManager mm = (IMenuManager) ci; if (mm.isVisible()) { mm.updateAll(force); } } } } } updateMenuItem(); } /* (non-Javadoc) * @see org.eclipse.jface.action.IContributionItem#update(java.lang.String) */ public void update(String property) { IContributionItem items[] = getItems(); for (int i = 0; i < items.length; i++) { items[i].update(property); } if (menu != null && !menu.isDisposed() && menu.getParentItem() != null && IAction.TEXT.equals(property)) { String text = getOverrides().getText(this); if (text == null) { text = getMenuText(); } if (text != null) { ExternalActionManager.ICallback callback = ExternalActionManager .getInstance().getCallback(); if (callback != null) { int index = text.indexOf('&'); if (index >= 0 && index < text.length() - 1) { char character = Character.toUpperCase(text .charAt(index + 1)); if (callback.isAcceleratorInUse(SWT.ALT | character)) { if (index == 0) { text = text.substring(1); } else { text = text.substring(0, index) + text.substring(index + 1); } } } } menu.getParentItem().setText(text); } } } /* (non-Javadoc) * @see org.eclipse.jface.action.IMenuManager#updateAll(boolean) */ public void updateAll(boolean force) { update(force, true); } /** * Updates the menu item for this sub menu. * The menu item is disabled if this sub menu is empty. * Does nothing if this menu is not a submenu. */ private void updateMenuItem() { /* * Commented out until proper solution to enablement of * menu item for a sub-menu is found. See bug 30833 for * more details. * if (menuItem != null && !menuItem.isDisposed() && menuExist()) { IContributionItem items[] = getItems(); boolean enabled = false; for (int i = 0; i < items.length; i++) { IContributionItem item = items[i]; enabled = item.isEnabled(); if(enabled) break; } // Workaround for 1GDDCN2: SWT:Linux - MenuItem.setEnabled() always causes a redraw if (menuItem.getEnabled() != enabled) menuItem.setEnabled(enabled); } */ // Partial fix for bug #34969 - diable the menu item if no // items in sub-menu (for context menus). if (menuItem != null && !menuItem.isDisposed() && menuExist()) { boolean enabled = menu.getItemCount() > 0; // Workaround for 1GDDCN2: SWT:Linux - MenuItem.setEnabled() always causes a redraw if (menuItem.getEnabled() != enabled) { // We only do this for context menus (for bug #34969) Menu topMenu = menu; while (topMenu.getParentMenu() != null) { topMenu = topMenu.getParentMenu(); } if ((topMenu.getStyle() & SWT.BAR) == 0) { menuItem.setEnabled(enabled); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -