KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > ChangeHoverInformationControl


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.jdt.internal.ui.text;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.custom.StyledText;
16 import org.eclipse.swt.graphics.Font;
17 import org.eclipse.swt.graphics.GC;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jface.resource.JFaceResources;
24
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.IRegion;
28 import org.eclipse.jface.text.ITextViewerExtension;
29
30 import org.eclipse.jdt.ui.text.IJavaPartitions;
31
32 import org.eclipse.jdt.internal.ui.text.java.hover.SourceViewerInformationControl;
33
34 /**
35  * Specialized source viewer information control used to display quick diff hovers.
36  *
37  * @since 3.0
38  */

39 class ChangeHoverInformationControl extends SourceViewerInformationControl {
40
41     /** The font name for the viewer font - the same as the java editor's. */
42     private static final String JavaDoc SYMBOLIC_FONT_NAME= "org.eclipse.jdt.ui.editors.textfont"; //$NON-NLS-1$
43

44     /** The maximum width of the control, set in <code>setSizeConstraints(int, int)</code>. */
45     int fMaxWidth= Integer.MAX_VALUE;
46     /** The maximum height of the control, set in <code>setSizeConstraints(int, int)</code>. */
47     int fMaxHeight= Integer.MAX_VALUE;
48
49     /** The partition type to be used as the starting partition type by the partition scanner. */
50     private String JavaDoc fPartition;
51     /** The horizontal scroll index. */
52     private int fHorizontalScrollPixel;
53
54     /*
55      * @see org.eclipse.jface.text.IInformationControl#setSizeConstraints(int, int)
56      */

57     public void setSizeConstraints(int maxWidth, int maxHeight) {
58         fMaxWidth= maxWidth;
59         fMaxHeight= maxHeight;
60     }
61
62     /**
63      * Creates a new information control.
64      *
65      * @param parent the shell that is the parent of this hover / control
66      * @param shellStyle the additional styles for the shell
67      * @param style the additional styles for the styled text widget
68      * @param partition the initial partition type to be used for the underlying viewer
69      * @param statusFieldText the text to be used in the optional status field
70      * or <code>null</code> if the status field should be hidden
71      */

72     public ChangeHoverInformationControl(Shell parent, int shellStyle, int style, String JavaDoc partition, String JavaDoc statusFieldText) {
73         super(parent, shellStyle, style, statusFieldText);
74         setViewerFont();
75         setStartingPartitionType(partition);
76     }
77
78     /*
79      * @see org.eclipse.jface.text.IInformationControl#computeSizeHint()
80      */

81     public Point computeSizeHint() {
82         Point size= super.computeSizeHint();
83         size.x= Math.min(size.x, fMaxWidth);
84         size.y= Math.min(size.y, fMaxHeight);
85         return size;
86     }
87
88     /**
89      * Sets the font for this viewer sustaining selection and scroll position.
90      */

91     private void setViewerFont() {
92         Font font= JFaceResources.getFont(SYMBOLIC_FONT_NAME);
93
94         if (getViewer().getDocument() != null) {
95
96             Point selection= getViewer().getSelectedRange();
97             int topIndex= getViewer().getTopIndex();
98
99             StyledText styledText= getViewer().getTextWidget();
100             Control parent= styledText;
101             if (getViewer() instanceof ITextViewerExtension) {
102                 ITextViewerExtension extension= (ITextViewerExtension) getViewer();
103                 parent= extension.getControl();
104             }
105
106             parent.setRedraw(false);
107
108             styledText.setFont(font);
109
110             getViewer().setSelectedRange(selection.x , selection.y);
111             getViewer().setTopIndex(topIndex);
112
113             if (parent instanceof Composite) {
114                 Composite composite= (Composite) parent;
115                 composite.layout(true);
116             }
117
118             parent.setRedraw(true);
119
120         } else {
121             StyledText styledText= getViewer().getTextWidget();
122             styledText.setFont(font);
123         }
124     }
125
126     /**
127      * Sets the initial partition for the underlying source viewer.
128      *
129      * @param partition the partition type
130      */

131     public void setStartingPartitionType(String JavaDoc partition) {
132         if (partition == null)
133             fPartition= IDocument.DEFAULT_CONTENT_TYPE;
134         else
135             fPartition= partition;
136     }
137
138     /*
139      * @see org.eclipse.jface.text.IInformationControl#setInformation(java.lang.String)
140      */

141     public void setInformation(String JavaDoc content) {
142         super.setInformation(content);
143         IDocument doc= getViewer().getDocument();
144         if (doc == null)
145             return;
146
147         // ensure that we can scroll enough
148
ensureScrollable();
149
150         String JavaDoc start= null;
151         if (IJavaPartitions.JAVA_DOC.equals(fPartition)) {
152             start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
153
} else if (IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(fPartition)) {
154             start= "/*" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
155
}
156         if (start != null) {
157             try {
158                 doc.replace(0, 0, start);
159                 int startLen= start.length();
160                 getViewer().setDocument(doc, startLen, doc.getLength() - startLen);
161             } catch (BadLocationException e) {
162                 // impossible
163
Assert.isTrue(false);
164             }
165         }
166
167         getViewer().getTextWidget().setHorizontalPixel(fHorizontalScrollPixel);
168     }
169
170     /**
171      * Ensures that the control can be scrolled at least to
172      * <code>fHorizontalScrollPixel</code> and adjusts <code>fMaxWidth</code>
173      * accordingly.
174      */

175     private void ensureScrollable() {
176         IDocument doc= getViewer().getDocument();
177         if (doc == null)
178             return;
179
180         StyledText widget= getViewer().getTextWidget();
181         if (widget == null || widget.isDisposed())
182             return;
183
184         int last= doc.getNumberOfLines() - 1;
185         GC gc= new GC(widget);
186         gc.setFont(widget.getFont());
187         int maxWidth= 0;
188         String JavaDoc content= new String JavaDoc();
189
190         try {
191             for (int i= 0; i <= last; i++) {
192                 IRegion line;
193                 line= doc.getLineInformation(i);
194                 content= doc.get(line.getOffset(), line.getLength());
195                 int width= gc.textExtent(content).x;
196                 if (width > maxWidth) {
197                     maxWidth= width;
198                 }
199             }
200         } catch (BadLocationException e) {
201             return;
202         } finally {
203             gc.dispose();
204         }
205
206         // limit the size of the window to the maximum width minus scrolling,
207
// but never more than the configured max size (viewport size).
208
fMaxWidth= Math.max(0, Math.min(fMaxWidth, maxWidth - fHorizontalScrollPixel + 8));
209     }
210
211     /*
212      * @see org.eclipse.jdt.internal.ui.text.java.hover.SourceViewerInformationControl#hasContents()
213      */

214     public boolean hasContents() {
215         return super.hasContents() && fMaxWidth > 0;
216     }
217
218     /**
219      * Sets the horizontal scroll index in pixels.
220      *
221      * @param scrollIndex the new horizontal scroll index
222      */

223     public void setHorizontalScrollPixel(int scrollIndex) {
224         scrollIndex= Math.max(0, scrollIndex);
225         fHorizontalScrollPixel= scrollIndex;
226     }
227 }
228
Popular Tags