Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Activities based on source variables(Problem with or statement in activity enabledWhen)
Activities based on source variables [message #509219] Thu, 21 January 2010 17:14 Go to next message
dwain Missing name is currently offline dwain Missing nameFriend
Messages: 35
Registered: October 2009
Member
I am attempting to use activities to set when a perspective is enabled or disabled. The activity has an enabled when that uses a source variable to determine if it is enabled or disabled. I have several source variables that could set the perspective to be visible so I am using an 'or' statement in my enabled when to accomplish this. However, it does not seem to be working properly for me, which I am certain is something that I am doing wrong, lol. If I do any one of these alone then it works perfectly. Otherwise it seems to only check the first variable and not bother to check the rest of the variables. So if the variable EMAIL.ADMIN is set to ENABLED, then the perspective shows up. But if EMAIL.ADMIN is not set to ENABLED and EMAIL_DELETE_EMAIL_ADDRESS is set to ENABLED, the perspective is not enabled. Here is what my activity looks like:

   <extension point="org.eclipse.ui.activities">
      <activity id="com.myExample.plugin.email.activity.emailPerspective" name="Email Perspective">
         <enabledWhen>
            <or>
               <with variable="EMAIL.ADMIN">
                  <equals value="ENABLED"> </equals>
               </with>
               <with variable="EMAIL_DELETE_EMAIL_ADDRESS">
                  <equals value="ENABLED"> </equals>
               </with>
               <with variable="EMAIL_DELETE_TEMPLATE">
                  <equals value="ENABLED"> </equals>
               </with>
               <with variable="EMAIL_FIND_EMAIL_AUDIT">
                  <equals value="ENABLED"> </equals>
               </with>
               <with variable="EMAIL_SAVE_TEMPLATE">
                  <equals value="ENABLED"> </equals>
               </with>
               <with variable="EMAIL_SAVE_EMAIL_ADDRESS">
                  <equals value="ENABLED"> </equals>
               </with>
               <with variable="EMAIL_FIND_TEMPLATES">
                  <equals value="ENABLED"> </equals>
               </with>
               <with variable="EMAIL_FIND_EMAIL_ADDRESS">
                  <equals value="ENABLED"> </equals>
               </with>
               <with variable="EMAIL_DELETE_AUDIT">
                  <equals value="ENABLED"> </equals>
               </with>
            </or>
         </enabledWhen>
      </activity>
      <activityPatternBinding activityId="com.myExample.plugin.email.activity.emailPerspective" pattern="com.myExample.plugin.email/com.myExample.plugin.email.perspective">
      </activityPatternBinding>
   </extension>


Thanks,
Dwain
Re: Activities based on source variables [message #509262 is a reply to message #509219] Thu, 21 January 2010 19:14 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Are there errors in your log?

Are your variables always set to something? i.e. Is your source
provider always returning
org.eclipse.core.expressions.IEvaluationContext.UNDEFINED_VA RIABLE for
variables that are effectively null?

PW

--
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/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Activities based on source variables [message #509280 is a reply to message #509262] Thu, 21 January 2010 20:02 Go to previous messageGo to next message
dwain Missing name is currently offline dwain Missing nameFriend
Messages: 35
Registered: October 2009
Member
Thanks for your reply Paul, there are no errors in my log files. I see where it is throwing the error for my undefined source variable now though. Maybe I am going about this the wrong way then. What I am really trying to do is create source variables dynamically. So if a user has a certain role, then when they log in they will get a source variable that can be used to turn certain features on or off. The reason I was looking at using source variables this way was so that I could use an enabledWhen on activities. Is there a way of dynamically declaring source variables, so that I don't have to declare all of my roles when I create the SourceProvider? Is there a way of setting a different WithExpression.class?

Or would it be possible for me to add a new "withNullSetFalse" definition that I could use with enabledWhen?

[Updated on: Thu, 21 January 2010 21:09]

Report message to a moderator

Re: Activities based on source variables [message #509915 is a reply to message #509280] Mon, 25 January 2010 18:43 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

The problem with "with" is that the variable referenced must either
exist, or must be IEvaluationContext.UNDEFINED_VARIABLE (or some other
useless value). Otherwise WithExpression throws a CoreException and the
entire expression (enabledWhen, activeWhen, etc) ends up being treated
as FALSE.

You have a couple of options. You can have one variable that's a
Collection or Set, "my.roles". Then your expressions look like:

<with variable="my.roles">
<iterate ifEmpty="false">
<equals value="role.I.care.about"/>
</iterate>
</with>

As my.roles is updated your expressions will be re-evaluated.

Or you can supply the user and a property tester:
<with variable="my.user">
<test property="my.role" value="role.I.care.about"/>
</with>

then you can use
org.eclipse.ui.services.IEvaluationService.requestEvaluation (String) and
have all roled based expression update when you know they need to be
re-evaluated.

Otherwise you would have to provide an extension to your source provider
that could tell "oh, these plugin contributions are using new variables
in their with, I better fill in the no-op value for them".

Let me know which one works out best for you.

Later,
PW


--
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/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Activities based on source variables [message #509969 is a reply to message #509915] Mon, 25 January 2010 22:28 Go to previous messageGo to next message
dwain Missing name is currently offline dwain Missing nameFriend
Messages: 35
Registered: October 2009
Member
Thanks for your help Paul. I was able to get it to work by doing the first one. My source provider just fills a list of all the users roles and that is what is provided. So my enabled whens look like:

         <enabledWhen>
            <and>
               <with variable="CSIRoles">
                  <iterate ifEmpty="false" operator="or">
                     <equals value="EMAIL_FIND_EMAIL_ADDRESS">
                     </equals>
                  </iterate>
               </with>
               <with variable="activeWorkbenchWindow.activePerspective">
                  <equals value="com.example.email.perspective">
                  </equals>
               </with>
            </and>
         </enabledWhen>


Would an addition to the with statement be a good enhancement? I was thinking that just adding a boolean field to the with like this:
http://farm5.static.flickr.com/4030/4304980766_2643e4ba84_b.jpg

And changing the WithExpression.java to something like the following would make it possible to have variables that aren't defined but the with would just evaluate to false rather then throwing an exception. I don't know if that is a good addition or not, but seems pretty easy and I can see some use in it, just a thought though.

public class WithExpression extends CompositeExpression {

	private String fVariable;
	private static final String ATT_VARIABLE= "variable";  //$NON-NLS-1$
	
	/**  the boolean flag indicating if you should return false or throw exception when a variable is null  */
	private boolean nullReturnsFalse = false;

	/**
	 * The seed for the hash code for all with expressions.
	 */
	private static final int HASH_INITIAL= WithExpression.class.getName().hashCode();

	public WithExpression(IConfigurationElement configElement) throws CoreException {
		fVariable= configElement.getAttribute(ATT_VARIABLE);
		Expressions.checkAttribute(ATT_VARIABLE, fVariable);
		
		String nullFalse = configElement.getAttribute("nullReturnsFalse"); //$NON-NLS-1$
		
		if(nullFalse != null)
			this.nullReturnsFalse = Boolean.parseBoolean(nullFalse);
	}
				.
				.
				.
				.
	public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
		Object variable= context.getVariable(fVariable);
		if (variable == null) {
			if(this.nullReturnsFalse)
				return EvaluationResult.FALSE;
			else
				throw new CoreException(new ExpressionStatus(
						ExpressionStatus.VARIABLE_NOT_DEFINED,
						Messages.format(ExpressionMessages.WithExpression_variable_not_defined, fVariable)));
		}
		if (variable == IEvaluationContext.UNDEFINED_VARIABLE) {
			return EvaluationResult.FALSE;
		}
		return evaluateAnd(new EvaluationContext(context, variable));
	}


Anyway, thanks again for the help Paul.

Dwain
Re: Activities based on source variables [message #510071 is a reply to message #509969] Tue, 26 January 2010 13:04 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

dwain wrote:

> And changing the WithExpression.java to something like the following
> would make it possible to have variables that aren't defined but the
> with would just evaluate to false rather then throwing an exception.

That's what we provided the EvaluationContext.UNDEFINED_VARIABLE for ...
core expressions has a long history and we needed to remain compatible
(and that meant throwing the exception, although that's not the
behaviour I need for the IEvaluationService).

If it became necessary your idea would be the way to go, since it is
opt-in (and schema is also considered API).

PW

--
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/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Previous Topic:Using asyncExec in TitleAreaDialog
Next Topic:Accessing native libraries from fragments using JNI
Goto Forum:
  


Current Time: Sun Dec 21 08:34:27 GMT 2014

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

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