Eclipse RCP: Conditionally enabling/disabling perspective in Perspectie Switcher - eclipse-rcp

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

Related

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>

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.

How to remove views from Windows-->Show View list?

I have view which I am setting permanently on my perspective.
This view can not be closed and can not be opened from Windows --> show views
I struct to remove View from Windows --> view list.
How would I achieve this?
I tried your solution it is doing the things but it is also removing the view from perspective. Below are the steps I followed..
I have added the following view in plugin.XML
<view
allowMultiple="false"
category="org.view.ui.IDECategory"
class="org.view.ui.BannerInformationView"
id="org.view.ui.BannerInformationView"
name="BannerInfo"
restorable="true">
</view>
After this I have added this view in my Perspective
public void defineLayout( IPageLayout layout )
{
layout.setEditorAreaVisible( true );
layout.addStandaloneView( BANNER_INFO_VIEW_ID, false, IPageLayout.TOP, 0.03f, layout.getEditorArea() );
IViewLayout viewLayout = layout.getViewLayout( BANNER_INFO_VIEW_ID );
viewLayout.setMoveable( false );
}
Now I have added the activity to hide my view name from show view menu
<extension point="org.eclipse.ui.activities">
<activity
id="activity.ide"
name="ide">
</activity>
<activityPatternBinding
activityId="activity.ide"
isEqualityPattern="true"
pattern="org.view.ui.IDECategory.pluginid/org.view.ui.BannerInformationView">
</activityPatternBinding>
</extension>
Now my problem is, along with hiding the view entry from window -> show view, it is also hiding the view from my perspective.
I want to hide the only entry from show view so that user can not do anything with it, but it should be always visible in my perspective.
The view list is filtered by the activities list. So you can define an activity to suppress the view:
<extension point="org.eclipse.ui.activities">
<activity id="activity.id" name="Name">
</activity>
<activityPatternBinding
activityId="activity.id"
isEqualityPattern="true"
pattern="plugin.id/view.id">
</activityPatternBinding>
</extension>
Note: The pattern value is 'contributing plugin id / view id', a common mistake is leaving out the plugin id.

Checking the state of menu items inside handlers

