KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > contentassist > display > HTMLTextPresenter


1 /*******************************************************************************
2  * Copyright (c) 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.pde.internal.ui.editor.contentassist.display;
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.pde.internal.ui.util.LineBreakingReader;
21 import org.eclipse.swt.custom.StyleRange;
22 import org.eclipse.swt.graphics.Drawable;
23 import org.eclipse.swt.graphics.GC;
24 import org.eclipse.swt.widgets.Display;
25
26 import org.eclipse.jface.text.DefaultInformationControl;
27 import org.eclipse.jface.text.Region;
28 import org.eclipse.jface.text.TextPresentation;
29
30
31
32 public class HTMLTextPresenter implements DefaultInformationControl.IInformationPresenter, DefaultInformationControl.IInformationPresenterExtension {
33
34     private static final String JavaDoc LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
35

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

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

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

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