Hide/Remove a perspective provided by another plugin - eclipse - 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>

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

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

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

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.