I tend to use IStatus
whenever I can to indicate the status of some object. This have two severity states which are worth extra attention; ERROR
and WARNING
. Usually this object also have a graphical presentation, an icon. So would it not be nice to decorate this icon with an overlay image representing the severity of the status?
JFace
‘s DecorationOverlayIcon
comes to help. I usually use it like this:
private static final Point size = new Point(16, 16); private Image getDecoratedImage(ImageRegistry registry, String baseImageId, IStatus status){ ImageDescriptor overlay = null; Image baseImage = registry.get(baseImageId); switch (status.getSeverity()) { case IStatus.ERROR: overlay = PlatformUI.getWorkbench().getSharedImages() .getImageDescriptor(ISharedImages.IMG_DEC_FIELD_ERROR); break; case IStatus.WARNING: overlay = PlatformUI.getWorkbench().getSharedImages() .getImageDescriptor(ISharedImages.IMG_DEC_FIELD_WARNING); break; default: return baseImage; } // Construct a new image identifier String decoratedImageId = baseImageId.concat(String.valueOf(status .getSeverity())); // Return the stored image if we have one if (registry.get(decoratedImageId) == null) { // Otherwise create a new image and store it DecorationOverlayIcon decoratedImage = new DecorationOverlayIcon( baseImage, new ImageDescriptor[] { null, null, null, overlay, null }, size) { }; registry.put(decoratedImageId, decoratedImage); } return registry.get(decoratedImageId); }
Since images are system resources we must take care to release these when we no longer need them. So I’m using the image registry of the base image to store the decorated image. This will make the process a little bit faster when the same decorated image is needed again and it will also make sure that the resources are released when then system shuts down.