donderdag 10 december 2009

Decorating your Jobs

Reinforcing your brand with every UI contact of your plugin is important in this competitive world. Many plugins launch Jobs when when Eclipse is starting up, to refresh their data, check the license, or whatever. Some Jobs display an icon to reveal the identity and reinforce the brand. Below is an example, showing two jobs that use text only and one with an icon.So how is this achieved?

step 1: register with Workbench ProgressService

An Eclipse Job can belong to a family, where a family can be any java Object. The ProgressService maintains a table of icons associated with each family. So step one is to register your icon and family in your Activator's start() method.
@Override
public void start(final BundleContext context) throws Exception {
super.start(context);
getWorkbench().getProgressService().
registerIconForFamily(
getImageDescriptor(ICONPATH),
MyTools.PLUGIN_ID);
[...]
Rebuilder rebuilder = new Rebuilder("Initializing My Tools");
rebuilder.schedule(20L);
}

step 2: override belongsTo() in your Job subclass

Next you override the method belongsTo() in your subclass with some simpel logic, calling super.belongsTo()when not equal top allow for Job class hierarchies.
public class Rebuilder extends Job {
[...]
@Override
public boolean belongsTo(final Object family) {
if (family.equals(MyTools.PLUGIN_ID)) {
return true;
}
return super.belongsTo(family);
}
[...]
}

Conclusion

As always in Eclipse programming this solution took a long time to find, but can be implemented in a few lines of code once you know how.

Caveat

What remains is that you can only do this for Jobs that you launch yourself, and not for other background tasks like the Auto Build jobs that call your Builders.
They all share the same icon :-(
I have created a bug for this, please support and vote here.

3 opmerkingen:

  1. Do you think that this is something you might consider contributing to the Eclipse FAQ?

    http://wiki.eclipse.org/The_Official_Eclipse_FAQs#Runtime_Facilities

    BeantwoordenVerwijderen
  2. Thanks for pointing this out Maarten. The next EA release of Tasktop will be using job families so that all Tasktop Jobs have an appropriate icon.

    BeantwoordenVerwijderen
  3. I know, I'm late to the party :-)

    Thank you for this post. You will see it implemented in the upcoming release of nWire. Once small comment: I noticed that the getImageDescriptor(ICONPATH) will not always work, as the Bundle might not be found at this point. To overcome this, you can get the bundle yourself from the BundleContext argument and use bundle.getResource followed by ImageDescriptor.createFromURL. This should work just fine.

    BeantwoordenVerwijderen