KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.pde.internal.ui.editor.contentassist.display;
13
14 import java.io.IOException JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.jface.text.TextPresentation;
19 import org.eclipse.jface.text.DefaultInformationControl.IInformationPresenter;
20 import org.eclipse.jface.text.DefaultInformationControl.IInformationPresenterExtension;
21 import org.eclipse.pde.internal.ui.PDEPlugin;
22 import org.eclipse.pde.internal.ui.PDEUIMessages;
23 import org.eclipse.pde.internal.ui.util.LineBreakingReader;
24 import org.eclipse.swt.custom.StyleRange;
25 import org.eclipse.swt.graphics.Drawable;
26 import org.eclipse.swt.graphics.Font;
27 import org.eclipse.swt.graphics.GC;
28 import org.eclipse.swt.widgets.Display;
29
30 /**
31  * InfoControlTextPresenter
32  * Derived from org.eclipse.jdt.internal.ui.text.HTMLTextPresenter
33  */

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

40     /**
41      * Used to ensure that text does not rest right against the right border and
42      * bottom border
43      */

44     private static final int LINE_REDUCTION = 2;
45
46     private static final String JavaDoc INDENT = " "; //$NON-NLS-1$
47

48     private int fCounter;
49
50     protected void adaptTextPresentation(TextPresentation presentation,
51             int offset, int insertLength) {
52
53         int yoursStart = offset;
54         int yoursEnd = offset + insertLength - 1;
55         yoursEnd = Math.max(yoursStart, yoursEnd);
56         Iterator JavaDoc e = presentation.getAllStyleRangeIterator();
57
58         while (e.hasNext()) {
59             StyleRange range = (StyleRange) e.next();
60             int myStart = range.start;
61             int myEnd = range.start + range.length - 1;
62             myEnd = Math.max(myStart, myEnd);
63
64             if (myEnd < yoursStart)
65                 continue;
66
67             if (myStart < yoursStart) {
68                 range.length += insertLength;
69             } else {
70                 range.start += insertLength;
71             }
72         }
73     }
74
75     private void append(StringBuffer JavaDoc buffer, String JavaDoc string,
76             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     /*
88      * (non-Javadoc)
89      *
90      * @see org.eclipse.jface.text.DefaultInformationControl.IInformationPresenter#updatePresentation(org.eclipse.swt.widgets.Display,
91      * java.lang.String, org.eclipse.jface.text.TextPresentation, int, int)
92      */

93     public String JavaDoc updatePresentation(Display display, String JavaDoc hoverInfo,
94             TextPresentation presentation, int maxWidth, int maxHeight) {
95         return updatePresentation((Drawable) display, hoverInfo, presentation,
96                 maxWidth, maxHeight);
97     }
98
99     /*
100      * (non-Javadoc)
101      *
102      * @see org.eclipse.jface.text.DefaultInformationControl.IInformationPresenterExtension#updatePresentation(org.eclipse.swt.graphics.Drawable,
103      * java.lang.String, org.eclipse.jface.text.TextPresentation, int, int)
104      */

105     public String JavaDoc updatePresentation(Drawable drawable, String JavaDoc hoverInfo,
106             TextPresentation presentation, int maxWidth, int maxHeight) {
107         
108         if (hoverInfo == null)
109             return null;
110
111         GC gc = new GC(drawable);
112         Font font = null;
113
114         try {
115             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
116             // Calculate the maximum number of lines that will fit
117
// vertically
118
int maxNumberOfLines = Math.round((float) maxHeight
119                     / gc.getFontMetrics().getHeight())
120                     - LINE_REDUCTION;
121             fCounter = 0;
122             // Break continuous string into a set of lines that conform
123
// to the maximum width allowed
124
LineBreakingReader reader = new LineBreakingReader(
125                     new StringReader JavaDoc(hoverInfo), gc,
126                     maxWidth - LINE_REDUCTION);
127             boolean lastLineFormatted = false;
128             String JavaDoc line = reader.readLine();
129             boolean lineFormatted = reader.isFormattedLine();
130             boolean firstLineProcessed = false;
131
132             while (line != null) {
133                 // Stop processing if the maximum number of lines is
134
// exceeded
135
if (maxNumberOfLines <= 0)
136                     break;
137
138                 if (firstLineProcessed) {
139                     if (!lastLineFormatted)
140                         // Add line delimeter
141
append(buffer, LINE_DELIM, null);
142                     else {
143                         // Add line delimeter + indent
144
append(buffer, LINE_DELIM, presentation);
145                         append(buffer, INDENT, presentation);
146                     }
147                 }
148                 // Add line itself
149
append(buffer, line, null);
150                 firstLineProcessed = true;
151                 lastLineFormatted = lineFormatted;
152                 // Get the next line
153
line = reader.readLine();
154                 lineFormatted = reader.isFormattedLine();
155                 // Track the number of lines left to process before
156
// maxing out
157
maxNumberOfLines--;
158             }
159             // Maximum number of lines available exceeded by available
160
// content. Trail off with ...
161
if (line != null && buffer.length() > 0) {
162                 append(buffer, LINE_DELIM, lineFormatted ? presentation : null);
163                 append(
164                         buffer,
165                         PDEUIMessages.InfoControlTextPresenter_ContinuationChars,
166                         presentation);
167             }
168             // Pad with a space because first line is not indented
169
return INDENT + buffer.toString(); //$NON-NLS-1$
170

171         } catch (IOException JavaDoc e) {
172             PDEPlugin.log(e);
173             return null;
174         } finally {
175             if (font != null)
176                 font.dispose();
177             gc.dispose();
178         }
179     }
180 }
181
Popular Tags