1 package net.sourceforge.phpdt.internal.ui.text;
 
   4  * (c) Copyright IBM Corp. 2000, 2001.
 
   9 import java.io.IOException;
 
  10 import java.io.Reader;
 
  11 import java.io.StringReader;
 
  12 import java.util.Iterator;
 
  14 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
 
  15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  17 import org.eclipse.jface.text.DefaultInformationControl;
 
  18 import org.eclipse.jface.text.Region;
 
  19 import org.eclipse.jface.text.TextPresentation;
 
  20 import org.eclipse.swt.custom.StyleRange;
 
  21 import org.eclipse.swt.graphics.GC;
 
  22 import org.eclipse.swt.widgets.Display;
 
  27 public class HTMLTextPresenter implements DefaultInformationControl.IInformationPresenter {
 
  29         private static final String LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
 
  32         private boolean fEnforceUpperLineLimit;
 
  34         public HTMLTextPresenter(boolean enforceUpperLineLimit) {
 
  36                 fEnforceUpperLineLimit= enforceUpperLineLimit;
 
  39         public HTMLTextPresenter() {
 
  43         protected Reader createReader(String hoverInfo, TextPresentation presentation) {
 
  44                 return new HTML2TextReader(new StringReader(hoverInfo), presentation);
 
  47         protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) {
 
  49                 int yoursStart= offset;
 
  50                 int yoursEnd=   offset + insertLength -1;
 
  51                 yoursEnd= Math.max(yoursStart, yoursEnd);
 
  53                 Iterator e= presentation.getAllStyleRangeIterator();
 
  56                         StyleRange range= (StyleRange) e.next();
 
  58                         int myStart= range.start;
 
  59                         int myEnd=   range.start + range.length -1;
 
  60                         myEnd= Math.max(myStart, myEnd);
 
  62                         if (myEnd < yoursStart)
 
  65                         if (myStart < yoursStart)
 
  66                                 range.length += insertLength;
 
  68                                 range.start += insertLength;
 
  72         private void append(StringBuffer buffer, String string, TextPresentation presentation) {
 
  74                 int length= string.length();
 
  75                 buffer.append(string);
 
  77                 if (presentation != null)
 
  78                         adaptTextPresentation(presentation, fCounter, length);
 
  83         private String getIndent(String line) {
 
  84                 int length= line.length();
 
  87                 while (i < length && Character.isWhitespace(line.charAt(i))) ++i;
 
  89                 return (i == length ? line : line.substring(0, i)) + " "; //$NON-NLS-1$
 
  93          * @see IHoverInformationPresenter#updatePresentation(Display display, String, TextPresentation, int, int)
 
  95         public String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
 
  97                 if (hoverInfo == null)
 
 100                 GC gc= new GC(display);
 
 103                         StringBuffer buffer= new StringBuffer();
 
 104                         int maxNumberOfLines= Math.round(maxHeight / gc.getFontMetrics().getHeight());
 
 107                         LineBreakingReader reader= new LineBreakingReader(createReader(hoverInfo, presentation), gc, maxWidth);
 
 109                         boolean lastLineFormatted= false;
 
 110                         String lastLineIndent= null;
 
 112                         String line=reader.readLine();
 
 113                         boolean lineFormatted= reader.isFormattedLine();
 
 114                         boolean firstLineProcessed= false;
 
 116                         while (line != null) {
 
 118                                 if (fEnforceUpperLineLimit && maxNumberOfLines <= 0)
 
 121                                 if (firstLineProcessed) {
 
 122                                         if (!lastLineFormatted)
 
 123                                                 append(buffer, LINE_DELIM, null);
 
 125                                                 append(buffer, LINE_DELIM, presentation);
 
 126                                                 if (lastLineIndent != null)
 
 127                                                         append(buffer, lastLineIndent, presentation);
 
 131                                 append(buffer, line, null);
 
 132                                 firstLineProcessed= true;
 
 134                                 lastLineFormatted= lineFormatted;
 
 136                                         lastLineIndent= null;
 
 137                                 else if (lastLineIndent == null)
 
 138                                         lastLineIndent= getIndent(line);
 
 140                                 line= reader.readLine();
 
 141                                 lineFormatted= reader.isFormattedLine();
 
 147                                 append(buffer, LINE_DELIM, lineFormatted ? presentation : null);
 
 148                                 append(buffer, PHPUIMessages.getString("HTMLTextPresenter.ellipsis"), presentation); //$NON-NLS-1$
 
 151                         return trim(buffer, presentation);
 
 153                 } catch (IOException e) {
 
 155                         PHPeclipsePlugin.log(e);
 
 163         private String trim(StringBuffer buffer, TextPresentation presentation) {
 
 165                 int length= buffer.length();
 
 168                 while (end >= 0 && Character.isWhitespace(buffer.charAt(end)))
 
 172                         return ""; //$NON-NLS-1$
 
 175                         buffer.delete(end + 1, length);
 
 180                 while (start < end && Character.isWhitespace(buffer.charAt(start)))
 
 183                 buffer.delete(0, start);
 
 184                 presentation.setResultWindow(new Region(start, buffer.length()));
 
 185                 return buffer.toString();