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 that defines what values can be set, including COMMAND_PROPERTY, which can be used to associate a command with a Job.

  1. Open the HelloHandler and go to 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. This will require adding an argument into the method signature and adding org.eclipse.core.commands as a dependent bundle:
    public void execute(final UISynchronize display, final ICommandService commandService) {
      ...
      Command command = commandService.getCommand(
       "com.packtpub.e4.clock.ui.command.hello");
      if (command != null) {
        job.setProperty(IProgressConstants2.COMMAND_PROPERTY, command);
      }
      job.schedule()
      return;
  2. Run the target Eclipse instance, open the Hello command, and go to the Progress view. Nothing will be shown, because the Job expects a ParameterizedCommand instead. Use the ParameterizedCommand method generateCommand to wrap a non-parameterized command:
    if (command != null) {
      // job.setProperty(IProgressConstants2.COMMAND_PROPERTY, command);
      job.setProperty(IProgressConstants2.COMMAND_PROPERTY,
       ParameterizedCommand.generateCommand(command, null));
    }
  3. Now run the target 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 of another Hello command:
    Time for action – setting job properties
  4. 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
  5. 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 ImageDescriptor method createFromURL and set as a property:
    job.setProperty(IProgressConstants2.ICON_PROPERTY,
      ImageDescriptor.createFromURL(
       HelloHandler.class.getResource("/icons/sample.gif")));
  6. 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 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 using, for example, org.eclipse.ui.workbench.properties.command) is that the long prefix string is stored only once in memory.

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.

The Command can be acquired from the ICommandService, which is acquired through injection in E4.

To associate a command with a Job, set a property that contains a ParameterizedCommand, if necessary using the factory method generateCommand to convert a Command into a ParameterizedCommand.

Have a go hero – displaying in the taskbar

The IProgressConstants2 interface also defines a property named SHOW_IN_TASKBAR_ICON_PROPERTY, which shows whether the progress of the job is exposed to those operating systems that support it. On macOS, 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 whether 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 taken).

Pop quiz – understanding Jobs

Q1. What is the difference between UISynchronize.syncExec and UISynchronize.asyncExec?

Q2. What is UISynchronize used for?

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 be used instead of a SubProgressMonitor?

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
3.145.151.141