rich client 2.0

Support your LabelProvider!! - ImageContribution


16. January 2006
Tom Seidel @ 17:09

In complex applications you also have very many images that want to be placed to the right time on the right place.

Example RCP for Image-Contribution

And because some good software-architects already have concepted the model ;) , in the most cases you want to visualize a special object or a special property of an object with an image in your JFace-List or Tree. Imagine you have 50 JFace-Viewers and 300 different Icons. Do you really want to implement in every LabelProvider how and when to access to which image? I guess, no.

At the following I will provide an easy way to manage image-contribution to your different label-providers.

The contribution of images is handled in one static Method: ImageContributor#getImageIdByObject(Object obj)

Before this image will return the correct images we have to implement

  1. An Assigment for images to special Objects
  2. An Assignment of so called ImagePolicies to provide images for special conditions, that have to be determined on runtime

Therefore we will implement a two static maps which define the assignments.

JAVA:
  1. /**
  2. * Includes all Mappings from a class-type of an image-id.
  3. */
  4. private static Map imageMap = new HashMap();
  5. /**
  6. * Defines the map with policy for objects where we have to
  7. * "calculate" the special on the concrete instance.
  8. */
  9. private static Map policyMap = new HashMap();
  10.  
  11. static {
  12. // assigning the images to an object
  13. imageMap.put(SubProcess.class,<code>ProcessImages.SUPPROCESS_IMG_SMALL</code>);
  14. imageMap.put(Condition.class,ProcessImages.SUPPROCESS_IMG_SMALL);
  15. // assigning the images to policies.
  16. policyMap.put(Step.class, StepPolicy.class);
  17. }

In this case we define, that all SubProcess-Objects that will be passed through a label-provider will be assigned with the Image-ID ProcessImages.SUPPROCESS_IMG_SMALL.
But imagine: If we pass a Step-Object through the provider we have to provide a special image for a start-step (Step#isStartStep() and an end-step (Step#isEndStep()). We have to realize this with a policy.
The policy will be instanciated and executes the following method to determine with the special Object, which image we have to provide:

JAVA:
  1. public String getImageIdByObject(Object obj) {
  2. String returnValue = null;
  3. if (obj instanceof Step) {
  4. Step currentStep = (Step) obj;
  5. if (currentStep.isStartStep()) {
  6. returnValue = ProcessImages.STEP_START_IMG_SMALL;
  7. } else if (currentStep.isEndStep()) {
  8. returnValue = ProcessImages.STEP_END_IMG_SMALL;
  9. } else {
  10. returnValue = ProcessImages.STEP_IMG_SMALL;
  11. }
  12. }
  13. return returnValue;
  14. }

Your LabelProvider will alway look like this:

JAVA:
  1. /**
  2. *
  3. * The label provider searches for the correct images.
  4. * @author Tom Seidel
  5. */
  6. class ViewLabelProvider extends LabelProvider {
  7. /* (non-Javadoc)
  8. * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
  9. */
  10. public String getText(Object element) {
  11. return ((AbstractBaseElement)element).getName() + " (" + //$NON-NLS-1$
  12. element.getClass() + ")"; //$NON-NLS-1$
  13. }
  14. /* (non-Javadoc)
  15. * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
  16. */
  17. public Image getImage(Object obj) {
  18. // searches for the correct image-id.
  19. String imageId = ImageContributor.getImageIdByObject(obj);
  20. return ImagecontributionPlugin.getDefault().getImage(imageId);
  21. }
  22. }

For the complete source-code download the Plugin. Alternatively you can download the Ready-to-go RCP.

The Image-Contributor Plugin (source included - 27 kByte) (Requires Model-Plugin)
Image-Contribution RCP (source included - 5,62 MByte)

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress