SWT custom HtmlToolTip


public class HtmlTooltip extends BrowserInformationControl{
	
	public final static String APPEARANCE_JAVADOC_FONT= "org.eclipse.jdt.ui.javadocfont"; //$NON-NLS-1$
	
	private static final String fgStyleSheet= "/* Font definitions */
" + //$NON-NLS-1$ "body, h1, h2, h3, h4, h5, h6, p, table, td, caption, th, ul, ol, dl, li, dd, dt {font-family: sans-serif; font-size: 9pt }
" + //$NON-NLS-1$ "pre { font-family: monospace; font-size: 9pt }
" + //$NON-NLS-1$ "
" + //$NON-NLS-1$ "/* Margins */
" + //$NON-NLS-1$ "body { overflow: auto; margin-top: 0; margin-bottom: 4; margin-left: 3; margin-right: 0 }
" + //$NON-NLS-1$ "h1 { margin-top: 5; margin-bottom: 1 }
" + //$NON-NLS-1$ "h2 { margin-top: 25; margin-bottom: 3 }
" + //$NON-NLS-1$ "h3 { margin-top: 20; margin-bottom: 3 }
" + //$NON-NLS-1$ "h4 { margin-top: 20; margin-bottom: 3 }
" + //$NON-NLS-1$ "h5 { margin-top: 0; margin-bottom: 0 }
" + //$NON-NLS-1$ "p { margin-top: 10px; margin-bottom: 10px }
" + //$NON-NLS-1$ "pre { margin-left: 6 }
" + //$NON-NLS-1$ "ul { margin-top: 0; margin-bottom: 10 }
" + //$NON-NLS-1$ "li { margin-top: 0; margin-bottom: 0 }
" + //$NON-NLS-1$ "li p { margin-top: 0; margin-bottom: 0 }
" + //$NON-NLS-1$ "ol { margin-top: 0; margin-bottom: 10 }
" + //$NON-NLS-1$ "dl { margin-top: 0; margin-bottom: 10 }
" + //$NON-NLS-1$ "dt { margin-top: 0; margin-bottom: 0; font-weight: bold }
" + //$NON-NLS-1$ "dd { margin-top: 0; margin-bottom: 0 }
" + //$NON-NLS-1$ "
" + //$NON-NLS-1$ "/* Styles and colors */
" + //$NON-NLS-1$ "a:link { color: #0000FF }
" + //$NON-NLS-1$ "a:hover { color: #000080 }
" + //$NON-NLS-1$ "a:visited { text-decoration: underline }
" + //$NON-NLS-1$ "h4 { font-style: italic }
" + //$NON-NLS-1$ "strong { font-weight: bold }
" + //$NON-NLS-1$ "em { font-style: italic }
" + //$NON-NLS-1$ "var { font-style: italic }
" + //$NON-NLS-1$ "th { font-weight: bold }
" + //$NON-NLS-1$ ""; //$NON-NLS-1$ public HtmlTooltip(Shell parent,boolean resizable) { super(parent, APPEARANCE_JAVADOC_FONT, resizable); } public HtmlTooltip(Shell parent, String symbolicFontName, boolean resizable) { super(parent, symbolicFontName, resizable); } protected void createContent(Composite parent) { super.createContent(parent); super.addLocationListener(new LocationListener() { public void changing(LocationEvent event) { String url = event.location; if (url.startsWith("about:")) { return; } event.doit = false; if( url.startsWith("http")) launchBrowser(url); } public void changed(LocationEvent event) { } }); } protected void launchBrowser(String url) { // open url in external browser Program.launch(url); } public void setTooltip(String content){ content= addCSSToHTMLFragment(content); super.setInformation(content); } /** * Adds a HTML header and CSS info if <code>html</code> is only an HTML fragment (has no * &lt;html&gt; section). * * @param html the html / text produced by a revision * @return modified html */ private String addCSSToHTMLFragment(String html) { int max= Math.min(100, html.length()); if (html.substring(0, max).indexOf("<html>") != -1) //$NON-NLS-1$ // there is already a header return html; StringBuffer info= new StringBuffer(512 + html.length()); HTMLPrinter.insertPageProlog(info, 0, fgStyleSheet); info.append(html); HTMLPrinter.addPageEpilog(info); return info.toString(); } }