Can HTML-Style Links be added to SWT StyledText?

泄露秘密 提交于 2019-12-01 04:51:44

Since JFace 3.5, there is a special style for links:

styleRange.underlineStyle = SWT.UNDERLINE_LINK;
styleRange.data = "http://www.google.com/";

This makes it much more simple to identify a link and you can store the URL in the style. As for automatically finding links, just look for the pattern http://[^ ] (blanks are not allowed in links) in the lines you get and add the style.

You need to add a LineStyleListener to the StyledText widget:

textField.addLineStyleListener (...);

...

public void lineGetStyle (LineStyleEvent e)
{
  // alloc a set of styles for the requested line
  e.styles = new StyleRange [...];

  for (int i = 0; i < e.styles.length; i++)
  {
    StyleRange styleRange = new StyleRange ();

    styleRange.start = ...;
    styleRange.length = ...;
    styleRange.underline = true;
    styleRange.foreground = <URL colour>;

    e.styles [i] = styleRange;
  }
}

The javadoc for LineStyleListener will give you some more info.

To add the click behaviour, you need some more logic: I could also paste some code that we use to automatically add HTML-style clickable links URL's in a StyledText widget if that would help.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!