1 11 package org.eclipse.jdt.internal.ui.text; 12 13 14 15 import java.io.IOException ; 16 import java.io.StringReader ; 17 import java.util.Iterator ; 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 LINE_DELIM= System.getProperty("line.separator", "\n"); 40 private int fCounter; 41 private boolean fEnforceUpperLineLimit; 42 43 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 hoverInfo, TextPresentation presentation) { 62 return new HTML2TextReader(new StringReader (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 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 buffer, String 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 getIndent(String 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)) + " "; } 109 110 113 public String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) { 114 return updatePresentation((Drawable)display, hoverInfo, presentation, maxWidth, maxHeight); 115 } 116 117 121 public String updatePresentation(Drawable drawable, String 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 buffer= new StringBuffer (); 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 lastLineIndent= null; 148 149 String 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 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 trim(StringBuffer 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 ""; 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 |