1 11 package org.eclipse.jface.text.source.projection; 12 13 14 import java.util.Iterator ; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.events.MouseEvent; 18 import org.eclipse.swt.events.MouseMoveListener; 19 import org.eclipse.swt.events.MouseTrackAdapter; 20 import org.eclipse.swt.graphics.Color; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.Display; 24 25 import org.eclipse.jface.text.BadLocationException; 26 import org.eclipse.jface.text.IDocument; 27 import org.eclipse.jface.text.Position; 28 import org.eclipse.jface.text.source.AnnotationRulerColumn; 29 import org.eclipse.jface.text.source.CompositeRuler; 30 import org.eclipse.jface.text.source.IAnnotationAccess; 31 import org.eclipse.jface.text.source.IAnnotationModel; 32 import org.eclipse.jface.text.source.IAnnotationModelExtension; 33 34 35 41 class ProjectionRulerColumn extends AnnotationRulerColumn { 42 43 private ProjectionAnnotation fCurrentAnnotation; 44 45 52 public ProjectionRulerColumn(IAnnotationModel model, int width, IAnnotationAccess annotationAccess) { 53 super(model, width, annotationAccess); 54 } 55 56 62 public ProjectionRulerColumn(int width, IAnnotationAccess annotationAccess) { 63 super(width, annotationAccess); 64 } 65 66 69 protected void mouseClicked(int line) { 70 clearCurrentAnnotation(); 71 ProjectionAnnotation annotation= findAnnotation(line, true); 72 if (annotation != null) { 73 ProjectionAnnotationModel model= (ProjectionAnnotationModel) getModel(); 74 model.toggleExpansionState(annotation); 75 } 76 } 77 78 86 private ProjectionAnnotation findAnnotation(int line, boolean exact) { 87 88 ProjectionAnnotation previousAnnotation= null; 89 90 IAnnotationModel model= getModel(); 91 if (model != null) { 92 IDocument document= getCachedTextViewer().getDocument(); 93 94 int previousDistance= Integer.MAX_VALUE; 95 96 Iterator e= model.getAnnotationIterator(); 97 while (e.hasNext()) { 98 Object next= e.next(); 99 if (next instanceof ProjectionAnnotation) { 100 ProjectionAnnotation annotation= (ProjectionAnnotation) next; 101 Position p= model.getPosition(annotation); 102 if (p == null) 103 continue; 104 105 int distance= getDistance(annotation, p, document, line); 106 if (distance == -1) 107 continue; 108 109 if (!exact) { 110 if (distance < previousDistance) { 111 previousAnnotation= annotation; 112 previousDistance= distance; 113 } 114 } else if (distance == 0) { 115 previousAnnotation= annotation; 116 } 117 } 118 } 119 } 120 121 return previousAnnotation; 122 } 123 124 134 private int getDistance(ProjectionAnnotation annotation, Position position, IDocument document, int line) { 135 if (position.getOffset() > -1 && position.getLength() > -1) { 136 try { 137 int startLine= document.getLineOfOffset(position.getOffset()); 138 int endLine= document.getLineOfOffset(position.getOffset() + position.getLength()); 139 if (startLine <= line && line < endLine) { 140 if (annotation.isCollapsed()) { 141 int captionOffset; 142 if (position instanceof IProjectionPosition) 143 captionOffset= ((IProjectionPosition) position).computeCaptionOffset(document); 144 else 145 captionOffset= 0; 146 147 int captionLine= document.getLineOfOffset(position.getOffset() + captionOffset); 148 if (startLine <= captionLine && captionLine < endLine) 149 return Math.abs(line - captionLine); 150 } 151 return line - startLine; 152 } 153 } catch (BadLocationException x) { 154 } 155 } 156 return -1; 157 } 158 159 private boolean clearCurrentAnnotation() { 160 if (fCurrentAnnotation != null) { 161 fCurrentAnnotation.setRangeIndication(false); 162 fCurrentAnnotation= null; 163 return true; 164 } 165 return false; 166 } 167 168 171 public Control createControl(CompositeRuler parentRuler, Composite parentControl) { 172 Control control= super.createControl(parentRuler, parentControl); 173 174 Display display= parentControl.getDisplay(); 176 Color background= display.getSystemColor(SWT.COLOR_LIST_BACKGROUND); 177 control.setBackground(background); 178 179 control.addMouseTrackListener(new MouseTrackAdapter() { 181 public void mouseExit(MouseEvent e) { 182 if (clearCurrentAnnotation()) 183 redraw(); 184 } 185 }); 186 187 control.addMouseMoveListener(new MouseMoveListener() { 189 public void mouseMove(MouseEvent e) { 190 boolean redraw= false; 191 ProjectionAnnotation annotation= findAnnotation(toDocumentLineNumber(e.y), false); 192 if (annotation != fCurrentAnnotation) { 193 if (fCurrentAnnotation != null) { 194 fCurrentAnnotation.setRangeIndication(false); 195 redraw= true; 196 } 197 fCurrentAnnotation= annotation; 198 if (fCurrentAnnotation != null && !fCurrentAnnotation.isCollapsed()) { 199 fCurrentAnnotation.setRangeIndication(true); 200 redraw= true; 201 } 202 } 203 if (redraw) 204 redraw(); 205 } 206 }); 207 return control; 208 } 209 210 213 public void setModel(IAnnotationModel model) { 214 if (model instanceof IAnnotationModelExtension) { 215 IAnnotationModelExtension extension= (IAnnotationModelExtension) model; 216 model= extension.getAnnotationModel(ProjectionSupport.PROJECTION); 217 } 218 super.setModel(model); 219 } 220 221 224 protected boolean isPropagatingMouseListener() { 225 return false; 226 } 227 228 231 protected boolean hasAnnotation(int lineNumber) { 232 return findAnnotation(lineNumber, true) != null; 233 } 234 } 235 | Popular Tags |