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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text;
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;
21 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.jface.resource.JFaceResources;
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;
30 import org.eclipse.jdt.ui.text.IJavaPartitions;
32 import org.eclipse.jdt.internal.ui.text.java.hover.SourceViewerInformationControl;
35 * Specialized source viewer information control used to display quick diff hovers.
39 class ChangeHoverInformationControl extends SourceViewerInformationControl {
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$
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;
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;
55 * @see org.eclipse.jface.text.IInformationControl#setSizeConstraints(int, int)
58 public void setSizeConstraints(int maxWidth, int maxHeight) {
60 fMaxHeight= maxHeight;
64 * Creates a new information control.
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
73 public ChangeHoverInformationControl(Shell parent, boolean isResizable, int orientation, String partition, String statusFieldText) {
74 super(parent, isResizable, orientation, statusFieldText);
76 setStartingPartitionType(partition);
80 * @see org.eclipse.jface.text.IInformationControl#computeSizeHint()
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);
91 * Sets the font for this viewer sustaining selection and scroll position.
93 private void setViewerFont() {
94 Font font= JFaceResources.getFont(SYMBOLIC_FONT_NAME);
96 if (getViewer().getDocument() != null) {
98 Point selection= getViewer().getSelectedRange();
99 int topIndex= getViewer().getTopIndex();
101 StyledText styledText= getViewer().getTextWidget();
102 Control parent= styledText;
103 if (getViewer() instanceof ITextViewerExtension) {
104 ITextViewerExtension extension= (ITextViewerExtension) getViewer();
105 parent= extension.getControl();
108 parent.setRedraw(false);
110 styledText.setFont(font);
112 getViewer().setSelectedRange(selection.x , selection.y);
113 getViewer().setTopIndex(topIndex);
115 if (parent instanceof Composite) {
116 Composite composite= (Composite) parent;
117 composite.layout(true);
120 parent.setRedraw(true);
123 StyledText styledText= getViewer().getTextWidget();
124 styledText.setFont(font);
129 * Sets the initial partition for the underlying source viewer.
131 * @param partition the partition type
133 public void setStartingPartitionType(String partition) {
134 if (partition == null)
135 fPartition= IDocument.DEFAULT_CONTENT_TYPE;
137 fPartition= partition;
141 * @see org.eclipse.jface.text.IInformationControl#setInformation(java.lang.String)
144 public void setInformation(String content) {
145 super.setInformation(content);
146 IDocument doc= getViewer().getDocument();
150 // ensure that we can scroll enough
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$
161 doc.replace(0, 0, start);
162 int startLen= start.length();
163 getViewer().setDocument(doc, startLen, doc.getLength() - startLen);
164 } catch (BadLocationException e) {
166 Assert.isTrue(false);
170 getViewer().getTextWidget().setHorizontalPixel(fHorizontalScrollPixel);
174 * Ensures that the control can be scrolled at least to
175 * <code>fHorizontalScrollPixel</code> and adjusts <code>fMaxWidth</code>
178 private void ensureScrollable() {
179 IDocument doc= getViewer().getDocument();
183 StyledText widget= getViewer().getTextWidget();
184 if (widget == null || widget.isDisposed())
187 int last= doc.getNumberOfLines() - 1;
188 GC gc= new GC(widget);
189 gc.setFont(widget.getFont());
191 String content= new String();
194 for (int i= 0; i <= last; i++) {
196 line= doc.getLineInformation(i);
197 content= doc.get(line.getOffset(), line.getLength());
198 int width= gc.textExtent(content).x;
199 if (width > maxWidth) {
203 } catch (BadLocationException e) {
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));
215 * @see org.eclipse.jdt.internal.ui.text.java.hover.SourceViewerInformationControl#hasContents()
218 public boolean hasContents() {
219 return super.hasContents() && fMaxWidth > 0;
223 * Sets the horizontal scroll index in pixels.
225 * @param scrollIndex the new horizontal scroll index
227 public void setHorizontalScrollPixel(int scrollIndex) {
228 scrollIndex= Math.max(0, scrollIndex);
229 fHorizontalScrollPixel= scrollIndex;
232 public void generated_3096472255417592767(JavaChangeHover javachangehover) {
233 setStartingPartitionType(javachangehover.fPartition);
234 setHorizontalScrollPixel(javachangehover.fLastScrollIndex);