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.ui.WebUI;
15 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
17 import org.eclipse.jface.text.DefaultInformationControl;
18 import org.eclipse.jface.text.Region;
19 import org.eclipse.jface.text.TextPresentation;
20 import org.eclipse.swt.custom.StyleRange;
21 import org.eclipse.swt.graphics.GC;
22 import org.eclipse.swt.widgets.Display;
24 public class HTMLTextPresenter implements
25 DefaultInformationControl.IInformationPresenter {
27 private static final String LINE_DELIM = System.getProperty(
28 "line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
32 private boolean fEnforceUpperLineLimit;
34 public HTMLTextPresenter(boolean enforceUpperLineLimit) {
36 fEnforceUpperLineLimit = enforceUpperLineLimit;
39 public HTMLTextPresenter() {
43 protected Reader createReader(String hoverInfo,
44 TextPresentation presentation) {
45 return new HTML2TextReader(new StringReader(hoverInfo), presentation);
48 protected void adaptTextPresentation(TextPresentation presentation,
49 int offset, int insertLength) {
51 int yoursStart = offset;
52 int yoursEnd = offset + insertLength - 1;
53 yoursEnd = Math.max(yoursStart, yoursEnd);
55 Iterator e = presentation.getAllStyleRangeIterator();
58 StyleRange range = (StyleRange) e.next();
60 int myStart = range.start;
61 int myEnd = range.start + range.length - 1;
62 myEnd = Math.max(myStart, myEnd);
64 if (myEnd < yoursStart)
67 if (myStart < yoursStart)
68 range.length += insertLength;
70 range.start += insertLength;
74 private void append(StringBuffer buffer, String string,
75 TextPresentation presentation) {
77 int length = string.length();
78 buffer.append(string);
80 if (presentation != null)
81 adaptTextPresentation(presentation, fCounter, length);
86 private String getIndent(String line) {
87 int length = line.length();
90 while (i < length && Character.isWhitespace(line.charAt(i)))
93 return (i == length ? line : line.substring(0, i)) + " "; //$NON-NLS-1$
97 * @see IHoverInformationPresenter#updatePresentation(Display display,
98 * String, TextPresentation, int, int)
100 public String updatePresentation(Display display, String hoverInfo,
101 TextPresentation presentation, int maxWidth, int maxHeight) {
103 if (hoverInfo == null)
106 GC gc = new GC(display);
109 StringBuffer buffer = new StringBuffer();
110 int maxNumberOfLines = Math.round(maxHeight
111 / gc.getFontMetrics().getHeight());
114 LineBreakingReader reader = new LineBreakingReader(createReader(
115 hoverInfo, presentation), gc, maxWidth);
117 boolean lastLineFormatted = false;
118 String lastLineIndent = null;
120 String line = reader.readLine();
121 boolean lineFormatted = reader.isFormattedLine();
122 boolean firstLineProcessed = false;
124 while (line != null) {
126 if (fEnforceUpperLineLimit && maxNumberOfLines <= 0)
129 if (firstLineProcessed) {
130 if (!lastLineFormatted)
131 append(buffer, LINE_DELIM, null);
133 append(buffer, LINE_DELIM, presentation);
134 if (lastLineIndent != null)
135 append(buffer, lastLineIndent, presentation);
139 append(buffer, line, null);
140 firstLineProcessed = true;
142 lastLineFormatted = lineFormatted;
144 lastLineIndent = null;
145 else if (lastLineIndent == null)
146 lastLineIndent = getIndent(line);
148 line = reader.readLine();
149 lineFormatted = reader.isFormattedLine();
155 append(buffer, LINE_DELIM, lineFormatted ? presentation : null);
156 append(buffer, PHPUIMessages
157 .getString("HTMLTextPresenter.ellipsis"), presentation); //$NON-NLS-1$
160 return trim(buffer, presentation);
162 } catch (IOException e) {
172 private String trim(StringBuffer buffer, TextPresentation presentation) {
174 int length = buffer.length();
176 int end = length - 1;
177 while (end >= 0 && Character.isWhitespace(buffer.charAt(end)))
181 return ""; //$NON-NLS-1$
183 if (end < length - 1)
184 buffer.delete(end + 1, length);
189 while (start < end && Character.isWhitespace(buffer.charAt(start)))
192 buffer.delete(0, start);
193 presentation.setResultWindow(new Region(start, buffer.length()));
194 return buffer.toString();