Eclipse插件:通过在SWT表视图中的行中添加超链接来跳转到一行

时间:2014-02-21 10:04:42

标签: eclipse hyperlink eclipse-plugin

表格如下:

+-----------------+-----------------------+
|File Path        |         Line Number   |
|-----------------|-----------------------|
| File1.java      |         10            |
|-----------------|-----------------------|
| File2.java      |         19            |
+-----------------+-----------------------+

双击表格中的任何一行,我想跳转到表格行中文件中的指定行号。

我正在使用以下函数转到Active Editor的指定行。

private static void goToLine(IEditorPart editorPart, int LineNumber)
{
    if (!(editorPart instanceof ITextEditor) || LineNumber <= 0)
    {
        return;
    }

    ITextEditor editor = (ITextEditor) editorPart;
    IDocument document = editor.getDocumentProvider().getDocument(
            editor.getEditorInput());

    if (document != null)
    {
        IRegion lineInfo = null;

        // line count internally starts with 0, and not with 1 like in GUI
        try
        {
            lineInfo = document.getLineInformation(LineNumber - 1);
        }

        catch (org.eclipse.jface.text.BadLocationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (lineInfo != null)
        {
            editor.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength());
        }
      }
    }   
}

如何将超链接添加到表的行以跳转到相应文件的行?

1 个答案:

答案 0 :(得分:1)

向表格添加超链接需要使用OwnerDrawLabelProvider自行绘制表格,以便添加Hyperlink控件。

如果您只想支持双击某一行,可以使用TableViewer.addDoubleClickListener(或TableViewer.addOpenListener)添加IDoubleClickListener(或IOpenListener)。

相关问题