|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.ui.internal.menus; |
| 13 |
|
| 14 |
import org.eclipse.core.runtime.IConfigurationElement; |
| 15 |
import org.eclipse.jface.action.IContributionItem; |
| 16 |
import org.eclipse.jface.action.IContributionManager; |
| 17 |
import org.eclipse.swt.widgets.Composite; |
| 18 |
import org.eclipse.swt.widgets.Control; |
| 19 |
import org.eclipse.swt.widgets.CoolBar; |
| 20 |
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; |
| 21 |
import org.eclipse.ui.internal.util.Util; |
| 22 |
import org.eclipse.ui.menus.IWorkbenchContribution; |
| 23 |
import org.eclipse.ui.menus.WorkbenchWindowControlContribution; |
| 24 |
import org.eclipse.ui.services.IServiceLocator; |
| 25 |
|
| 26 |
/** |
| 27 |
* A contribution item which proxies a dynamic tool contribution. |
| 28 |
* <p> |
| 29 |
* It currently supports placement in menus. |
| 30 |
* </p> |
| 31 |
* <p> |
| 32 |
* |
| 33 |
* @author Prakash G.R. |
| 34 |
* |
| 35 |
* @since 3.6 |
| 36 |
* |
| 37 |
*/ |
| 38 |
public class DynamicToolBarContributionItem extends WorkbenchWindowControlContribution { |
| 39 |
|
| 40 |
private final IConfigurationElement dynamicAddition; |
| 41 |
private final IServiceLocator locator; |
| 42 |
private boolean alreadyFailed; |
| 43 |
private WorkbenchWindowControlContribution loadedDynamicContribution; |
| 44 |
|
| 45 |
/** |
| 46 |
* Creates a DynamicToolBarContributionItem |
| 47 |
* |
| 48 |
* @param id |
| 49 |
* - Id of the menu item |
| 50 |
* @param locator |
| 51 |
* - The Service Locator |
| 52 |
* @param dynamicAddition |
| 53 |
* - The Configuration Element defined in the plugin.xml |
| 54 |
* |
| 55 |
*/ |
| 56 |
public DynamicToolBarContributionItem(String id, IServiceLocator locator, |
| 57 |
IConfigurationElement dynamicAddition) { |
| 58 |
super(id); |
| 59 |
|
| 60 |
this.locator = locator; |
| 61 |
this.dynamicAddition = dynamicAddition; |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
* (non-Javadoc) |
| 66 |
* |
| 67 |
* @see org.eclipse.jface.action.ContributionItem#isDynamic() |
| 68 |
*/ |
| 69 |
public boolean isDynamic() { |
| 70 |
if (loadedDynamicContribution != null) { |
| 71 |
return loadedDynamicContribution.isDynamic(); |
| 72 |
} |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
/* |
| 77 |
* (non-Javadoc) |
| 78 |
* |
| 79 |
* @see org.eclipse.jface.action.ContributionItem#isDirty() |
| 80 |
*/ |
| 81 |
public boolean isDirty() { |
| 82 |
if (loadedDynamicContribution != null) { |
| 83 |
return loadedDynamicContribution.isDirty(); |
| 84 |
} |
| 85 |
return super.isDirty(); |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
* (non-Javadoc) |
| 90 |
* |
| 91 |
* @see |
| 92 |
* org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets |
| 93 |
* .CoolBar, int) |
| 94 |
*/ |
| 95 |
public void fill(CoolBar parent, int index) { |
| 96 |
IContributionItem contributionItem = getContributionItem(); |
| 97 |
if (contributionItem != null) |
| 98 |
contributionItem.fill(parent, index); |
| 99 |
} |
| 100 |
|
| 101 |
private WorkbenchWindowControlContribution getContributionItem() { |
| 102 |
if (loadedDynamicContribution == null && !alreadyFailed) |
| 103 |
createContributionItem(); |
| 104 |
return loadedDynamicContribution; |
| 105 |
} |
| 106 |
|
| 107 |
private void createContributionItem() { |
| 108 |
|
| 109 |
loadedDynamicContribution = (WorkbenchWindowControlContribution) Util |
| 110 |
.safeLoadExecutableExtension(dynamicAddition, |
| 111 |
IWorkbenchRegistryConstants.ATT_CLASS, |
| 112 |
WorkbenchWindowControlContribution.class); |
| 113 |
|
| 114 |
if (loadedDynamicContribution == null) { |
| 115 |
alreadyFailed = true; |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
loadedDynamicContribution.setId(getId()); |
| 120 |
loadedDynamicContribution.setParent(getParent()); |
| 121 |
if (loadedDynamicContribution instanceof IWorkbenchContribution) { |
| 122 |
((IWorkbenchContribution) loadedDynamicContribution) |
| 123 |
.initialize(locator); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/* |
| 128 |
* (non-Javadoc) |
| 129 |
* |
| 130 |
* @see org.eclipse.jface.action.ContributionItem#dispose() |
| 131 |
*/ |
| 132 |
public void dispose() { |
| 133 |
if (loadedDynamicContribution != null) { |
| 134 |
loadedDynamicContribution.dispose(); |
| 135 |
loadedDynamicContribution = null; |
| 136 |
} |
| 137 |
super.dispose(); |
| 138 |
} |
| 139 |
|
| 140 |
/* |
| 141 |
* (non-Javadoc) |
| 142 |
* |
| 143 |
* @see org.eclipse.jface.action.ContributionItem#update() |
| 144 |
*/ |
| 145 |
public void update() { |
| 146 |
if (loadedDynamicContribution != null) { |
| 147 |
loadedDynamicContribution.update(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
public void update(String id) { |
| 152 |
if (loadedDynamicContribution != null) { |
| 153 |
loadedDynamicContribution.update(id); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/* |
| 158 |
* (non-Javadoc) |
| 159 |
* |
| 160 |
* @see |
| 161 |
* org.eclipse.jface.action.ContributionItem#setParent(org.eclipse.jface |
| 162 |
* .action.IContributionManager) |
| 163 |
*/ |
| 164 |
public void setParent(IContributionManager parent) { |
| 165 |
super.setParent(parent); |
| 166 |
if (loadedDynamicContribution != null) { |
| 167 |
loadedDynamicContribution.setParent(parent); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/* |
| 172 |
* (non-Javadoc) |
| 173 |
* @see org.eclipse.jface.action.ControlContribution#createControl(org.eclipse.swt.widgets.Composite) |
| 174 |
*/ |
| 175 |
public Control createControl(Composite parent) { |
| 176 |
|
| 177 |
WorkbenchWindowControlContribution contributionItem = getContributionItem(); |
| 178 |
if (contributionItem != null) |
| 179 |
return contributionItem.delegateCreateControl(parent); |
| 180 |
return null; |
| 181 |
} |
| 182 |
|
| 183 |
} |