It is possible to associate arbitrary properties with a Job
, which can be used to present its progress in different ways. For example, by specifying a command it's possible to click on a running Job
and then execute something in the user interface, such as a detailed job description. Job
properties are set with setProperty()
, and can include any key/value combination. The keys use a QualifiedName
, which is like a pair of strings for namespace/value. In the case of the Progress view, there is an IProgressConstants2
interface, which defines values that can be set, including COMMAND_PROPERTY
, which can be used to invoke a command.
HelloHandler
and go to the end of the execute()
method. Just before the Job
is scheduled, acquire the Command
from the ICommandService
and then stamp it on the Job
as a property.ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); Command command = service == null ? null : service.getCommand("com.packtpub.e4.clock.ui.command.hello"); if(command != null) { job.setProperty(IProgressConstants2.COMMAND_PROPERTY,command); } job.schedule() return null;
E4: In E4, the ICommandService
can be obtained via injection, using @Inject ICommandService
service. Injection will be covered in more detail in Chapter 7, Understanding the Eclipse 4 Model.
ParameterizedCommand
instead. Modify the property value, and using the generateCommand()
factory of ParameterizedCommand
.if(command != null) { //The following commented line needs to be removed /*job.setProperty(IProgressConstants2.COMMAND_PROPERTY,command);*/ job.setProperty(IProgressConstants2.COMMAND_PROPERTY, ParameterizedCommand.generateCommand(command, null)); }
HelloHandler
and launches another job instance. Each click will spawn off a new Job
:ImageDescriptor
as a Job
property with the key ICON_PROPERTY
. The image descriptor can be loaded from the createFromURL()
method of ImageDescriptor
and set as a property.job.setProperty(IProgressConstants2.ICON_PROPERTY, ImageDescriptor.createFromURL( HelloHandler.class.getResource("/icons/sample.gif")));
Setting properties on the running Job
allows viewers to extract information and present it in different ways. Properties are specified with a QualifiedName
key and the value is passed in as an object, which is property specific.
The purpose of the QualifiedName
key is to act as a string identifier, but partitioned into different namespaces. For example, the properties used by the IProgressConstants
use org.eclipse.ui.workbench.progress
as the namespace qualifier, and shorter strings such as command
and icon
for individual properties. The benefit of this (instead of org.eclipse.ui.workbench.properties.command
) is that the long prefix string is stored once in memory and so doesn't take up repeated space in either the class file or the PermGen space, which can be limited on JDK 7. (JDK 8 will remove the PermGen, so it should be less of an issue in the future.)
Valid values for the Job
in the Progress view can be found in the IProgressConstants
and IProgressContstants2
interfaces. Note that this is not a fixed set; additional Job
properties can be added for use both elsewhere in the Eclipse platform and by independent extensions.
To associate a Command
with a Job
, set a property which contains a ParameterizedCommand
. The factory method generateCommand()
on the ParameterizedCommand
class can be used to convert a command into a ParameterizedCommand
.
The Command
can be acquired from the ICommandService
, which is acquired via the PlatformUI
workbench or through injection in E4.
The IProgressConstants2
interface also defines a property name SHOW_IN_TASKBAR_ICON_PROPERTY
, which shows whether the progress of the Job
is exposed to those operating systems that support it. On OS X, a bar will be shown over the Eclipse application icon. Set the property to the value Boolean.TRUE
and see the effect it has on the Job
.
The Job
can also indicate if it is running in the foreground or the background, and can query its state via the Job
property PROPERTY_IN_DIALOG
. This is not intended to be set by clients, but can be read and displayed (or different actions be taken).
Q1. What is the difference between Display.syncExec()
and Display.asyncExec()
?
Q2. What is the difference between Display
and UISynchronize
?
Q3. What is the difference between Job
and UIJob
?
Q4. What is the singleton Status
object that indicates everything is ok?
Q5. How is the CommandService
obtained in Eclipse?
Q6. How is an icon associated with a Job
in the Progress view?
Q7. When should a SubMonitor
instead of a SubProgressMonitor
be used?
Q8. How frequently should the Job
cancellation status be checked?