Eclipse plugins - Add a menucontribution to Toolbar dynamically - eclipse

Is it possible to add menucontribution, basically a new button, to the toolbar:org.eclipse.ui.main.toolbar dynamically?
I tried with AbstractContributionFactory but it seems to be bugged, since the createContributionItems is never called.
My code so far tries to add a new button to the Eclipse toolbar after pressing another button. The thing is that the createContributionItems is never called so "Creating Stuff" never appears:
//If a certain button is pressed run will be executed
public void run(IAction action) {
MessageDialog.openInformation(
window.getShell(),
"Runtimecommands",
"Hello, Eclipse world");
IMenuService menuService = (IMenuService) PlatformUI.getWorkbench().getService(IMenuService.class);
AbstractContributionFactory factory = new AbstractContributionFactory("toolbar:org.eclipse.ui.main.toolbar", null)
#Override
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
System.out.println("Creating Stuff");
};
menuService.addContributionFactory(factory);
}
And my current plugin.xml
<plugin>
<extension
point="org.eclipse.ui.actionSets">
<actionSet
label="Sample Action Set"
visible="true"
id="com.dynamo.actionSet">
<menu
label="Sample &Menu"
id="sampleMenu">
<separator
name="sampleGroup">
</separator>
</menu>
<action
label="&Sample Action"
icon="icons/sample.gif"
class="com.dynamo.actions.SampleAction"
tooltip="Hello, Eclipse world"
menubarPath="sampleMenu/sampleGroup"
toolbarPath="sampleGroup"
id="com.dynamo.actions.SampleAction">
</action>
</actionSet>
</extension>
</plugin>
Tks for your attention.

Related

Eclipse RCP: Conditionally enabling/disabling perspective in Perspectie Switcher

The Open Perspective View shows a list of perspectives to which a user can switch. My custom perspective is there, too. Can I have it not listed or greyed out, if a certain condition bis not met?
The second best alternative that comes to my mind is to have it listed, but the actual switch is not carried out, if b == false.
The best way for me was to use the extension point org.eclipse.ui.activities, as greg-449 suggested, and add a propertyTester. In order to hide perspective related UI elements and not every UI element from the same plugin, one can use isEqualityPattern="true".
<extension point="org.eclipse.ui.activities">
<activity
description="Remove some UI Elements"
id="com.my.plugin.ui.hideUI"
name="HideUI">
<enabledWhen>
<test
forcePluginActivation="false"
property="com.my.plugin.ui.PlanningPropertyTester">
</test>
</enabledWhen>
</activity>
<activityPatternBinding
activityId="com.btc.edm.hidePlanning"
isEqualityPattern="true"
pattern="com.my.plugin.ui/id.of.the.perspective.to.hide">
</activityPatternBinding>
</extension>
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="com.my.plugin.ui.MyPropertyTester"
id="com.my.plugin.ui.propertyTester"
namespace="com.my.plugin.ui"
properties="MyPropertyTester"
type="java.lang.Object">
</propertyTester>
</extension>
It is important to have a fully qualified name for type.
The MyPropertyTester class extends org.eclipse.core.expressions.PropertyTester and overrides the test method.
public class MyPropertyTester extends PropertyTester {
#Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
if (goodWeather()) {
return true;
return false;
}
}

EclipseRCP, Add Context Menu Item for project

in my custom RCP plugin, I want to add a context menu with command to the project view.
So when I right click the project, then the new context menu item should be shown.
But I do not know how to achieve this, via adding an extension point to plugin.xml?
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions">
<command commandId="com.itemis.test.MyOpenCommand"
label="Open My" style="push" tooltip="Open My Editor.">
</command>
</menuContribution>
<extension point="org.eclipse.ui.commands">
<command id="com.itemis.test.MyOpenCommand" name="Open My"
description="Open My Editor.">
</command>
</extension>
<extension point="org.eclipse.ui.handlers">
<handler commandId="com.itemis.test.MyOpenCommand"
class="com.itemis.test.MyOpenHandler">
</handler>
</extension>
package com.itemis.test;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
public class MyOpenHandler extends AbstractHandler {
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
return null;
}
}

Hide/Remove a perspective provided by another plugin - eclipse

I noticed that I had dependencies on some of the Eclipse JDT and Plugin development features and due to this the RCP was showing some other perspective from the other ui plugins, which is unnecessary entries in my product of Preference Perspective.
I tried with the below snippet, but still the perspective is showing in the preference perspective.
Call the removeUnWantedPerspectives() (attached below) method from postWindowCreate() method of your RCP’s WorkbenchWindowAdvisor extension class –
private String[] IGNORE_PERSPECTIVES = new String[] {
"com.abc.xyz.ui.calibrationPerspective" };
/**
* Removes the unwanted perspectives from your RCP application
*/
private void removeUnWantedPerspectives() {
IPerspectiveRegistry perspectiveRegistry = PlatformUI.getWorkbench().getPerspectiveRegistry();
IPerspectiveDescriptor[] perspectiveDescriptors = perspectiveRegistry.getPerspectives();
List ignoredPerspectives = Arrays.asList(GenericConstants.IGNORE_PERSPECTIVES);
List removePerspectiveDesc = new ArrayList();
// Add the perspective descriptors with the matching perspective ids to the list
for (IPerspectiveDescriptor perspectiveDescriptor : perspectiveDescriptors) {
if(ignoredPerspectives.contains(perspectiveDescriptor.getId())) {
removePerspectiveDesc.add(perspectiveDescriptor);
}
}
// If the list is non-empty then remove all such perspectives from the IExtensionChangeHandler
if(perspectiveRegistry instanceof IExtensionChangeHandler && !removePerspectiveDesc.isEmpty()) {
IExtensionChangeHandler extChgHandler = (IExtensionChangeHandler) perspectiveRegistry;
extChgHandler.removeExtension(null, removePerspectiveDesc.toArray());
}
}
I tried with activities extension also, but this dosen't works for me
<extension
id="com.abc.xyz.pmse.application.activities.hideperspective"
name="Hide Perspectives"
point="org.eclipse.ui.activities">
<activityPatternBinding
activityId="com.abc.xyz.pmse.application.activities.hideperspective"
pattern=".*/com.abc.xyz.ui.calibrationperspective">
</activityPatternBinding>
<activityPatternBinding
activityId="com.abc.xyz.pmse.application.activities.hideperspective"
pattern=".*/com.abc.xyz.ui.varianthandlingperspective">
</activityPatternBinding>
</extension>
My working activities extension snippet looks like
<extension
point="org.eclipse.ui.activities">
<activity
id="com.abc.xyz.svn.ui.activity.hideunwantedperspective"
name="Hide UnwantedPerspective">
<enabledWhen>
<equals
value="false">
</equals>
</enabledWhen>
</activity>
<activityPatternBinding
activityId="com.abc.xyz.svn.ui.activity.hideunwantedperspective"
isEqualityPattern="true"
pattern="com.abc.xyz.ui.parameter.impl/com.abc.xyz.ui.calibrationperspective">
</activityPatternBinding>
<activityPatternBinding
activityId="com.abc.xyz.svn.ui.activity.hideunwantedperspective"
isEqualityPattern="true"
pattern="com.abc.xyz.ui.dcmmapping/com.abc.xyz.ui.varianthandlingperspective">
</activityPatternBinding>
<activityPatternBinding
activityId="com.abc.xyz.svn.ui.activity.hideunwantedperspective"
isEqualityPattern="true"
pattern="com.abc.xyz.ui.dcmmapping/com.abc.xyz.ui.varianthandlingperspectivenew">
</activityPatternBinding>
</extension>

Not able to extend the Help menu

I want to add from an eclipse plugin a Request Support button to the Help menu.
I tried first from a *.e4xmi file and now I tried from the plugin.xml but still can't make a button to appear under the Help menu.
I got the menu URI with the help of Eclipse Spy Plug-in.
The content of plugin.xml:
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false" (tried with true and same result)
locationURI="menu:help?after=about">
<menu
commandId="com.plugin.RequestSupport"
id="requestSupport"
label="Request Support">
</menu>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
defaultHandler="com.plugin.handlers.RequestSupportHandler"
description="Opens up default e-mail client with preset basic informations"
id="com.plugin.RequestSupport"
name="Request Support">
</command>
</extension>
What am I missing?
SOLUTION from greg's answer:
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false" (tried with true and same result)
locationURI="menu:help?after=about">
<menu
commandId="com.plugin.RequestSupport"
id="requestSupport"
label="Request Support">
</menu>
<command
commandId="com.plugin.RequestSupport"
id="requestSupport"
label="Request Support"
style="push">
</command>
</menuContribution>
</extension>
This is what 'Check for Updates' uses:
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:help?after=additions">
<command
commandId="org.eclipse.equinox.p2.ui.sdk.update"
mnemonic="%Update.command.mnemonic"
id="org.eclipse.equinox.p2.ui.sdk.update"
icon="icons/obj/iu_update_obj.png">
</command>
Which is adding after the 'additions' location.
The code which creates the help menu defines a lot of locations:
private MenuManager createHelpMenu() {
MenuManager menu = new MenuManager(IDEWorkbenchMessages.Workbench_help, IWorkbenchActionConstants.M_HELP);
addSeparatorOrGroupMarker(menu, "group.intro"); //$NON-NLS-1$
// See if a welcome or intro page is specified
if (introAction != null) {
menu.add(introAction);
} else if (quickStartAction != null) {
menu.add(quickStartAction);
}
menu.add(new GroupMarker("group.intro.ext")); //$NON-NLS-1$
addSeparatorOrGroupMarker(menu, "group.main"); //$NON-NLS-1$
menu.add(helpContentsAction);
menu.add(helpSearchAction);
menu.add(dynamicHelpAction);
addSeparatorOrGroupMarker(menu, "group.assist"); //$NON-NLS-1$
// See if a tips and tricks page is specified
if (tipsAndTricksAction != null) {
menu.add(tipsAndTricksAction);
}
// HELP_START should really be the first item, but it was after
// quickStartAction and tipsAndTricksAction in 2.1.
menu.add(new GroupMarker(IWorkbenchActionConstants.HELP_START));
menu.add(new GroupMarker("group.main.ext")); //$NON-NLS-1$
addSeparatorOrGroupMarker(menu, "group.tutorials"); //$NON-NLS-1$
addSeparatorOrGroupMarker(menu, "group.tools"); //$NON-NLS-1$
addSeparatorOrGroupMarker(menu, "group.updates"); //$NON-NLS-1$
menu.add(new GroupMarker(IWorkbenchActionConstants.HELP_END));
addSeparatorOrGroupMarker(menu, IWorkbenchActionConstants.MB_ADDITIONS);
// about should always be at the bottom
menu.add(new Separator("group.about")); //$NON-NLS-1$
ActionContributionItem aboutItem = new ActionContributionItem(aboutAction);
aboutItem.setVisible(!Util.isMac());
menu.add(aboutItem);
menu.add(new GroupMarker("group.about.ext")); //$NON-NLS-1$
return menu;
}
(from org.eclipse.ui.internal.ide.WorkbenchActionBuilder)
All the addSeparatorOrGroupMarker, new Separator and new GroupMaker calls define ids to can add after.

Eclipse RCP Can't Contribute to Main Toolbar

My RCP app has the coolbar visible by setting configurer.setShowCoolBar(true) in WorkbenchWindowAdvisor#preWindowOpen. But when I contribute a toolbar to the main toolbar, it never shows up. Here's my contribution code:
<extension point="org.eclipse.ui.menus">
<menuContribution
allPopups="true"
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar id="toolbar.perspectivesDynamic">
<dynamic
class="my.package.PerspectiveSwitcherToolbar"
id="perspectiveSwitcherToolbar">
</dynamic>
</toolbar>
</menuContribution>
</extension>
And the ContributionItem class:
public class PerspectiveSwitcherToolbar extends ContributionItem {
...
#Override
public void fill(final ToolBar parent, int index) {
//Does not get called
}
#Override
public void fill(CoolBar parent, int index) {
//Does not get called
}
...
}
I'm using this code for adding a custom perspective switcher. It's rather old, but I see examples everywhere on the Internet adding a toolbar like this to the main toolbar, so I'm missing something elsewher, I assume
I think that is bug 392457: <toolbar><dynamic></toolbar> doesn't work at the moment. You can work around it by using a <control> and managing the contents yourself.