Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » How do I create this handler expression at runtime?
How do I create this handler expression at runtime? [message #325018] Tue, 05 February 2008 17:36 Go to next message
Eclipse UserFriend
Originally posted by: none.ibm.com

From the help, there is a handler with an enablement expression.

<extension
point="org.eclipse.ui.handlers">
<handler
commandId="commandId"
class="org.eclipse.Handler">
<enabledWhen>
<with variable="activeContexts">
<iterator operator="or">
<equals value="org.eclipse.ui.contexts.window"/>
</iterator>
</with>
</enabledWhen>
</handler>
</extension>

How do I create this expression at runtime? Do I have to create a DOM and
run it through ExpressionConverter?
Re: How do I create this handler expression at runtime? [message #325021 is a reply to message #325018] Wed, 06 February 2008 01:06 Go to previous messageGo to next message
Eclipse UserFriend
I think the answer to your question is yes if you want to stick to the API.

If you don't want to create a DOM, you can use another mechanism that
defines extension points directly in XML, though I think that API is
subject to change. Here is an example of how it's used:

IExtensionRegistry reg = Platform.getExtensionRegistry();

StringBuffer sb = new StringBuffer();

sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<?eclipse version=\"3.2\"?>"
+ "<plugin>\n");
sb.append("<extension id='transform'"
+ " name='"
+ cap.getCategoryName()
+ "'"
+ " point='org.eclipse.ui.importWizards'>\n");
sb.append("<category "
+ " id='com.oaklandsw.transform.category' "
+ " name='"
+ cap.getCategoryName()
+ "'>"
+ " </category>\n");
sb.append(<more stuff>b);
sb.append("\n</extension></plugin>");

ByteArrayInputStream is = new ByteArrayInputStream(sb.toString()
.getBytes());

Object ut = ((ExtensionRegistry)reg).getTemporaryUserToken();

IContributor cont =
ContributorFactoryOSGi.createContributor(Platform
.getBundle(PLUGIN_ID));

if (!reg.addContribution(is, cont, false, null, null, ut))
Util.impossible("Contribution not added due to error");


Randy Hudson wrote:
> From the help, there is a handler with an enablement expression.
>
> <extension
> point="org.eclipse.ui.handlers">
> <handler
> commandId="commandId"
> class="org.eclipse.Handler">
> <enabledWhen>
> <with variable="activeContexts">
> <iterator operator="or">
> <equals value="org.eclipse.ui.contexts.window"/>
> </iterator>
> </with>
> </enabledWhen>
> </handler>
> </extension>
>
> How do I create this expression at runtime? Do I have to create a DOM and
> run it through ExpressionConverter?
>
>
Re: How do I create this handler expression at runtime? [message #325051 is a reply to message #325021] Wed, 06 February 2008 22:26 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: none.ibm.com

Thanks for the detailed example. I sure hope there is an easier way.

Here's what I'm trying to do. I have two instances of an action with a
keybinding. For example, "Select All". When the user is in one place of
the UI, I want one action to be active. And in another place, the other
action. I was thinking I could simply turn on and off the contexts for
these "places" or modes of the UI. But these contexts aren't of interest to
the User, so I didn't want to declare them and have them appear in the
keybinding preferences.

Shouldn't I be able to programmatically register a Handler with the
condition "context is A", or "color is blue"? My backup plan is to just
unregister and reregister actions with the keybinding service based on the
users context.

"Francis Upton" <francisu@ieee.org> wrote in message
news:fobiot$r0o$1@build.eclipse.org...
>I think the answer to your question is yes if you want to stick to the API.
>
> If you don't want to create a DOM, you can use another mechanism that
> defines extension points directly in XML, though I think that API is
> subject to change. Here is an example of how it's used:
Re: How do I create this handler expression at runtime? [message #325056 is a reply to message #325051] Thu, 07 February 2008 03:15 Go to previous messageGo to next message
Eclipse UserFriend
Yes, I think there is a much easier way to do this. I'm assuming you
are using 3.3 or higher. The key to the solution is your statement
about "when the user is in one place in the UI". Do you mean a
particular editor or view?

You can set handlers for commands which will be active only when the
Part is active. Like this:

// Do this when your part is initializing
IHandlerService handlerService = (IHandlerService)part.getSite()
.getService(IHandlerService.class);

handlerService.activateHandler(ID_COMMAND_COPY, new
AbstractHandler()
{
@Override
public Object execute(ExecutionEvent event)
throws ExecutionException
{
// Do the work
return null;
}
});

This handler is then made active whenever the part is active.

If you need more control, then you can associate an expression with your
handler in the configuration to check other conditions, like the current
selection.


Randy Hudson wrote:
> Thanks for the detailed example. I sure hope there is an easier way.
>
> Here's what I'm trying to do. I have two instances of an action with a
> keybinding. For example, "Select All". When the user is in one place of
> the UI, I want one action to be active. And in another place, the other
> action. I was thinking I could simply turn on and off the contexts for
> these "places" or modes of the UI. But these contexts aren't of interest to
> the User, so I didn't want to declare them and have them appear in the
> keybinding preferences.
>
> Shouldn't I be able to programmatically register a Handler with the
> condition "context is A", or "color is blue"? My backup plan is to just
> unregister and reregister actions with the keybinding service based on the
> users context.
>
> "Francis Upton" <francisu@ieee.org> wrote in message
> news:fobiot$r0o$1@build.eclipse.org...
>> I think the answer to your question is yes if you want to stick to the API.
>>
>> If you don't want to create a DOM, you can use another mechanism that
>> defines extension points directly in XML, though I think that API is
>> subject to change. Here is an example of how it's used:
>
>
Re: How do I create this handler expression at runtime? [message #325067 is a reply to message #325056] Thu, 07 February 2008 09:44 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: none.ibm.com

> Yes, I think there is a much easier way to do this. I'm assuming you are
> using 3.3 or higher. The key to the solution is your statement about
> "when the user is in one place in the UI". Do you mean a particular
> editor or view?

No, I mean a sub-section inside an editor or view. The editor is compound.

> You can set handlers for commands which will be active only when the Part
> is active. Like this:
>
> // Do this when your part is initializing
> IHandlerService handlerService = (IHandlerService)part.getSite()
> .getService(IHandlerService.class);

I want this, except "part" is not a workbench part, but an arbitrary "mode"
or perhaps an SWT control nested within my (editor)part.

> If you need more control, then you can associate an expression with your
> handler in the configuration to check other conditions, like the current
> selection.

I think I do need more control. The problem with associating an expression
seems to be that I have to do it at development time, instead of at runtime.
This seems like a gap in the expressions support. I shouldn't have to use
extensions unless I am extending some other plug-in. Also, I want to
inherit the command definition from the editor, but simply swap out the
handler based on sub-context. I don't want to define real command
definitions and contexts for each specific handler for the *same* command.
Re: How do I create this handler expression at runtime? [message #325074 is a reply to message #325018] Thu, 07 February 2008 11:12 Go to previous message
Eclipse UserFriend
Randy, I think you have 2 choices.

If you want to use the core expressions route, you have 2 subchoices.
One you've already found, you need to send an org.w3c.dom.Element into
ExpressionConverter.perform(*) or yank one of the IConfigurationElements
from a known contribution (i.e. if you know the same expression is used
in your plugin.xml somewhere).

The easiest is just use a programmatic expression:

Expression exp = new Expression() {
public void collectExpressionInfo(ExpressionInfo info) {
info.addVariableNameAccess(ISources.ACTIVE_CONTEXT_NAME);
}
public EvaluationResult evaluate(IEvaluationContext context) {
final Object variable = context
.getVariable(ISources.ACTIVE_CONTEXT_NAME);
if (variable instanceof Collection) {
if (((Collection) variable)
.contains("org.eclipse.ui.contexts.window")) { //$NON-NLS-1$
return EvaluationResult.TRUE;
}
}
return EvaluationResult.FALSE;
}
};


The other option is to use something similar to
org.eclipse.ui.actions.TextActionHandler. It creates a delegating
action, for the case where a select all action makes sense for the part,
but when you're in a text control the default select all handler makes
sense. It could be adapted to work for multiple handlers. The
delegating handler decides which specific handler to delegate for.


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm
Previous Topic:Getting the package fragment from the method any class
Next Topic:Custom ordering views on a stack
Goto Forum:
  


Current Time: Fri Apr 25 01:20:04 EDT 2025

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

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

Back to the top