diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/menus/CommandContributionItem.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/menus/CommandContributionItem.java index 1ddf6f1..b607833 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/menus/CommandContributionItem.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/menus/CommandContributionItem.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2009 IBM Corporation and others. + * Copyright (c) 2006, 2012 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 @@ -216,41 +216,6 @@ contributionParameters.parameters); if (command != null) { - try { - UIElement callback = new UIElement( - contributionParameters.serviceLocator) { - - public void setChecked(boolean checked) { - CommandContributionItem.this.setChecked(checked); - } - - public void setDisabledIcon(ImageDescriptor desc) { - CommandContributionItem.this.setDisabledIcon(desc); - } - - public void setHoverIcon(ImageDescriptor desc) { - CommandContributionItem.this.setHoverIcon(desc); - } - - public void setIcon(ImageDescriptor desc) { - CommandContributionItem.this.setIcon(desc); - } - - public void setText(String text) { - CommandContributionItem.this.setText(text); - } - - public void setTooltip(String text) { - CommandContributionItem.this.setTooltip(text); - } - - public void setDropDownId(String id) { - dropDownMenuOverride = id; - } - }; - elementRef = commandService.registerElementForCommand(command, - callback); - command.getCommand().addCommandListener(getCommandListener()); setImages(contributionParameters.serviceLocator, contributionParameters.iconStyle); @@ -268,17 +233,6 @@ if (workbench != null && helpContextId != null) { this.workbenchHelpSystem = workbench.getHelpSystem(); } - } catch (NotDefinedException e) { - StatusManager - .getManager() - .handle( - StatusUtil - .newStatus( - IStatus.ERROR, - "Unable to register menu item \"" + getId() //$NON-NLS-1$ - + "\", command \"" + contributionParameters.commandId + "\" not defined", //$NON-NLS-1$ //$NON-NLS-2$ - null)); - } } } @@ -490,8 +444,7 @@ update(null); updateIcons(); - bindingService.addBindingManagerListener(bindingManagerListener); - + establishReferences(); } /* (non-Javadoc) @@ -522,8 +475,7 @@ update(null); updateIcons(); - bindingService.addBindingManagerListener(bindingManagerListener); - + establishReferences(); } /* @@ -557,7 +509,7 @@ update(null); updateIcons(); - bindingService.addBindingManagerListener(bindingManagerListener); + establishReferences(); } /* @@ -748,16 +700,58 @@ } } - /* - * (non-Javadoc) - * - * @see org.eclipse.jface.action.ContributionItem#dispose() - */ - public void dispose() { - if (widget != null) { - widget.dispose(); - widget = null; + public void setParent(IContributionManager parent) { + super.setParent(parent); + if (parent == null) + disconnectReferences(); + } + + private void establishReferences() { + if (command != null) { + UIElement callback = new UIElement(serviceLocator) { + + public void setChecked(boolean checked) { + CommandContributionItem.this.setChecked(checked); + } + + public void setDisabledIcon(ImageDescriptor desc) { + CommandContributionItem.this.setDisabledIcon(desc); + } + + public void setHoverIcon(ImageDescriptor desc) { + CommandContributionItem.this.setHoverIcon(desc); + } + + public void setIcon(ImageDescriptor desc) { + CommandContributionItem.this.setIcon(desc); + } + + public void setText(String text) { + CommandContributionItem.this.setText(text); + } + + public void setTooltip(String text) { + CommandContributionItem.this.setTooltip(text); + } + + public void setDropDownId(String id) { + dropDownMenuOverride = id; + } + }; + try { + elementRef = commandService.registerElementForCommand(command, callback); + } catch (NotDefinedException e) { + StatusManager.getManager().handle( + StatusUtil.newStatus(IStatus.ERROR, "Unable to register menu item \"" + getId() //$NON-NLS-1$ + + "\", command \"" + command.getId() + "\" not defined", //$NON-NLS-1$ //$NON-NLS-2$ + null)); + } + command.getCommand().addCommandListener(getCommandListener()); } + bindingService.addBindingManagerListener(bindingManagerListener); + } + + private void disconnectReferences() { if (elementRef != null) { commandService.unregisterElement(elementRef); elementRef = null; @@ -770,6 +764,20 @@ if (bindingService != null) { bindingService.removeBindingManagerListener(bindingManagerListener); } + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.action.ContributionItem#dispose() + */ + public void dispose() { + if (widget != null) { + widget.dispose(); + widget = null; + } + + disconnectReferences(); command = null; commandService = null;