KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > link > contentassist > HTMLTextPresenter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.link.contentassist;
12
13
14
15 import java.io.IOException JavaDoc;
16 import java.io.Reader JavaDoc;
17 import java.io.StringReader JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import org.eclipse.swt.custom.StyleRange;
21 import org.eclipse.swt.graphics.Drawable;
22 import org.eclipse.swt.graphics.GC;
23 import org.eclipse.swt.widgets.Display;
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 public class HTMLTextPresenter implements DefaultInformationControl.IInformationPresenter, DefaultInformationControl.IInformationPresenterExtension {
32
33     private static final String JavaDoc LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
34

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

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

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

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