For my Eclipse rcp application I want to use activities to show and hide some views. I read the Eclipse documentation about activities and tried to get a working example based on the 'Using expression-based activities' snippets from the documentation.
In the first step i created a new view and add a placeholder for it in my perspective class:
layout.addPlaceholder(View1.ID, IPageLayout.RIGHT, 0.5f, layout.getEditorArea());
Then i added my activity with a 'enabled when' expression and a binding:
<extension point="org.eclipse.ui.activities">
<activity id="org.project.activities.activity1" name="myActivity">
<enabledWhen>
<with variable="org.project.activities.sessionState">
<equals value="loggedIn"></equals>
</with>
</enabledWhen>
</activity>
</extension>
<activityPatternBinding
activityId="org.project.activities.activity1"
pattern="org.project.activities/org.project.activities.View1">
</activityPatternBinding>
In the last step i added my source-provider:
public class ActivitiySourceProvider extends AbstractSourceProvider {
public static final String SESSION_STATE = "org.project.activities.sessionState";
private static final String LOGGED_OUT = "loggedOut";
private static final String LOGGED_IN = "loggedIn";
private static final String[] SOURCE_NAMES = new String[] { SESSION_STATE };
private boolean loggedIn = false;
#Override
public Map<String, String> getCurrentState() {
Map<String, String> map = new HashMap<String, String>(1);
String value = loggedIn ? LOGGED_IN : LOGGED_OUT;
map.put(SESSION_STATE, value);
return map;
}
#Override
public String[] getProvidedSourceNames() {
return SOURCE_NAMES;
}
public void setLoggedIn() {
loggedIn = !loggedIn;
String value = loggedIn ? LOGGED_IN : LOGGED_OUT;
fireSourceChanged(ISources.WORKBENCH, SESSION_STATE, value);
}
}
When I start the test application my view 'View1' is hidden and when I toggle my variable the view is still hidden. To toggle my variable i used a handle and i don't receive any exceptions. I also tried to set my variable to explicit to 'loggedOut' at the application start, but i didn't worked either.
Did I missed something from the documentation?
Did you register your ActivitySourceProvider as source provider in an extension for extension point org.eclipse.ui.services? Otherwise it won't be used for expression evaluation.
Related
I'm building a custom text editor plugin for a domain specific language in Eclipse.
I can detect errors in the format of the editor contents and want to use eclipse's marker's to point out the errors to the user.
I have the following code in the plugin:
public static void createMarkerForResource(int linenumber, String message) throws CoreException {
IResource resource = getFile();
createMarkerForResource(resource, linenumber, message);
}
public static void createMarkerForResource(IResource resource, int linenumber, String message)
throws CoreException {
HashMap<String, Object> map = new HashMap<String, Object>();
MarkerUtilities.setLineNumber(map, linenumber);
MarkerUtilities.setMessage(map, message);
MarkerUtilities.createMarker(resource, map, IMarker.PROBLEM);
IMarker[] markers = resource.findMarkers(null, true, IResource.DEPTH_INFINITE);
for (IMarker marker : markers){
System.out.println("Marker contents"+MarkerUtilities.getMessage(marker));
}
}
I run this code with the command:
createMarkerForResource(2, "hello");
This successfully gives me an image on the correct line
and if I hover over it, I get a 'you can click this thing' cursor. But I can't get the message to turn up.
The message has definitely been placed, because the:
for (IMarker marker : markers){
System.out.println("Marker contents"+MarkerUtilities.getMessage(marker));
}
code produces the "Marker contentshello" output as expected. What am I doing wrong?
EDIT:
The message is appearing in the problem view:
The answer of Njol is correct and works for me (Eclipse Neon.1).
But two additional recommendations:
I am reusing a created field, so annotation hoover is not always new created (getter... not create method)
Default annotation hoover does show all annotations. So when you want only markers to be shown (and no others - e.g. Diff annotations from GIT) you should override isIncluded as in my following example.
Example:
import org.eclipse.jface.text.source.SourceViewerConfiguration;
...
public class MySourceViewerConfiguration extends SourceViewerConfiguration {
...
private IAnnotationHover annotationHoover;
...
public MySourceViewerConfiguration(){
this.annotationHoover=new MyAnnotationHoover();
}
...
#Override
public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
return annotationHoover;
}
}
And here the annotation hoover class
private class MyAnnotationHoover extends DefaultAnnotationHover{
#Override
protected boolean isIncluded(Annotation annotation) {
if (annotation instanceof MarkerAnnotation){
return true;
}
/* we do not support other annotations than markers*/
return false;
}
}
You need to use a proper IAnnotationHover, which can e.g. be defined in your SourceViewerConfiguration like this:
#Override
public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
return new DefaultAnnotationHover(false);
}
I am trying to use a property tester on a menu contribution that I have done for my eclipse plugin.
Basically I added a new menu in the main menu bar (by extending menu:org.eclipse.ui.main.menu and adding a menu). I then added my commands in there with the correct handlers.
Everything works as expected.
My only problem is that I am not able to decide when to have them active.
I am trying to use the activeWhen for my handlers. i want them to be active when there is certain data on a server.
I tried using a property tester but it does not get called everytime. It only gets called when you select a different view.
What is the correct way of doing this?
EDIT: here is the code I am using
http://pastebin.com/TGtZaBtM
My property tester runs because I print out stuff when it does.
The only problem is that it does not run every time the menu is opened.
I would like it to run every time so that I can check if a user is logged in or not.
I'm probably answering late...
Anyway to do that, I always define a sourceProvider and some variable using the org.eclipse.ui.services extension point:
As example for a pause action somewhere in a toolbar, here is the piece of code of the source provider and its definition in the plugins.xml:
<extension
point="org.eclipse.ui.services">
<sourceProvider
provider="DataCollectionSourceProvider">
<variable
name="Pause"
priorityLevel="workbench">
</variable>
</sourceProvider>
</extension>
source provider:
public class DataCollectionSourceProvider extends AbstractSourceProvider {
public final static String ID = "DataCollectionSourceProvider";
public final static String ID_PAUSED = "Pause";
public final static String VAL_TRUE = "TRUE";
public final static String VAL_FALSE = "FALSE";
/**
* #return the instance of this source provider in this workbench
*/
public static DataCollectionSourceProvider getInstance() {
ISourceProviderService sourceProviderService = ISourceProviderService)PlatformUI.getWorkbench().getService(ISourceProviderService.class);
DataCollectionSourceProvider dcProvider = (DataCollectionSourceProvider)sourceProviderService.getSourceProvider(ID);
return dcProvider;
}
private boolean paused = false;
public DataCollectionSourceProvider() {
// do nothing
}
#Override
public Map<?, ?> getCurrentState() {
String value = null;
Map<String, String> map = new HashMap<String, String>(2);
// fake variable (my id)
map.put(ID, VAL_TRUE);
// paused state
value = paused ? VAL_TRUE : VAL_FALSE;
map.put(ID_PAUSED, value);
return map;
}
#Override
public String[] getProvidedSourceNames() {
return new String[] { ID, ID_PAUSED };
}
public void setPaused(boolean paused) {
this.paused = paused;
String value = paused ? VAL_TRUE : VAL_FALSE;
fireSourceChanged(ISources.WORKBENCH, ID_PAUSED, value);
}
}
Then on your org.eclipse.ui.handlers contribution, add the enableWhen by using the variable from its defined id:
<extension
point="org.eclipse.ui.handlers">
<handler
commandId="__your_command_id__">
<class
class="__your_handler_class__">
</class>
<enabledWhen>
<with
variable="Pause">
<equals
value="FALSE">
</equals>
</with>
</enabledWhen>
</handler>
</extension>
At last, if you want to update the handler/action state, you just have to call the following piece of code somewhere in your code
DataCollectionSourceProvider.getInstance().setPause(...)
At a quick glance: You are using 'activeWhen' in your handler. You can probably try using 'enabledWhen' in the XML
You can also look into overriding isEnabled() in your Handler. This will work, when your plugin is activated. Look into the docs for more information.
I am trying to display checkboxlist with prechecked items.
The values are saved in a list in my database and for "editing" the user should be able to select new options as well as "uncheck" some of the earlier selected one.
Thats why I need to translate my List back to the checkboxlist...
Any idea how that could possibly work?
Thanks a lot!
I'll give you an example of how you can do it.
[1]edit.jsp page:
It is the page where you want to display your checked checkboxlist:
<s:checkboxlist name="type" list="typeList" />
here "type" is name of checkbox and "typeList" is a list which is loaded from actionclass in my case.
[2]loadEditData method in action class:
public class your_action_class_name extends ActionSupport {
private List<String> type;
private List<String> typeList;
public List<String> getType() {
return type;}
public void setType(List<String> type) {
this.type = type;}
public List<String> getTypeList() {
return typeList;
}
public void setTypeList(List<String> typeList) {
this.typeList = typeList;
}
public String loadEditData(){
tpyeList=\\add whole checkboxlist here;
type.add("value that you want to prechecked");
return SUCCESS;
}
}
[3]struts.xml:
<action name="edit" method="loadEditData" class="your_action_class_Name" >
<result name="success">/edit.jsp</result>
</action>
Now your flow is like follow:
1st call the edit Action that will implement loadEditData method and on returning success display edit.jsp page with checkboxlist which have prechecked value.
Is this answer helpful?
I am working on eclipse plugin that will allow a java bean to be dragged onto jsp file then on the drop event some code generators will be called.
I'm attempting to use the extension point "org.eclipse.ui.dropActions" but drag and drop listeners never get called .Is there any way to attach drag and drop listener to IFile object.
Am I on the right track with the DropActionDelegate?
Code:
DragListener
class DragListener implements DragSourceListener {
#Override
public void dragFinished(DragSourceEvent event) {
System.out.println("Finish");
}
#Override
public void dragSetData(DragSourceEvent event) {
PluginTransferData p;
p = new PluginTransferData (
"dream_action", // must be id of registered drop action
"some_data".getBytes() // may be of arbitrary type
);
event.data = p;
}
#Override
public void dragStart(DragSourceEvent event) {
// TODO Auto-generated method stub
System.out.println("Start");
}
}
DropActionDelegate
class DropActionDelegate implements IDropActionDelegate {
#Override
public boolean run(Object source, Object target) {
String Data= (String) target;
return true;
}
}
Plugin.xml
<extension point="org.eclipse.ui.dropActions">
<action
id="dream_action"
class="newdreamfileplugin.wizards.DropActionDelegate">
</action>
</extension>
Thanks.
Solved it.Finally i created my own navigator using org.eclipse.ui.navigator.navigatorContent extension which have a property dropAssistant.
JDT and other plugins for Eclipse decorate the editor title image with problem status (compilation errors etc.). In my plugin I want to mimic that behaviour.
However, looking at the sources, JDT seems to do a lot of extra handling to do the decoration.
Decorators, especially lightweight ditto, is a handy way of doing decorations on icons, but I can find no way to programatically enable them for the title image of an editor. (And I don't want to pull in all of JDT UI in my plugin...)
Is there such a way or do I need to implement my own ILabelProvider and then
public void updatedTitleImage(Image image) {
setTitleImage(image);
}
like the JavaEditor does?
There seems to be no way to use decorators with the editor title image (as of 3.7 at least).
I ended up creating a EditorLabelUpdator which implemented the IResourceChangeListener interface (to find out when markers changed), basically the resourceChanged() method. It then uses a simple decorator-type class built from the pattern of OverlayImageIcon (of which you can find a lot of examples on Google).
The constructor is called from the initalization of the editor, sending the editor as a parameter which is saved for getting at the resource and its title icon.
The editor also had to be amended with a callback method triggering the title icon updating (updatedTitleImage(Image image)).
This is the core of the code I got:
public void resourceChanged(IResourceChangeEvent event) {
if (isMarkerChangeForResource(event, editor)) {
/* Changes in markers on this resource, so re-decorate title image */
decorate();
}
}
private boolean isMarkerChangeForResource(IResourceChangeEvent event, AlanEditor editor) {
boolean isMarkerChangeForThisResource;
final IResource resource = ResourceUtil.getResource(editor.getEditorInput());
final IPath path = resource.getFullPath();
IResourceDelta delta = event.getDelta().findMember(path);
isMarkerChangeForThisResource = (delta != null) && ((delta.getFlags() & IResourceDelta.MARKERS) != 0);
return isMarkerChangeForThisResource;
}
public void decorate() {
Shell shell = editor.getEditorSite().getShell();
if (shell != null && !shell.isDisposed()) {
shell.getDisplay().syncExec(new Runnable() {
public void run() {
Image decoratedImage = decorateImage(editor.getTitleImage(), getSeverity());
editor.updatedTitleImage(decoratedImage);
}
});
}
}
private Image decorateImage(Image titleImage, int severity) {
final ImageRegistry registry = AlanIDEPlugin.getDefault().getImageRegistry();
String key = createKey(severity);
ImageDescriptor descriptor = AlanIDEPlugin.getImageDescriptor(key);
if (descriptor != null)
return descriptor.createImage();
OverlayImageDescriptor overlayImageDescriptor = buildDecoratedImage(severity, key);
registry.put(key, overlayImageDescriptor);
return overlayImageDescriptor.createImage();
}
private String createKey(int severity) {
String key;
switch (severity) {
case IMarker.SEVERITY_ERROR: key = EDITOR_TITLE_ICON + ".error"; break;
case IMarker.SEVERITY_WARNING: key = EDITOR_TITLE_ICON + ".warning"; break;
default: key = EDITOR_TITLE_ICON; break;
}
return key;
}
private OverlayImageDescriptor buildDecoratedImage(int severity, String key) {
ImageDescriptor overlay = null;
if (severity >= IMarker.SEVERITY_ERROR)
overlay = AlanIDEPlugin.getImageDescriptor("ovr16.error_ovr");
else if (severity == IMarker.SEVERITY_WARNING)
overlay = AlanIDEPlugin.getImageDescriptor("ovr16.warning_ovr");
ImageDescriptor baseImage = AlanIDEPlugin.getImageDescriptor(EDITOR_TITLE_ICON);
OverlayImageDescriptor overlayIcon = new OverlayImageDescriptor(baseImage);
if (overlay != null)
overlayIcon.addOverlay(overlay, IDecoration.BOTTOM_LEFT);
return overlayIcon;
}
private int getSeverity() {
int severity = 0;
try {
final IResource resource = ResourceUtil.getResource(editor.getEditorInput());
severity = resource.findMaxProblemSeverity(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
} catch (CoreException e) {
// Might be a project that is not open
}
return severity;
}
This was the simplest solution I could come up with.
Yes, there is an extension point which you can use to achieve that. See the code below:
<!--define the decorator -->
<extension point="org.eclipse.ui.decorators">
<decorator
adaptable="true"
class="org.example.PackageExplorerDecorator"
id="org.example.PackageExplorerDecorator"
label="File Decorator"
lightweight="true"
state="true">
<enablement>
<or>
<objectClass
name="org.eclipse.jdt.core.IMethod">
</objectClass>
<objectClass
name="org.eclipse.core.resources.IResource">
</objectClass>
</or>
</enablement>
For the PackageExplorerDecorator: extends LabelProvider implements ILightweightLabelDecorator