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.
我知道SWT有一个Link类来创建HTML一个href样式链接作为小部件,但我试图找到一种方法来使StyledText控件中的某些文本出现并作为一个链接。
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.
我觉得Eclipse会在代码编辑器中执行此操作,如果您按住控件并将鼠标悬停在方法名称上,但我知道Eclipse java编辑器比StyledText控件复杂得多。
2 个解决方案
#1
8
Since JFace 3.5, there is a special style for links:
从JFace 3.5开始,链接有一种特殊的风格:
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.
这使得识别链接变得更加简单,您可以将URL存储在样式中。至于自动查找链接,只需在您获得的行中查找模式http:// [^](链接中不允许空白)并添加样式。
#2
2
You need to add a LineStyleListener to the StyledText widget:
您需要将一个LineStyleListener添加到StyledText小部件:
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.
LineStyleListener的javadoc将为您提供更多信息。
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.
要添加点击行为,您需要更多逻辑:我还可以粘贴一些代码,用于在StyledText小部件中自动添加HTML样式的可点击链接URL,如果这样做有帮助的话。
#1
8
Since JFace 3.5, there is a special style for links:
从JFace 3.5开始,链接有一种特殊的风格:
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.
这使得识别链接变得更加简单,您可以将URL存储在样式中。至于自动查找链接,只需在您获得的行中查找模式http:// [^](链接中不允许空白)并添加样式。
#2
2
You need to add a LineStyleListener to the StyledText widget:
您需要将一个LineStyleListener添加到StyledText小部件:
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.
LineStyleListener的javadoc将为您提供更多信息。
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.
要添加点击行为,您需要更多逻辑:我还可以粘贴一些代码,用于在StyledText小部件中自动添加HTML样式的可点击链接URL,如果这样做有帮助的话。