Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » How to enable or disable an action based on the kind of selection?
How to enable or disable an action based on the kind of selection? [message #16387] Sun, 17 August 2008 20:28 Go to next message
Eclipse UserFriend
Originally posted by: nick.allen.onlinehome.de

Hi,

I am new to Eclipse plugin development and am having a problem that I
thought would be easy but doesn't seem to be.

Basically, I have some actions in my plugin that appear in Eclipse's
menu bar and I want to change their enabled state based on the current
selection.

What I assumed I would have to do is override the selectionChanged
method from the IWorkbenchWindowActionDelegate interface and use the
selection passed in to decide and update the action's enabled state.
Unfortunately, this does not work because the delegate is only created
after it has been invoked at least once. This means it starts off
enabled and after I select it I get an error from Eclipse "Operation not
available" and then the correct enabled state is set on it based on the
selection.

Of course, I don't want the user to have to invoke my action at least
once in order to get the correct enabled state shown. Basically, I need
to know when selection is changed before my action delegate has been
invoked. How do I do this?

Any help greatly appreciated.

Many thanks in advance,

Nicholas Allen
Re: How to enable or disable an action based on the kind of selection? [message #16964 is a reply to message #16387] Mon, 18 August 2008 08:43 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nick.allen.onlinehome.de

Could it be I need to use the handlers and menu extension instead of the
action sets extension?

Nick

Nicholas Allen wrote:
> Hi,
>
> I am new to Eclipse plugin development and am having a problem that I
> thought would be easy but doesn't seem to be.
>
> Basically, I have some actions in my plugin that appear in Eclipse's
> menu bar and I want to change their enabled state based on the current
> selection.
>
> What I assumed I would have to do is override the selectionChanged
> method from the IWorkbenchWindowActionDelegate interface and use the
> selection passed in to decide and update the action's enabled state.
> Unfortunately, this does not work because the delegate is only created
> after it has been invoked at least once. This means it starts off
> enabled and after I select it I get an error from Eclipse "Operation not
> available" and then the correct enabled state is set on it based on the
> selection.
>
> Of course, I don't want the user to have to invoke my action at least
> once in order to get the correct enabled state shown. Basically, I need
> to know when selection is changed before my action delegate has been
> invoked. How do I do this?
>
> Any help greatly appreciated.
>
> Many thanks in advance,
>
> Nicholas Allen
Re: How to enable or disable an action based on the kind of selection? [message #17020 is a reply to message #16387] Mon, 18 August 2008 17:53 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nick.allen.onlinehome.de

I have tried everything I can think of and being new to Eclipse I would
really appreciate if someone more experienced could at least point me in
the right direction. It is such a simple requirement but I cannot find
any way to do it.

Nick
Re: How to enable or disable an action based on the kind of selection? [message #17049 is a reply to message #16387] Mon, 18 August 2008 19:08 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nick.allen.onlinehome.de

I found this after 2 days of searching. Am I on the right track? Is this
what I need to do? If so, how do I get the IHandlerService in order to
register my expression that decides when my handler is enabled?

The Eclipse documentation is extremely poor with regard to this.

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;
}
};
Re: How to enable or disable an action based on the kind of selection? [message #17133 is a reply to message #17049] Tue, 19 August 2008 16:01 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: annamalai.ancitconsulting.com

Dear Nicholas

I think if the action is created using ActionSet then it can be achieved
using Selection or Enablement available on the Action ... U can right click
on an action and then you will get these Choices where u can Disable or
enable based on Condition ...

Not really sure if you have been using Menu, Command etc .. then i think
your suggested way looks ok ...

Hope that helps.

Regards
Malai
"Nicholas Allen" <nick.allen@onlinehome.de> wrote in message
news:g8chah$82b$1@build.eclipse.org...
>I found this after 2 days of searching. Am I on the right track? Is this
> what I need to do? If so, how do I get the IHandlerService in order to
> register my expression that decides when my handler is enabled?
>
> The Eclipse documentation is extremely poor with regard to this.
>
> 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;
> }
> };
Re: How to enable or disable an action based on the kind of selection? [message #17190 is a reply to message #17133] Tue, 19 August 2008 10:37 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nick.allen.onlinehome.de

My condition is not something that can be expressed in the XML file for
an action. Basically, I'm writing a plug-in that integrates with the
Bazaar version control system. I need to enable a command based on
whether there is a file selected and it is inside a Bazaar branch (i.e
there is a ".bzr" directory it it or one of its parent directories).

I read the Eclipse documentation and noticed that Action sets are
deprecated in favour of commands and the menu extension. I am now using
this but I still have not found a way to enable or disable my command
handler.

Any help greatly appreciated!

Nick

Malai wrote:
> Dear Nicholas
>
> I think if the action is created using ActionSet then it can be achieved
> using Selection or Enablement available on the Action ... U can right click
> on an action and then you will get these Choices where u can Disable or
> enable based on Condition ...
>
> Not really sure if you have been using Menu, Command etc .. then i think
> your suggested way looks ok ...
>
> Hope that helps.
>
> Regards
> Malai
> "Nicholas Allen" <nick.allen@onlinehome.de> wrote in message
> news:g8chah$82b$1@build.eclipse.org...
>> I found this after 2 days of searching. Am I on the right track? Is this
>> what I need to do? If so, how do I get the IHandlerService in order to
>> register my expression that decides when my handler is enabled?
>>
>> The Eclipse documentation is extremely poor with regard to this.
>>
>> 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;
>> }
>> };
>
>
Re: How to enable or disable an action based on the kind of selection? [message #19806 is a reply to message #17190] Sat, 06 September 2008 15:50 Go to previous message
Eclipse UserFriend
Originally posted by: mark.hewett.gmail.com

Nicholas Allen wrote:
> My condition is not something that can be expressed in the XML file for
> an action. Basically, I'm writing a plug-in that integrates with the
> Bazaar version control system. I need to enable a command based on
> whether there is a file selected and it is inside a Bazaar branch (i.e
> there is a ".bzr" directory it it or one of its parent directories).
>
> I read the Eclipse documentation and noticed that Action sets are
> deprecated in favour of commands and the menu extension. I am now using
> this but I still have not found a way to enable or disable my command
> handler.
>
> Any help greatly appreciated!
>
> Nick

See my reply to "Need help with 'menus' extension" posted a few minutes
ago for some ideas about using your own property tester to achieve what
you want.

Mark
Re: How to enable or disable an action based on the kind of selection? [message #576692 is a reply to message #16387] Mon, 18 August 2008 08:43 Go to previous message
Nicholas Allen is currently offline Nicholas AllenFriend
Messages: 10
Registered: July 2009
Junior Member
Could it be I need to use the handlers and menu extension instead of the
action sets extension?

Nick

Nicholas Allen wrote:
> Hi,
>
> I am new to Eclipse plugin development and am having a problem that I
> thought would be easy but doesn't seem to be.
>
> Basically, I have some actions in my plugin that appear in Eclipse's
> menu bar and I want to change their enabled state based on the current
> selection.
>
> What I assumed I would have to do is override the selectionChanged
> method from the IWorkbenchWindowActionDelegate interface and use the
> selection passed in to decide and update the action's enabled state.
> Unfortunately, this does not work because the delegate is only created
> after it has been invoked at least once. This means it starts off
> enabled and after I select it I get an error from Eclipse "Operation not
> available" and then the correct enabled state is set on it based on the
> selection.
>
> Of course, I don't want the user to have to invoke my action at least
> once in order to get the correct enabled state shown. Basically, I need
> to know when selection is changed before my action delegate has been
> invoked. How do I do this?
>
> Any help greatly appreciated.
>
> Many thanks in advance,
>
> Nicholas Allen
Re: How to enable or disable an action based on the kind of selection? [message #576772 is a reply to message #16387] Mon, 18 August 2008 17:53 Go to previous message
Nicholas Allen is currently offline Nicholas AllenFriend
Messages: 10
Registered: July 2009
Junior Member
I have tried everything I can think of and being new to Eclipse I would
really appreciate if someone more experienced could at least point me in
the right direction. It is such a simple requirement but I cannot find
any way to do it.

Nick
Re: How to enable or disable an action based on the kind of selection? [message #576793 is a reply to message #16387] Mon, 18 August 2008 19:08 Go to previous message
Nicholas Allen is currently offline Nicholas AllenFriend
Messages: 10
Registered: July 2009
Junior Member
I found this after 2 days of searching. Am I on the right track? Is this
what I need to do? If so, how do I get the IHandlerService in order to
register my expression that decides when my handler is enabled?

The Eclipse documentation is extremely poor with regard to this.

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;
}
};
Re: How to enable or disable an action based on the kind of selection? [message #576926 is a reply to message #17049] Tue, 19 August 2008 16:01 Go to previous message
Eclipse UserFriend
Originally posted by: annamalai.ancitconsulting.com

Dear Nicholas

I think if the action is created using ActionSet then it can be achieved
using Selection or Enablement available on the Action ... U can right click
on an action and then you will get these Choices where u can Disable or
enable based on Condition ...

Not really sure if you have been using Menu, Command etc .. then i think
your suggested way looks ok ...

Hope that helps.

Regards
Malai
"Nicholas Allen" <nick.allen@onlinehome.de> wrote in message
news:g8chah$82b$1@build.eclipse.org...
>I found this after 2 days of searching. Am I on the right track? Is this
> what I need to do? If so, how do I get the IHandlerService in order to
> register my expression that decides when my handler is enabled?
>
> The Eclipse documentation is extremely poor with regard to this.
>
> 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;
> }
> };
Re: How to enable or disable an action based on the kind of selection? [message #576985 is a reply to message #17133] Tue, 19 August 2008 10:37 Go to previous message
Nicholas Allen is currently offline Nicholas AllenFriend
Messages: 10
Registered: July 2009
Junior Member
My condition is not something that can be expressed in the XML file for
an action. Basically, I'm writing a plug-in that integrates with the
Bazaar version control system. I need to enable a command based on
whether there is a file selected and it is inside a Bazaar branch (i.e
there is a ".bzr" directory it it or one of its parent directories).

I read the Eclipse documentation and noticed that Action sets are
deprecated in favour of commands and the menu extension. I am now using
this but I still have not found a way to enable or disable my command
handler.

Any help greatly appreciated!

Nick

Malai wrote:
> Dear Nicholas
>
> I think if the action is created using ActionSet then it can be achieved
> using Selection or Enablement available on the Action ... U can right click
> on an action and then you will get these Choices where u can Disable or
> enable based on Condition ...
>
> Not really sure if you have been using Menu, Command etc .. then i think
> your suggested way looks ok ...
>
> Hope that helps.
>
> Regards
> Malai
> "Nicholas Allen" <nick.allen@onlinehome.de> wrote in message
> news:g8chah$82b$1@build.eclipse.org...
>> I found this after 2 days of searching. Am I on the right track? Is this
>> what I need to do? If so, how do I get the IHandlerService in order to
>> register my expression that decides when my handler is enabled?
>>
>> The Eclipse documentation is extremely poor with regard to this.
>>
>> 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;
>> }
>> };
>
>
Re: How to enable or disable an action based on the kind of selection? [message #578870 is a reply to message #17190] Sat, 06 September 2008 15:50 Go to previous message
Eclipse UserFriend
Originally posted by: mark.hewett.gmail.com

Nicholas Allen wrote:
> My condition is not something that can be expressed in the XML file for
> an action. Basically, I'm writing a plug-in that integrates with the
> Bazaar version control system. I need to enable a command based on
> whether there is a file selected and it is inside a Bazaar branch (i.e
> there is a ".bzr" directory it it or one of its parent directories).
>
> I read the Eclipse documentation and noticed that Action sets are
> deprecated in favour of commands and the menu extension. I am now using
> this but I still have not found a way to enable or disable my command
> handler.
>
> Any help greatly appreciated!
>
> Nick

See my reply to "Need help with 'menus' extension" posted a few minutes
ago for some ideas about using your own property tester to achieve what
you want.

Mark
Previous Topic:Need help with 'menus' extension
Next Topic:How to open programmatically my Preference Page?
Goto Forum:
  


Current Time: Sat May 11 02:21:30 GMT 2024

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

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

Back to the top