129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.source.projection.ProjectionAnnotationHover;
|
|
14
|
131
|
15 import dwtx.jface.text.source.projection.ProjectionViewer; // packageimport
|
|
16 import dwtx.jface.text.source.projection.ProjectionSupport; // packageimport
|
|
17 import dwtx.jface.text.source.projection.IProjectionPosition; // packageimport
|
|
18 import dwtx.jface.text.source.projection.AnnotationBag; // packageimport
|
|
19 import dwtx.jface.text.source.projection.ProjectionSummary; // packageimport
|
|
20 import dwtx.jface.text.source.projection.ProjectionRulerColumn; // packageimport
|
|
21 import dwtx.jface.text.source.projection.ProjectionAnnotationModel; // packageimport
|
|
22 import dwtx.jface.text.source.projection.SourceViewerInformationControl; // packageimport
|
|
23 import dwtx.jface.text.source.projection.IProjectionListener; // packageimport
|
|
24 import dwtx.jface.text.source.projection.ProjectionAnnotation; // packageimport
|
|
25
|
|
26
|
129
|
27 import dwt.dwthelper.utils;
|
|
28
|
|
29 import java.util.Iterator;
|
|
30
|
|
31 import dwt.widgets.Shell;
|
|
32 import dwtx.jface.resource.JFaceResources;
|
|
33 import dwtx.jface.text.BadLocationException;
|
|
34 import dwtx.jface.text.IDocument;
|
|
35 import dwtx.jface.text.IInformationControl;
|
|
36 import dwtx.jface.text.IInformationControlCreator;
|
|
37 import dwtx.jface.text.IRegion;
|
|
38 import dwtx.jface.text.Position;
|
|
39 import dwtx.jface.text.information.IInformationProviderExtension2;
|
|
40 import dwtx.jface.text.source.IAnnotationHover;
|
|
41 import dwtx.jface.text.source.IAnnotationHoverExtension;
|
|
42 import dwtx.jface.text.source.IAnnotationModel;
|
|
43 import dwtx.jface.text.source.IAnnotationModelExtension;
|
|
44 import dwtx.jface.text.source.ILineRange;
|
|
45 import dwtx.jface.text.source.ISourceViewer;
|
|
46 import dwtx.jface.text.source.ISourceViewerExtension2;
|
|
47 import dwtx.jface.text.source.LineRange;
|
|
48
|
|
49 /**
|
|
50 * Annotation hover for projection annotations.
|
|
51 *
|
|
52 * @since 3.0
|
|
53 */
|
|
54 class ProjectionAnnotationHover : IAnnotationHover, IAnnotationHoverExtension, IInformationProviderExtension2 {
|
|
55
|
|
56
|
|
57 private IInformationControlCreator fInformationControlCreator;
|
|
58 private IInformationControlCreator fInformationPresenterControlCreator;
|
|
59
|
|
60 /**
|
|
61 * Sets the hover control creator for this projection annotation hover.
|
|
62 *
|
|
63 * @param creator the creator
|
|
64 */
|
|
65 public void setHoverControlCreator(IInformationControlCreator creator) {
|
|
66 fInformationControlCreator= creator;
|
|
67 }
|
|
68
|
|
69 /**
|
|
70 * Sets the information presenter control creator for this projection annotation hover.
|
|
71 *
|
|
72 * @param creator the creator
|
|
73 * @since 3.3
|
|
74 */
|
|
75 public void setInformationPresenterControlCreator(IInformationControlCreator creator) {
|
|
76 fInformationPresenterControlCreator= creator;
|
|
77 }
|
|
78
|
|
79 /*
|
|
80 * @see dwtx.jface.text.source.IAnnotationHover#getHoverInfo(dwtx.jface.text.source.ISourceViewer, int)
|
|
81 */
|
|
82 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
|
|
83 // this is a no-op as semantics is defined by the implementation of the annotation hover extension
|
|
84 return null;
|
|
85 }
|
|
86
|
|
87 /*
|
|
88 * @since 3.1
|
|
89 */
|
|
90 private bool isCaptionLine(ProjectionAnnotation annotation, Position position, IDocument document, int line) {
|
|
91 if (position.getOffset() > -1 && position.getLength() > -1) {
|
|
92 try {
|
|
93 int captionOffset;
|
|
94 if (position instanceof IProjectionPosition)
|
|
95 captionOffset= ((IProjectionPosition) position).computeCaptionOffset(document);
|
|
96 else
|
|
97 captionOffset= 0;
|
|
98 int startLine= document.getLineOfOffset(position.getOffset() + captionOffset);
|
|
99 return line is startLine;
|
|
100 } catch (BadLocationException x) {
|
|
101 }
|
|
102 }
|
|
103 return false;
|
|
104 }
|
|
105
|
|
106 private String getProjectionTextAtLine(ISourceViewer viewer, int line, int numberOfLines) {
|
|
107
|
|
108 IAnnotationModel model= null;
|
|
109 if (viewer instanceof ISourceViewerExtension2) {
|
|
110 ISourceViewerExtension2 viewerExtension= (ISourceViewerExtension2) viewer;
|
|
111 IAnnotationModel visual= viewerExtension.getVisualAnnotationModel();
|
|
112 if (visual instanceof IAnnotationModelExtension) {
|
|
113 IAnnotationModelExtension modelExtension= (IAnnotationModelExtension) visual;
|
|
114 model= modelExtension.getAnnotationModel(ProjectionSupport.PROJECTION);
|
|
115 }
|
|
116 }
|
|
117
|
|
118 if (model !is null) {
|
|
119 try {
|
|
120 IDocument document= viewer.getDocument();
|
|
121 Iterator e= model.getAnnotationIterator();
|
|
122 while (e.hasNext()) {
|
|
123 ProjectionAnnotation annotation= (ProjectionAnnotation) e.next();
|
|
124 if (!annotation.isCollapsed())
|
|
125 continue;
|
|
126
|
|
127 Position position= model.getPosition(annotation);
|
|
128 if (position is null)
|
|
129 continue;
|
|
130
|
|
131 if (isCaptionLine(annotation, position, document, line))
|
|
132 return getText(document, position.getOffset(), position.getLength(), numberOfLines);
|
|
133
|
|
134 }
|
|
135 } catch (BadLocationException x) {
|
|
136 }
|
|
137 }
|
|
138
|
|
139 return null;
|
|
140 }
|
|
141
|
|
142 private String getText(IDocument document, int offset, int length, int numberOfLines) throws BadLocationException {
|
|
143 int endOffset= offset + length;
|
|
144
|
|
145 try {
|
|
146 int endLine= document.getLineOfOffset(offset) + Math.max(0, numberOfLines -1);
|
|
147 IRegion lineInfo= document.getLineInformation(endLine);
|
|
148 endOffset= Math.min(endOffset, lineInfo.getOffset() + lineInfo.getLength());
|
|
149 } catch (BadLocationException x) {
|
|
150 }
|
|
151
|
|
152 return document.get(offset, endOffset - offset);
|
|
153 }
|
|
154
|
|
155 /*
|
|
156 * @see dwtx.jface.text.source.IAnnotationHoverExtension#getHoverInfo(dwtx.jface.text.source.ISourceViewer, dwtx.jface.text.source.ILineRange, int)
|
|
157 */
|
|
158 public Object getHoverInfo(ISourceViewer sourceViewer, ILineRange lineRange, int visibleLines) {
|
|
159 return getProjectionTextAtLine(sourceViewer, lineRange.getStartLine(), visibleLines);
|
|
160 }
|
|
161
|
|
162 /*
|
|
163 * @see dwtx.jface.text.source.IAnnotationHoverExtension#getHoverLineRange(dwtx.jface.text.source.ISourceViewer, int)
|
|
164 */
|
|
165 public ILineRange getHoverLineRange(ISourceViewer viewer, int lineNumber) {
|
|
166 return new LineRange(lineNumber, 1);
|
|
167 }
|
|
168
|
|
169 /*
|
|
170 * @see dwtx.jface.text.source.IAnnotationHoverExtension#canHandleMouseCursor()
|
|
171 */
|
|
172 public bool canHandleMouseCursor() {
|
|
173 return false;
|
|
174 }
|
|
175
|
|
176 /*
|
|
177 * @see dwtx.jface.text.source.IAnnotationHoverExtension#getHoverControlCreator()
|
|
178 */
|
|
179 public IInformationControlCreator getHoverControlCreator() {
|
|
180 if (fInformationControlCreator is null) {
|
|
181 fInformationControlCreator= new IInformationControlCreator() {
|
|
182 public IInformationControl createInformationControl(Shell parent) {
|
|
183 return new SourceViewerInformationControl(parent, false, JFaceResources.TEXT_FONT, null);
|
|
184 }
|
|
185 };
|
|
186 }
|
|
187 return fInformationControlCreator;
|
|
188 }
|
|
189
|
|
190 /*
|
|
191 * @see dwtx.jface.text.information.IInformationProviderExtension2#getInformationPresenterControlCreator()
|
|
192 * @since 3.3
|
|
193 */
|
|
194 public IInformationControlCreator getInformationPresenterControlCreator() {
|
|
195 if (fInformationPresenterControlCreator is null) {
|
|
196 fInformationPresenterControlCreator= new IInformationControlCreator() {
|
|
197 public IInformationControl createInformationControl(Shell parent) {
|
|
198 return new SourceViewerInformationControl(parent, true, JFaceResources.TEXT_FONT, null);
|
|
199 }
|
|
200 };
|
|
201 }
|
|
202 return fInformationPresenterControlCreator;
|
|
203 }
|
|
204 }
|