nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2005 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Common Public License v1.0 |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 5 | * which accompanies this distribution, and is available at |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 6 | * http://www.eclipse.org/legal/cpl-v10.html |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 7 | * |
| 8 | * Contributors: |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 9 | * IBM - Initial API and implementation |
| 10 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | */ |
nitind | c698900 | 2005-11-09 23:56:40 +0000 | [diff] [blame] | 13 | package org.eclipse.wst.sse.ui.internal.derived; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 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.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.GC; |
| 25 | import org.eclipse.swt.widgets.Display; |
nitind | c698900 | 2005-11-09 23:56:40 +0000 | [diff] [blame] | 26 | import org.eclipse.wst.sse.ui.internal.Logger; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 27 | |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 28 | /* |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 29 | * Copied from org.eclipse.jdt.internal.ui.text.HTMLTextPresenter |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 30 | * Modifications were made to use own Logger to log exception, and the |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 31 | * ellipses constant |
| 32 | */ |
| 33 | public class HTMLTextPresenter implements DefaultInformationControl.IInformationPresenter { |
| 34 | private static final String ELLIPSES = "..."; //$NON-NLS-1$ |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 35 | private static final String LINE_DELIM = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 36 | |
| 37 | private int fCounter; |
| 38 | private boolean fEnforceUpperLineLimit; |
| 39 | |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 40 | public HTMLTextPresenter(boolean enforceUpperLineLimit) { |
| 41 | super(); |
| 42 | fEnforceUpperLineLimit = enforceUpperLineLimit; |
| 43 | } |
| 44 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 45 | public HTMLTextPresenter() { |
| 46 | this(true); |
| 47 | } |
| 48 | |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 49 | protected Reader createReader(String hoverInfo, TextPresentation presentation) { |
| 50 | return new HTML2TextReader(new StringReader(hoverInfo), presentation); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) { |
| 54 | |
| 55 | int yoursStart = offset; |
| 56 | int yoursEnd = offset + insertLength - 1; |
| 57 | yoursEnd = Math.max(yoursStart, yoursEnd); |
| 58 | |
| 59 | Iterator e = presentation.getAllStyleRangeIterator(); |
| 60 | while (e.hasNext()) { |
| 61 | |
| 62 | StyleRange range = (StyleRange) e.next(); |
| 63 | |
| 64 | int myStart = range.start; |
| 65 | int myEnd = range.start + range.length - 1; |
| 66 | myEnd = Math.max(myStart, myEnd); |
| 67 | |
| 68 | if (myEnd < yoursStart) |
| 69 | continue; |
| 70 | |
| 71 | if (myStart < yoursStart) |
| 72 | range.length += insertLength; |
| 73 | else |
| 74 | range.start += insertLength; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private void append(StringBuffer buffer, String string, TextPresentation presentation) { |
| 79 | |
| 80 | int length = string.length(); |
| 81 | buffer.append(string); |
| 82 | |
| 83 | if (presentation != null) |
| 84 | adaptTextPresentation(presentation, fCounter, length); |
| 85 | |
| 86 | fCounter += length; |
| 87 | } |
| 88 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 89 | private String getIndent(String line) { |
| 90 | int length = line.length(); |
| 91 | |
| 92 | int i = 0; |
| 93 | while (i < length && Character.isWhitespace(line.charAt(i))) |
| 94 | ++i; |
| 95 | |
| 96 | return (i == length ? line : line.substring(0, i)) + " "; //$NON-NLS-1$ |
| 97 | } |
| 98 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 99 | /* |
| 100 | * @see IHoverInformationPresenter#updatePresentation(Display display, |
| 101 | * String, TextPresentation, int, int) |
| 102 | */ |
| 103 | public String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) { |
| 104 | |
| 105 | if (hoverInfo == null) |
| 106 | return null; |
| 107 | |
| 108 | GC gc = new GC(display); |
| 109 | try { |
| 110 | |
| 111 | StringBuffer buffer = new StringBuffer(); |
| 112 | int maxNumberOfLines = Math.round(maxHeight / gc.getFontMetrics().getHeight()); |
| 113 | |
| 114 | fCounter = 0; |
| 115 | LineBreakingReader reader = new LineBreakingReader(createReader(hoverInfo, presentation), gc, maxWidth); |
| 116 | |
| 117 | boolean lastLineFormatted = false; |
| 118 | String lastLineIndent = null; |
| 119 | |
| 120 | String line = reader.readLine(); |
| 121 | boolean lineFormatted = reader.isFormattedLine(); |
| 122 | boolean firstLineProcessed = false; |
| 123 | |
| 124 | while (line != null) { |
| 125 | |
| 126 | if (fEnforceUpperLineLimit && maxNumberOfLines <= 0) |
| 127 | break; |
| 128 | |
| 129 | if (firstLineProcessed) { |
| 130 | if (!lastLineFormatted) |
| 131 | append(buffer, LINE_DELIM, null); |
| 132 | else { |
| 133 | append(buffer, LINE_DELIM, presentation); |
| 134 | if (lastLineIndent != null) |
| 135 | append(buffer, lastLineIndent, presentation); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | append(buffer, line, null); |
| 140 | firstLineProcessed = true; |
| 141 | |
| 142 | lastLineFormatted = lineFormatted; |
| 143 | if (!lineFormatted) |
| 144 | lastLineIndent = null; |
| 145 | else if (lastLineIndent == null) |
| 146 | lastLineIndent = getIndent(line); |
| 147 | |
| 148 | line = reader.readLine(); |
| 149 | lineFormatted = reader.isFormattedLine(); |
| 150 | |
| 151 | maxNumberOfLines--; |
| 152 | } |
| 153 | |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 154 | if (line != null && buffer.length() > 0) { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 155 | append(buffer, LINE_DELIM, lineFormatted ? presentation : null); |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 156 | append(buffer, ELLIPSES, presentation); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return trim(buffer, presentation); |
| 160 | |
| 161 | } catch (IOException e) { |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 162 | Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 163 | return null; |
| 164 | |
| 165 | } finally { |
| 166 | gc.dispose(); |
| 167 | } |
| 168 | } |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 169 | |
| 170 | private String trim(StringBuffer buffer, TextPresentation presentation) { |
| 171 | |
| 172 | int length = buffer.length(); |
| 173 | |
| 174 | int end = length - 1; |
| 175 | while (end >= 0 && Character.isWhitespace(buffer.charAt(end))) |
| 176 | --end; |
| 177 | |
| 178 | if (end == -1) |
| 179 | return ""; //$NON-NLS-1$ |
| 180 | |
| 181 | if (end < length - 1) |
| 182 | buffer.delete(end + 1, length); |
| 183 | else |
| 184 | end = length; |
| 185 | |
| 186 | int start = 0; |
| 187 | while (start < end && Character.isWhitespace(buffer.charAt(start))) |
| 188 | ++start; |
| 189 | |
| 190 | buffer.delete(0, start); |
| 191 | presentation.setResultWindow(new Region(start, buffer.length())); |
| 192 | return buffer.toString(); |
| 193 | } |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 194 | } |