1 11 12 package org.eclipse.ui.texteditor; 13 14 15 import org.eclipse.jface.text.source.Annotation; 16 import org.eclipse.jface.text.source.IAnnotationPresentation; 17 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.events.DisposeEvent; 20 import org.eclipse.swt.events.DisposeListener; 21 import org.eclipse.swt.graphics.Color; 22 import org.eclipse.swt.graphics.GC; 23 import org.eclipse.swt.graphics.Image; 24 import org.eclipse.swt.graphics.ImageData; 25 import org.eclipse.swt.graphics.PaletteData; 26 import org.eclipse.swt.graphics.Point; 27 import org.eclipse.swt.graphics.RGB; 28 import org.eclipse.swt.graphics.Rectangle; 29 import org.eclipse.swt.widgets.Canvas; 30 import org.eclipse.swt.widgets.Control; 31 import org.eclipse.swt.widgets.Display; 32 33 34 41 public class DefaultRangeIndicator extends Annotation implements IAnnotationPresentation { 42 43 44 private static PaletteData fgPaletteData; 45 46 private Image fImage; 47 48 51 public DefaultRangeIndicator() { 52 } 53 54 57 public void paint(GC gc, Canvas canvas, Rectangle bounds) { 58 59 Point canvasSize= canvas.getSize(); 60 61 int x= 0; 62 int y= bounds.y; 63 int w= canvasSize.x; 64 int h= bounds.height; 65 int b= 1; 66 67 if (y + h > canvasSize.y) 68 h= canvasSize.y - y; 69 70 if (y < 0) { 71 h= h + y; 72 y= 0; 73 } 74 75 if (h <= 0) 76 return; 77 78 Image image = getImage(canvas); 79 gc.drawImage(image, 0, 0, w, h, x, y, w, h); 80 81 gc.setBackground(canvas.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION)); 82 gc.fillRectangle(x, bounds.y, w, b); 83 gc.fillRectangle(x, bounds.y + bounds.height - b, w, b); 84 } 85 86 89 public int getLayer() { 90 return IAnnotationPresentation.DEFAULT_LAYER; 91 } 92 93 99 private Image getImage(Control control) { 100 if (fImage == null) { 101 fImage= createImage(control.getDisplay(), control.getSize()); 102 103 control.addDisposeListener(new DisposeListener() { 104 public void widgetDisposed(DisposeEvent e) { 105 if (fImage != null && !fImage.isDisposed()) { 106 fImage.dispose(); 107 fImage= null; 108 } 109 } 110 }); 111 } else { 112 Rectangle imageRectangle= fImage.getBounds(); 113 Point controlSize= control.getSize(); 114 115 if (imageRectangle.width < controlSize.x || imageRectangle.height < controlSize.y) { 116 fImage.dispose(); 117 fImage= createImage(control.getDisplay(), controlSize); 118 } 119 } 120 121 return fImage; 122 } 123 124 132 private static Image createImage(Display display, Point size) { 133 134 int width= size.x; 135 int height= size.y; 136 137 if (fgPaletteData == null) 138 fgPaletteData= createPalette(display); 139 140 ImageData imageData= new ImageData(width, height, 1, fgPaletteData); 141 142 for (int y= 0; y < height; y++) 143 for (int x= 0; x < width; x++) 144 imageData.setPixel(x, y, (x + y) % 2); 145 146 return new Image(display, imageData); 147 } 148 149 155 private static PaletteData createPalette(Display display) { 156 Color c1; 157 Color c2; 158 159 if (false) { 160 c1= display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); 162 c2= display.getSystemColor(SWT.COLOR_LIST_BACKGROUND); 163 } else { 164 c1= display.getSystemColor(SWT.COLOR_LIST_SELECTION); 166 c2= display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); 167 } 168 169 RGB rgbs[]= new RGB[] { 170 new RGB(c1.getRed(), c1.getGreen(), c1.getBlue()), 171 new RGB(c2.getRed(), c2.getGreen(), c2.getBlue())}; 172 173 return new PaletteData(rgbs); 174 } 175 } 176 | Popular Tags |