⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menumanager.java

📁 jfa2ce 源码帮助开发人员更好的理解运用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: *     IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.jface.action;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.eclipse.core.runtime.ListenerList;import org.eclipse.swt.SWT;import org.eclipse.swt.events.MenuAdapter;import org.eclipse.swt.events.MenuEvent;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.CoolBar;import org.eclipse.swt.widgets.Decorations;import org.eclipse.swt.widgets.Menu;import org.eclipse.swt.widgets.MenuItem;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.ToolBar;/** * A menu manager is a contribution manager which realizes itself and its items * in a menu control; either as a menu bar, a sub-menu, or a context menu. * <p> * This class may be instantiated; it may also be subclassed. * </p> */public class MenuManager extends ContributionManager implements IMenuManager {    /**     * The menu id.     */    private String id;    /**     * List of registered menu listeners (element type: <code>IMenuListener</code>).     */    private ListenerList listeners = new ListenerList();    /**     * The menu control; <code>null</code> before     * creation and after disposal.     */    private Menu menu = null;    /**     * The menu item widget; <code>null</code> before     * creation and after disposal. This field is used     * when this menu manager is a sub-menu.     */    private MenuItem menuItem;    /**     * The text for a sub-menu.     */    private String menuText;    /**     * The overrides for items of this manager     */    private IContributionManagerOverrides overrides;    /**     * The parent contribution manager.     */    private IContributionManager parent;    /**     * Indicates whether <code>removeAll</code> should be     * called just before the menu is displayed.     */    private boolean removeAllWhenShown = false;    /**     * Indicates this item is visible in its manager; <code>true</code>      * by default.     */    private boolean visible = true;    /**     * Creates a menu manager.  The text and id are <code>null</code>.     * Typically used for creating a context menu, where it doesn't need to be referred to by id.     */    public MenuManager() {        this(null, null);    }    /**     * Creates a menu manager with the given text. The id of the menu     * is <code>null</code>.     * Typically used for creating a sub-menu, where it doesn't need to be referred to by id.     *     * @param text the text for the menu, or <code>null</code> if none     */    public MenuManager(String text) {        this(text, null);    }    /**     * Creates a menu manager with the given text and id.     * Typically used for creating a sub-menu, where it needs to be referred to by id.     *     * @param text the text for the menu, or <code>null</code> if none     * @param id the menu id, or <code>null</code> if it is to have no id     */    public MenuManager(String text, String id) {        this.menuText = text;        this.id = id;    }    /* (non-Javadoc)     * @see org.eclipse.jface.action.IMenuManager#addMenuListener(org.eclipse.jface.action.IMenuListener)     */    public void addMenuListener(IMenuListener listener) {        listeners.add(listener);    }    /**     * Creates and returns an SWT context menu control for this menu,     * and installs all registered contributions.     * Does not create a new control if one already exists.     * <p>     * Note that the menu is not expected to be dynamic.     * </p>     *     * @param parent the parent control     * @return the menu control     */    public Menu createContextMenu(Control parent) {        if (!menuExist()) {            menu = new Menu(parent);            initializeMenu();        }        return menu;    }    /**     * Creates and returns an SWT menu bar control for this menu,     * for use in the given <code>Decorations</code>, and installs all registered     * contributions. Does not create a new control if one already exists.     *     * @param parent the parent decorations     * @return the menu control     * @since 2.1     */    public Menu createMenuBar(Decorations parent) {        if (!menuExist()) {            menu = new Menu(parent, SWT.BAR);            update(false);        }        return menu;    }    /**     * Creates and returns an SWT menu bar control for this menu, for use in the     * given <code>Shell</code>, and installs all registered contributions. Does not     * create a new control if one already exists. This implementation simply calls     * the <code>createMenuBar(Decorations)</code> method     *     * @param parent the parent decorations     * @return the menu control     * @deprecated use <code>createMenuBar(Decorations)</code> instead.     */    public Menu createMenuBar(Shell parent) {        return createMenuBar((Decorations) parent);    }    /**     * Disposes of this menu manager and frees all allocated SWT resources.     * Notifies all contribution items of the dispose. Note that this method does     * not clean up references between this menu manager and its associated     * contribution items. Use <code>removeAll</code> for that purpose.     */    public void dispose() {        if (menuExist()) {			menu.dispose();		}        menu = null;        if (menuItem != null) {            menuItem.dispose();            menuItem = null;        }        IContributionItem[] items = getItems();        for (int i = 0; i < items.length; i++) {            items[i].dispose();        }    }    /* (non-Javadoc)     * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.Composite)     */    public void fill(Composite parent) {    }    /* (non-Javadoc)     * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.CoolBar, int)     */    public void fill(CoolBar parent, int index) {    }    /* (non-Javadoc)     * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.Menu, int)     */    public void fill(Menu parent, int index) {        if (menuItem == null || menuItem.isDisposed()) {            if (index >= 0) {				menuItem = new MenuItem(parent, SWT.CASCADE, index);			} else {				menuItem = new MenuItem(parent, SWT.CASCADE);			}            menuItem.setText(getMenuText());            if (!menuExist()) {				menu = new Menu(parent);			}            menuItem.setMenu(menu);            initializeMenu();            // populate the submenu, in order to enable accelerators            // and to set enabled state on the menuItem properly            update(true);        }    }    /* (non-Javadoc)     * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.ToolBar, int)     */    public void fill(ToolBar parent, int index) {    }    /* (non-Javadoc)     * @see org.eclipse.jface.action.IMenuManager#findMenuUsingPath(java.lang.String)     */    public IMenuManager findMenuUsingPath(String path) {        IContributionItem item = findUsingPath(path);        if (item instanceof IMenuManager) {			return (IMenuManager) item;		}        return null;    }    /* (non-Javadoc)     * @see org.eclipse.jface.action.IMenuManager#findUsingPath(java.lang.String)     */    public IContributionItem findUsingPath(String path) {        String id = path;        String rest = null;        int separator = path.indexOf('/');        if (separator != -1) {            id = path.substring(0, separator);            rest = path.substring(separator + 1);        } else {            return super.find(path);        }        IContributionItem item = super.find(id);        if (item instanceof IMenuManager) {            IMenuManager manager = (IMenuManager) item;            return manager.findUsingPath(rest);        }        return null;    }    /**     * Notifies any menu listeners that a menu is about to show.     * Only listeners registered at the time this method is called are notified.     *     * @param manager the menu manager     *     * @see IMenuListener#menuAboutToShow     */    private void fireAboutToShow(IMenuManager manager) {        Object[] listeners = this.listeners.getListeners();        for (int i = 0; i < listeners.length; ++i) {            ((IMenuListener) listeners[i]).menuAboutToShow(manager);        }    }    /**     * Notifies any menu listeners that a menu is about to hide.     * Only listeners registered at the time this method is called are notified.     *     * @param manager the menu manager     *     * @see IMenuListener#menuAboutToHide     */    private void fireAboutToHide(IMenuManager manager) {        final Object[] listeners = this.listeners.getListeners();        for (int i = 0; i < listeners.length; ++i) {        	final Object listener = listeners[i];			if (listener instanceof IMenuListener2) {				final IMenuListener2 listener2 = (IMenuListener2) listener;				listener2.menuAboutToHide(manager);			}        }    }    /**	 * Returns the menu id. The menu id is used when creating a contribution	 * item for adding this menu as a sub menu of another.	 * 	 * @return the menu id	 */    public String getId() {        return id;    }    /**     * Returns the SWT menu control for this menu manager.     *     * @return the menu control     */    public Menu getMenu() {        return menu;    }    /**     * Returns the text shown in the menu.     *     * @return the menu text     */    public String getMenuText() {        return menuText;    }    /* (non-Javadoc)     * @see org.eclipse.jface.action.IContributionManager#getOverrides()     */    public IContributionManagerOverrides getOverrides() {        if (overrides == null) {            if (parent == null) {                overrides = new IContributionManagerOverrides() {                    public Integer getAccelerator(IContributionItem item) {                        return null;                    }                    public String getAcceleratorText(IContributionItem item) {                        return null;                    }                    public Boolean getEnabled(IContributionItem item) {                        return null;                    }                    public String getText(IContributionItem item) {                        return null;                    }                };            } else {                overrides = parent.getOverrides();            }            super.setOverrides(overrides);        }        return overrides;    }    /**     * Returns the parent contribution manager of this manger.     *      * @return the parent contribution manager     * @since 2.0     */    public IContributionManager getParent() {        return parent;    }    /* (non-Javadoc)     * @see org.eclipse.jface.action.IMenuManager#getRemoveAllWhenShown()     */    public boolean getRemoveAllWhenShown() {        return removeAllWhenShown;    }    /**     * Notifies all listeners that this menu is about to appear.     */    private void handleAboutToShow() {        if (removeAllWhenShown) {			removeAll();		}        fireAboutToShow(this);        update(false, true);    }    /**     * Notifies all listeners that this menu is about to disappear.     */    private void handleAboutToHide() {        fireAboutToHide(this);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -