1 11 package org.eclipse.jface.text.source.projection; 12 13 import java.util.Iterator ; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.widgets.Shell; 17 18 import org.eclipse.jface.resource.JFaceResources; 19 20 import org.eclipse.jface.text.BadLocationException; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.text.IInformationControl; 23 import org.eclipse.jface.text.IInformationControlCreator; 24 import org.eclipse.jface.text.IRegion; 25 import org.eclipse.jface.text.Position; 26 import org.eclipse.jface.text.information.IInformationProviderExtension2; 27 import org.eclipse.jface.text.source.IAnnotationHover; 28 import org.eclipse.jface.text.source.IAnnotationHoverExtension; 29 import org.eclipse.jface.text.source.IAnnotationModel; 30 import org.eclipse.jface.text.source.IAnnotationModelExtension; 31 import org.eclipse.jface.text.source.ILineRange; 32 import org.eclipse.jface.text.source.ISourceViewer; 33 import org.eclipse.jface.text.source.ISourceViewerExtension2; 34 import org.eclipse.jface.text.source.LineRange; 35 36 41 class ProjectionAnnotationHover implements IAnnotationHover, IAnnotationHoverExtension, IInformationProviderExtension2 { 42 43 44 private IInformationControlCreator fInformationControlCreator; 45 private IInformationControlCreator fInformationPresenterControlCreator; 46 47 52 public void setHoverControlCreator(IInformationControlCreator creator) { 53 fInformationControlCreator= creator; 54 } 55 56 62 public void setInformationPresenterControlCreator(IInformationControlCreator creator) { 63 fInformationPresenterControlCreator= creator; 64 } 65 66 69 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { 70 return null; 72 } 73 74 77 private boolean isCaptionLine(ProjectionAnnotation annotation, Position position, IDocument document, int line) { 78 if (position.getOffset() > -1 && position.getLength() > -1) { 79 try { 80 int captionOffset; 81 if (position instanceof IProjectionPosition) 82 captionOffset= ((IProjectionPosition) position).computeCaptionOffset(document); 83 else 84 captionOffset= 0; 85 int startLine= document.getLineOfOffset(position.getOffset() + captionOffset); 86 return line == startLine; 87 } catch (BadLocationException x) { 88 } 89 } 90 return false; 91 } 92 93 private String getProjectionTextAtLine(ISourceViewer viewer, int line, int numberOfLines) { 94 95 IAnnotationModel model= null; 96 if (viewer instanceof ISourceViewerExtension2) { 97 ISourceViewerExtension2 viewerExtension= (ISourceViewerExtension2) viewer; 98 IAnnotationModel visual= viewerExtension.getVisualAnnotationModel(); 99 if (visual instanceof IAnnotationModelExtension) { 100 IAnnotationModelExtension modelExtension= (IAnnotationModelExtension) visual; 101 model= modelExtension.getAnnotationModel(ProjectionSupport.PROJECTION); 102 } 103 } 104 105 if (model != null) { 106 try { 107 IDocument document= viewer.getDocument(); 108 Iterator e= model.getAnnotationIterator(); 109 while (e.hasNext()) { 110 ProjectionAnnotation annotation= (ProjectionAnnotation) e.next(); 111 if (!annotation.isCollapsed()) 112 continue; 113 114 Position position= model.getPosition(annotation); 115 if (position == null) 116 continue; 117 118 if (isCaptionLine(annotation, position, document, line)) 119 return getText(document, position.getOffset(), position.getLength(), numberOfLines); 120 121 } 122 } catch (BadLocationException x) { 123 } 124 } 125 126 return null; 127 } 128 129 private String getText(IDocument document, int offset, int length, int numberOfLines) throws BadLocationException { 130 int endOffset= offset + length; 131 132 try { 133 int endLine= document.getLineOfOffset(offset) + Math.max(0, numberOfLines -1); 134 IRegion lineInfo= document.getLineInformation(endLine); 135 endOffset= Math.min(endOffset, lineInfo.getOffset() + lineInfo.getLength()); 136 } catch (BadLocationException x) { 137 } 138 139 return document.get(offset, endOffset - offset); 140 } 141 142 145 public Object getHoverInfo(ISourceViewer sourceViewer, ILineRange lineRange, int visibleLines) { 146 return getProjectionTextAtLine(sourceViewer, lineRange.getStartLine(), visibleLines); 147 } 148 149 152 public ILineRange getHoverLineRange(ISourceViewer viewer, int lineNumber) { 153 return new LineRange(lineNumber, 1); 154 } 155 156 159 public boolean canHandleMouseCursor() { 160 return false; 161 } 162 163 166 public IInformationControlCreator getHoverControlCreator() { 167 if (fInformationControlCreator == null) { 168 fInformationControlCreator= new IInformationControlCreator() { 169 public IInformationControl createInformationControl(Shell parent) { 170 return new SourceViewerInformationControl(parent, JFaceResources.TEXT_FONT); 171 } 172 }; 173 } 174 return fInformationControlCreator; 175 } 176 177 181 public IInformationControlCreator getInformationPresenterControlCreator() { 182 if (fInformationPresenterControlCreator == null) { 183 fInformationPresenterControlCreator= new IInformationControlCreator() { 184 public IInformationControl createInformationControl(Shell parent) { 185 int shellStyle= SWT.RESIZE | SWT.TOOL; 186 int style= SWT.V_SCROLL | SWT.H_SCROLL; 187 return new SourceViewerInformationControl(parent, shellStyle, style, JFaceResources.TEXT_FONT, ""); } 189 }; 190 } 191 return fInformationPresenterControlCreator; 192 } 193 } 194 | Popular Tags |