1 11 package org.eclipse.ui.internal.forms.widgets; 12 13 import java.util.Hashtable ; 14 15 import org.eclipse.swt.graphics.*; 16 17 21 public class ImageSegment extends ObjectSegment { 22 public static final String SEL_IMAGE_PREFIX = "isel."; 24 public Image getImage(Hashtable objectTable) { 25 return getImage(getObjectId(), objectTable); 26 } 27 28 private Image getImage(String key, Hashtable objectTable) { 29 if (key == null) 30 return null; 31 Object obj = objectTable.get(key); 32 if (obj == null) 33 return null; 34 if (obj instanceof Image) 35 return (Image) obj; 36 return null; 37 } 38 39 private Image getSelectedImage(Hashtable objectTable, SelectionData selData) { 40 String key = SEL_IMAGE_PREFIX + getObjectId(); 41 Image image = getImage(key, objectTable); 42 if (image==null) { 43 image = FormUtil.createAlphaMashImage(selData.display, getImage(objectTable)); 44 if (image!=null) 45 objectTable.put(key, image); 46 } 47 return image; 48 } 49 56 57 public void paint(GC gc, boolean hover, Hashtable resourceTable, boolean selected, SelectionData selData, Rectangle repaintRegion) { 58 Image image = getImage(resourceTable); 59 int iwidth = 0; 60 int iheight = 0; 61 if (image != null) { 62 Rectangle rect = image.getBounds(); 63 iwidth = rect.width + (isSelectable()?2:0); 64 iheight = rect.height + (isSelectable()?2:0); 65 } else 66 return; 67 Rectangle bounds = getBounds(); 68 int ix = bounds.x+(isSelectable()?1:0); 69 int iy = bounds.y+(isSelectable()?1:0); 70 71 if (selData != null) { 72 int leftOffset = selData.getLeftOffset(bounds.height); 73 int rightOffset = selData.getRightOffset(bounds.height); 74 boolean firstRow = selData.isFirstSelectionRow(bounds.y, 75 bounds.height); 76 boolean lastRow = selData.isLastSelectionRow(bounds.y, 77 bounds.height); 78 boolean selectedRow = selData 79 .isSelectedRow(bounds.y, bounds.height); 80 if (selectedRow) { 81 if ((firstRow && leftOffset > ix) || 82 (lastRow && rightOffset < ix + iwidth/2)) { 83 drawClipImage(gc, image, ix, iy, repaintRegion); 84 } 85 else { 86 Color savedBg = gc.getBackground(); 87 gc.setBackground(selData.bg); 88 int sx = ix; 89 int sy = iy; 90 if (repaintRegion!=null) { 91 sx -= repaintRegion.x; 92 sy -= repaintRegion.y; 93 } 94 gc.fillRectangle(sx, sy, iwidth, iheight); 95 Image selImage = getSelectedImage(resourceTable, selData); 96 gc.drawImage(selImage, sx, sy); 97 gc.setBackground(savedBg); 98 } 99 } 100 else 101 drawClipImage(gc, image, ix, iy, repaintRegion); 102 } else 103 drawClipImage(gc, image, ix, iy, repaintRegion); 104 if (selected) { 105 int fx = bounds.x; 106 int fy = bounds.y; 107 if (repaintRegion!=null) { 108 fx -= repaintRegion.x; 109 fy -= repaintRegion.y; 110 } 111 Color fg = gc.getForeground(); 112 gc.setForeground(gc.getBackground()); 113 gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1, 116 bounds.height - 1); 117 gc.setForeground(fg); 118 gc.drawFocus(fx, fy, bounds.width, bounds.height); 119 } 120 } 121 private void drawClipImage(GC gc, Image image, int ix, int iy, Rectangle repaintRegion) { 122 if (repaintRegion!=null) { 123 ix -= repaintRegion.x; 124 iy -= repaintRegion.y; 125 } 126 gc.drawImage(image, ix, iy); 127 } 128 129 protected Point getObjectSize(Hashtable resourceTable, int wHint) { 130 Image image = getImage(resourceTable); 131 if (image==null) 132 return new Point(0, 0); 133 Rectangle ibounds = image.getBounds(); 134 return new Point(ibounds.width, ibounds.height); 135 } 136 } 137 | Popular Tags |