1 11 package org.eclipse.ant.internal.ui.editor.derived; 12 13 14 import java.io.IOException ; 15 import java.io.Reader ; 16 import java.io.StringReader ; 17 import java.util.Iterator ; 18 19 import org.eclipse.ant.internal.ui.AntUIPlugin; 20 import org.eclipse.jface.text.DefaultInformationControl; 21 import org.eclipse.jface.text.Region; 22 import org.eclipse.jface.text.TextPresentation; 23 import org.eclipse.swt.custom.StyleRange; 24 import org.eclipse.swt.graphics.Drawable; 25 import org.eclipse.swt.graphics.GC; 26 import org.eclipse.swt.widgets.Display; 27 28 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, "...", presentation); } 162 163 return trim(buffer, presentation); 164 165 } catch (IOException e) { 166 AntUIPlugin.log(e); 167 return null; 168 } finally { 169 gc.dispose(); 170 } 171 } 172 173 private String trim(StringBuffer buffer, TextPresentation presentation) { 174 175 int length= buffer.length(); 176 177 int end= length -1; 178 while (end >= 0 && Character.isWhitespace(buffer.charAt(end))) 179 -- end; 180 181 if (end == -1) 182 return ""; 184 if (end < length -1) 185 buffer.delete(end + 1, length); 186 else 187 end= length; 188 189 int start= 0; 190 while (start < end && Character.isWhitespace(buffer.charAt(start))) 191 ++ start; 192 193 buffer.delete(0, start); 194 presentation.setResultWindow(new Region(start, buffer.length())); 195 return buffer.toString(); 196 } 197 } 198 199 | Popular Tags |