SWT:将可点击的链接集成到StyledText中 [英] SWT: Integrate clickable link into StyledText

查看:52
本文介绍了SWT:将可点击的链接集成到StyledText中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助此问题,我能够弄清楚如何在SwT的StyledText小部件内显示链接.颜色是正确的,甚至将鼠标悬停在链接上时,光标也会改变形状.

With the help of this question I was able to figure out how I can display a link inside a StyledText widget in SwT. The color is correct and even the cursor changes shape when hovering over the link.

到目前为止,效果很好,但实际上该链接不可点击.尽管光标会改变其形状,但是如果单击链接,则不会发生任何事情.因此,我问我如何才能单击链接以在浏览器中实际打开它.

So far so good, but the link is not actually clickable. Although the cursor changes its shape, nothing happens if clicking on the link. Therefore I am asking how I can make clicking the link to actually open it in the browser.

我想到了使用MouseListener,将点击位置跟踪回到执行了单击的相应文本,然后决定是否打开链接.但是,鉴于已经存在一些相应地更改光标的例程,这似乎太复杂了.我相信有一些简单的方法可以做到这一点(并确保单击行为实际上与光标更改其形状时保持一致).

I thought of using a MouseListener, tracking the click-location back to the respective text the click has been performed on and then deciding whether to open the link or not. However that seems way too complicated given that there already is some routine going on for changing the cursor accordingly. I believe that there is some easy way to do this (and assuring that the clicking-behavior is actually consistent to when the cursor changes its shape).

有人有什么建议吗?

这是一个MWE,展示了我到目前为止所做的事情:

Here's an MWE demonstrating what I have done so far:

public static void main(String[] args) throws MalformedURLException {
final URL testURL = new URL("https://stackoverflow.com/questions/1494337/can-html-style-links-be-added-to-swt-styledtext");

Display display = new Display();

Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, true));

StyledText sTextWidget = new StyledText(shell, SWT.READ_ONLY);

final String firstPart = "Some text before ";
String msg = firstPart + testURL.toString() + " some text after";

sTextWidget.setText(msg);
sTextWidget.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

StyleRange linkStyleRange = new StyleRange(firstPart.length(), testURL.toString().length(), null, null);
linkStyleRange.underline = true;
linkStyleRange.underlineStyle = SWT.UNDERLINE_LINK;
linkStyleRange.data = testURL.toString();

sTextWidget.setStyleRange(linkStyleRange);

shell.open();

while(!shell.isDisposed()) {
    display.readAndDispatch();
}
}

推荐答案

好吧,我在发布此问题时有点太快了……有一个片段正好处理了这个问题,它表明,确实需要使用额外的MouseListener来使一切正常.

Okay I was being a little too fast on posting this question... There's a snippet that deals with exactly this problem and it shows, that one indeed has to use an extra MouseListener in order to get things working.

可以在

The snippet can be found here and this is the relevant part setting up the listener:

styledText.addListener(SWT.MouseDown, event -> {
    // It is up to the application to determine when and how a link should be activated.
    // In this snippet links are activated on mouse down when the control key is held down
    if ((event.stateMask & SWT.MOD1) != 0) {
        int offset = styledText.getOffsetAtLocation(new Point (event.x, event.y));
        if (offset != -1) {
            StyleRange style1 = null;
            try {
                style1 = styledText.getStyleRangeAtOffset(offset);
            } catch (IllegalArgumentException e) {
                // no character under event.x, event.y
            }
            if (style1 != null && style1.underline && style1.underlineStyle == SWT.UNDERLINE_LINK) {
                System.out.println("Click on a Link");
            }
        }
    }
});

这篇关于SWT:将可点击的链接集成到StyledText中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