KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > source > projection > ProjectionAnnotationHover


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  *******************************************************************************/

11 package org.eclipse.jface.text.source.projection;
12
13 import java.util.Iterator JavaDoc;
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 /**
37  * Annotation hover for projection annotations.
38  *
39  * @since 3.0
40  */

41 class ProjectionAnnotationHover implements IAnnotationHover, IAnnotationHoverExtension, IInformationProviderExtension2 {
42     
43     
44     private IInformationControlCreator fInformationControlCreator;
45     private IInformationControlCreator fInformationPresenterControlCreator;
46
47     /**
48      * Sets the hover control creator for this projection annotation hover.
49      *
50      * @param creator the creator
51      */

52     public void setHoverControlCreator(IInformationControlCreator creator) {
53         fInformationControlCreator= creator;
54     }
55     
56     /**
57      * Sets the information presenter control creator for this projection annotation hover.
58      *
59      * @param creator the creator
60      * @since 3.3
61      */

62     public void setInformationPresenterControlCreator(IInformationControlCreator creator) {
63         fInformationPresenterControlCreator= creator;
64     }
65
66     /*
67      * @see org.eclipse.jface.text.source.IAnnotationHover#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer, int)
68      */

69     public String JavaDoc getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
70         // this is a no-op as semantics is defined by the implementation of the annotation hover extension
71
return null;
72     }
73
74     /*
75      * @since 3.1
76      */

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 JavaDoc 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 JavaDoc 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 JavaDoc 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     /*
143      * @see org.eclipse.jface.text.source.IAnnotationHoverExtension#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer, org.eclipse.jface.text.source.ILineRange, int)
144      */

145     public Object JavaDoc getHoverInfo(ISourceViewer sourceViewer, ILineRange lineRange, int visibleLines) {
146         return getProjectionTextAtLine(sourceViewer, lineRange.getStartLine(), visibleLines);
147     }
148
149     /*
150      * @see org.eclipse.jface.text.source.IAnnotationHoverExtension#getHoverLineRange(org.eclipse.jface.text.source.ISourceViewer, int)
151      */

152     public ILineRange getHoverLineRange(ISourceViewer viewer, int lineNumber) {
153         return new LineRange(lineNumber, 1);
154     }
155
156     /*
157      * @see org.eclipse.jface.text.source.IAnnotationHoverExtension#canHandleMouseCursor()
158      */

159     public boolean canHandleMouseCursor() {
160         return false;
161     }
162
163     /*
164      * @see org.eclipse.jface.text.source.IAnnotationHoverExtension#getHoverControlCreator()
165      */

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     /*
178      * @see org.eclipse.jface.text.information.IInformationProviderExtension2#getInformationPresenterControlCreator()
179      * @since 3.3
180      */

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, ""); //$NON-NLS-1$
188
}
189             };
190         }
191         return fInformationPresenterControlCreator;
192     }
193 }
194
Popular Tags