1 package com.android.ide.eclipse.adt.internal.editors.ui; 2 3 import static org.eclipse.ui.ISharedImages.IMG_DEC_FIELD_ERROR; 4 import static org.eclipse.ui.ISharedImages.IMG_DEC_FIELD_WARNING; 5 import static org.eclipse.ui.ISharedImages.IMG_OBJS_ERROR_TSK; 6 import static org.eclipse.ui.ISharedImages.IMG_OBJS_WARN_TSK; 7 8 import org.eclipse.jface.resource.CompositeImageDescriptor; 9 import org.eclipse.jface.resource.ImageDescriptor; 10 import org.eclipse.jface.viewers.DecorationOverlayIcon; 11 import org.eclipse.swt.graphics.Image; 12 import org.eclipse.swt.graphics.ImageData; 13 import org.eclipse.swt.graphics.Point; 14 import org.eclipse.ui.ISharedImages; 15 import org.eclipse.ui.PlatformUI; 16 17 /** 18 * ImageDescriptor that adds a error marker. 19 * Based on {@link DecorationOverlayIcon} only available in Eclipse 3.3 20 */ 21 public class ErrorImageComposite extends CompositeImageDescriptor { 22 23 private Image mBaseImage; 24 private ImageDescriptor mErrorImageDescriptor; 25 private Point mSize; 26 27 /** 28 * Creates a new {@link ErrorImageComposite} 29 * 30 * @param baseImage the base image to overlay an icon on top of 31 */ 32 public ErrorImageComposite(Image baseImage) { 33 this(baseImage, false); 34 } 35 36 /** 37 * Creates a new {@link ErrorImageComposite} 38 * 39 * @param baseImage the base image to overlay an icon on top of 40 * @param warning if true, add a warning icon, otherwise an error icon 41 */ 42 public ErrorImageComposite(Image baseImage, boolean warning) { 43 mBaseImage = baseImage; 44 ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages(); 45 mErrorImageDescriptor = sharedImages.getImageDescriptor( 46 warning ? IMG_DEC_FIELD_WARNING : IMG_DEC_FIELD_ERROR); 47 if (mErrorImageDescriptor == null) { 48 mErrorImageDescriptor = sharedImages.getImageDescriptor( 49 warning ? IMG_OBJS_WARN_TSK : IMG_OBJS_ERROR_TSK); 50 } 51 mSize = new Point(baseImage.getBounds().width, baseImage.getBounds().height); 52 } 53 54 @Override 55 protected void drawCompositeImage(int width, int height) { 56 ImageData baseData = mBaseImage.getImageData(); 57 drawImage(baseData, 0, 0); 58 59 ImageData overlayData = mErrorImageDescriptor.getImageData(); 60 if (overlayData.width == baseData.width && overlayData.height == baseData.height) { 61 overlayData = overlayData.scaledTo(14, 14); 62 drawImage(overlayData, -3, mSize.y - overlayData.height + 3); 63 } else { 64 drawImage(overlayData, 0, mSize.y - overlayData.height); 65 } 66 } 67 68 @Override 69 protected Point getSize() { 70 return mSize; 71 } 72 } 73