Time for action – setting job properties

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.

  1. Open the 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;

    Note

    Time for action – setting job properties 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.

  2. Run the Eclipse instance, open the Hello command and go to the Progress view. Nothing will be shown, because the Job expects a 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));
    }
  3. Now run the Eclipse instance, go to the Progress view, and click on the Hello command. Underneath the Progress view, a hyperlink will be provided to allow firing off another Hello command:
    Time for action – setting job properties
  4. If the command has no handler (or the handler is disabled), a pop-up error message will be shown:
    Time for action – setting job properties
  5. If the command is handled, clicking on the link will run the command, which in this case runs the HelloHandler and launches another job instance. Each click will spawn off a new Job:
    Time for action – setting job properties
  6. It's possible to change the icon shown in the view by specifying an 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")));
  7. Run the Eclipse instance, go to the Progress view, and then click on the Hello menu. The icon should be shown against the job:
    Time for action – setting job properties

What just happened?

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.

Have a go hero – displaying in the taskbar

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

Pop quiz: understanding jobs

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?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset