blob: 2a8e7b5eeec43ea9add8d295c8fa20ff4d8561b5 [file] [log] [blame]
nitindf8e77632005-09-07 23:49:25 +00001/*
2 * Copyright (c) 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
david_williamscfdb2cd2004-11-11 08:37:49 +00005 * which accompanies this distribution, and is available at
nitindf8e77632005-09-07 23:49:25 +00006 * http://www.eclipse.org/legal/cpl-v10.html
david_williamscfdb2cd2004-11-11 08:37:49 +00007 *
8 * Contributors:
nitindf8e77632005-09-07 23:49:25 +00009 * IBM - Initial API and implementation
10 * Jens Lukowski/Innoopract - initial renaming/restructuring
11 *
12 */
nitindc6989002005-11-09 23:56:40 +000013package org.eclipse.wst.sse.ui.internal.derived;
david_williamscfdb2cd2004-11-11 08:37:49 +000014
15import java.io.IOException;
16import java.io.Reader;
17import java.io.StringReader;
18import java.util.Iterator;
19
20import org.eclipse.jface.text.DefaultInformationControl;
21import org.eclipse.jface.text.Region;
22import org.eclipse.jface.text.TextPresentation;
23import org.eclipse.swt.custom.StyleRange;
24import org.eclipse.swt.graphics.GC;
25import org.eclipse.swt.widgets.Display;
nitindc6989002005-11-09 23:56:40 +000026import org.eclipse.wst.sse.ui.internal.Logger;
david_williamscfdb2cd2004-11-11 08:37:49 +000027
nitindf8e77632005-09-07 23:49:25 +000028/*
david_williamscfdb2cd2004-11-11 08:37:49 +000029 * Copied from org.eclipse.jdt.internal.ui.text.HTMLTextPresenter
nitindf8e77632005-09-07 23:49:25 +000030 * Modifications were made to use own Logger to log exception, and the
david_williamscfdb2cd2004-11-11 08:37:49 +000031 * ellipses constant
32 */
33public class HTMLTextPresenter implements DefaultInformationControl.IInformationPresenter {
34 private static final String ELLIPSES = "..."; //$NON-NLS-1$
david_williamscfdb2cd2004-11-11 08:37:49 +000035 private static final String LINE_DELIM = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
36
37 private int fCounter;
38 private boolean fEnforceUpperLineLimit;
39
nitindf8e77632005-09-07 23:49:25 +000040 public HTMLTextPresenter(boolean enforceUpperLineLimit) {
41 super();
42 fEnforceUpperLineLimit = enforceUpperLineLimit;
43 }
44
david_williamscfdb2cd2004-11-11 08:37:49 +000045 public HTMLTextPresenter() {
46 this(true);
47 }
48
nitindf8e77632005-09-07 23:49:25 +000049 protected Reader createReader(String hoverInfo, TextPresentation presentation) {
50 return new HTML2TextReader(new StringReader(hoverInfo), presentation);
david_williamscfdb2cd2004-11-11 08:37:49 +000051 }
52
53 protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) {
54
55 int yoursStart = offset;
56 int yoursEnd = offset + insertLength - 1;
57 yoursEnd = Math.max(yoursStart, yoursEnd);
58
59 Iterator e = presentation.getAllStyleRangeIterator();
60 while (e.hasNext()) {
61
62 StyleRange range = (StyleRange) e.next();
63
64 int myStart = range.start;
65 int myEnd = range.start + range.length - 1;
66 myEnd = Math.max(myStart, myEnd);
67
68 if (myEnd < yoursStart)
69 continue;
70
71 if (myStart < yoursStart)
72 range.length += insertLength;
73 else
74 range.start += insertLength;
75 }
76 }
77
78 private void append(StringBuffer buffer, String string, TextPresentation presentation) {
79
80 int length = string.length();
81 buffer.append(string);
82
83 if (presentation != null)
84 adaptTextPresentation(presentation, fCounter, length);
85
86 fCounter += length;
87 }
88
david_williamscfdb2cd2004-11-11 08:37:49 +000089 private String getIndent(String line) {
90 int length = line.length();
91
92 int i = 0;
93 while (i < length && Character.isWhitespace(line.charAt(i)))
94 ++i;
95
96 return (i == length ? line : line.substring(0, i)) + " "; //$NON-NLS-1$
97 }
98
david_williamscfdb2cd2004-11-11 08:37:49 +000099 /*
100 * @see IHoverInformationPresenter#updatePresentation(Display display,
101 * String, TextPresentation, int, int)
102 */
103 public String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
104
105 if (hoverInfo == null)
106 return null;
107
108 GC gc = new GC(display);
109 try {
110
111 StringBuffer buffer = new StringBuffer();
112 int maxNumberOfLines = Math.round(maxHeight / gc.getFontMetrics().getHeight());
113
114 fCounter = 0;
115 LineBreakingReader reader = new LineBreakingReader(createReader(hoverInfo, presentation), gc, maxWidth);
116
117 boolean lastLineFormatted = false;
118 String lastLineIndent = null;
119
120 String line = reader.readLine();
121 boolean lineFormatted = reader.isFormattedLine();
122 boolean firstLineProcessed = false;
123
124 while (line != null) {
125
126 if (fEnforceUpperLineLimit && maxNumberOfLines <= 0)
127 break;
128
129 if (firstLineProcessed) {
130 if (!lastLineFormatted)
131 append(buffer, LINE_DELIM, null);
132 else {
133 append(buffer, LINE_DELIM, presentation);
134 if (lastLineIndent != null)
135 append(buffer, lastLineIndent, presentation);
136 }
137 }
138
139 append(buffer, line, null);
140 firstLineProcessed = true;
141
142 lastLineFormatted = lineFormatted;
143 if (!lineFormatted)
144 lastLineIndent = null;
145 else if (lastLineIndent == null)
146 lastLineIndent = getIndent(line);
147
148 line = reader.readLine();
149 lineFormatted = reader.isFormattedLine();
150
151 maxNumberOfLines--;
152 }
153
nitindf8e77632005-09-07 23:49:25 +0000154 if (line != null && buffer.length() > 0) {
david_williamscfdb2cd2004-11-11 08:37:49 +0000155 append(buffer, LINE_DELIM, lineFormatted ? presentation : null);
nitindf8e77632005-09-07 23:49:25 +0000156 append(buffer, ELLIPSES, presentation);
david_williamscfdb2cd2004-11-11 08:37:49 +0000157 }
158
159 return trim(buffer, presentation);
160
161 } catch (IOException e) {
nitindf8e77632005-09-07 23:49:25 +0000162 Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
david_williamscfdb2cd2004-11-11 08:37:49 +0000163 return null;
164
165 } finally {
166 gc.dispose();
167 }
168 }
nitindf8e77632005-09-07 23:49:25 +0000169
170 private String trim(StringBuffer buffer, TextPresentation presentation) {
171
172 int length = buffer.length();
173
174 int end = length - 1;
175 while (end >= 0 && Character.isWhitespace(buffer.charAt(end)))
176 --end;
177
178 if (end == -1)
179 return ""; //$NON-NLS-1$
180
181 if (end < length - 1)
182 buffer.delete(end + 1, length);
183 else
184 end = length;
185
186 int start = 0;
187 while (start < end && Character.isWhitespace(buffer.charAt(start)))
188 ++start;
189
190 buffer.delete(0, start);
191 presentation.setResultWindow(new Region(start, buffer.length()));
192 return buffer.toString();
193 }
david_williamscfdb2cd2004-11-11 08:37:49 +0000194}