Support your LabelProvider!! - ImageContribution
16. January 2006
In complex applications you also have very many images that want to be placed to the right time on the right place.

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
- An Assigment for images to special Objects
- 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.
-
/**
-
* Includes all Mappings from a class-type of an image-id.
-
*/
-
/**
-
* Defines the map with policy for objects where we have to
-
* "calculate" the special on the concrete instance.
-
*/
-
-
static {
-
// assigning the images to an object
-
imageMap.put(SubProcess.class,<code>ProcessImages.SUPPROCESS_IMG_SMALL</code>);
-
imageMap.put(Condition.class,ProcessImages.SUPPROCESS_IMG_SMALL);
-
// assigning the images to policies.
-
policyMap.put(Step.class, StepPolicy.class);
-
}
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:
-
String returnValue = null;
-
if (obj instanceof Step) {
-
Step currentStep = (Step) obj;
-
if (currentStep.isStartStep()) {
-
returnValue = ProcessImages.STEP_START_IMG_SMALL;
-
} else if (currentStep.isEndStep()) {
-
returnValue = ProcessImages.STEP_END_IMG_SMALL;
-
} else {
-
returnValue = ProcessImages.STEP_IMG_SMALL;
-
}
-
}
-
return returnValue;
-
}
Your LabelProvider will alway look like this:
-
/**
-
*
-
* The label provider searches for the correct images.
-
* @author Tom Seidel
-
*/
-
class ViewLabelProvider extends LabelProvider {
-
/* (non-Javadoc)
-
* @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
-
*/
-
return ((AbstractBaseElement)element).getName() + " (" + //$NON-NLS-1$
-
element.getClass() + ")"; //$NON-NLS-1$
-
}
-
/* (non-Javadoc)
-
* @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
-
*/
-
// searches for the correct image-id.
-
return ImagecontributionPlugin.getDefault().getImage(imageId);
-
}
-
}
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)

