1 11 12 package org.eclipse.pde.internal.ui.editor.contentassist.display; 13 14 import java.io.IOException ; 15 import java.io.StringReader ; 16 import java.util.Iterator ; 17 18 import org.eclipse.jface.text.TextPresentation; 19 import org.eclipse.jface.text.DefaultInformationControl.IInformationPresenter; 20 import org.eclipse.jface.text.DefaultInformationControl.IInformationPresenterExtension; 21 import org.eclipse.pde.internal.ui.PDEPlugin; 22 import org.eclipse.pde.internal.ui.PDEUIMessages; 23 import org.eclipse.pde.internal.ui.util.LineBreakingReader; 24 import org.eclipse.swt.custom.StyleRange; 25 import org.eclipse.swt.graphics.Drawable; 26 import org.eclipse.swt.graphics.Font; 27 import org.eclipse.swt.graphics.GC; 28 import org.eclipse.swt.widgets.Display; 29 30 34 public class InfoControlTextPresenter implements IInformationPresenter, 35 IInformationPresenterExtension { 36 37 private static final String LINE_DELIM = System.getProperty( 38 "line.separator", "\n"); 40 44 private static final int LINE_REDUCTION = 2; 45 46 private static final String INDENT = " "; 48 private int fCounter; 49 50 protected void adaptTextPresentation(TextPresentation presentation, 51 int offset, int insertLength) { 52 53 int yoursStart = offset; 54 int yoursEnd = offset + insertLength - 1; 55 yoursEnd = Math.max(yoursStart, yoursEnd); 56 Iterator e = presentation.getAllStyleRangeIterator(); 57 58 while (e.hasNext()) { 59 StyleRange range = (StyleRange) e.next(); 60 int myStart = range.start; 61 int myEnd = range.start + range.length - 1; 62 myEnd = Math.max(myStart, myEnd); 63 64 if (myEnd < yoursStart) 65 continue; 66 67 if (myStart < yoursStart) { 68 range.length += insertLength; 69 } else { 70 range.start += insertLength; 71 } 72 } 73 } 74 75 private void append(StringBuffer buffer, String string, 76 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 93 public String updatePresentation(Display display, String hoverInfo, 94 TextPresentation presentation, int maxWidth, int maxHeight) { 95 return updatePresentation((Drawable) display, hoverInfo, presentation, 96 maxWidth, maxHeight); 97 } 98 99 105 public String updatePresentation(Drawable drawable, String hoverInfo, 106 TextPresentation presentation, int maxWidth, int maxHeight) { 107 108 if (hoverInfo == null) 109 return null; 110 111 GC gc = new GC(drawable); 112 Font font = null; 113 114 try { 115 StringBuffer buffer = new StringBuffer (); 116 int maxNumberOfLines = Math.round((float) maxHeight 119 / gc.getFontMetrics().getHeight()) 120 - LINE_REDUCTION; 121 fCounter = 0; 122 LineBreakingReader reader = new LineBreakingReader( 125 new StringReader (hoverInfo), gc, 126 maxWidth - LINE_REDUCTION); 127 boolean lastLineFormatted = false; 128 String line = reader.readLine(); 129 boolean lineFormatted = reader.isFormattedLine(); 130 boolean firstLineProcessed = false; 131 132 while (line != null) { 133 if (maxNumberOfLines <= 0) 136 break; 137 138 if (firstLineProcessed) { 139 if (!lastLineFormatted) 140 append(buffer, LINE_DELIM, null); 142 else { 143 append(buffer, LINE_DELIM, presentation); 145 append(buffer, INDENT, presentation); 146 } 147 } 148 append(buffer, line, null); 150 firstLineProcessed = true; 151 lastLineFormatted = lineFormatted; 152 line = reader.readLine(); 154 lineFormatted = reader.isFormattedLine(); 155 maxNumberOfLines--; 158 } 159 if (line != null && buffer.length() > 0) { 162 append(buffer, LINE_DELIM, lineFormatted ? presentation : null); 163 append( 164 buffer, 165 PDEUIMessages.InfoControlTextPresenter_ContinuationChars, 166 presentation); 167 } 168 return INDENT + buffer.toString(); 171 } catch (IOException e) { 172 PDEPlugin.log(e); 173 return null; 174 } finally { 175 if (font != null) 176 font.dispose(); 177 gc.dispose(); 178 } 179 } 180 } 181 | Popular Tags |