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