KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

56     public void setSizeConstraints(int maxWidth, int maxHeight) {
57         fMaxWidth= maxWidth;
58         fMaxHeight= maxHeight;
59     }
60
61     /**
62      * Creates a new information control.
63      *
64      * @param parent the shell that is the parent of this hover / control
65      * @param partition the initial partition type to be used for the underlying viewer
66      */

67     public CustomSourceInformationControl(Shell parent, String JavaDoc partition) {
68         super(parent);
69         setViewerFont();
70         setStartingPartitionType(partition);
71     }
72
73     /*
74      * @see org.eclipse.jface.text.IInformationControl#computeSizeHint()
75      */

76     public Point computeSizeHint() {
77         Point size= super.computeSizeHint();
78         size.x= Math.min(size.x, fMaxWidth);
79         size.y= Math.min(size.y, fMaxHeight);
80         return size;
81     }
82
83     /**
84      * Sets the font for this viewer sustaining selection and scroll position.
85      */

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

126     public void setStartingPartitionType(String JavaDoc partition) {
127         if (partition == null)
128             fPartition= IDocument.DEFAULT_CONTENT_TYPE;
129         else
130             fPartition= partition;
131     }
132
133     /*
134      * @see org.eclipse.jface.text.IInformationControl#setInformation(java.lang.String)
135      */

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

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

209     public boolean hasContents() {
210         return super.hasContents() && fMaxWidth > 0;
211     }
212
213     /**
214      * Sets the horizontal scroll index in pixels.
215      *
216      * @param scrollIndex the new horizontal scroll index
217      */

218     public void setHorizontalScrollPixel(int scrollIndex) {
219         scrollIndex= Math.max(0, scrollIndex);
220         fHorizontalScrollPixel= scrollIndex;
221     }
222 }
223
Popular Tags