1 11 package org.eclipse.jface.text.source.projection; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.graphics.FontMetrics; 15 import org.eclipse.swt.graphics.GC; 16 import org.eclipse.swt.graphics.Image; 17 import org.eclipse.swt.graphics.Rectangle; 18 import org.eclipse.swt.widgets.Canvas; 19 import org.eclipse.swt.widgets.Display; 20 21 import org.eclipse.jface.resource.ImageDescriptor; 22 23 import org.eclipse.jface.text.source.Annotation; 24 import org.eclipse.jface.text.source.IAnnotationPresentation; 25 import org.eclipse.jface.text.source.ImageUtilities; 26 27 40 public class ProjectionAnnotation extends Annotation implements IAnnotationPresentation { 41 42 private static class DisplayDisposeRunnable implements Runnable { 43 44 public void run() { 45 if (fgCollapsedImage != null) { 46 fgCollapsedImage.dispose(); 47 fgCollapsedImage= null; 48 } 49 if (fgExpandedImage != null) { 50 fgExpandedImage.dispose(); 51 fgExpandedImage= null; 52 } 53 } 54 } 55 56 59 public static final String TYPE= "org.eclipse.projection"; 61 62 private static final int COLOR= SWT.COLOR_GRAY; 63 private static Image fgCollapsedImage; 64 private static Image fgExpandedImage; 65 66 67 68 private boolean fIsCollapsed= false; 69 70 private boolean fIsRangeIndication= false; 71 72 75 public ProjectionAnnotation() { 76 this(false); 77 } 78 79 85 public ProjectionAnnotation(boolean isCollapsed) { 86 super(TYPE, false, null); 87 fIsCollapsed= isCollapsed; 88 } 89 90 95 public void setRangeIndication(boolean rangeIndication) { 96 fIsRangeIndication= rangeIndication; 97 } 98 99 private void drawRangeIndication(GC gc, Canvas canvas, Rectangle r) { 100 final int MARGIN= 3; 101 102 104 int height= Math.min(r.y + r.height - MARGIN, canvas.getSize().y); 105 106 gc.setForeground(canvas.getDisplay().getSystemColor(COLOR)); 107 gc.setLineWidth(0); gc.drawLine(r.x + 4, r.y + 12, r.x + 4, height); 109 gc.drawLine(r.x + 4, height, r.x + r.width - MARGIN, height); 110 } 111 112 115 public void paint(GC gc, Canvas canvas, Rectangle rectangle) { 116 Image image= getImage(canvas.getDisplay()); 117 if (image != null) { 118 ImageUtilities.drawImage(image, gc, canvas, rectangle, SWT.CENTER, SWT.TOP); 119 if (fIsRangeIndication) { 120 FontMetrics fontMetrics= gc.getFontMetrics(); 121 int delta= (fontMetrics.getHeight() - image.getBounds().height)/2; 122 rectangle.y += delta; 123 rectangle.height -= delta; 124 drawRangeIndication(gc, canvas, rectangle); 125 } 126 } 127 } 128 129 132 public int getLayer() { 133 return IAnnotationPresentation.DEFAULT_LAYER; 134 } 135 136 private Image getImage(Display display) { 137 initializeImages(display); 138 return isCollapsed() ? fgCollapsedImage : fgExpandedImage; 139 } 140 141 private void initializeImages(Display display) { 142 if (fgCollapsedImage == null) { 143 144 ImageDescriptor descriptor= ImageDescriptor.createFromFile(ProjectionAnnotation.class, "images/collapsed.gif"); fgCollapsedImage= descriptor.createImage(display); 146 descriptor= ImageDescriptor.createFromFile(ProjectionAnnotation.class, "images/expanded.gif"); fgExpandedImage= descriptor.createImage(display); 148 149 display.disposeExec(new DisplayDisposeRunnable()); 150 } 151 } 152 153 158 public boolean isCollapsed() { 159 return fIsCollapsed; 160 } 161 162 165 public void markCollapsed() { 166 fIsCollapsed= true; 167 } 168 169 172 public void markExpanded() { 173 fIsCollapsed= false; 174 } 175 } 176 | Popular Tags |