# Handlers

###### Identifier:

org.eclipse.ui.handlers

###### Since:

3.1

###### Description:

The handlers extension point is an elaboration of the experimental `handlerSubmission` element defined in Eclipse 3.0. A handler is the behaviour of a command at a particular point in time. A command can have zero or more handlers associated with it. At any one point in time, however, a command will either have no active handler or one active handler. The active handler is the one which is currently responsible for carrying out the behaviour of the command. This is very similar to the concept of an action handler and a retargettable action.

The handlers extension point allows a plug-in developer to specify a handler that should become active and/or enabled under certain conditions. If a handler is inactive, then no command will delegate its behaviour to the handler. If a handler is disabled, then the handler will not be asked to execute; execution of the handler is blocked. The conditions are defined using the expression language facility added during 3.0. They are expressed using `activeWhen` and `enabledWhen` clauses.

The workbench provides some variables that these expressions can rely on. Variables that are valid in activeWhen and enabledWhen expressions can be found in `org.eclipse.ui.ISources`. The types of the variables are determined by the `org.eclipse.ui.ISourceProvider` that provides them.

A handler that specifies no conditions is a default handler. A default handler is only active if no other handler has all of its conditions satisfied. If two handlers still have conditions that are satisfied, then the conditions are compared. The idea is to select a handler whose condition is more specific or more local. To do this, the variables referred to by the condition are looked at. The condition that refers to the most specific variable "wins". The order of specificity (from least specific to most specific) is suggested in `org.eclipse.ui.ISources`.

If this still doesn't resolve the conflict, then no handler is active. If a particular tracing option is turned on, then this leads to a message in the log. A conflict can also occur if there are two default handlers. It is the responsibility of the plug-in developers and integration testers to ensure that this does not happen.

These conditions are used to avoid unnecessary plug-in loading. These handler definitions are wrapped in a proxy. For a proxy to load its underlying handler, two things must happen: the conditions for the proxy must be met so that it becomes active, and the command must be asked to do something which it must delegate (e.g., execute(), isEnabled()).

###### Configuration Markup:

<!ELEMENT extension (handler*)>

<!ATTLIST extension

point CDATA #REQUIRED

id CDATA #IMPLIED

name CDATA #IMPLIED

>

<!ELEMENT handler (activeWhen? , class? , enabledWhen?)>

<!ATTLIST handler

commandId IDREF #REQUIRED

class CDATA #IMPLIED

helpContextId CDATA #IMPLIED

>

Associated a command with a handler implementation.

- commandId -

  The id of the command to associate with this handler implementation.

- class -

  The handler class that imlements `org.eclipse.core.commands.IHandler` or extends `org.eclipse.core.commands.AbstractHandler`.

- helpContextId -

  The identifier of the help context that relates to this specific handler. While a command can provide a general description of a command's behaviour, it is sometimes appropriate for a handler to provide help more specific to their implementation.

  Since: 3.2

<!ELEMENT activeWhen (not | and | or | instanceof | test | systemTest | equals | count | with | resolve | adapt | iterate | reference)>

Contains a core expression used by the `IHandlerService` to determine when this handler is active.

<!ELEMENT enabledWhen (not | and | or | instanceof | test | systemTest | equals | count | with | resolve | adapt | iterate | reference)>

Contains a core expression used by the workbench handler proxy to determine when this handler is enabled without loading it.

<!ELEMENT class (parameter*)>

<!ATTLIST class

class CDATA #IMPLIED

>

Used when creating an `IExecutableExtension` with a named parameter, or more than one.

- class -

  The handler class that imlements `org.eclipse.core.commands.IHandler` or extends `org.eclipse.core.commands.AbstractHandler`.

<!ELEMENT parameter EMPTY>

<!ATTLIST parameter

name CDATA #REQUIRED

value CDATA #REQUIRED

>

A parameter for an `IExecutableExtension`.

- name -

  The parameter name.

- value -

  The parameter value.

<!ELEMENT enablement (not , and , or , instanceof , test , systemTest , equals , count , with , resolve , adapt , iterate , reference)*>

A generic root element. The element can be used inside an extension point to define its enablement expression. The children of an enablement expression are combined using the and operator.

<!ELEMENT not (not | and | or | instanceof | test | systemTest | equals | count | with | resolve | adapt | iterate | reference)>

This element represent a NOT operation on the result of evaluating it's sub-element expression.

<!ELEMENT and (not , and , or , instanceof , test , systemTest , equals , count , with , resolve , adapt , iterate , reference)*>

This element represent an AND operation on the result of evaluating all it's sub-elements expressions.

<!ELEMENT or (not , and , or , instanceof , test , systemTest , equals , count , with , resolve , adapt , iterate , reference)*>

This element represent an OR operation on the result of evaluating all it's sub-element expressions.

<!ELEMENT instanceof EMPTY>

<!ATTLIST instanceof

value CDATA #REQUIRED

>

This element is used to perform an instanceof check of the object in focus. The expression returns EvaluationResult.TRUE if the object's type is a sub type of the type specified by the attribute value. Otherwise EvaluationResult.FALSE is returned.

- value - a fully qualified name of a class or interface.

<!ELEMENT test EMPTY>

<!ATTLIST test

property CDATA #REQUIRED

args CDATA #IMPLIED

value CDATA #IMPLIED

forcePluginActivation (true | false)

>

This element is used to evaluate the property state of the object in focus. The set of testable properties can be extended using the propery tester extension point. The test expression returns EvaluationResult.NOT_LOADED if the property tester doing the actual testing isn't loaded yet and the attribute forcePluginActivation is set to false. If forcePluginActivation is set to true and the evaluation context used to evaluate this expression support plug-in activation then evaluating the property will result in activating the plug-in defining the tester.

- property - the name of an object's property to test.
- args - additional arguments passed to the property tester. Multiple arguments are separated by commas. Each individual argument is converted into a Java base type using the same rules as defined for the value attribute of the test expression.
- value - the expected value of the property. Can be omitted if the property is a boolean property. The test expression is supposed to return EvaluationResult.TRUE if the property matches the value and EvaluationResult.FALSE otherwise. The value attribute is converted into a Java base type using the following rules:
  - the string "true" is converted into Boolean.TRUE
  - the string "false" is converted into Boolean.FALSE
  - if the string contains a dot then the interpreter tries to convert the value into a Float object. If this fails the string is treated as a java.lang.String
  - if the string only consists of numbers then the interpreter converts the value in an Integer object.
  - in all other cases the string is treated as a java.lang.String
  - the conversion of the string into a Boolean, Float, or Integer can be suppressed by surrounding the string with single quotes. For example, the attribute value="'true'" is converted into the string "true"
- forcePluginActivation - a flag indicating whether the plug-in contributing the property tester should be loaded if necessary. As such, this flag should be used judiciously, in order to avoid unnecessary plug-in activations. Most clients should avoid setting this flag to true. This flag is only honored if the evaluation context used to evaluate this expression allows plug-in activation. Otherwise the flag is ignored and no plug-in loading takes place.

<!ELEMENT systemTest EMPTY>

<!ATTLIST systemTest

property CDATA #REQUIRED

value CDATA #REQUIRED

>

Tests a system property by calling the System.getProperty method and compares the result with the value specified through the value attribute.

- property - the name of an system property to test.
- value - the expected value of the property. The value is interpreted as a string value.

<!ELEMENT equals EMPTY>

<!ATTLIST equals

value CDATA #REQUIRED

>

This element is used to perform an equals check of the object in focus. The expression returns EvaluationResult.TRUE if the object is equal to the value provided by the attribute value. Otherwise EvaluationResult.FALSE is returned.

- value - the expected value. The value provided as a string is converted into a Java base type using the same rules as for the value attribute of the test expression.

<!ELEMENT count EMPTY>

<!ATTLIST count

value CDATA #REQUIRED

>

This element is used to test the number of elements in a collection.

