1 package net.sourceforge.phpdt.internal.ui.text;
4 * (c) Copyright IBM Corp. 2000, 2001.
8 import java.io.IOException;
10 import java.io.StringReader;
11 import java.util.Iterator;
13 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import org.eclipse.jface.text.DefaultInformationControl;
17 import org.eclipse.jface.text.Region;
18 import org.eclipse.jface.text.TextPresentation;
19 import org.eclipse.swt.custom.StyleRange;
20 import org.eclipse.swt.graphics.GC;
21 import org.eclipse.swt.widgets.Display;
23 public class HTMLTextPresenter implements
24 DefaultInformationControl.IInformationPresenter {
26 private static final String LINE_DELIM = System.getProperty(
27 "line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
31 private boolean fEnforceUpperLineLimit;
33 public HTMLTextPresenter(boolean enforceUpperLineLimit) {
35 fEnforceUpperLineLimit = enforceUpperLineLimit;
38 public HTMLTextPresenter() {
42 protected Reader createReader(String hoverInfo,
43 TextPresentation presentation) {
44 return new HTML2TextReader(new StringReader(hoverInfo), presentation);
47 protected void adaptTextPresentation(TextPresentation presentation,
48 int offset, int insertLength) {
50 int yoursStart = offset;
51 int yoursEnd = offset + insertLength - 1;
52 yoursEnd = Math.max(yoursStart, yoursEnd);
54 Iterator e = presentation.getAllStyleRangeIterator();
57 StyleRange range = (StyleRange) e.next();
59 int myStart = range.start;
60 int myEnd = range.start + range.length - 1;
61 myEnd = Math.max(myStart, myEnd);
63 if (myEnd < yoursStart)
66 if (myStart < yoursStart)
67 range.length += insertLength;
69 range.start += insertLength;
73 private void append(StringBuffer buffer, String string,
74 TextPresentation presentation) {
76 int length = string.length();
77 buffer.append(string);
79 if (presentation != null)
80 adaptTextPresentation(presentation, fCounter, length);
85 private String getIndent(String line) {
86 int length = line.length();
89 while (i < length && Character.isWhitespace(line.charAt(i)))
92 return (i == length ? line : line.substring(0, i)) + " "; //$NON-NLS-1$
96 * @see IHoverInformationPresenter#updatePresentation(Display display,
97 * String, TextPresentation, int, int)
99 public String updatePresentation(Display display, String hoverInfo,
100 TextPresentation presentation, int maxWidth, int maxHeight) {
102 if (hoverInfo == null)
105 GC gc = new GC(display);
108 StringBuffer buffer = new StringBuffer();
109 int maxNumberOfLines = Math.round(maxHeight
110 / gc.getFontMetrics().getHeight());
113 LineBreakingReader reader = new LineBreakingReader(createReader(
114 hoverInfo, presentation), gc, maxWidth);
116 boolean lastLineFormatted = false;
117 String lastLineIndent = null;
119 String line = reader.readLine();
120 boolean lineFormatted = reader.isFormattedLine();
121 boolean firstLineProcessed = false;
123 while (line != null) {
125 if (fEnforceUpperLineLimit && maxNumberOfLines <= 0)
128 if (firstLineProcessed) {
129 if (!lastLineFormatted)
130 append(buffer, LINE_DELIM, null);
132 append(buffer, LINE_DELIM, presentation);
133 if (lastLineIndent != null)
134 append(buffer, lastLineIndent, presentation);
138 append(buffer, line, null);
139 firstLineProcessed = true;
141 lastLineFormatted = lineFormatted;
143 lastLineIndent = null;
144 else if (lastLineIndent == null)
145 lastLineIndent = getIndent(line);
147 line = reader.readLine();
148 lineFormatted = reader.isFormattedLine();
154 append(buffer, LINE_DELIM, lineFormatted ? presentation : null);
155 append(buffer, PHPUIMessages
156 .getString("HTMLTextPresenter.ellipsis"), presentation); //$NON-NLS-1$
159 return trim(buffer, presentation);
161 } catch (IOException e) {
163 PHPeclipsePlugin.log(e);
171 private String trim(StringBuffer buffer, TextPresentation presentation) {
173 int length = buffer.length();
175 int end = length - 1;
176 while (end >= 0 && Character.isWhitespace(buffer.charAt(end)))
180 return ""; //$NON-NLS-1$
182 if (end < length - 1)
183 buffer.delete(end + 1, length);
188 while (start < end && Character.isWhitespace(buffer.charAt(start)))
191 buffer.delete(0, start);
192 presentation.setResultWindow(new Region(start, buffer.length()));
193 return buffer.toString();