Can HTML-Style Links be added to SWT StyledText?
Asked Answered
C

2

7

I know SWT has a Link class to create HTML a href style links as widgets, but I wast trying to find a way to make certain text in a StyledText control appear and function as a link.

I feel like Eclipse does this in their code editor if you hold down control and hover over a method name, but I know the Eclipse java editor is much more complicated than a StyledText control.

Cassation answered 29/9, 2009 at 18:59 Comment(0)
N
8

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.

Nguyetni answered 1/1, 2010 at 20:7 Comment(1)
In case someone stumbles upon this and wonders how to make the link clickable: https://mcmap.net/q/1620798/-swt-integrate-clickable-link-into-styledtext. Also note that styleRange.underline = true has to be set as well.Akiko
L
2

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.

Listed answered 28/10, 2009 at 5:32 Comment(1)
Thanks! I would be interested in seeing the code you use to automatically add links, but this is helpful.Cassation

© 2022 - 2024 — McMap. All rights reserved.