1 11 package org.eclipse.jface.text.source.projection; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.custom.StyledText; 18 import org.eclipse.swt.custom.StyledTextContent; 19 import org.eclipse.swt.graphics.Color; 20 import org.eclipse.swt.graphics.FontMetrics; 21 import org.eclipse.swt.graphics.GC; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.graphics.RGB; 24 import org.eclipse.swt.widgets.Display; 25 26 import org.eclipse.jface.text.IInformationControlCreator; 27 import org.eclipse.jface.text.source.Annotation; 28 import org.eclipse.jface.text.source.AnnotationPainter; 29 import org.eclipse.jface.text.source.IAnnotationAccess; 30 import org.eclipse.jface.text.source.IAnnotationHover; 31 import org.eclipse.jface.text.source.IAnnotationModel; 32 import org.eclipse.jface.text.source.ISharedTextColors; 33 import org.eclipse.jface.text.source.ISourceViewer; 34 35 42 public class ProjectionSupport { 43 44 48 public final static Object PROJECTION= new Object (); 49 50 private static class ProjectionAnnotationsPainter extends AnnotationPainter { 51 52 58 public ProjectionAnnotationsPainter(ISourceViewer sourceViewer, IAnnotationAccess access) { 59 super(sourceViewer, access); 60 } 61 62 65 protected IAnnotationModel findAnnotationModel(ISourceViewer sourceViewer) { 66 if (sourceViewer instanceof ProjectionViewer) { 67 ProjectionViewer projectionViewer= (ProjectionViewer) sourceViewer; 68 return projectionViewer.getProjectionAnnotationModel(); 69 } 70 return null; 71 } 72 73 76 protected boolean skip(Annotation annotation) { 77 if (annotation instanceof ProjectionAnnotation) 78 return !((ProjectionAnnotation) annotation).isCollapsed(); 79 80 return super.skip(annotation); 81 } 82 } 83 84 private static class ProjectionDrawingStrategy implements AnnotationPainter.IDrawingStrategy { 85 88 public void draw(Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) { 89 if (annotation instanceof ProjectionAnnotation) { 90 ProjectionAnnotation projectionAnnotation= (ProjectionAnnotation) annotation; 91 if (projectionAnnotation.isCollapsed()) { 92 93 if (gc != null) { 94 95 StyledTextContent content= textWidget.getContent(); 96 int line= content.getLineAtOffset(offset); 97 int lineStart= content.getOffsetAtLine(line); 98 String text= content.getLine(line); 99 int lineLength= text == null ? 0 : text.length(); 100 int lineEnd= lineStart + lineLength; 101 Point p= textWidget.getLocationAtOffset(lineEnd); 102 103 Color c= gc.getForeground(); 104 gc.setForeground(color); 105 106 FontMetrics metrics= gc.getFontMetrics(); 107 108 int baseline= textWidget.getBaseline(offset); 110 int descent= Math.min(2, textWidget.getLineHeight(offset) - baseline); 112 int ascent= metrics.getAscent(); 114 int leading= baseline - ascent; 116 int height= ascent + descent; 118 119 int width= metrics.getAverageCharWidth(); 120 gc.drawRectangle(p.x, p.y + leading, width, height); 121 int third= width/3; 122 int dotsVertical= p.y + baseline - 1; 123 gc.drawPoint(p.x + third, dotsVertical); 124 gc.drawPoint(p.x + width - third, dotsVertical); 125 126 gc.setForeground(c); 127 128 } else { 129 textWidget.redrawRange(offset, length, true); 130 } 131 } 132 } 133 } 134 } 135 136 private class ProjectionListener implements IProjectionListener { 137 138 141 public void projectionEnabled() { 142 doEnableProjection(); 143 } 144 145 148 public void projectionDisabled() { 149 doDisableProjection(); 150 } 151 } 152 153 private ProjectionViewer fViewer; 154 private IAnnotationAccess fAnnotationAccess; 155 private ISharedTextColors fSharedTextColors; 156 private List fSummarizableTypes; 157 private IInformationControlCreator fInformationControlCreator; 158 private IInformationControlCreator fInformationPresenterControlCreator; 159 private ProjectionListener fProjectionListener; 160 private ProjectionAnnotationsPainter fPainter; 161 private ProjectionRulerColumn fColumn; 162 165 private AnnotationPainter.IDrawingStrategy fDrawingStrategy; 166 167 176 public ProjectionSupport(ProjectionViewer viewer, IAnnotationAccess annotationAccess, ISharedTextColors sharedTextColors) { 177 fViewer= viewer; 178 fAnnotationAccess= annotationAccess; 179 fSharedTextColors= sharedTextColors; 180 } 181 182 193 public void addSummarizableAnnotationType(String annotationType) { 194 if (fSummarizableTypes == null) { 195 fSummarizableTypes= new ArrayList (); 196 fSummarizableTypes.add(annotationType); 197 } else if (!fSummarizableTypes.contains(annotationType)) 198 fSummarizableTypes.add(annotationType); 199 } 200 201 214 public void removeSummarizableAnnotationType(String annotationType) { 215 if (fSummarizableTypes != null) 216 fSummarizableTypes.remove(annotationType); 217 if (fSummarizableTypes.size() == 0) 218 fSummarizableTypes= null; 219 } 220 221 227 public void setHoverControlCreator(IInformationControlCreator creator) { 228 fInformationControlCreator= creator; 229 } 230 231 238 public void setInformationPresenterControlCreator(IInformationControlCreator creator) { 239 fInformationPresenterControlCreator= creator; 240 } 241 242 253 public void setAnnotationPainterDrawingStrategy(AnnotationPainter.IDrawingStrategy strategy) { 254 fDrawingStrategy= strategy; 255 } 256 257 263 private AnnotationPainter.IDrawingStrategy getDrawingStrategy() { 264 if (fDrawingStrategy == null) 265 fDrawingStrategy= new ProjectionDrawingStrategy(); 266 return fDrawingStrategy; 267 } 268 269 272 public void install() { 273 fViewer.setProjectionSummary(createProjectionSummary()); 274 275 fProjectionListener= new ProjectionListener(); 276 fViewer.addProjectionListener(fProjectionListener); 277 } 278 279 282 public void dispose() { 283 if (fProjectionListener != null) { 284 fViewer.removeProjectionListener(fProjectionListener); 285 fProjectionListener= null; 286 } 287 } 288 289 295 protected void doEnableProjection() { 296 297 if (fPainter == null) { 298 fPainter= new ProjectionAnnotationsPainter(fViewer, fAnnotationAccess); 299 fPainter.addDrawingStrategy(PROJECTION, getDrawingStrategy()); 300 fPainter.addAnnotationType(ProjectionAnnotation.TYPE, PROJECTION); 301 fPainter.setAnnotationTypeColor(ProjectionAnnotation.TYPE, fSharedTextColors.getColor(getColor())); 302 fViewer.addPainter(fPainter); 303 } 304 305 if (fColumn == null) { 306 fColumn= new ProjectionRulerColumn(9, fAnnotationAccess); 307 fColumn.addAnnotationType(ProjectionAnnotation.TYPE); 308 fColumn.setHover(createProjectionAnnotationHover()); 309 fViewer.addVerticalRulerColumn(fColumn); 310 } 311 312 fColumn.setModel(fViewer.getVisualAnnotationModel()); 313 } 314 315 319 protected void doDisableProjection() { 320 if (fPainter != null) { 321 fViewer.removePainter(fPainter); 322 fPainter.dispose(); 323 fPainter= null; 324 } 325 326 if (fColumn != null) { 327 fViewer.removeVerticalRulerColumn(fColumn); 328 fColumn= null; 329 } 330 } 331 332 private ProjectionSummary createProjectionSummary() { 333 ProjectionSummary summary= new ProjectionSummary(fViewer, fAnnotationAccess); 334 if (fSummarizableTypes != null) { 335 int size= fSummarizableTypes.size(); 336 for (int i= 0; i < size; i++) 337 summary.addAnnotationType((String ) fSummarizableTypes.get(i)); 338 } 339 return summary; 340 } 341 342 private IAnnotationHover createProjectionAnnotationHover() { 343 ProjectionAnnotationHover hover= new ProjectionAnnotationHover(); 344 hover.setHoverControlCreator(fInformationControlCreator); 345 hover.setInformationPresenterControlCreator(fInformationPresenterControlCreator); 346 return hover; 347 } 348 349 358 public Object getAdapter(ISourceViewer viewer, Class required) { 359 if (ProjectionAnnotationModel.class.equals(required)) { 360 if (viewer instanceof ProjectionViewer) { 361 ProjectionViewer projectionViewer= (ProjectionViewer) viewer; 362 return projectionViewer.getProjectionAnnotationModel(); 363 } 364 } 365 return null; 366 } 367 368 private RGB getColor() { 369 Color c= Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY); 371 return c.getRGB(); 372 } 373 } 374 | Popular Tags |