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