The workbench uses core expressions (See
org.eclipse.core.expressions.definitions
for a description) for enabledWhen
and activeWhen
for handlers, programmatic
activation of contexts, and for visibleWhen
for menu contributions. Core expressions are also used
as conditionals for certain property page contribution and object contribution expressions, using the
enablement
element.
In most cases, the workbench provides the
IEvaluationContext
that command core expressions are evaluate against.
The IEvaluationService provides methods that allow other clients and extension point builders to hook into the workbench core expressions.
Core expressions are used declaratively in the plugin.xml files, and programmatically with some of the services provided in the workbench.
Note: Elements from workbench extension points that use core expressions, like enabledWhen
,
take one child core expression element.
They do not and their elements together.
Most of the tree or table like viewers return an IStructuredSelection. For example, the Project Explorer and Package Explorer.
When using the default variable you must treat it as an java.util.Collection
.
That means using <count> or <iterate>. The expression below returns true if all of
the selected items are Person
.
<enabledWhen> <iterate ifEmpty="false"> <instanceof value="org.eclipse.ui.examples.contributions.model.Person"/> </iterate> </enabledWhen>
This is equivalent to:
<enabledWhen> <with variable="selection"> <iterate ifEmpty="false"> <instanceof value="org.eclipse.ui.examples.contributions.model.Person"/> </iterate> </with> </enabledWhen>
The behaviour of iterate
is:
iterate
ands the results of evaluating its child expressions for each
element in the collection, unless you set the operator
attribute.iterate
has the same semantics for its children as <and>iterate
with an operator of and returns true if the collection is empty,
unless you set the ifEmpty
attribute.
If you want to be enabled if only one Person is selected, you can include a count
element.
<enabledWhen> <with variable="selection"> <count value="1"/> <iterate ifEmpty="false"> <instanceof value="org.eclipse.ui.examples.contributions.model.Person"/> </iterate> </with> </enabledWhen>
The same expression using the default variable:
<enabledWhen> <and> <count value="1"/> <iterate ifEmpty="false"> <instanceof value="org.eclipse.ui.examples.contributions.model.Person"/> </iterate> </and> </enabledWhen>
The Package Explorer is a mixture of
org.eclipse.core.resources.IResource,
org.eclipse.jdt.core.IJavaElement
and other classes. If you are trying to find all of
the "*.java" files, you would need to:
<enabledWhen> <with variable="selection"> <iterate ifEmpty="false"> <adapt type="org.eclipse.core.resources.IResource"> <test property="org.eclipse.core.resources.name" value="*.java"/> </adapt> </iterate> </with> </enabledWhen>
When working with the current selection and property testers it does not always make sense to use
adapt
. In these situations it is a good practice to check that the object under test
is a valid type for that property tester:
<enabledWhen> <with variable="selection"> <iterate ifEmpty="false"> <instanceof type="org.eclipse.core.resources.IResource"/> <test property="org.eclipse.core.resources.name" value="*.java"/> </iterate> </with> </enabledWhen>
If your handler should be enabled when your view or editor activates a context, you can use the
activeContexts
variable. Contexts are defined in the
org.eclipse.ui.contexts
extension point and activated programmatically using the
org.eclipse.ui.contexts.IContextService.
<enabledWhen> <with variable="activeContexts"> <iterate ifEmpty="false" operator="or"> <equals value="org.example.view.context"/> </iterate> </with> </enabledWhen>
For handlers that are to be contributed to a specific view or editor, you can use activeEditorId
and activePartId
in the activeWhen
clause. This is an example for a handler
that should be active in text editors:
<activeWhen> <with variable="activeEditorId"> <equals value="org.eclipse.ui.DefaultTextEditor"/> </with> </activeWhen>
The following clause is for a handler that's active while in the Project Explorer:
<activeWhen> <with variable="activePartId"> <equals value="org.eclipse.ui.navigator.ProjectExplorer"/> </with> </activeWhen>
As of 3.3 all org.eclipse.ui.actionSets (Deprecated)
generate a context with a parent of org.eclipse.ui.contexts.actionSet
. Contexts with
this parent are filtered from the
General > Keys preference page.
Showing an actionSet activates the matching context. This allows contributed commands to "join" actionSets, like the debug launch actionSet.
<enabledWhen> <with variable="activeContexts"> <iterate ifEmpty="false" operator="or"> <equals value="org.eclipse.debug.ui.launchActionSet"/> </iterate> </with> </enabledWhen>
Note: commands that are enabled or visible with actionSets are not currently displayed in the Customize Perspective Dialog.
The IEvaluationService
provides the global selection as the default variable (in a java.util.Collection
) for
expression evaluation.
It can either be empty, have one entry (if the
ISelection was something like an
ITextSelection),
or have the contents of an
IStructuredSelection.
The workbench publishes variables in ISources that can be used in the <with/> element and can be retrieved from the IEvaluationContext. Some of the variables may not be set, depending on the current application context when they are evaluated. The following table explains some of the more commonly used ones:
Name | Type | Description | Since |
---|---|---|---|
activeContexts | A java.util.Collection of java.lang.String |
This is a collection of the active context IDs as strings. Most commonly used with <iterate/>,
<count/>, and <test/> with a combined |
3.2 |
activeShell | org.eclipse.swt.widgets.Shell |
The currently active shell. It can be a dialog or workbench window shell. |
3.2 |
activeWorkbenchWindowShell | org.eclipse.swt.widgets.Shell |
The active workbench window shell. |
3.2 |
activeWorkbenchWindow | org.eclipse.ui.IWorkbenchWindow |
The active workbench window. |
3.2 |
activeWorkbenchWindow.isCoolbarVisible | java.lang.Boolean |
Reports coolbar visibility for the currently active workbench window. |
3.3 |
activeWorkbenchWindow.isPerspectiveBarVisible | java.lang.Boolean |
Reports perspective bar visibility for the currently active workbench window. |
3.3 |
activeWorkbenchWindow.activePerspective | java.lang.String |
Reports the name of the current perspective of the active workbench window. |
3.4 |
activeEditor | org.eclipse.ui.IEditorPart |
The currently active editor. This is remembered even if the editor is not the currently active part. |
3.2 |
activeEditorId | java.lang.String |
The ID of the currently active editor. This can be used for expressions on the editor type. |
3.2 |
activeEditorInput | org.eclipse.ui.IEditorInput |
The input of the currently active editor. This is useful for property testers. |
3.5 |
activePart | org.eclipse.ui.IWorkbenchPart |
The active part, which can be the same as the active editor. |
3.2 |
activePartId | java.lang.String |
The ID of the currently active part. |
3.2 |
activeSite | org.eclipse.ui.IWorkbenchPartSite |
The site of the currently active part. |
3.2 |
selection | org.eclipse.jface.viewers.ISelection |
The current global selection. It is often used with <test/> elements with
|
3.2 |
activeMenu | A java.util.Collection of java.lang.String |
This is the list of IDs of the showing context menu. Examples are like #TextEditorRuler or
a part ID. Most commonly used with <iterate/>, <count/>, and <test/> with
a combined |
3.2 |
activeMenuSelection | org.eclipse.jface.viewers.ISelection |
This is a selection that is available while a context menu is showing. It is the selection
from the selection provider used to register the context menu, usually from
getSite().registerContextMenu(*).
It is usually the same as the |
3.3 |
activeMenuEditorInput | org.eclipse.jface.viewers.ISelection |
This is a selection that is available while a context menu is showing. It is the selection from
the editor input, usually if includeEditorInput was set to |
3.3 |
activeFocusControl | org.eclipse.swt.widgets.Control |
A control that has focus and has been registered with the IFocusService. |
3.3 |
activeFocusControlId | java.lang.String |
The ID of a control that has focus and has been registered with the IFocusService. |
3.3 |
org.eclipse.core.runtime.Platform | org.eclipse.core.runtime.Platform |
The runtime Platform class is available, for use with property testers like |
3.3 |
The workbench provides a couple of property testers that can be used in core expressions.
The expression defines a property attribute and then takes a combination of 'args' and a 'value' that
is tester implementation dependent. The property attribute is the combination of the namespace
and property name. For example, to test an IResource name the property would be
org.eclipse.core.resources.name
.
Namespace | Type | Implementation |
---|---|---|
org.eclipse.core.runtime |
|
org.eclipse.core.internal.expressions.propertytester.PlatformPropertyTester
|
Property | Description | |
product |
Test the ID of the currently active product. |
|
isBundleInstalled |
Test if a given bundle is installed in the running environment. Use the 'args' attribute to pass in the bundle ID. |
|
bundleState |
Test the state of the bundle in the running environment. Use the 'args' attribute to pass in the bundle ID and the 'value' attribute to pass the state as defined in Bundle, e.g. "ACTIVE". |
|
Namespace | Type | Implementation |
org.eclipse.core.resources |
|
org.eclipse.core.internal.propertytester.ResourcePropertyTester
|
Property | Description | |
name |
A property indicating the file name (value |
|
path |
A property indicating the file path (value |
|
extension |
A property indicating the file extension (value |
|
readOnly |
A property indicating whether the file is read only (value |
|
projectNature |
A property indicating the project nature (value |
|
persistentProperty |
A property indicating a persistent property on the selected resource (value |
|
projectPersistentProperty |
A property indicating a persistent property on the selected resource's project.
(value |
|
sessionProperty |
A property indicating a session property on the selected resource
(value |
|
projectSessionProperty |
A property indicating a session property on the selected resource's project.
(value |
|
Namespace | Type | Implementation |
org.eclipse.core.resources |
|
org.eclipse.core.internal.propertytester.FilePropertyTester
|
Property | Description | |
contentTypeId |
A property indicating that we are looking to verify that the file matches the content type matching the given identifier. The identifier is provided as the expected value. |
|
Namespace | Type | Implementation |
org.eclipse.core.resources |
|
org.eclipse.core.internal.propertytester.ProjectPropertyTester
|
Property | Description | |
open |
A property indicating whether the project is open (value |
|
Namespace | Type | Implementation |
org.eclipse.core.resources |
|
org.eclipse.core.internal.propertytester.ResourceMappingPropertyTester
|
Property | Description | |
projectPersistentProperty |
A property indicating a persistent property on the selected resource's project.
(value |
|
Namespace | Type | Implementation |
org.eclipse.ui |
|
org.eclipse.ui.internal.activities.ActivityPropertyTester
|
Property | Description | |
isActivityEnabled |
Test if the activity in 'args' is enabled. |
|
isCategoryEnabled |
Test if the category in 'args' is enabled. |
|
Namespace | Type | Implementation |
org.eclipse.ui.workbenchWindow |
|
org.eclipse.ui.internal.OpenPerspectivePropertyTester
|
Property | Description | |
isPerspectiveOpen |
Tests if any perspective is open. |