* @return the image or <code>null</code>
* @since 3.1
*/
private Image getImage(AnnotationPreference preference) {
ImageRegistry registry= EditorsPlugin.getDefault().getImageRegistry();
String annotationType= (String) preference.getAnnotationType();
if (annotationType == null)
return null;
String customImage= annotationType + "__AnnotationsConfigurationBlock_Image"; //$NON-NLS-1$
Image image;
image= registry.get(customImage);
if (image != null)
return image;
image= registry.get(annotationType);
if (image == null) {
ImageDescriptor descriptor= preference.getImageDescriptor();
if (descriptor != null) {
registry.put(annotationType, descriptor);
image= registry.get(annotationType);
} else {
String key= translateSymbolicImageName(preference.getSymbolicImageName());
if (key != null) {
ISharedImages sharedImages= PlatformUI.getWorkbench().getSharedImages();
image= sharedImages.getImage(key);
}
}
}
if (image == null)
return image;
// create custom image
final int SIZE= 16; // square images
ImageData data= image.getImageData();
Image copy;
if (data.height > SIZE || data.width > SIZE) {
// scale down to icon size
copy= new Image(Display.getCurrent(), data.scaledTo(SIZE, SIZE));
} else {
// don't scale up, but rather copy into the middle and mark everything else transparent
ImageData mask= data.getTransparencyMask();
ImageData resized= new ImageData(SIZE, SIZE, data.depth, data.palette);
ImageData resizedMask= new ImageData(SIZE, SIZE, mask.depth, mask.palette);
int xo= Math.max(0, (SIZE - data.width) / 2);
int yo= Math.max(0, (SIZE - data.height) / 2);
for (int y= 0; y < SIZE; y++) {
for (int x= 0; x < SIZE; x++) {
if (y >= yo && x >= xo && y < yo + data.height && x < xo + data.width) {
resized.setPixel(x, y, data.getPixel(x - xo, y - yo));
resizedMask.setPixel(x, y, mask.getPixel(x - xo, y - yo));
}
}
}
copy= new Image(Display.getCurrent(), resized, resizedMask);
}
fImageKeys.add(customImage);
registry.put(customImage, copy);
return copy;
}