📄 actioncontributionitem.java
字号:
return action; } /** * Returns the listener for SWT button widget events. * * @return a listener for button events */ private Listener getButtonListener() { if (buttonListener == null) { buttonListener = new Listener() { public void handleEvent(Event event) { switch (event.type) { case SWT.Dispose: handleWidgetDispose(event); break; case SWT.Selection: Widget ew = event.widget; if (ew != null) { handleWidgetSelection(event, ((Button) ew) .getSelection()); } break; } } }; } return buttonListener; } /** * Returns the listener for SWT menu item widget events. * * @return a listener for menu item events */ private Listener getMenuItemListener() { if (menuItemListener == null) { menuItemListener = new Listener() { public void handleEvent(Event event) { switch (event.type) { case SWT.Dispose: handleWidgetDispose(event); break; case SWT.Selection: Widget ew = event.widget; if (ew != null) { handleWidgetSelection(event, ((MenuItem) ew) .getSelection()); } break; } } }; } return menuItemListener; } /** * Returns the presentation mode, which is the bitwise-or of the * <code>MODE_*</code> constants. The default mode setting is 0, meaning * that for menu items, both text and image are shown (if present), but for * tool items, the text is shown only if there is no image. * * @return the presentation mode settings * * @since 3.0 */ public int getMode() { return mode; } /** * Returns the listener for SWT tool item widget events. * * @return a listener for tool item events */ private Listener getToolItemListener() { if (toolItemListener == null) { toolItemListener = new Listener() { public void handleEvent(Event event) { switch (event.type) { case SWT.Dispose: handleWidgetDispose(event); break; case SWT.Selection: Widget ew = event.widget; if (ew != null) { handleWidgetSelection(event, ((ToolItem) ew) .getSelection()); } break; } } }; } return toolItemListener; } /** * Handles a widget dispose event for the widget corresponding to this item. */ private void handleWidgetDispose(Event e) { // Check if our widget is the one being disposed. if (e.widget == widget) { // Dispose of the menu creator. if (action.getStyle() == IAction.AS_DROP_DOWN_MENU) { IMenuCreator mc = action.getMenuCreator(); if (mc != null) { mc.dispose(); } } // Unhook all of the listeners. action.removePropertyChangeListener(propertyListener); if (action != null) { String commandId = action.getActionDefinitionId(); ExternalActionManager.ICallback callback = ExternalActionManager .getInstance().getCallback(); if ((callback != null) && (commandId != null)) { callback.removePropertyChangeListener(commandId, actionTextListener); } } // Clear the widget field. widget = null; disposeOldImages(); } } /** * Handles a widget selection event. */ private void handleWidgetSelection(Event e, boolean selection) { Widget item = e.widget; if (item != null) { int style = item.getStyle(); if ((style & (SWT.TOGGLE | SWT.CHECK)) != 0) { if (action.getStyle() == IAction.AS_CHECK_BOX) { action.setChecked(selection); } } else if ((style & SWT.RADIO) != 0) { if (action.getStyle() == IAction.AS_RADIO_BUTTON) { action.setChecked(selection); } } else if ((style & SWT.DROP_DOWN) != 0) { if (e.detail == 4) { // on drop-down button if (action.getStyle() == IAction.AS_DROP_DOWN_MENU) { IMenuCreator mc = action.getMenuCreator(); ToolItem ti = (ToolItem) item; // we create the menu as a sub-menu of "dummy" so that we can use // it in a cascading menu too. // If created on a SWT control we would get an SWT error... //Menu dummy= new Menu(ti.getParent()); //Menu m= mc.getMenu(dummy); //dummy.dispose(); if (mc != null) { Menu m = mc.getMenu(ti.getParent()); if (m != null) { // position the menu below the drop down item Rectangle b = ti.getBounds(); Point p = ti.getParent().toDisplay( new Point(b.x, b.y + b.height)); m.setLocation(p.x, p.y); // waiting for SWT 0.42 m.setVisible(true); return; // we don't fire the action } } } } } // Ensure action is enabled first. // See 1GAN3M6: ITPUI:WINNT - Any IAction in the workbench can be executed while disabled. if (action.isEnabled()) { boolean trace = Policy.TRACE_ACTIONS; long ms = System.currentTimeMillis(); if (trace) { System.out.println("Running action: " + action.getText()); //$NON-NLS-1$ } action.runWithEvent(e); if (trace) { System.out.println((System.currentTimeMillis() - ms) + " ms to run action: " + action.getText()); //$NON-NLS-1$ } } } } /* (non-Javadoc) * Method declared on Object. */ public int hashCode() { return action.hashCode(); } /** * Returns whether the given action has any images. * * @param actionToCheck the action * @return <code>true</code> if the action has any images, <code>false</code> if not */ private boolean hasImages(IAction actionToCheck) { return actionToCheck.getImageDescriptor() != null || actionToCheck.getHoverImageDescriptor() != null || actionToCheck.getDisabledImageDescriptor() != null; } /** * Returns whether the command corresponding to this action * is active. */ private boolean isCommandActive() { IAction actionToCheck = getAction(); if (actionToCheck != null) { String commandId = actionToCheck.getActionDefinitionId(); ExternalActionManager.ICallback callback = ExternalActionManager .getInstance().getCallback(); if (callback != null) { return callback.isActive(commandId); } } return true; } /** * The action item implementation of this <code>IContributionItem</code> * method returns <code>true</code> for menu items and <code>false</code> * for everything else. */ public boolean isDynamic() { if (widget instanceof MenuItem) { //Optimization. Only recreate the item is the check or radio style has changed. boolean itemIsCheck = (widget.getStyle() & SWT.CHECK) != 0; boolean actionIsCheck = getAction() != null && getAction().getStyle() == IAction.AS_CHECK_BOX; boolean itemIsRadio = (widget.getStyle() & SWT.RADIO) != 0; boolean actionIsRadio = getAction() != null && getAction().getStyle() == IAction.AS_RADIO_BUTTON; return (itemIsCheck != actionIsCheck) || (itemIsRadio != actionIsRadio); } return false; } /* (non-Javadoc) * Method declared on IContributionItem. */ public boolean isEnabled() { return action != null && action.isEnabled(); } /** * Returns <code>true</code> if this item is allowed to enable, * <code>false</code> otherwise. * * @return if this item is allowed to be enabled * @since 2.0 */ protected boolean isEnabledAllowed() { if (getParent() == null) { return true; } Boolean value = getParent().getOverrides().getEnabled(this); return (value == null) ? true : value.booleanValue(); } /** * The <code>ActionContributionItem</code> implementation of this * <code>ContributionItem</code> method extends the super implementation * by also checking whether the command corresponding to this action is active. */ public boolean isVisible() { return super.isVisible() && isCommandActive(); } /** * Sets the presentation mode, which is the bitwise-or of the * <code>MODE_*</code> constants. * * @param mode the presentation mode settings * * @since 3.0 */ public void setMode(int mode) { this.mode = mode; update(); } /** * The action item implementation of this <code>IContributionItem</code> * method calls <code>update(null)</code>. */ public final void update() { update(null); } /** * Synchronizes the UI with the given property. * * @param propertyName the name of the property, or <code>null</code> meaning all applicable * properties */ public void update(String propertyName) { if (widget != null) { // determine what to do boolean textChanged = propertyName == null || propertyName.equals(IAction.TEXT); boolean imageChanged = propertyName == null || propertyName.equals(IAction.IMAGE); boolean tooltipTextChanged = propertyName == null || propertyName.equals(IAction.TOOL_TIP_TEXT); boolean enableStateChanged = propertyName == null || propertyName.equals(IAction.ENABLED) || propertyName .equals(IContributionManagerOverrides.P_ENABLED); boolean checkChanged = (action.getStyle() == IAction.AS_CHECK_BOX || action .getStyle() == IAction.AS_RADIO_BUTTON) && (propertyName == null || propertyName .equals(IAction.CHECKED)); if (widget instanceof ToolItem) { ToolItem ti = (ToolItem) widget; String text = action.getText(); // the set text is shown only if there is no image or if forced by MODE_FORCE_TEXT boolean showText = text != null && ((getMode() & MODE_FORCE_TEXT) != 0 || !hasImages(action)); // only do the trimming if the text will be used if (showText && text != null) { text = Action.removeAcceleratorText(text); text = Action.removeMnemonics(text); } if (textChanged) { String textToSet = showText ? text : ""; //$NON-NLS-1$ boolean rightStyle = (ti.getParent().getStyle() & SWT.RIGHT) != 0; if (rightStyle || !ti.getText().equals(textToSet)) { // In addition to being required to update the text if it // gets nulled out in the action, this is also a workaround // for bug 50151: Using SWT.RIGHT on a ToolBar leaves blank space ti.setText(textToSet); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -