EclipseRCP, Add Context Menu Item for project - eclipse

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;
}
}

Related

Eclipse RCP bold font in proposal for custom editor

I have a strange problem. I tried two implemenations of Custom Editor, one is using GenericEditor without own Editor implemenation see 1 and the other is with own editor implementation.
Only for the first solution, I get the bold font. For the second implemenation no bold possible. For both I am using the same Proposal Implemenation.
1. Using GenericEditor
<extension
id="keywordsproposal"
name="Keywords Proposals"
point="org.eclipse.ui.genericeditor.contentAssistProcessors">
<contentAssistProcessor
contentType="com.me.editor"
class="com.me.proposalcomputer.AllKeywordsProposalComputer">
</contentAssistProcessor>
</extension>
<extension
point="org.eclipse.core.contenttype.contentTypes">
<content-type
file-extensions="me"
id="com.me.editor"
name="ME"
priority="high">
</content-type>
</extension>
<extension
point="org.eclipse.ui.editors">
<editorContentTypeBinding
contentTypeId="com.me.editor"
editorId="org.eclipse.ui.genericeditor.GenericEditor">
</editorContentTypeBinding>
</extension>
2. Own Editor class
<extension
point="org.eclipse.ui.editors">
<editor
id="com.me.editor"
name="ME Editor"
extensions="me"
class="com.me.editor"
contributorClass="org.eclipse.ui.texteditor.BasicTextEditorActionContributor">
</editor>
</extension>
public class MeSourceViewerConfigurator extends TextSourceViewerConfiguration{
/*
*/
#Override
public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
final ContentAssistant assistant = new ContentAssistant();
assistant.enableAutoActivation(true);
assistant.setAutoActivationDelay(1);
final IContentAssistProcessor processor= new AllKeywordsProposalComputer();
assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
assistant.addContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
return assistant;
}
Do I miss anything to add in plugin.xml or..?

How to add a command to an editor context menu in Eclipse only if text is selected

I would like to add an item to the eclipse context menu only if a text is marked as selected
below is my XML relevant snippet:
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.ui.popup.any">
<command
commandId="com.test.ide.menu.commands.sampleCommand"
icon="icons/sample.png"
id="com.test.ide.menu.toolbars.sampleCommand"
tooltip="Popup test">
<visibleWhen>
<with variable="selection">
<instanceof value="org.eclipse.jface.text.ITextSelection"/>
</with>
</visibleWhen>
</command>
</menuContribution>
I tried the solution in How do you contribute a command to an editor context menu in Eclipse which does not resolve the issue
What am i missing?
Can you help?
ThanksAvner
Excuse my detailed question, I am a newb to eclipse programming
i tried:
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="com.test.ide.TextSelectedPropertyTester"
id="com.test.ide.propertyTester1"
namespace="tested"
properties="textSelected"
type="org.eclipse.jface.text.ITextSelection">
</propertyTester>
</extension>
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.ui.popup.any">
<command
commandId="com.test.ide.menu.commands.sampleCommand"
icon="icons/sample.png"
id="com.test.ide.menu.toolbars.sampleCommand"
tooltip="Popup test">
<visibleWhen>
<with variable="selection">
<adapt type="org.eclipse.jface.text.ITextSelection">
<test
property="tested.textSelected">
</test>
</adapt>
</with>
</visibleWhen>
</command>
</menuContribution>
created a new package - com.test.ide in it created a new class TextSelectedPropertyTester
however the class is never invoked
package com.test.ide;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.jface.text.ITextSelection;
public class TextSelectedPropertyTester extends PropertyTester
{
#Override
public boolean test(Object receiver, String property, Object [] args, Object expectedValue)
{
System.out.println("testing");
if (receiver instanceof ITextSelection) {
return ((ITextSelection)receiver).getLength() > 0;
}
return false;
}
}
however the class is never used
what am I missing?
The problem is that editors normally always have a text selection set - it will just be zero length if no characters are selected.
I can't see a way to test this using existing expressions so it may be necessary to define your own property tester using the org.eclipse.core.expressions.propertyTester extension point.
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="tested.TextSelectedPropertyTester"
id="tested.propertyTester1"
namespace="tested"
properties="textSelected"
type="org.eclipse.jface.text.ITextSelection">
</propertyTester>
</extension>
use like this:
<visibleWhen>
<with variable="selection">
<adapt type="org.eclipse.jface.text.ITextSelection">
<test
property="tested.textSelected"
forcePluginActivation="true">
</test>
</adapt>
</with>
</visibleWhen>
The property tester is very simple:
public class TextSelectedPropertyTester extends PropertyTester
{
#Override
public boolean test(Object receiver, String property, Object [] args, Object expectedValue)
{
if (receiver instanceof ITextSelection textSel) {
return textSel.getLength() > 0;
}
return false;
}
}
Note as shown the if statement uses a Java 16 instanceof type pattern, if you need to run with an older Java use:
if (receiver instanceof ITextSelection) {
return ((ITextSelection)receiver).getLength() > 0;
}

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 plugins - Add a menucontribution to Toolbar dynamically

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.