1 11 package org.eclipse.jface.internal.text.html; 12 13 import java.io.IOException ; 14 import java.io.Reader ; 15 import java.io.StringReader ; 16 import java.util.Iterator ; 17 18 import org.eclipse.swt.custom.StyleRange; 19 import org.eclipse.swt.graphics.Drawable; 20 import org.eclipse.swt.graphics.GC; 21 import org.eclipse.swt.widgets.Display; 22 23 import org.eclipse.jface.internal.text.link.contentassist.LineBreakingReader; 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 34 public class HTMLTextPresenter implements DefaultInformationControl.IInformationPresenter, DefaultInformationControl.IInformationPresenterExtension { 35 36 private static final String LINE_DELIM= System.getProperty("line.separator", "\n"); 38 private int fCounter; 39 private boolean fEnforceUpperLineLimit; 40 41 public HTMLTextPresenter(boolean enforceUpperLineLimit) { 42 super(); 43 fEnforceUpperLineLimit= enforceUpperLineLimit; 44 } 45 46 public HTMLTextPresenter() { 47 this(true); 48 } 49 50 protected Reader createReader(String hoverInfo, TextPresentation presentation) { 51 return new HTML2TextReader(new StringReader (hoverInfo), presentation); 52 } 53 54 protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) { 55 56 int yoursStart= offset; 57 int yoursEnd= offset + insertLength -1; 58 yoursEnd= Math.max(yoursStart, yoursEnd); 59 60 Iterator e= presentation.getAllStyleRangeIterator(); 61 while (e.hasNext()) { 62 63 StyleRange range= (StyleRange) e.next(); 64 65 int myStart= range.start; 66 int myEnd= range.start + range.length -1; 67 myEnd= Math.max(myStart, myEnd); 68 69 if (myEnd < yoursStart) 70 continue; 71 72 if (myStart < yoursStart) 73 range.length += insertLength; 74 else 75 range.start += insertLength; 76 } 77 } 78 79 private void append(StringBuffer buffer, String string, TextPresentation presentation) { 80 81 int length= string.length(); 82 buffer.append(string); 83 84 if (presentation != null) 85 adaptTextPresentation(presentation, fCounter, length); 86 87 fCounter += length; 88 } 89 90 private String getIndent(String line) { 91 int length= line.length(); 92 93 int i= 0; 94 while (i < length && Character.isWhitespace(line.charAt(i))) ++i; 95 96 return (i == length ? line : line.substring(0, i)) + " "; } 98 99 102 public String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) { 103 return updatePresentation((Drawable)display, hoverInfo, presentation, maxWidth, maxHeight); 104 } 105 106 110 public String updatePresentation(Drawable drawable, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) { 111 112 if (hoverInfo == null) 113 return null; 114 115 GC gc= new GC(drawable); 116 try { 117 118 StringBuffer buffer= new StringBuffer (); 119 int maxNumberOfLines= Math.round(maxHeight / gc.getFontMetrics().getHeight()); 120 121 fCounter= 0; 122 LineBreakingReader reader= new LineBreakingReader(createReader(hoverInfo, presentation), gc, maxWidth); 123 124 boolean lastLineFormatted= false; 125 String lastLineIndent= null; 126 127 String line=reader.readLine(); 128 boolean lineFormatted= reader.isFormattedLine(); 129 boolean firstLineProcessed= false; 130 131 while (line != null) { 132 133 if (fEnforceUpperLineLimit && maxNumberOfLines <= 0) 134 break; 135 136 if (firstLineProcessed) { 137 if (!lastLineFormatted) 138 append(buffer, LINE_DELIM, null); 139 else { 140 append(buffer, LINE_DELIM, presentation); 141 if (lastLineIndent != null) 142 append(buffer, lastLineIndent, presentation); 143 } 144 } 145 146 append(buffer, line, null); 147 firstLineProcessed= true; 148 149 lastLineFormatted= lineFormatted; 150 if (!lineFormatted) 151 lastLineIndent= null; 152 else if (lastLineIndent == null) 153 lastLineIndent= getIndent(line); 154 155 line= reader.readLine(); 156 lineFormatted= reader.isFormattedLine(); 157 158 maxNumberOfLines--; 159 } 160 161 if (line != null) { 162 append(buffer, LINE_DELIM, lineFormatted ? presentation : null); 163 append(buffer, HTMLMessages.getString("HTMLTextPresenter.ellipse"), presentation); } 165 166 return trim(buffer, presentation); 167 168 } catch (IOException e) { 169 170 return null; 172 173 } finally { 174 gc.dispose(); 175 } 176 } 177 178 private String trim(StringBuffer buffer, TextPresentation presentation) { 179 180 int length= buffer.length(); 181 182 int end= length -1; 183 while (end >= 0 && Character.isWhitespace(buffer.charAt(end))) 184 -- end; 185 186 if (end == -1) 187 return ""; 189 if (end < length -1) 190 buffer.delete(end + 1, length); 191 else 192 end= length; 193 194 int start= 0; 195 while (start < end && Character.isWhitespace(buffer.charAt(start))) 196 ++ start; 197 198 buffer.delete(0, start); 199 presentation.setResultWindow(new Region(start, buffer.length())); 200 return buffer.toString(); 201 } 202 } 203 204 | Popular Tags |