### Eclipse Workspace Patch 1.0 #P org.eclipse.ui.workbench Index: Eclipse UI/org/eclipse/ui/menus/CommandContributionItem.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/menus/CommandContributionItem.java,v retrieving revision 1.47 diff -u -r1.47 CommandContributionItem.java --- Eclipse UI/org/eclipse/ui/menus/CommandContributionItem.java 12 Sep 2008 19:54:44 -0000 1.47 +++ Eclipse UI/org/eclipse/ui/menus/CommandContributionItem.java 6 Oct 2008 16:36:23 -0000 @@ -25,6 +25,8 @@ import org.eclipse.jface.action.IMenuListener; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.MenuManager; +import org.eclipse.jface.bindings.BindingManagerEvent; +import org.eclipse.jface.bindings.IBindingManagerListener; import org.eclipse.jface.bindings.TriggerSequence; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.resource.JFaceResources; @@ -399,6 +401,9 @@ update(null); updateIcons(); + + bindingService.addBindingManagerListener(bindingManagerListener); + } /* (non-Javadoc) @@ -428,6 +433,9 @@ update(null); updateIcons(); + + bindingService.addBindingManagerListener(bindingManagerListener); + } /* @@ -460,6 +468,8 @@ update(null); updateIcons(); + + bindingService.addBindingManagerListener(bindingManagerListener); } /* @@ -479,119 +489,143 @@ public void update(String id) { if (widget != null) { if (widget instanceof MenuItem) { - MenuItem item = (MenuItem) widget; + updateMenuItem(); + } else if (widget instanceof ToolItem) { + updateToolItem(); + } else if (widget instanceof Button) { + updateButton(); + } + } + } - String text = label; - if (text == null) { - if (command != null) { - try { - text = command.getCommand().getName(); - } catch (NotDefinedException e) { - WorkbenchPlugin.log("Update item failed " //$NON-NLS-1$ - + getId(), e); - } - } - } - text = updateMnemonic(text); + private void updateMenuItem() { + MenuItem item = (MenuItem) widget; - String keyBindingText = null; - if (command != null) { - TriggerSequence binding = bindingService - .getBestActiveBindingFor(command); - if (binding != null) { - keyBindingText = binding.format(); - } - } - if (text != null) { - if (keyBindingText == null) { - item.setText(text); - } else { - item.setText(text + '\t' + keyBindingText); - } + String text = label; + if (text == null) { + if (command != null) { + try { + text = command.getCommand().getName(); + } catch (NotDefinedException e) { + WorkbenchPlugin.log("Update item failed " //$NON-NLS-1$ + + getId(), e); } + } + } + text = updateMnemonic(text); - if (item.getSelection() != checkedState) { - item.setSelection(checkedState); - } + String keyBindingText = null; + if (command != null) { + TriggerSequence binding = bindingService + .getBestActiveBindingFor(command); + if (binding != null) { + keyBindingText = binding.format(); + } + } + if (text != null) { + if (keyBindingText == null) { + item.setText(text); + } else { + item.setText(text + '\t' + keyBindingText); + } + } - boolean shouldBeEnabled = isEnabled(); - if (item.getEnabled() != shouldBeEnabled) { - item.setEnabled(shouldBeEnabled); - } - } else if (widget instanceof ToolItem) { - ToolItem item = (ToolItem) widget; + if (item.getSelection() != checkedState) { + item.setSelection(checkedState); + } - String text = label; - if (text == null) { - if (command != null) { - try { - text = command.getCommand().getName(); - } catch (NotDefinedException e) { - WorkbenchPlugin.log("Update item failed " //$NON-NLS-1$ - + getId(), e); - } - } - } + boolean shouldBeEnabled = isEnabled(); + if (item.getEnabled() != shouldBeEnabled) { + item.setEnabled(shouldBeEnabled); + } + } - if ((icon == null || (mode & MODE_FORCE_TEXT) == MODE_FORCE_TEXT) - && text != null) { - item.setText(text); - } + private void updateToolItem() { + ToolItem item = (ToolItem) widget; - if (tooltip != null) - item.setToolTipText(tooltip); - else { - if (text != null) { - item.setToolTipText(text); - } + String text = label; + if (text == null) { + if (command != null) { + try { + text = command.getCommand().getName(); + } catch (NotDefinedException e) { + WorkbenchPlugin.log("Update item failed " //$NON-NLS-1$ + + getId(), e); } + } + } - if (item.getSelection() != checkedState) { - item.setSelection(checkedState); - } + if ((icon == null || (mode & MODE_FORCE_TEXT) == MODE_FORCE_TEXT) + && text != null) { + item.setText(text); + } + + String toolTipText = getToolTipText(text); + item.setToolTipText(toolTipText); + + if (item.getSelection() != checkedState) { + item.setSelection(checkedState); + } + + boolean shouldBeEnabled = isEnabled(); + if (item.getEnabled() != shouldBeEnabled) { + item.setEnabled(shouldBeEnabled); + } + } - boolean shouldBeEnabled = isEnabled(); - if (item.getEnabled() != shouldBeEnabled) { - item.setEnabled(shouldBeEnabled); + private void updateButton() { + Button item = (Button) widget; + + String text = label; + if (text == null) { + if (command != null) { + try { + text = command.getCommand().getName(); + } catch (NotDefinedException e) { + WorkbenchPlugin.log("Update item failed " //$NON-NLS-1$ + + getId(), e); } } - else if (widget instanceof Button) { - Button item = (Button) widget; + } - String text = label; - if (text == null) { - if (command != null) { - try { - text = command.getCommand().getName(); - } catch (NotDefinedException e) { - WorkbenchPlugin.log("Update item failed " //$NON-NLS-1$ - + getId(), e); - } - } - } + if (text != null) { + item.setText(text); + } - if (text != null) { - item.setText(text); - } + String toolTipText = getToolTipText(text); + item.setToolTipText(toolTipText); - if (tooltip != null) - item.setToolTipText(tooltip); - else { - if (text != null) { - item.setToolTipText(text); - } - } + if (item.getSelection() != checkedState) { + item.setSelection(checkedState); + } - if (item.getSelection() != checkedState) { - item.setSelection(checkedState); - } + boolean shouldBeEnabled = isEnabled(); + if (item.getEnabled() != shouldBeEnabled) { + item.setEnabled(shouldBeEnabled); + } + } - boolean shouldBeEnabled = isEnabled(); - if (item.getEnabled() != shouldBeEnabled) { - item.setEnabled(shouldBeEnabled); - } + private String getToolTipText(String text) { + String tooltipText = tooltip; + if (tooltip == null) + if (text != null) + tooltipText = text; + else + tooltipText = ""; //$NON-NLS-1$ + + TriggerSequence activeBinding = bindingService + .getBestActiveBindingFor(command); + if (activeBinding != null && !activeBinding.isEmpty()) { + String acceleratorText = activeBinding.format(); + if (acceleratorText != null + && acceleratorText.length() != 0) { + tooltipText = JFaceResources.format( + "Toolbar_Tooltip_Accelerator", //$NON-NLS-1$ + new Object[] { tooltipText, acceleratorText }); } } + + return tooltipText; } private String updateMnemonic(String s) { @@ -633,6 +667,11 @@ command.getCommand().removeCommandListener(commandListener); commandListener = null; } + + if (bindingService != null) { + bindingService.removeBindingManagerListener(bindingManagerListener); + } + command = null; commandService = null; bindingService = null; @@ -829,4 +868,16 @@ } return super.isVisible(); } + + private IBindingManagerListener bindingManagerListener = new IBindingManagerListener() { + + public void bindingManagerChanged(BindingManagerEvent event) { + if (event.isActiveBindingsChanged() + && event.isActiveBindingsChangedFor(getCommand())) { + update(); + } + + } + + }; } Index: Eclipse UI/org/eclipse/ui/internal/keys/BindingService.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/keys/BindingService.java,v retrieving revision 1.27 diff -u -r1.27 BindingService.java --- Eclipse UI/org/eclipse/ui/internal/keys/BindingService.java 24 Mar 2008 19:22:01 -0000 1.27 +++ Eclipse UI/org/eclipse/ui/internal/keys/BindingService.java 6 Oct 2008 16:36:23 -0000 @@ -17,6 +17,7 @@ import org.eclipse.core.commands.common.NotDefinedException; import org.eclipse.jface.bindings.Binding; import org.eclipse.jface.bindings.BindingManager; +import org.eclipse.jface.bindings.IBindingManagerListener; import org.eclipse.jface.bindings.Scheme; import org.eclipse.jface.bindings.TriggerSequence; import org.eclipse.jface.bindings.keys.SWTKeySupport; @@ -248,5 +249,27 @@ public BindingPersistence getBindingPersistence() { return bindingPersistence; } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ui.keys.IBindingService#addBindingManagerListener(org.eclipse + * .jface.bindings.IBindingManagerListener) + */ + public void addBindingManagerListener(IBindingManagerListener listener) { + bindingManager.addBindingManagerListener(listener); + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ui.keys.IBindingService#removeBindingManagerListener(org. + * eclipse.jface.bindings.IBindingManagerListener) + */ + public void removeBindingManagerListener(IBindingManagerListener listener) { + bindingManager.removeBindingManagerListener(listener); + } } Index: Eclipse UI/org/eclipse/ui/keys/IBindingService.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/keys/IBindingService.java,v retrieving revision 1.21 diff -u -r1.21 IBindingService.java --- Eclipse UI/org/eclipse/ui/keys/IBindingService.java 5 Jun 2008 13:46:55 -0000 1.21 +++ Eclipse UI/org/eclipse/ui/keys/IBindingService.java 6 Oct 2008 16:36:23 -0000 @@ -15,6 +15,7 @@ import org.eclipse.core.commands.ParameterizedCommand; import org.eclipse.jface.bindings.Binding; +import org.eclipse.jface.bindings.IBindingManagerListener; import org.eclipse.jface.bindings.Scheme; import org.eclipse.jface.bindings.TriggerSequence; import org.eclipse.ui.commands.ICommandService; @@ -53,6 +54,38 @@ public static final String DEFAULT_DEFAULT_ACTIVE_SCHEME_ID = "org.eclipse.ui.defaultAcceleratorConfiguration"; //$NON-NLS-1$ /** + *

+ * Adds a listener to this binding service. The listener will be notified + * when the set of defined schemes or bindings changes. This can be used to + * track the global appearance and disappearance of bindings. + *

+ *

+ * This method completes in amortized constant time (O(1)). + *

+ * + * @param listener + * The listener to attach; must not be null. + * + * @since 3.5 + */ + public void addBindingManagerListener(IBindingManagerListener listener); + + /** + *

+ * Removes a listener from this binding service. + *

+ *

+ * This method completes in amortized O(1). + *

+ * + * @param listener + * The listener to be removed; must not be null. + * + * @since 3.5 + */ + public void removeBindingManagerListener(IBindingManagerListener listener); + + /** * Gets the active bindings for a given parameterized command. * * @param parameterizedCommand