Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Need help with 'menus' extension
Need help with 'menus' extension [message #16380] Sat, 16 August 2008 03:41 Go to next message
Craig Foote is currently offline Craig FooteFriend
Messages: 217
Registered: July 2009
Senior Member
I'm having trouble using the newer 'menus' extension. I have a toolbar
menu contribution and command with a default handler. How do I enable and
disable the toolbar button based some state of the view? I figure this
should be simple and I've tried many things but have had no success. Can
someone post a simple example please?
Re: Need help with 'menus' extension [message #16936 is a reply to message #16380] Sun, 17 August 2008 22:35 Go to previous messageGo to next message
Craig Foote is currently offline Craig FooteFriend
Messages: 217
Registered: July 2009
Senior Member
Anyone?

On Sat, 16 Aug 2008 03:41:19 +0000, Craig Foote wrote:

> I'm having trouble using the newer 'menus' extension. I have a toolbar
> menu contribution and command with a default handler. How do I enable
> and disable the toolbar button based some state of the view? I figure
> this should be simple and I've tried many things but have had no
> success. Can someone post a simple example please?
Re: Need help with 'menus' extension [message #17219 is a reply to message #16380] Tue, 19 August 2008 10:40 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nick.allen.onlinehome.de

Just noticed you are having the same problem as me. It seems there is no
easy way to do what we want. I found this possible solution but have not
been able to get it to work because noone will tell me how to get the
IHandlerService so I can register my own enabled expression for a
command handler. Let me know if you get anywhere though....

Possible solution I found:

if you only want your handler to be active when you conditions are met,
you can use handlerService.activateHandler("command.id", myHandler,
myExpression) to provide a programmatic core expression. It is just a
subclass of Expression.

Expression expression = new Expression() {
public void collectExpressionInfo(ExpressionInfo info) {
info.addVariableNameAccess(ISources.ACTIVE_CURRENT_SELECTION _NAME);
}
public EvaluationResult evaluate(IEvaluationContext context)
throws CoreException {
Object o = context
.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
if (o instanceof IStructuredSelection) {
// work with your selection and return TRUE
}
return EvaluationResult.FALSE;
}
};

Craig Foote wrote:
> I'm having trouble using the newer 'menus' extension. I have a toolbar
> menu contribution and command with a default handler. How do I enable and
> disable the toolbar button based some state of the view? I figure this
> should be simple and I've tried many things but have had no success. Can
> someone post a simple example please?
Re: Need help with 'menus' extension [message #19773 is a reply to message #16380] Sat, 06 September 2008 15:45 Go to previous message
Eclipse UserFriend
Originally posted by: mark.hewett.gmail.com

Craig Foote wrote:
> I'm having trouble using the newer 'menus' extension. I have a toolbar
> menu contribution and command with a default handler. How do I enable and
> disable the toolbar button based some state of the view? I figure this
> should be simple and I've tried many things but have had no success. Can
> someone post a simple example please?

Hopefully you've worked this out already, but here are some ideas -
although I've only used this for menu items (main or popup) I suppose it
will work for toolbar buttons.

First, you need to remove your default handler and configure it as a
handler for your command under the org.eclipse.ui.handlers extension
point. Default handlers are enabled/active by, err, default.

Once you have the handler defined (configured with your command id and
handler class) you can use the elements under handler to configure when
it is active and/or enabled with activeWhen and enabledWhen elements.
If you only have one handler for your command id, then the two elements
are probably interchangeable, but the docs for the
org.eclipse.ui.handlers extension point do a decent job of explaining
the difference.

What conditions you specify depends on what you want to use to trigger
the button to be enabled/disabled. For example, I often use the same
command id for multiple handlers (e.g. reusing a "built in" command like
org.eclipse.ui.edit.delete) and activating the appropriate handlers
based on the item that is selected in one of my views. For that, I use
something like the following:

<extension
point="org.eclipse.ui.handlers">
<handler
class="org.s3xplorer.ui.handlers.BucketDeleteHandler"
commandId="org.eclipse.ui.edit.delete">
<activeWhen>
<iterate
ifEmpty="false"
operator="and">
<instanceof
value="org.jets3t.service.model.S3Bucket">
</instanceof>
</iterate>
</activeWhen>
</handler>
<handler
class="org.s3xplorer.ui.handlers.ObjectDeleteHandler"
commandId="org.eclipse.ui.edit.delete">
<activeWhen>
<iterate
ifEmpty="false"
operator="and">
<instanceof
value="org.jets3t.service.model.S3Object">
</instanceof>
</iterate>
</activeWhen>
</handler>
</extension>

This activates the BucketDeleteHandler if one or more S3Bucket objects
are selected in the active part, and the ObjectDeleteHandler if one or
more S3Object objects are selected (both listed in table viewers in two
different views). There are a bunch of tests you can apply just using
XML - I'm not familiar with many of them, and I find the documentation
to be somewhat lacking, but with some trial and error hopefully you can
work something out.

You can also specify conditions which check properties which you
implement in code. You can create a subclass of
org.eclipse.core.expressions.PropertyTester:

public class ObjectListViewPropertyTester extends PropertyTester {

@Override
public boolean test(Object receiver, String property, Object[] args,
Object expectedValue) {
ObjectListView view = (ObjectListView) receiver;
if ("isBucketSelected".equals(property)) {
return view.getBucket() != null;
}
return false;
}

}

Define this under the org.eclipse.core.expressions.propertyTesters
extension point:

<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester

class=" org.s3xplorer.ui.propertytesters.ObjectListViewPropertyTeste r "
id="org.s3xplorer.ui.propertytesters.objectlistview"
namespace="org.s3xplorer.ui.views.ObjectListView"
properties="isBucketSelected"
type="org.s3xplorer.ui.views.ObjectListView">
</propertyTester>
</extension>

And then use this in your handler definition:

<handler
class="org.s3xplorer.ui.handlers.ObjectUploadHandler"
commandId="org.s3xplorer.ui.commands.uploadobject">
<enabledWhen>
<or>
<iterate
ifEmpty="false"
operator="and">
<instanceof
value="org.jets3t.service.model.S3Bucket">
</instanceof>
</iterate>
<with
variable="activePart">
<test

property="org.s3xplorer.ui.views.ObjectListView.isBucketSelected ">
</test>
</with>
</or>
</enabledWhen>
</handler>

The activePart variable used above is defined in org.eclipse.ui.ISources
as ACTIVE_PART_NAME - there are more variable you can use (I suppose
there may be a way to define your own).

Well, hopefully this gives you some ideas - if nothing else I'll have
somewhere to refer to when I forget all this in the near future!

Mark
Re: Need help with 'menus' extension [message #576655 is a reply to message #16380] Sun, 17 August 2008 22:35 Go to previous message
Craig Foote is currently offline Craig FooteFriend
Messages: 217
Registered: July 2009
Senior Member
Anyone?

On Sat, 16 Aug 2008 03:41:19 +0000, Craig Foote wrote:

> I'm having trouble using the newer 'menus' extension. I have a toolbar
> menu contribution and command with a default handler. How do I enable
> and disable the toolbar button based some state of the view? I figure
> this should be simple and I've tried many things but have had no
> success. Can someone post a simple example please?
Re: Need help with 'menus' extension [message #577043 is a reply to message #16380] Tue, 19 August 2008 10:40 Go to previous message
Nicholas Allen is currently offline Nicholas AllenFriend
Messages: 10
Registered: July 2009
Junior Member
Just noticed you are having the same problem as me. It seems there is no
easy way to do what we want. I found this possible solution but have not
been able to get it to work because noone will tell me how to get the
IHandlerService so I can register my own enabled expression for a
command handler. Let me know if you get anywhere though....

Possible solution I found:

if you only want your handler to be active when you conditions are met,
you can use handlerService.activateHandler("command.id", myHandler,
myExpression) to provide a programmatic core expression. It is just a
subclass of Expression.

Expression expression = new Expression() {
public void collectExpressionInfo(ExpressionInfo info) {
info.addVariableNameAccess(ISources.ACTIVE_CURRENT_SELECTION _NAME);
}
public EvaluationResult evaluate(IEvaluationContext context)
throws CoreException {
Object o = context
.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
if (o instanceof IStructuredSelection) {
// work with your selection and return TRUE
}
return EvaluationResult.FALSE;
}
};

Craig Foote wrote:
> I'm having trouble using the newer 'menus' extension. I have a toolbar
> menu contribution and command with a default handler. How do I enable and
> disable the toolbar button based some state of the view? I figure this
> should be simple and I've tried many things but have had no success. Can
> someone post a simple example please?
Re: Need help with 'menus' extension [message #578822 is a reply to message #16380] Sat, 06 September 2008 15:45 Go to previous message
Eclipse UserFriend
Originally posted by: mark.hewett.gmail.com

Craig Foote wrote:
> I'm having trouble using the newer 'menus' extension. I have a toolbar
> menu contribution and command with a default handler. How do I enable and
> disable the toolbar button based some state of the view? I figure this
> should be simple and I've tried many things but have had no success. Can
> someone post a simple example please?

Hopefully you've worked this out already, but here are some ideas -
although I've only used this for menu items (main or popup) I suppose it
will work for toolbar buttons.

First, you need to remove your default handler and configure it as a
handler for your command under the org.eclipse.ui.handlers extension
point. Default handlers are enabled/active by, err, default.

Once you have the handler defined (configured with your command id and
handler class) you can use the elements under handler to configure when
it is active and/or enabled with activeWhen and enabledWhen elements.
If you only have one handler for your command id, then the two elements
are probably interchangeable, but the docs for the
org.eclipse.ui.handlers extension point do a decent job of explaining
the difference.

What conditions you specify depends on what you want to use to trigger
the button to be enabled/disabled. For example, I often use the same
command id for multiple handlers (e.g. reusing a "built in" command like
org.eclipse.ui.edit.delete) and activating the appropriate handlers
based on the item that is selected in one of my views. For that, I use
something like the following:

<extension
point="org.eclipse.ui.handlers">
<handler
class="org.s3xplorer.ui.handlers.BucketDeleteHandler"
commandId="org.eclipse.ui.edit.delete">
<activeWhen>
<iterate
ifEmpty="false"
operator="and">
<instanceof
value="org.jets3t.service.model.S3Bucket">
</instanceof>
</iterate>
</activeWhen>
</handler>
<handler
class="org.s3xplorer.ui.handlers.ObjectDeleteHandler"
commandId="org.eclipse.ui.edit.delete">
<activeWhen>
<iterate
ifEmpty="false"
operator="and">
<instanceof
value="org.jets3t.service.model.S3Object">
</instanceof>
</iterate>
</activeWhen>
</handler>
</extension>

This activates the BucketDeleteHandler if one or more S3Bucket objects
are selected in the active part, and the ObjectDeleteHandler if one or
more S3Object objects are selected (both listed in table viewers in two
different views). There are a bunch of tests you can apply just using
XML - I'm not familiar with many of them, and I find the documentation
to be somewhat lacking, but with some trial and error hopefully you can
work something out.

You can also specify conditions which check properties which you
implement in code. You can create a subclass of
org.eclipse.core.expressions.PropertyTester:

public class ObjectListViewPropertyTester extends PropertyTester {

@Override
public boolean test(Object receiver, String property, Object[] args,
Object expectedValue) {
ObjectListView view = (ObjectListView) receiver;
if ("isBucketSelected".equals(property)) {
return view.getBucket() != null;
}
return false;
}

}

Define this under the org.eclipse.core.expressions.propertyTesters
extension point:

<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester

class=" org.s3xplorer.ui.propertytesters.ObjectListViewPropertyTeste r "
id="org.s3xplorer.ui.propertytesters.objectlistview"
namespace="org.s3xplorer.ui.views.ObjectListView"
properties="isBucketSelected"
type="org.s3xplorer.ui.views.ObjectListView">
</propertyTester>
</extension>

And then use this in your handler definition:

<handler
class="org.s3xplorer.ui.handlers.ObjectUploadHandler"
commandId="org.s3xplorer.ui.commands.uploadobject">
<enabledWhen>
<or>
<iterate
ifEmpty="false"
operator="and">
<instanceof
value="org.jets3t.service.model.S3Bucket">
</instanceof>
</iterate>
<with
variable="activePart">
<test

property="org.s3xplorer.ui.views.ObjectListView.isBucketSelected ">
</test>
</with>
</or>
</enabledWhen>
</handler>

The activePart variable used above is defined in org.eclipse.ui.ISources
as ACTIVE_PART_NAME - there are more variable you can use (I suppose
there may be a way to define your own).

Well, hopefully this gives you some ideas - if nothing else I'll have
somewhere to refer to when I forget all this in the near future!

Mark
Previous Topic:Eclipse PDE creates invalid zip files that can't be installed
Next Topic:How to enable or disable an action based on the kind of selection?
Goto Forum:
  


Current Time: Fri Apr 26 11:47:24 GMT 2024

Powered by FUDForum. Page generated in 0.03504 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top