001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.contentassist.display;
011:
012: import java.io.IOException;
013: import java.io.Reader;
014: import java.io.StringReader;
015: import java.util.Iterator;
016:
017: import org.eclipse.pde.internal.ui.util.LineBreakingReader;
018: import org.eclipse.swt.custom.StyleRange;
019: import org.eclipse.swt.graphics.Drawable;
020: import org.eclipse.swt.graphics.GC;
021: import org.eclipse.swt.widgets.Display;
022:
023: import org.eclipse.jface.text.DefaultInformationControl;
024: import org.eclipse.jface.text.Region;
025: import org.eclipse.jface.text.TextPresentation;
026:
027: public class HTMLTextPresenter implements
028: DefaultInformationControl.IInformationPresenter,
029: DefaultInformationControl.IInformationPresenterExtension {
030:
031: private static final String LINE_DELIM = System.getProperty(
032: "line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
033:
034: private int fCounter;
035: private boolean fEnforceUpperLineLimit;
036:
037: public HTMLTextPresenter(boolean enforceUpperLineLimit) {
038: super ();
039: fEnforceUpperLineLimit = enforceUpperLineLimit;
040: }
041:
042: protected Reader createReader(String hoverInfo,
043: TextPresentation presentation) {
044: return new HTML2TextReader(new StringReader(hoverInfo),
045: presentation);
046: }
047:
048: protected void adaptTextPresentation(TextPresentation presentation,
049: int offset, int insertLength) {
050:
051: int yoursStart = offset;
052: int yoursEnd = offset + insertLength - 1;
053: yoursEnd = Math.max(yoursStart, yoursEnd);
054:
055: Iterator e = presentation.getAllStyleRangeIterator();
056: while (e.hasNext()) {
057:
058: StyleRange range = (StyleRange) e.next();
059:
060: int myStart = range.start;
061: int myEnd = range.start + range.length - 1;
062: myEnd = Math.max(myStart, myEnd);
063:
064: if (myEnd < yoursStart)
065: continue;
066:
067: if (myStart < yoursStart)
068: range.length += insertLength;
069: else
070: range.start += insertLength;
071: }
072: }
073:
074: private void append(StringBuffer buffer, String string,
075: TextPresentation presentation) {
076:
077: int length = string.length();
078: buffer.append(string);
079:
080: if (presentation != null)
081: adaptTextPresentation(presentation, fCounter, length);
082:
083: fCounter += length;
084: }
085:
086: private String getIndent(String line) {
087: int length = line.length();
088:
089: int i = 0;
090: while (i < length && Character.isWhitespace(line.charAt(i)))
091: ++i;
092:
093: return (i == length ? line : line.substring(0, i)) + " "; //$NON-NLS-1$
094: }
095:
096: /*
097: * @see IHoverInformationPresenter#updatePresentation(Display display, String, TextPresentation, int, int)
098: */
099: public String updatePresentation(Display display, String hoverInfo,
100: TextPresentation presentation, int maxWidth, int maxHeight) {
101: return updatePresentation((Drawable) display, hoverInfo,
102: presentation, maxWidth, maxHeight);
103: }
104:
105: /*
106: * @see IHoverInformationPresenterExtension#updatePresentation(Drawable drawable, String, TextPresentation, int, int)
107: * @since 3.2
108: */
109: public String updatePresentation(Drawable drawable,
110: String hoverInfo, TextPresentation presentation,
111: int maxWidth, int maxHeight) {
112:
113: if (hoverInfo == null)
114: return null;
115:
116: GC gc = new GC(drawable);
117: try {
118:
119: StringBuffer buffer = new StringBuffer();
120: int maxNumberOfLines = Math.round(maxHeight
121: / gc.getFontMetrics().getHeight());
122:
123: fCounter = 0;
124: LineBreakingReader reader = new LineBreakingReader(
125: createReader(hoverInfo, presentation), gc, maxWidth);
126:
127: boolean lastLineFormatted = false;
128: String lastLineIndent = null;
129:
130: String line = reader.readLine();
131: boolean lineFormatted = reader.isFormattedLine();
132: boolean firstLineProcessed = false;
133:
134: while (line != null) {
135:
136: if (fEnforceUpperLineLimit && maxNumberOfLines <= 0)
137: break;
138:
139: if (firstLineProcessed) {
140: if (!lastLineFormatted)
141: append(buffer, LINE_DELIM, null);
142: else {
143: append(buffer, LINE_DELIM, presentation);
144: if (lastLineIndent != null)
145: append(buffer, lastLineIndent, presentation);
146: }
147: }
148:
149: append(buffer, line, null);
150: firstLineProcessed = true;
151:
152: lastLineFormatted = lineFormatted;
153: if (!lineFormatted)
154: lastLineIndent = null;
155: else if (lastLineIndent == null)
156: lastLineIndent = getIndent(line);
157:
158: line = reader.readLine();
159: lineFormatted = reader.isFormattedLine();
160:
161: maxNumberOfLines--;
162: }
163:
164: if (line != null) {
165: append(buffer, LINE_DELIM, lineFormatted ? presentation
166: : null);
167: append(buffer, "...", presentation); //$NON-NLS-1$
168: }
169:
170: return trim(buffer, presentation);
171:
172: } catch (IOException e) {
173:
174: // ignore TODO do something else?
175: return null;
176:
177: } finally {
178: gc.dispose();
179: }
180: }
181:
182: private String trim(StringBuffer buffer,
183: TextPresentation presentation) {
184:
185: int length = buffer.length();
186:
187: int end = length - 1;
188: while (end >= 0 && Character.isWhitespace(buffer.charAt(end)))
189: --end;
190:
191: if (end == -1)
192: return ""; //$NON-NLS-1$
193:
194: if (end < length - 1)
195: buffer.delete(end + 1, length);
196: else
197: end = length;
198:
199: int start = 0;
200: while (start < end
201: && Character.isWhitespace(buffer.charAt(start)))
202: ++start;
203:
204: buffer.delete(0, start);
205: presentation
206: .setResultWindow(new Region(start, buffer.length()));
207: return buffer.toString();
208: }
209: }
|