1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
13 import net.sourceforge.phpdt.internal.ui.text.java.hover.SourceViewerInformationControl;
15 import org.eclipse.jface.resource.JFaceResources;
17 //import org.eclipse.jface.text.Assert;
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.jface.text.ITextViewerExtension;
23 import org.eclipse.swt.custom.StyledText;
24 import org.eclipse.swt.graphics.Font;
25 import org.eclipse.swt.graphics.GC;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Shell;
32 * Source viewer used to display quick diff hovers.
36 public class CustomSourceInformationControl extends
37 SourceViewerInformationControl {
39 /** The font name for the viewer font - the same as the java editor's. */
40 private static final String SYMBOLIC_FONT_NAME = "net.sourceforge.phpdt.ui.editors.textfont"; //$NON-NLS-1$
43 * The maximum width of the control, set in
44 * <code>setSizeConstraints(int, int)</code>.
46 int fMaxWidth = Integer.MAX_VALUE;
49 * The maximum height of the control, set in
50 * <code>setSizeConstraints(int, int)</code>.
52 int fMaxHeight = Integer.MAX_VALUE;
55 * The partition type to be used as the starting partition type by the
58 private String fPartition;
60 /** The horizontal scroll index. */
61 private int fHorizontalScrollPixel;
64 * @see org.eclipse.jface.text.IInformationControl#setSizeConstraints(int,
67 public void setSizeConstraints(int maxWidth, int maxHeight) {
69 fMaxHeight = maxHeight;
73 * Creates a new information control.
76 * the shell that is the parent of this hover / control
78 * the initial partition type to be used for the underlying
81 public CustomSourceInformationControl(Shell parent, String partition) {
84 setStartingPartitionType(partition);
88 * @see org.eclipse.jface.text.IInformationControl#computeSizeHint()
90 public Point computeSizeHint() {
91 Point size = super.computeSizeHint();
92 size.x = Math.min(size.x, fMaxWidth);
93 size.y = Math.min(size.y, fMaxHeight);
98 * Sets the font for this viewer sustaining selection and scroll position.
100 private void setViewerFont() {
101 Font font = JFaceResources.getFont(SYMBOLIC_FONT_NAME);
103 if (getViewer().getDocument() != null) {
105 Point selection = getViewer().getSelectedRange();
106 int topIndex = getViewer().getTopIndex();
108 StyledText styledText = getViewer().getTextWidget();
109 Control parent = styledText;
110 if (getViewer() instanceof ITextViewerExtension) {
111 ITextViewerExtension extension = (ITextViewerExtension) getViewer();
112 parent = extension.getControl();
115 parent.setRedraw(false);
117 styledText.setFont(font);
119 getViewer().setSelectedRange(selection.x, selection.y);
120 getViewer().setTopIndex(topIndex);
122 if (parent instanceof Composite) {
123 Composite composite = (Composite) parent;
124 composite.layout(true);
127 parent.setRedraw(true);
130 StyledText styledText = getViewer().getTextWidget();
131 styledText.setFont(font);
136 * Sets the initial partition for the underlying source viewer.
141 public void setStartingPartitionType(String partition) {
142 if (partition == null)
143 fPartition = IDocument.DEFAULT_CONTENT_TYPE;
145 fPartition = partition;
149 * @see org.eclipse.jface.text.IInformationControl#setInformation(java.lang.String)
151 public void setInformation(String content) {
152 super.setInformation(content);
153 IDocument doc = getViewer().getDocument();
157 // ensure that we can scroll enough
161 if (IPHPPartitions.PHP_PHPDOC_COMMENT.equals(fPartition)) {
162 start = "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
163 } else if (IPHPPartitions.PHP_MULTILINE_COMMENT.equals(fPartition)) {
164 start = "/*" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
168 doc.replace(0, 0, start);
169 int startLen = start.length();
170 getViewer().setDocument(doc, startLen,
171 doc.getLength() - startLen);
172 } catch (BadLocationException e) {
174 Assert.isTrue(false);
178 getViewer().getTextWidget().setHorizontalPixel(fHorizontalScrollPixel);
182 * Ensures that the control can be scrolled at least to
183 * <code>fHorizontalScrollPixel</code> and adjusts <code>fMaxWidth</code>
186 private void ensureScrollable() {
187 IDocument doc = getViewer().getDocument();
191 StyledText widget = getViewer().getTextWidget();
192 if (widget == null || widget.isDisposed())
195 int last = doc.getNumberOfLines() - 1;
196 GC gc = new GC(widget);
197 gc.setFont(widget.getFont());
199 String content = new String();
202 for (int i = 0; i <= last; i++) {
204 line = doc.getLineInformation(i);
205 content = doc.get(line.getOffset(), line.getLength());
206 int width = gc.textExtent(content).x;
207 if (width > maxWidth) {
211 } catch (BadLocationException e) {
217 // limit the size of the window to the maximum width minus scrolling,
218 // but never more than the configured max size (viewport size).
219 fMaxWidth = Math.max(0, Math.min(fMaxWidth, maxWidth
220 - fHorizontalScrollPixel + 8));
224 * @see net.sourceforge.phpdt.internal.ui.text.java.hover.SourceViewerInformationControl#hasContents()
226 public boolean hasContents() {
227 return super.hasContents() && fMaxWidth > 0;
231 * Sets the horizontal scroll index in pixels.
234 * the new horizontal scroll index
236 public void setHorizontalScrollPixel(int scrollIndex) {
237 scrollIndex = Math.max(0, scrollIndex);
238 fHorizontalScrollPixel = scrollIndex;