- value - an expression to specify the number of elements in a list. Following wildcard characters can be used:
  *
    any number of elements
  ?
    no elements or one element
  +
    one or more elements
  !
    no elements
  -N)
    less than N elements
  (N-
    greater than N elements
  integer value
    the list must contain the exact number of elements

<!ELEMENT with (not , and , or , instanceof , test , systemTest , equals , count , with , resolve , adapt , iterate , reference)*>

<!ATTLIST with

variable CDATA #REQUIRED

>

This element changes the object to be inspected for all its child element to the object referenced by the given variable. If the variable can not be resolved then the expression will throw an ExpressionException when evaluating it. The children of a with expression are combined using the and operator.

- variable - the name of the variable to be used for further inspection. It is up to the evaluator of an extension point to provide the variable in the variable pool.

<!ELEMENT resolve (not , and , or , instanceof , test , systemTest , equals , count , with , resolve , adapt , iterate , reference)*>

<!ATTLIST resolve

variable CDATA #REQUIRED

args CDATA #IMPLIED

>

This element changes the object to be inspected for all its child element to the object referenced by the given variable. If the variable can not be resolved then the expression will throw an ExpressionException when evaluating it. The children of a resolve expression are combined using the and operator.

- variable - the name of the variable to be resolved. This variable is then used as the object in focus for child element evaluation. It is up to the evaluator of an extension point to provide a corresponding variable resolver (see IVariableResolver) through the evaluation context passed to the root expression element when evaluating the expression.
- args - additional arguments passed to the variable resolver. Multiple arguments are separated by commas. Each individual argument is converted into a Java base type using the same rules as defined for the value attribute of the test expression.

<!ELEMENT adapt (not , and , or , instanceof , test , systemTest , equals , count , with , resolve , adapt , iterate , reference)*>

<!ATTLIST adapt

type CDATA #REQUIRED

>

This element is used to adapt the object in focus to the type specified by the attribute type. The expression returns not loaded if either the adapter or the type referenced isn't loaded yet. It throws an ExpressionException during evaluation if the type name doesn't exist at all. The children of an adapt expression are combined using the and operator.

- type - the type to which the object in focus is to be adapted.

<!ELEMENT iterate (not , and , or , instanceof , test , systemTest , equals , count , with , resolve , adapt , iterate , reference)*>

<!ATTLIST iterate

operator (or|and)

ifEmpty (true | false)

>

This element is used to iterate over a variable that is of type java.util.Collection, or a variable that adapts to org.eclipse.core.expressions.IIterable. If the object in focus is not one of the above then a CoreException with an ExpressionStatus will be thrown while evaluating the expression. The child expressions of an iterate expression are combined using the and operator.

- operator - either "and" or "or". The operator defines how the results of all the child expressions applied to each child of the Collection or IIterable will be combined and what (if any) short circuit evaluation will be used. If not specified, "and" will be used.
- ifEmpty - the value return from the iterate expression if the collection is empty. If not specified then true is returned when the operator equals "and" and false is return if the operator equals "or".

<!ELEMENT reference EMPTY>

<!ATTLIST reference

definitionId IDREF #REQUIRED

>

This element is used to reference an expression from the org.eclipse.core.expressions.definitions extension point. The expression definition will be evaluated within the current expression element using the current evaluation context.

- definitionId - The unique id of an expression from org.eclipse.core.expressions.definitions.

###### Examples:

Variables that are valid in activeWhen and enabledWhen expressions can be found in `org.eclipse.ui.ISources`. The types of the variables are determined by the `org.eclipse.ui.ISourceProvider` that provides them.

    <extension
     point="org.eclipse.ui.handlers">
     <handler
      commandId="commandId"
      class="org.eclipse.compare.Command">
      <activeWhen>
       <with variable="selection">
        <count value="1" />
        <iterate operator="and">
         <adapt type="org.eclipse.core.resources.IResource" />
        </iterate>
       </with>
      </activeWhen>
     </handler>
     <handler
      commandId="other.commandId"
      class="org.eclipse.ui.TalkToMe">
      <activeWhen>
       <with variable="activePartId">
        <equals value="org.eclipse.ui.views.SampleView"/>
       </with>
      </activeWhen>
     </handler>
    </extension>

To further avoid plug-in loading, it is possible to specify when the handler is enabled. If the proxy has not yet loaded the handler, then only the expressions syntax is used to decide if the handler is enabled. If the proxy has loaded the handler, then the expressions syntax is consulted first. If the expressions syntax evaluates to true, then the handler is asked if it is enabled. (This is a short-circuit Boolean "and" operation between the expressions syntax and the handler's enabled state.)

    <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>

###### API Information:

All handlers implement `org.eclipse.core.commands.IHandler`, and can use `org.eclipse.core.commands.AbstractHandler` as a base class. Within the workbench, it is possible to activate and deactivate handlers using the `org.eclipse.ui.handlers.IHandlerService` interface. This interface can be retrieved from supporting workbench objects, such as `IWorkbench` itself, a workbench window, or a part site. To retrieve the service, you would make a call like `IWorkbench.getService(IHandlerService.class)`.

It is also possible to activate and deactive handlers using legacy code in the workbench. This can be done through the legacy mechanism shown below. This mechanism is useful to clients who are using actions to contribute to menus or toolbars. This is deprecated and not recommended.

     IWorkbenchPartSite mySite;
     IAction myAction;

     myAction.setActionDefinitionId(commandId);
     IKeyBindingService service = mySite.getKeyBindingService();
     service.registerAction(myAction);

Copyright (c) 2005, 2007 IBM Corporation and others.
All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html