So i have a class like this:
public class A {
...
public static class B {
...
}
}
And i have a handler in my rcp application defined like this:
<extension
point="org.eclipse.ui.handlers">
<handler
class="HandlerClass"
commandId="commandId">
<activeWhen>
<with
variable="selection">
<iterate
ifEmpty="false">
<instanceof
value="A.B">
</instanceof>
</iterate>
<count
value="1">
</count>
</with>
</activeWhen>
</handler>
</extension>
The instanceof part doesnt work. What i want to do is to check if the items in the selection variable are of type B. Is this possible?
You can use Property Testers, to check programmatically if a handler is active.
Check this blog entry: Me, myself and Property Testers
Related
I'm building a plugin that display images with some exclusive style.
Think something like you've got an colour image, and you can choose style "Black and white" or "Sepia" etc..
My plugin is like this:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.ui.commands">
<command
categoryId="fr.comp.app.category"
defaultHandler="fr.comp.common.styles.impl.DefaultStyle"
id="fr.comp.common.styles.defaultStyle"
name="Default Style">
<state
class="org.eclipse.ui.handlers.RadioState"
id="org.eclipse.ui.commands.radioState">
</state>
</command>
<command
categoryId="fr.comp.app.category"
defaultHandler="fr.comp.common.styles.impl.SepiaStyle"
id="fr.comp.common.styles.SepiaStyle"
name="Print Style">
<state
class="org.eclipse.ui.handlers.RadioState"
id="org.eclipse.ui.commands.radioState">
</state>
</command>
</extension>
<extension
point="org.eclipse.ui.bindings">
<key
commandId="fr.comp.common.styles.defaultStyle"
contextId="org.eclipse.ui.contexts.window"
schemeId="fr.comp.app.scheme"
sequence="F1">
</key>
<key
commandId="fr.comp.common.styles.SepiaStyle"
contextId="org.eclipse.ui.contexts.window"
schemeId="fr.comp.app.scheme"
sequence="F6">
</key>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="fr.comp.app.style.toolbar">
<command
commandId="fr.comp.common.styles.defaultStyle"
icon="resources/default.png"
style="radio">
</command>
<command
commandId="fr.comp.common.styles.SepiaStyle"
icon="resources/sepia.png"
style="radio">
</command>
</toolbar>
</menuContribution>
</extension>
</plugin>
My Handlers execute look like this:
public Object execute(ExecutionEvent event) throws ExecutionException {
// blabla change the style
final IStyleManager styleManager = MagicHub.getService(IStyleManager.class);
// ... end blabla
// change the state of the org.eclipse.ui.commands.radioState of the cmd
Command command = event.getCommand();
State state = command.getState(RadioState.STATE_ID);
state.setValue(Boolean.TRUE);
return null;
}
Ok, now I've got a nice toolbar, that display my commands.
I can click them, and the style apply.
I can use my keybinding too, and the style apply too.
But, when I use keybinding, radios button is not toggle properly.
What am I doing wrong ?
BTW: my target platform is Kepler.
In my current project, I have created popup menus using eclipse plugin. Following is the code of plugin.xml.
<?xml version="1.0" encoding="windows-1252"?>
<?eclipse version="3.0"?>
<plugin>
<extension point="org.eclipse.emf.ecore.generated_package">
<package
uri = "http://www.xtext.org/example/mydsl/MyDsl"
class = "org.xtext.example.mydsl.myDsl.MyDslPackage"
genModel = "model/generated/MyDsl.genmodel" />
</extension>
<extension
point="org.eclipse.ui.popupMenus">
<objectContribution
id="org.xtext.example.mydsl.contribution1"
objectClass="org.eclipse.core.resources.IFile">
<menu
id="org.xtext.example.mydsl.menu1"
label="IoTSuite Compilation"
path="additions">
<separator
name="group1">
</separator>
</menu>
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="or">
<test
property="org.eclipse.core.resources.IFile"
value="vocab.mydsl">
<action
class="org.xtext.example.mydsl.popup.actions.CompileVocabSpec"
enablesFor="1"
id="org.xtext.example.mydsl.newAction"
label="Compile Vocab.mydsl"
menubarPath="org.xtext.example.mydsl.menu1/group1">
</action>
</test>
</iterate>
</visibleWhen>
<action
class="org.xtext.example.mydsl.popup.actions.CompileArchSpec"
enablesFor="1"
id="org.xtext.example.mydsl.newAction"
label="Compile Arch.mydsl"
menubarPath="org.xtext.example.mydsl.menu1/group1">
</action>
<action
class="org.xtext.example.mydsl.popup.actions.CompileInteractionSpec"
enablesFor="1"
id="org.xtext.example.mydsl.newAction"
label="Compile Interaction.mydsl"
menubarPath="org.xtext.example.mydsl.menu1/group1">
</action>
<action
class="org.xtext.example.mydsl.popup.actions.CompileDeploySpec"
enablesFor="1"
id="org.xtext.example.mydsl.newAction"
label="Compile Deploy.mydsl"
menubarPath="org.xtext.example.mydsl.menu1/group1">
</action>
</objectContribution>
When I run above eclipse plugin application, it displays popup-menu as follows.Click here for image. The problem is, as shown in image I have four files vocab.mydsl, arch.mydsl, userinteraction.mydsl and deploy.mydsl and also four actions (Compile Vocab.mydsl, Compile Arch.mydsl, Compile Interaction.mydsl and Compile Deploy.mydsl ) under a popup menu. Now I want to customize action in such way that, when I click on vocab.mydsl then it should display only Compile Vocab.mydsl as an action in popup menu similarly when I click in arch.mydsl than it should display only Compile Arch.mydsl etc. I have made changes as per suggestion, but it display error like MESSAGE Plugin org.xtext.example.mydsl, extension org.eclipse.ui.popupMenus: Unknown extension tag found: visibleWhen. Am I missing something ??
Edit
Finally partial problem is solved using comment. Content of updated plugin.xml file is as below:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="menu:org.eclipse.ui.main.menu">
<command
commandId="org.xtext.example.mydsl.popup.actions.CompileVocabSpec"
defaultHandler="org.xtext.example.mydsl.popup.actions.CompileVocabSpec"
label="Compile Vocab"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="or">
<test
property="org.eclipse.core.resources.name"
value="vocab.mydsl">
</test>
</iterate>
</visibleWhen>
</command>
<command
commandId="org.xtext.example.mydsl.popup.actions.CompileArchSpec"
defaultHandler="org.xtext.example.mydsl.popup.actions.CompileArchSpec"
label="Compile Arch"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="or">
<test
property="org.eclipse.core.resources.name"
value="arch.mydsl">
</test>
</iterate>
</visibleWhen>
</command>
</menuContribution>
</extension>
</plugin>
I am very new to this eclipse plugin development. When I fired Compile Vocab command than it should performed action mentioned in CompileVocabSpec.java. The content of CompileVocabSpec.java is as below:
package org.xtext.example.mydsl.popup.actions;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
public class CompileVocabSpec implements IObjectActionDelegate {
private Shell shell;
/**
* Constructor for Action1.
*/
public CompileVocabSpec() {
super();
}
/**
* #see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
*/
#Override
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
shell = targetPart.getSite().getShell();
}
/**
* #see IActionDelegate#run(IAction)
*/
#Override
public void run(IAction action) {
String[] args = new String[3];
args[0] = "compile-vocab-spec";
args[1] = "C:/Template/";
// Call to Main method in ToolSuite
try {
// Main.main(args);
System.out.println("Compilation of Vocab");
} catch (Exception e) {
e.printStackTrace();
}
;
}
/**
* #see IActionDelegate#selectionChanged(IAction, ISelection)
*/
#Override
public void selectionChanged(IAction action, ISelection selection) {
}
}
Here when I fired Compile Vocab command it doesn't performed any action. Am I missing something ??
The org.eclipse.ui.popupMenus is deprecated and has been for a long time. You can only set visibility for the entire objectContribution and I don't think you can specify a single file name.
The org.eclipse.ui.menus extension point allows visibility of individual commands to be controlled.
For example:
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.ui.popup.any">
<command
commandId="test.command"
label="Command Vocab"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="or">
<test
property="org.eclipse.core.resources.name"
value="vocab.mydsl">
</test>
</iterate>
</visibleWhen>
</command>
</menuContribution>
The visibleWhen element is restricting the visibility to just the 'vocab.mydsl' file.
I want to show a particular menu contribution when user selects a particular project with project nature customnature . User can select any file or folder in the project with project nature customnature and it will show the menu also.
Currently I have the visibleWhen for the menu contribution as following:
<visibleWhen
checkEnabled="false">
<with
variable="activeMenuSelection">
<iterate>
<adapt type="org.eclipse.core.resources.IProject">
<and>
<test
property="org.eclipse.core.resources.projectNature"
value="customnature">
</test>
</and>
</adapt>
</iterate>
<count
value="1">
</count>
</with>
</visibleWhen>
This configuration successfully show menu when selecting project folder only.
Please give me some pointer to achieve this.
Just test for adapting to IResource instead of IProject:
<adapt
type="org.eclipse.core.resources.IResource">
<test
property="org.eclipse.core.resources.projectNature"
value="customnature">
</test>
</adapt>
I have achieve it using property tester, the class is as below:
public class ProjectNaturePropertyTester extends PropertyTester {
#Override
public boolean test(Object receiver, String property, Object[] args,
Object expectedValue) {
IResource rsc=(IResource)receiver;
try {
IProject project = rsc.getProject();
if(project.hasNature(CustomNature.NATURE_ID))
return true;
} catch (CoreException e) {
throw new RuntimeException("Problem getting nature from IResource" + e.getMessage() , e);
}
return false;
}
}
and plugin.xml
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="org.example.ui.propertytester.ProjectNaturePropertyTester"
id="ProjectNaturePropertyTester"
namespace="org.example.ui.propertytester"
properties="checkProjectNature"
type="org.eclipse.core.resources.IResource">
</propertyTester>
and using it
<visibleWhen
checkEnabled="false">
<with
variable="activeMenuSelection">
<iterate>
<adapt
type="org.eclipse.core.resources.IResource">
<test
property="org.example.ui.propertytester.checkProjectNature">
</test>
</adapt>
</iterate>
<count
value="1">
</count>
</with>
</visibleWhen>
The result is working when selecting a file/folder in a project, the menu item will show up.
I have a problem presumably with eclipse 3.5.1. I have a plugin that, besides other things, adds a string decoration to files that have been changed. Everything works fine in eclipse 3.7.1, but on the target application which stands on eclipse 3.5.1 the navigator does not display the styled string correctly.
The code for string decorations is as follows (it also adds the file version, which also does not work on eclipse 3.5.1):
public class CustomLabelProvider extends ResourceExtensionLabelProvider implements
org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider {
#Override
public StyledString getStyledText(Object element) {
StyledString label = new StyledString();
try {
Set modifiedFiles =
CustomContentProvider.getModifiedSegments().get(
((Resource) element).toString().substring(1));
if ((modifiedFiles != null) && (modifiedFiles.size() > 0)) {
label.append(">", StyledString.DECORATIONS_STYLER);
}
label.append(getText(element));
if (element instanceof ExtendedFile) {
Integer fileVersion = ((ExtendedFile) element).getFileVersion();
if (fileVersion != null) {
label.append(" ").append(fileVersion.toString(), StyledString.DECORATIONS_STYLER);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return label;
}
A comparison between both eclipse versions:
Both eclipse 3.5.1 and 3.7.1 implement IStyledLabelProvider. The plugin starts in both eclipse versions, osgi lists it as ACTIVE, and there is some logging that I cut out, that works in both eclipse versions. What could be the reason for this behaviour?
UPDATE: The label provider plugin.xml
<plugin>
<extension
point="org.eclipse.ui.navigator.viewer">
<viewer
viewerId="myApplication.NavigatorView">
</viewer>
<viewerContentBinding viewerId="myApplication.NavigatorView">
<includes>
<contentExtension pattern="NavigatorLabelProvider.NavigatorContent"/>
</includes>
</viewerContentBinding>
</extension>
<extension
point="org.eclipse.ui.navigator.navigatorContent">
<navigatorContent
name="myNavigatorContent"
activeByDefault="true"
contentProvider="labelprovider.CustomContentProvider"
labelProvider="labelprovider.CustomLabelProvider"
id="NavigatorLabelProvider.NavigatorContent">
<enablement>
<or>
<adapt type="org.eclipse.core.resources.IProject" />
<instanceof
value="org.eclipse.core.resources.IResource" />
</or>
</enablement>
<commonSorter
class="org.eclipse.ui.internal.navigator.resources.workbench.ResourceExtensionSorter"
id="org.eclipse.ui.navigator.resources.sorters.defaultSorter">
<parentExpression>
<or>
<instanceof value="org.eclipse.core.resources.IResource" />
</or>
</parentExpression>
</commonSorter>
<dropAssistant
class="org.eclipse.ui.navigator.resources.ResourceDropAdapterAssistant"
id="org.eclipse.ui.navigator.resources.resourceDropAdapter">
<possibleDropTargets>
<or>
<adapt type="org.eclipse.core.resources.IProject"/>
<adapt type="org.eclipse.core.resources.IFolder"/>
<adapt type="org.eclipse.core.resources.IFile"/>
</or>
</possibleDropTargets>
</dropAssistant>
<actionProvider
class="org.eclipse.ui.internal.navigator.resources.actions.EditActionProvider"
id="org.eclipse.ui.navigator.resources.actions.EditActions"/>
<actionProvider
class="org.eclipse.ui.internal.navigator.resources.actions.RefactorActionProvider"
id="org.eclipse.ui.navigator.resources.actions.RefactorActions"/>
<!-- Menu Shortcut Actions for Wizards -->
<commonWizard
type="new"
wizardId="org.eclipse.ui.wizards.new.folder">
<enablement>
<or>
<adapt type="org.eclipse.core.resources.IFile" />
<adapt type="org.eclipse.core.resources.IFolder" />
<adapt type="org.eclipse.core.resources.IProject" />
<adapt type="org.eclipse.core.resources.IWorkspaceRoot" />
</or>
</enablement>
</commonWizard>
<commonWizard
type="new"
wizardId="org.eclipse.ui.wizards.new.file">
<enablement>
<or>
<adapt type="org.eclipse.core.resources.IFile" />
<adapt type="org.eclipse.core.resources.IFolder" />
<adapt type="org.eclipse.core.resources.IProject" />
<adapt type="org.eclipse.core.resources.IWorkspaceRoot" />
</or>
</enablement>
</commonWizard>
</navigatorContent>
</extension>
</plugin>
I am looking for a solution of "link with editor" but for FormEditor instead of ViewPart as described in http://murygin.wordpress.com/2012/06/13/link-eclipse-view-to-editor/
I try to do similar. But the "link with editor" does not fire any action.
Thanks for help!
I found out that I could use "Navigator Link Helper":
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fextension-points%2Forg_eclipse_ui_navigator_linkHelper.html
Hier is my code in plugin.xml:
<extension point="org.eclipse.ui.navigator.viewer">
...
<viewerContentBinding ...>
<includes>
<contentExtension pattern="my.ui.navigator.linkHelper.myExplorer"/>
</includes>
</viewerContentBinding>
...
</extension>
<extension point="org.eclipse.ui.navigator.linkHelper">
<linkHelper
class="my.ui.navigator.MyExplorerLinkHelper"
id="my.ui.navigator.linkHelper.myExplorer">
<selectionEnablement>
<or>
<adapt type="org.eclipse.core.resources.IProject"/>
<instanceof value="org.eclipse.core.resources.IProject"/>
</or>
</selectionEnablement>
<editorInputEnablement>
<or>
<adapt type="org.eclipse.core.resources.IProject"/>
<instanceof value="org.eclipse.core.resources.IProject"/>
</or>
</editorInputEnablement>
</linkHelper>
</extension>
I implemented the class:
public class MyExplorerLinkHelper implements ILinkHelper {
#Override
public IStructuredSelection findSelection(IEditorInput anInput) {
...
}
#Override
public void activateEditor(IWorkbenchPage aPage, IStructuredSelection aSelection) {
...
}
}