]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/text/ChangeHoverInformationControl.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / text / ChangeHoverInformationControl.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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.core.runtime.Assert;
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 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 fPartition;
51         /** The horizontal scroll index. */
52         private int fHorizontalScrollPixel;
53
54         /*
55          * @see org.eclipse.jface.text.IInformationControl#setSizeConstraints(int, int)
56          */
57         @Override
58         public void setSizeConstraints(int maxWidth, int maxHeight) {
59                 fMaxWidth= maxWidth;
60                 fMaxHeight= maxHeight;
61         }
62
63         /**
64          * Creates a new information control.
65          *
66          * @param parent the shell that is the parent of this hover / control
67          * @param isResizable <code>true</code> if resizable
68          * @param orientation the orientation
69          * @param partition the initial partition type to be used for the underlying viewer
70          * @param statusFieldText the text to be used in the optional status field
71          *                         or <code>null</code> if the status field should be hidden
72          */
73         public ChangeHoverInformationControl(Shell parent, boolean isResizable, int orientation, String partition, String statusFieldText) {
74                 super(parent, isResizable, orientation, statusFieldText);
75                 setViewerFont();
76                 setStartingPartitionType(partition);
77         }
78
79         /*
80          * @see org.eclipse.jface.text.IInformationControl#computeSizeHint()
81          */
82         @Override
83         public Point computeSizeHint() {
84                 Point size= super.computeSizeHint();
85                 size.x= Math.min(size.x, fMaxWidth);
86                 size.y= Math.min(size.y, fMaxHeight);
87                 return size;
88         }
89
90         /**
91          * Sets the font for this viewer sustaining selection and scroll position.
92          */
93         private void setViewerFont() {
94                 Font font= JFaceResources.getFont(SYMBOLIC_FONT_NAME);
95
96                 if (getViewer().getDocument() != null) {
97
98                         Point selection= getViewer().getSelectedRange();
99                         int topIndex= getViewer().getTopIndex();
100
101                         StyledText styledText= getViewer().getTextWidget();
102                         Control parent= styledText;
103                         if (getViewer() instanceof ITextViewerExtension) {
104                                 ITextViewerExtension extension= (ITextViewerExtension) getViewer();
105                                 parent= extension.getControl();
106                         }
107
108                         parent.setRedraw(false);
109
110                         styledText.setFont(font);
111
112                         getViewer().setSelectedRange(selection.x , selection.y);
113                         getViewer().setTopIndex(topIndex);
114
115                         if (parent instanceof Composite) {
116                                 Composite composite= (Composite) parent;
117                                 composite.layout(true);
118                         }
119
120                         parent.setRedraw(true);
121
122                 } else {
123                         StyledText styledText= getViewer().getTextWidget();
124                         styledText.setFont(font);
125                 }
126         }
127
128         /**
129          * Sets the initial partition for the underlying source viewer.
130          *
131          * @param partition the partition type
132          */
133         public void setStartingPartitionType(String partition) {
134                 if (partition == null)
135                         fPartition= IDocument.DEFAULT_CONTENT_TYPE;
136                 else
137                         fPartition= partition;
138         }
139
140         /*
141          * @see org.eclipse.jface.text.IInformationControl#setInformation(java.lang.String)
142          */
143         @Override
144         public void setInformation(String content) {
145                 super.setInformation(content);
146                 IDocument doc= getViewer().getDocument();
147                 if (doc == null)
148                         return;
149
150                 // ensure that we can scroll enough
151                 ensureScrollable();
152
153                 String start= null;
154                 if (IJavaPartitions.JAVA_DOC.equals(fPartition)) {
155                         start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
156                 } else if (IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(fPartition)) {
157                         start= "/*" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
158                 }
159                 if (start != null) {
160                         try {
161                                 doc.replace(0, 0, start);
162                                 int startLen= start.length();
163                                 getViewer().setDocument(doc, startLen, doc.getLength() - startLen);
164                         } catch (BadLocationException e) {
165                                 // impossible
166                                 Assert.isTrue(false);
167                         }
168                 }
169
170                 getViewer().getTextWidget().setHorizontalPixel(fHorizontalScrollPixel);
171         }
172
173         /**
174          * Ensures that the control can be scrolled at least to
175          * <code>fHorizontalScrollPixel</code> and adjusts <code>fMaxWidth</code>
176          * accordingly.
177          */
178         private void ensureScrollable() {
179                 IDocument doc= getViewer().getDocument();
180                 if (doc == null)
181                         return;
182
183                 StyledText widget= getViewer().getTextWidget();
184                 if (widget == null || widget.isDisposed())
185                         return;
186
187                 int last= doc.getNumberOfLines() - 1;
188                 GC gc= new GC(widget);
189                 gc.setFont(widget.getFont());
190                 int maxWidth= 0;
191                 String content= new String();
192
193                 try {
194                         for (int i= 0; i <= last; i++) {
195                                 IRegion line;
196                                 line= doc.getLineInformation(i);
197                                 content= doc.get(line.getOffset(), line.getLength());
198                                 int width= gc.textExtent(content).x;
199                                 if (width > maxWidth) {
200                                         maxWidth= width;
201                                 }
202                         }
203                 } catch (BadLocationException e) {
204                         return;
205                 } finally {
206                         gc.dispose();
207                 }
208
209                 // limit the size of the window to the maximum width minus scrolling,
210                 // but never more than the configured max size (viewport size).
211                 fMaxWidth= Math.max(0, Math.min(fMaxWidth, maxWidth - fHorizontalScrollPixel + 8));
212         }
213
214         /*
215          * @see org.eclipse.jdt.internal.ui.text.java.hover.SourceViewerInformationControl#hasContents()
216          */
217         @Override
218         public boolean hasContents() {
219                 return super.hasContents() && fMaxWidth > 0;
220         }
221
222         /**
223          * Sets the horizontal scroll index in pixels.
224          *
225          * @param scrollIndex the new horizontal scroll index
226          */
227         public void setHorizontalScrollPixel(int scrollIndex) {
228                 scrollIndex= Math.max(0, scrollIndex);
229                 fHorizontalScrollPixel= scrollIndex;
230         }
231
232         public void generated_3096472255417592767(JavaChangeHover javachangehover) {
233                 setStartingPartitionType(javachangehover.fPartition);
234                 setHorizontalScrollPixel(javachangehover.fLastScrollIndex);
235         }
236 }