KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > html > HTMLTextPresenter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.internal.text.html;
12
13 import java.io.IOException JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.swt.custom.StyleRange;
19 import org.eclipse.swt.graphics.Drawable;
20 import org.eclipse.swt.graphics.GC;
21 import org.eclipse.swt.widgets.Display;
22
23 import org.eclipse.jface.internal.text.link.contentassist.LineBreakingReader;
24
25 import org.eclipse.jface.text.DefaultInformationControl;
26 import org.eclipse.jface.text.Region;
27 import org.eclipse.jface.text.TextPresentation;
28
29
30 /**
31  * <p>
32  * Moved into this package from <code>org.eclipse.jface.internal.text.revisions</code>.</p>
33  */

34 public class HTMLTextPresenter implements DefaultInformationControl.IInformationPresenter, DefaultInformationControl.IInformationPresenterExtension {
35
36     private static final String JavaDoc LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
37

38     private int fCounter;
39     private boolean fEnforceUpperLineLimit;
40
41     public HTMLTextPresenter(boolean enforceUpperLineLimit) {
42         super();
43         fEnforceUpperLineLimit= enforceUpperLineLimit;
44     }
45
46     public HTMLTextPresenter() {
47         this(true);
48     }
49
50     protected Reader JavaDoc createReader(String JavaDoc hoverInfo, TextPresentation presentation) {
51         return new HTML2TextReader(new StringReader JavaDoc(hoverInfo), presentation);
52     }
53
54     protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) {
55
56         int yoursStart= offset;
57         int yoursEnd= offset + insertLength -1;
58         yoursEnd= Math.max(yoursStart, yoursEnd);
59
60         Iterator JavaDoc e= presentation.getAllStyleRangeIterator();
61         while (e.hasNext()) {
62
63             StyleRange range= (StyleRange) e.next();
64
65             int myStart= range.start;
66             int myEnd= range.start + range.length -1;
67             myEnd= Math.max(myStart, myEnd);
68
69             if (myEnd < yoursStart)
70                 continue;
71
72             if (myStart < yoursStart)
73                 range.length += insertLength;
74             else
75                 range.start += insertLength;
76         }
77     }
78
79     private void append(StringBuffer JavaDoc buffer, String JavaDoc string, TextPresentation presentation) {
80
81         int length= string.length();
82         buffer.append(string);
83
84         if (presentation != null)
85             adaptTextPresentation(presentation, fCounter, length);
86
87         fCounter += length;
88     }
89
90     private String JavaDoc getIndent(String JavaDoc line) {
91         int length= line.length();
92
93         int i= 0;
94         while (i < length && Character.isWhitespace(line.charAt(i))) ++i;
95
96         return (i == length ? line : line.substring(0, i)) + " "; //$NON-NLS-1$
97
}
98
99     /*
100      * @see IHoverInformationPresenter#updatePresentation(Display display, String, TextPresentation, int, int)
101      */

102     public String JavaDoc updatePresentation(Display display, String JavaDoc hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
103         return updatePresentation((Drawable)display, hoverInfo, presentation, maxWidth, maxHeight);
104     }
105
106     /*
107      * @see IHoverInformationPresenterExtension#updatePresentation(Drawable drawable, String, TextPresentation, int, int)
108      * @since 3.2
109      */

110     public String JavaDoc updatePresentation(Drawable drawable, String JavaDoc hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
111
112         if (hoverInfo == null)
113             return null;
114
115         GC gc= new GC(drawable);
116         try {
117
118             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
119             int maxNumberOfLines= Math.round(maxHeight / gc.getFontMetrics().getHeight());
120
121             fCounter= 0;
122             LineBreakingReader reader= new LineBreakingReader(createReader(hoverInfo, presentation), gc, maxWidth);
123
124             boolean lastLineFormatted= false;
125             String JavaDoc lastLineIndent= null;
126
127             String JavaDoc line=reader.readLine();
128             boolean lineFormatted= reader.isFormattedLine();
129             boolean firstLineProcessed= false;
130
131             while (line != null) {
132
133                 if (fEnforceUpperLineLimit && maxNumberOfLines <= 0)
134                     break;
135
136                 if (firstLineProcessed) {
137                     if (!lastLineFormatted)
138                         append(buffer, LINE_DELIM, null);
139                     else {
140                         append(buffer, LINE_DELIM, presentation);
141                         if (lastLineIndent != null)
142                             append(buffer, lastLineIndent, presentation);
143                     }
144                 }
145
146                 append(buffer, line, null);
147                 firstLineProcessed= true;
148
149                 lastLineFormatted= lineFormatted;
150                 if (!lineFormatted)
151                     lastLineIndent= null;
152                 else if (lastLineIndent == null)
153                     lastLineIndent= getIndent(line);
154
155                 line= reader.readLine();
156                 lineFormatted= reader.isFormattedLine();
157
158                 maxNumberOfLines--;
159             }
160
161             if (line != null) {
162                 append(buffer, LINE_DELIM, lineFormatted ? presentation : null);
163                 append(buffer, HTMLMessages.getString("HTMLTextPresenter.ellipse"), presentation); //$NON-NLS-1$
164
}
165
166             return trim(buffer, presentation);
167
168         } catch (IOException JavaDoc e) {
169
170             // ignore TODO do something else?
171
return null;
172
173         } finally {
174             gc.dispose();
175         }
176     }
177
178     private String JavaDoc trim(StringBuffer JavaDoc buffer, TextPresentation presentation) {
179
180         int length= buffer.length();
181
182         int end= length -1;
183         while (end >= 0 && Character.isWhitespace(buffer.charAt(end)))
184             -- end;
185
186         if (end == -1)
187             return ""; //$NON-NLS-1$
188

189         if (end < length -1)
190             buffer.delete(end + 1, length);
191         else
192             end= length;
193
194         int start= 0;
195         while (start < end && Character.isWhitespace(buffer.charAt(start)))
196             ++ start;
197
198         buffer.delete(0, start);
199         presentation.setResultWindow(new Region(start, buffer.length()));
200         return buffer.toString();
201     }
202 }
203
204
Popular Tags