How to check pro-grammatically whether command contributed as menu item(s) is/are checked/unchecked(If of CHECK BOX type), selected or unselected(if of type RADIO button) inside handlers "execute" method.
See snap shot here https://docs.google.com/file/d/0B3pxBGD-v-ycWVFaeElnSGdyTE0/edit .
Check out this blog: http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/
So, first of all ensure that your command has appropriate style:
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="...">
<command commandId="org.eclipse.example.command.toggle"
style="toggle" />
</menuContribution>
</extension>
Then, you can check the state like this:
ICommandService service =(ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command command = service.getCommand("org.eclipse.example.command.toggle");
State state = command.getState("org.eclipse.example.command.toggleState");
System.out.println(state.getValue());
//state.setValue(!(Boolean) state.getValue());
Also, consider taking a look at org.eclipse.ui.handlers.HandlerUtil, it might be sometime helpful.
Hope this helps.
I got solution,
Added this code in handler execute method method
public Object execute(ExecutionEvent event) throws ExecutionException {
Event selEvent = (Event) event.getTrigger();
MenuItem item = (MenuItem) selEvent.widget;
System.Out.Println(item.getSelection());
return null;
}

Removing popUpMenus from CNF (Common Navigator Framework) in Eclipse

I have been partially successful at removing almost all the popUp menus from the Commons Navigator Framework simply by configuring the plugin.xml file.
There are 2 menus that refuse to go:
group.edit and
group.reorganize.
My plugin.xml config looks like this:
<extension
point="org.eclipse.ui.navigator.viewer">
<viewer
viewerId="org.eclipse.ui.example.navigator.view">
<popupMenu allowsPlatformContributions="false">
<insertionPoint
name="group.edit" />
<insertionPoint
name="group.reorganize" />
</popupMenu>
</viewer>
<viewerContentBinding
viewerId="org.eclipse.ui.thermo.navigator.view">
<includes>
<contentExtension
pattern="org.eclipse.ui.navigator.resourceContent"/>
</includes>
</viewerContentBinding>
</extension>
Setting the allowsPlatformContribution to false DOES stop contributions to be added to the context menu except for group.edit and group.reorganize... this is starting to look like a bug to me.
The obvious solution is to remove the insertion points from my <popUpMenu> but without them the application throws an exception:
Throwable: java.lang.IllegalArgumentException: Group not found: group.edit
java.lang.IllegalArgumentException: Group not found: group.edit
at org.eclipse.jface.action.ContributionManager.addToGroup(ContributionManager.java:131)
at org.eclipse.jface.action.ContributionManager.appendToGroup(ContributionManager.java:138)
at org.eclipse.ui.internal.navigator.resources.actions.EditActionGroup.fillContextMenu(EditActionGroup.java:74)
at org.eclipse.ui.internal.navigator.resources.actions.EditActionProvider.fillContextMenu(EditActionProvider.java:50)
at org.eclipse.ui.navigator.NavigatorActionService.addCommonActionProviderMenu(NavigatorActionService.java:205)
at org.eclipse.ui.navigator.NavigatorActionService.fillContextMenu(NavigatorActionService.java:172)
at org.eclipse.ui.internal.navigator.CommonNavigatorManager.fillContextMenu(CommonNavigatorManager.java:258)
at org.eclipse.ui.internal.navigator.CommonNavigatorManager$4.menuAboutToShow(CommonNavigatorManager.java:273)
at org.eclipse.jface.action.MenuManager.fireAboutToShow(MenuManager.java:335)
at org.eclipse.jface.action.MenuManager.handleAboutToShow(MenuManager.java:463)
at org.eclipse.jface.action.MenuManager.access$1(MenuManager.java:459)
at org.eclipse.jface.action.MenuManager$2.menuShown(MenuManager.java:485)
It throws the same exception for the reorganize group.
I succeeded in removing the "group.edit" actions (Copy/Paste) and I've done it that way, using the Common Navigator extension points :
<extension
point="org.eclipse.ui.navigator.viewer">
<viewerActionBinding
viewerId="org.eclipse.ui.navigator.ProjectExplorer">
<includes>
<actionExtension
pattern="my.app.client.actions.MyAppEditActionExtension">
</actionExtension>
</includes>
</viewerActionBinding>
</extension>
<extension
point="org.eclipse.ui.navigator.navigatorContent">
<actionProvider
class="my.app.client.workshop.MyPasteActionProvider"
id="my.app.client.actions.MyAppEditActionExtension"
overrides="org.eclipse.ui.navigator.resources.actions.EditActions"
priority="highest">
<enablement>
<!-- A hack to allways be enabled -->
<not>
<systemTest
property="MyApp"
value="WONT-EVER-BE-SET">
</systemTest>
</not>
</enablement>
</actionProvider>
</extension>
And, with addition of "org.eclipse.ui.navigator.resources" in my plugin dependencies, I implemented "MyPasteActionProvider" like this :
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.ui.internal.navigator.resources.actions.EditActionProvider;
/**
* Create the Edit actions (Cut/Copy/Paste)
* and register then globally in the workbench using EditActionProvider.
* <p/>
* Then, removes the Copy/Paste contributions in the pop-up menu.
*/
public class MyPasteActionProvider extends EditActionProvider {
public void fillContextMenu(IMenuManager menu) { super.fillContextMenu(menu);
// remove Copy/Paste contributions
IContributionItem copyItemRemoved = menu.remove("org.eclipse.ui.CopyAction");
IContributionItem pasteItemRemoved = menu.remove("org.eclipse.ui.PasteAction");
}
}
Well, that's a "discouraged access" but I was getting myself discouraged ;-)
JM.D
Generally you should be using the Command Framework with any recent version of Eclipse (3.3 or higher), this replaces the mechanism for providing popup menus in the Common Navigator.
This thread suggests to remove the things that are causing the menu items to appear in the first place:
They are probably in action sets, so if you can identify the action set that's causing the offensive contribution, you can do something like this in your WorkbenchAdvisor:
ActionSetRegistry reg = WorkbenchPlugin.getDefault()
.getActionSetRegistry();
IActionSetDescriptor[] actionSets = reg.getActionSets();
String[] removeActionSets = new String[] {
"org.eclipse.ui.cheatsheets.actionSet",
"org.eclipse.ui.edit.text.actionSet.annotationNavigation",
"org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo",
"org.eclipse.ui.WorkingSetActionSet",
"org.eclipse.update.ui.softwareUpdates", };
for (int i = 0; i < actionSets.length; i++)
{
boolean found = false;
for (int j = 0; j < removeActionSets.length; j++)
{
if (removeActionSets[j].equals(actionSets[i].getId()))
found = true;
}
if (!found)
continue;
IExtension ext = actionSets[i].getConfigurationElement()
.getDeclaringExtension();
reg.removeExtension(ext, new Object[] { actionSets[i] });
}
The closest bug I have found is the 145233: Make more obvious way to specify input (for RCP apps), with a similar hack.
Bug 143430 (CommonNavigator requires initialInput to be Adaptable) is a more general one, and would indicate that then CNF has been improved with eclipse3.5 (Galileo).
So do you also have this issue with 3.5 and a custom CNF class?
As mentioned in the article "Eclipse CNF: Navigator Content Extensions", CNF has evolved with eclipse3.5, and this article seems to have some tree with true custom contextual menu entries.