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

📄 toolbarcontributionitem.java

📁 jfa2ce 源码帮助开发人员更好的理解运用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            return false;        }        return useChevron;    }    /**     * Create and display the chevron menu.     */    private void handleChevron(SelectionEvent event) {        CoolItem item = (CoolItem) event.widget;        Control control = item.getControl();        if ((control instanceof ToolBar) == false) {            return;        }        CoolBar coolBar = item.getParent();        ToolBar toolBar = (ToolBar) control;        Rectangle toolBarBounds = toolBar.getBounds();        ToolItem[] items = toolBar.getItems();        ArrayList hidden = new ArrayList();        for (int i = 0; i < items.length; ++i) {            Rectangle itemBounds = items[i].getBounds();            if (!((itemBounds.x + itemBounds.width <= toolBarBounds.width) && (itemBounds.y                    + itemBounds.height <= toolBarBounds.height))) {                hidden.add(items[i]);            }        }        // Create a pop-up menu with items for each of the hidden buttons.        if (chevronMenuManager != null) {            chevronMenuManager.dispose();        }        chevronMenuManager = new MenuManager();        for (Iterator i = hidden.iterator(); i.hasNext();) {            ToolItem toolItem = (ToolItem) i.next();            IContributionItem data = (IContributionItem) toolItem.getData();            if (data instanceof ActionContributionItem) {                ActionContributionItem contribution = new ActionContributionItem(                        ((ActionContributionItem) data).getAction());                chevronMenuManager.add(contribution);            } else if (data instanceof SubContributionItem) {                IContributionItem innerData = ((SubContributionItem) data)                        .getInnerItem();                if (innerData instanceof ActionContributionItem) {                    ActionContributionItem contribution = new ActionContributionItem(                            ((ActionContributionItem) innerData).getAction());                    chevronMenuManager.add(contribution);                }            } else if (data.isSeparator()) {                chevronMenuManager.add(new Separator());            }        }        Menu popup = chevronMenuManager.createContextMenu(coolBar);        Point chevronPosition = coolBar.toDisplay(event.x, event.y);        popup.setLocation(chevronPosition.x, chevronPosition.y);        popup.setVisible(true);    }    /**     * Handles the event when the toobar item does not have its own context     * menu.     *      * @param event     *            the event object     */    private void handleContextMenu(Event event) {        ToolBar toolBar = toolBarManager.getControl();        // If parent has a menu then use that one        Menu parentMenu = toolBar.getParent().getMenu();        if ((parentMenu != null) && (!parentMenu.isDisposed())) {            toolBar.setMenu(parentMenu);            // Hook listener to remove menu once it has disapeared            parentMenu.addListener(SWT.Hide, new Listener() {                public void handleEvent(Event innerEvent) {                    ToolBar innerToolBar = toolBarManager.getControl();                    if (innerToolBar != null) {                        innerToolBar.setMenu(null);                        Menu innerParentMenu = innerToolBar.getParent()                                .getMenu();                        if (innerParentMenu != null) {                            innerParentMenu.removeListener(SWT.Hide, this);                        }                    }                }            });        }    }    /**     * Handles the disposal of the widget.     *      * @param event     *            the event object     */    private void handleWidgetDispose(DisposeEvent event) {        coolItem = null;    }    /**     * A contribution item is visible iff its internal state is visible <em>or</em>     * the tool bar manager contains something other than group markers and     * separators.     *      * @return <code>true</code> if the tool bar manager contains something     *         other than group marks and separators, and the internal state is     *         set to be visible.     */    public boolean isVisible() {        if (checkDisposed()) {            return false;        }        boolean visibleItem = false;        if (toolBarManager != null) {            IContributionItem[] contributionItems = toolBarManager.getItems();            for (int i = 0; i < contributionItems.length; i++) {                IContributionItem contributionItem = contributionItems[i];                if ((!contributionItem.isGroupMarker())                        && (!contributionItem.isSeparator())) {                    visibleItem = true;                    break;                }            }        }        return (visibleItem || super.isVisible());    }    /*     * (non-Javadoc)     *      * @see org.eclipse.jface.action.IContributionItem#saveWidgetState()     */    public void saveWidgetState() {        if (checkDisposed()) {            return;        }        if (coolItem == null) {			return;		}        //1. Save current size        CoolBar coolBar = coolItem.getParent();        boolean isLastOnRow = false;        int lastIndex = coolBar.getItemCount() - 1;        int coolItemIndex = coolBar.indexOf(coolItem);        int[] wrapIndicies = getAdjustedWrapIndices(coolBar.getWrapIndices());        // Traverse through all wrap indicies backwards        for (int row = wrapIndicies.length - 1; row >= 0; row--) {            if (wrapIndicies[row] <= coolItemIndex) {                int nextRow = row + 1;                int nextRowStartIndex;                if (nextRow > (wrapIndicies.length - 1)) {                    nextRowStartIndex = lastIndex + 1;                } else {                    nextRowStartIndex = wrapIndicies[nextRow];                }                // Check to see if its the last item on the row                if (coolItemIndex == (nextRowStartIndex - 1)) {                    isLastOnRow = true;                }                break;            }        }        // Save the preferred size as actual size for the last item on a row        int nCurrentWidth;        if (isLastOnRow) {            nCurrentWidth = coolItem.getPreferredSize().x;        } else {            nCurrentWidth = coolItem.getSize().x;        }        setCurrentWidth(nCurrentWidth);        setCurrentHeight(coolItem.getSize().y);    }    /**     * Sets the current height of the cool item. Update(SIZE) should be called     * to adjust the widget.     *      * @param currentHeight     *            the current height to set     */    public void setCurrentHeight(int currentHeight) {        if (checkDisposed()) {            return;        }        this.currentHeight = currentHeight;    }    /**     * Sets the current width of the cool item. Update(SIZE) should be called     * to adjust the widget.     *      * @param currentWidth     *            the current width to set     */    public void setCurrentWidth(int currentWidth) {        if (checkDisposed()) {            return;        }        this.currentWidth = currentWidth;    }    /**     * Sets the minimum number of tool items to show in the cool item. If this     * number is less than the total tool items, a chevron will appear and the     * hidden tool items appear in a drop down menu. By default, all the tool     * items are shown in the cool item.     *      * @param minimumItemsToShow     *            the minimum number of tool items to show.     * @see #getMinimumItemsToShow()     * @see #setUseChevron(boolean)     */    public void setMinimumItemsToShow(int minimumItemsToShow) {        if (checkDisposed()) {            return;        }        this.minimumItemsToShow = minimumItemsToShow;    }    /**     * Enables or disables chevron support for the cool item. By default,     * chevron support is enabled.     *      * @param value     *            <code>true</code> to enable chevron support, <code>false</code>     *            otherwise.     */    public void setUseChevron(boolean value) {        if (checkDisposed()) {            return;        }        useChevron = value;    }    /*     * (non-Javadoc)     *      * @see org.eclipse.jface.action.IContributionItem#update(java.lang.String)     */    public void update(String propertyName) {        if (checkDisposed()) {            return;        }        if (coolItem != null) {            IToolBarManager manager = getToolBarManager();            if (manager != null) {                manager.update(true);            }            if ((propertyName == null)                    || propertyName.equals(ICoolBarManager.SIZE)) {                updateSize(true);            }        }    }    /**     * Updates the cool items' preferred, minimum, and current size. The     * preferred size is calculated based on the tool bar size and extra trim.     *      * @param changeCurrentSize     *            <code>true</code> if the current size should be changed to     *            the preferred size, <code>false</code> to not change the     *            current size     */    private void updateSize(boolean changeCurrentSize) {        if (checkDisposed()) {            return;        }        // cannot set size if coolItem is null        if (coolItem == null || coolItem.isDisposed()) {            return;        }        boolean locked = false;        CoolBar coolBar = coolItem.getParent();        try {            // Fix odd behaviour with locked tool bars            if (coolBar != null) {                if (coolBar.getLocked()) {                    coolBar.setLocked(false);                    locked = true;                }            }            ToolBar toolBar = (ToolBar) coolItem.getControl();            if ((toolBar == null) || (toolBar.isDisposed())                    || (toolBar.getItemCount() <= 0)) {                // if the toolbar does not contain any items then dispose of                // coolItem                coolItem.setData(null);                Control control = coolItem.getControl();                if ((control != null) && !control.isDisposed()) {                    control.dispose();                    coolItem.setControl(null);                }                if (!coolItem.isDisposed()) {                    coolItem.dispose();                }            } else {                // If the toolbar item exists then adjust the size of the cool                // item                Point toolBarSize = toolBar.computeSize(SWT.DEFAULT,                        SWT.DEFAULT);                // Set the preffered size to the size of the toolbar plus trim                Point preferredSize = coolItem.computeSize(toolBarSize.x,                        toolBarSize.y);                coolItem.setPreferredSize(preferredSize);                // note setMinimumSize must be called before setSize, see PR                // 15565                // Set minimum size                if (getMinimumItemsToShow() != SHOW_ALL_ITEMS) {                    int toolItemWidth = toolBar.getItems()[0].getWidth();                    int minimumWidth = toolItemWidth * getMinimumItemsToShow();                    coolItem.setMinimumSize(minimumWidth, toolBarSize.y);                } else {                    coolItem.setMinimumSize(toolBarSize.x, toolBarSize.y);                }                if (changeCurrentSize) {                    // Set current size to preferred size                    coolItem.setSize(preferredSize);                }            }        } finally {            // If the cool bar was locked, then set it back to locked            if ((locked) && (coolBar != null)) {                coolBar.setLocked(true);            }        }    }}

⌨️ 快捷键说明

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