comparison org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionAnnotationHover.d @ 12:bc29606a740c

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