KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > 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.jdt.internal.ui.text;
12
13
14
15 import java.io.IOException JavaDoc;
16 import java.io.StringReader JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.StyleRange;
21 import org.eclipse.swt.graphics.Drawable;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.graphics.FontData;
24 import org.eclipse.swt.graphics.GC;
25 import org.eclipse.swt.widgets.Display;
26
27 import org.eclipse.jface.text.DefaultInformationControl;
28 import org.eclipse.jface.text.Region;
29 import org.eclipse.jface.text.TextPresentation;
30
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32 import org.eclipse.jdt.internal.ui.JavaUIMessages;
33
34
35
36 public class HTMLTextPresenter implements DefaultInformationControl.IInformationPresenter, DefaultInformationControl.IInformationPresenterExtension {
37
38     private static final String JavaDoc LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
39

40     private int fCounter;
41     private boolean fEnforceUpperLineLimit;
42
43     /**
44      * Enables using bold font in order not to clip the text.
45      * <p>
46      * <em>Enabling this is a hack.</em>
47      *
48      * @since 3.2
49      */

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

113     public String JavaDoc updatePresentation(Display display, String JavaDoc hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
114         return updatePresentation((Drawable)display, hoverInfo, presentation, maxWidth, maxHeight);
115     }
116     
117     /*
118      * @see IHoverInformationPresenterExtension#updatePresentation(Drawable drawable, String, TextPresentation, int, int)
119      * @since 3.2
120      */

121     public String JavaDoc updatePresentation(Drawable drawable, String JavaDoc hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
122
123         if (hoverInfo == null)
124             return null;
125
126         GC gc= new GC(drawable);
127         
128         Font font= null;
129         if (fUseBoldFont) {
130             font= gc.getFont();
131             FontData[] fontData= font.getFontData();
132             for (int i= 0; i < fontData.length; i++)
133                 fontData[i].setStyle(SWT.BOLD);
134             font= new Font(gc.getDevice(), fontData);
135             gc.setFont(font);
136         }
137         
138         try {
139
140             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
141             int maxNumberOfLines= Math.round((float)maxHeight / gc.getFontMetrics().getHeight());
142
143             fCounter= 0;
144             LineBreakingReader reader= new LineBreakingReader(createReader(hoverInfo, presentation), gc, maxWidth);
145
146             boolean lastLineFormatted= false;
147             String JavaDoc lastLineIndent= null;
148
149             String JavaDoc line=reader.readLine();
150             boolean lineFormatted= reader.isFormattedLine();
151             boolean firstLineProcessed= false;
152
153             while (line != null) {
154
155                 if (fEnforceUpperLineLimit && maxNumberOfLines <= 0)
156                     break;
157
158                 if (firstLineProcessed) {
159                     if (!lastLineFormatted)
160                         append(buffer, LINE_DELIM, null);
161                     else {
162                         append(buffer, LINE_DELIM, presentation);
163                         if (lastLineIndent != null)
164                             append(buffer, lastLineIndent, presentation);
165                     }
166                 }
167
168                 append(buffer, line, null);
169                 firstLineProcessed= true;
170
171                 lastLineFormatted= lineFormatted;
172                 if (!lineFormatted)
173                     lastLineIndent= null;
174                 else if (lastLineIndent == null)
175                     lastLineIndent= getIndent(line);
176
177                 line= reader.readLine();
178                 lineFormatted= reader.isFormattedLine();
179
180                 maxNumberOfLines--;
181             }
182
183             if (line != null && buffer.length() > 0) {
184                 append(buffer, LINE_DELIM, lineFormatted ? presentation : null);
185                 append(buffer, JavaUIMessages.HTMLTextPresenter_ellipsis, presentation);
186             }
187
188             return trim(buffer, presentation);
189
190         } catch (IOException JavaDoc e) {
191
192             JavaPlugin.log(e);
193             return null;
194
195         } finally {
196             if (font != null)
197                 font.dispose();
198             gc.dispose();
199         }
200     }
201     
202     private String JavaDoc trim(StringBuffer JavaDoc buffer, TextPresentation presentation) {
203
204         int length= buffer.length();
205
206         int end= length -1;
207         while (end >= 0 && Character.isWhitespace(buffer.charAt(end)))
208             -- end;
209
210         if (end == -1)
211             return ""; //$NON-NLS-1$
212

213         if (end < length -1)
214             buffer.delete(end + 1, length);
215         else
216             end= length;
217
218         int start= 0;
219         while (start < end && Character.isWhitespace(buffer.charAt(start)))
220             ++ start;
221
222         buffer.delete(0, start);
223         presentation.setResultWindow(new Region(start, buffer.length()));
224         return buffer.toString();
225     }
226 }
227
228
Popular Tags