六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 170|回复: 0

SWT custom HtmlToolTip

[复制链接]

升级  50%

5

主题

5

主题

5

主题

童生

Rank: 1

积分
25
 楼主| 发表于 2013-2-8 00:23:56 | 显示全部楼层 |阅读模式
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 */\n" + //$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 }\n" + //$NON-NLS-1$"pre{ font-family: monospace; font-size: 9pt }\n" + //$NON-NLS-1$"\n" + //$NON-NLS-1$"/* Margins */\n" + //$NON-NLS-1$"body     { overflow: auto; margin-top: 0; margin-bottom: 4; margin-left: 3; margin-right: 0 }\n" + //$NON-NLS-1$"h1           { margin-top: 5; margin-bottom: 1 }\n" + //$NON-NLS-1$"h2           { margin-top: 25; margin-bottom: 3 }\n" + //$NON-NLS-1$"h3           { margin-top: 20; margin-bottom: 3 }\n" + //$NON-NLS-1$"h4           { margin-top: 20; margin-bottom: 3 }\n" + //$NON-NLS-1$"h5           { margin-top: 0; margin-bottom: 0 }\n" + //$NON-NLS-1$"p            { margin-top: 10px; margin-bottom: 10px }\n" + //$NON-NLS-1$"pre         { margin-left: 6 }\n" + //$NON-NLS-1$"ul         { margin-top: 0; margin-bottom: 10 }\n" + //$NON-NLS-1$"li         { margin-top: 0; margin-bottom: 0 } \n" + //$NON-NLS-1$"li p     { margin-top: 0; margin-bottom: 0 } \n" + //$NON-NLS-1$"ol         { margin-top: 0; margin-bottom: 10 }\n" + //$NON-NLS-1$"dl         { margin-top: 0; margin-bottom: 10 }\n" + //$NON-NLS-1$"dt         { margin-top: 0; margin-bottom: 0; font-weight: bold }\n" + //$NON-NLS-1$"dd         { margin-top: 0; margin-bottom: 0 }\n" + //$NON-NLS-1$"\n" + //$NON-NLS-1$"/* Styles and colors */\n" + //$NON-NLS-1$"a:link     { color: #0000FF }\n" + //$NON-NLS-1$"a:hover     { color: #000080 }\n" + //$NON-NLS-1$"a:visited    { text-decoration: underline }\n" + //$NON-NLS-1$"h4           { font-style: italic }\n" + //$NON-NLS-1$"strong     { font-weight: bold }\n" + //$NON-NLS-1$"em         { font-style: italic }\n" + //$NON-NLS-1$"var         { font-style: italic }\n" + //$NON-NLS-1$"th         { font-weight: bold }\n" + //$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 * <html> 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 headerreturn html;StringBuffer info= new StringBuffer(512 + html.length());HTMLPrinter.insertPageProlog(info, 0, fgStyleSheet);info.append(html);HTMLPrinter.addPageEpilog(info);return info.toString();}} 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表