SWT table 单元格多个超链接

SWT table cell multiple hyperlinks

我想在 SWT table 列的每个单元格上有多个超链接。 我关注了话题 SWT Table and Hyperlink widget integration 并根据我的要求修改了 Sambi 的回答。

我能够在每个单元格中获得一个超链接并使用 Hyperlinklistener 调用浏览器。我现在想在每个单元格中有多个超链接。

下面是修改后的代码供大家参考:

public class TableEditorTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        TableViewer viewer = new TableViewer(shell);
        viewer.getTable().setHeaderVisible(true);
        viewer.getTable().setLinesVisible(true);
        viewer.setContentProvider(new ArrayContentProvider());
        TableColumn column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("First Name");
        column.setWidth(100);
        TableViewerColumn firstNameCol = new TableViewerColumn(viewer, column);
        firstNameCol.setLabelProvider(new ColumnLabelProvider(){
            @Override
            public String getText(Object element) {
                Person p = (Person)element;
                return p.getFirstName();
            }
        });
        column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("Last Name");
        column.setWidth(100);
        TableViewerColumn lastNameCol = new TableViewerColumn(viewer, column);
        lastNameCol.setLabelProvider(new ColumnLabelProvider(){
            @Override
            public String getText(Object element) {
                Person p = (Person)element;

                return p.getLastName();
            }
        });
        column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("Actions");
        column.setWidth(100);
        TableViewerColumn actionsNameCol = new TableViewerColumn(viewer, column);
        actionsNameCol.setLabelProvider(new ColumnLabelProvider(){
            //make sure you dispose these buttons when viewer input changes
            Map<Object, Hyperlink> buttons = new HashMap<Object, Hyperlink>();
            @Override
            public void update(ViewerCell cell) {
                TableItem item = (TableItem) cell.getItem();
                final Hyperlink hyperlink;
                if(buttons.containsKey(cell.getElement()))
                {
                    hyperlink=buttons.get(cell.getElement());
                }
                else
                {
                    hyperlink=new Hyperlink ((Composite) (cell.getViewerRow().getControl()),SWT.NONE);
                    hyperlink.setText( "link1");
                    hyperlink.setHref("http://www.google.com/");
                    hyperlink.addHyperlinkListener(new HyperlinkAdapter(){
                        public void linkActivated(HyperlinkEvent e){
                            System.out.println(e.getHref());
                            org.eclipse.swt.program.Program.launch(hyperlink.getHref().toString());
                        }
                    });
                    buttons.put(cell.getElement(), hyperlink);
                }
                TableEditor editor = new TableEditor(item.getParent());
                editor.grabHorizontal  = true;
                editor.grabVertical = true;
                editor.setEditor(hyperlink , item, cell.getColumnIndex());

                editor.layout();
            }
        });
        Person p1 = new Person();
        p1.setFirstName("George");
        p1.setLastName("Burne");

        Person p2 = new Person();
        p2.setFirstName("Adam");
        p2.setLastName("Silva");

        Person p3 = new Person();
        p3.setFirstName("Nathan");
        p3.setLastName("Cowl");

        List<Person> persons = new ArrayList<Person>();
        persons.add(p1);
        persons.add(p2);
        persons.add(p3);

        viewer.setInput(persons);

        shell.open();
        while(!shell.isDisposed())
        {
            if(!display.readAndDispatch())
            {
                display.sleep();
            }
        }
        display.dispose();
    }


    private static class Person
    {
        String firstName;
        String lastName;

        Person()
        {

        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }
}

方法一:

您可以设置一个包含多个 link 的合成,然后在编辑器中设置该合成并完成。

此方法的问题是 SWT/JFACE 中不能有不同大小的行。它的已知错误。同样,如果您设置了,则不能减小行的大小(这也是已知的平台特定错误)。

方法二:

您可以设法在您的操作栏上显示一个小的自定义工具提示,其中将包含您所有的 link,想法最初是单元格将仅显示一个 link,当您移动鼠标时它将弹出一个自定义工具提示,其中包含所有 links.

由于我在办公室,我现在无法分享示例代码片段。但如果你需要我会在晚上分享。

我已经在这上面浪费了 1 周时间。我已经转向另一个更适合我的 RCP 应用程序的解决方案。

如果有人需要,下面是更新后的代码。

按照 Shrirang 回答的方法 2 设法获得工具提示。但是,工具提示仅适用于第一列。 如果有人设法获得除第一列以外的任何其他列的工具提示,请 post 此处的代码片段以供其他用户使用。

public class TableEditorTest {

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        final TableViewer viewer = new TableViewer(shell);
        viewer.getTable().setHeaderVisible(true);
        viewer.getTable().setLinesVisible(true);
        viewer.setContentProvider(new ArrayContentProvider());
        ColumnViewerToolTipSupport.enableFor(viewer);
        viewer.getTable().setToolTipText("");
        TableColumn column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("First Name");
        column.setWidth(100);
        TableViewerColumn firstNameCol = new TableViewerColumn(viewer, column);
        firstNameCol.setLabelProvider(new ColumnLabelProvider(){
            @Override
            public String getText(Object element) {
                Person p = (Person)element;
                return p.getFirstName();
            }
        });
        column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("Last Name");
        column.setWidth(100);
        TableViewerColumn lastNameCol = new TableViewerColumn(viewer, column);
        lastNameCol.setLabelProvider(new ColumnLabelProvider(){
            @Override
            public String getText(Object element) {
                Person p = (Person)element;

                return p.getLastName();
            }
        });
        column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("Actions");
        column.setWidth(100);
        TableViewerColumn actionsNameCol = new TableViewerColumn(viewer, column);
        actionsNameCol.setLabelProvider(new ColumnLabelProvider(){
            @Override
            public String getText(Object element) {
                Person p = (Person)element;

                return p.getLastName();
            }
        });

        Person p1 = new Person();
        p1.setFirstName("George");
        p1.setLastName("Burne");

        Person p2 = new Person();
        p2.setFirstName("Adam");
        p2.setLastName("Silva");

        Person p3 = new Person();
        p3.setFirstName("Nathan");
        p3.setLastName("Cowl");

        List<Person> persons = new ArrayList<Person>();
        persons.add(p1);
        persons.add(p2);
        persons.add(p3);

        viewer.setInput(persons);

        Listener tooltipListener = new Listener() {
            Shell tip = null;
            ArrayList<Hyperlink> arrHypLnk=new ArrayList<Hyperlink>();
            //Label label = null;
            @Override
            public void handleEvent(Event event) {              

                switch (event.type) {
                case SWT.Dispose:
                case SWT.KeyDown:
                case SWT.MouseMove: {
                    if (tip == null) break;
                    tip.dispose ();
                    tip = null;
                    for(Hyperlink h: arrHypLnk){
                        h.dispose();
                    }
                    arrHypLnk.clear();
                    break;
                }
                case SWT.MouseHover: {
                    TableItem item = viewer.getTable().getItem (new Point (event.x, event.y));

                    if (item != null) {
                        if (tip != null  && !tip.isDisposed ()) tip.dispose ();
                        tip = new Shell (shell, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
                        tip.setBackground (display.getSystemColor (SWT.COLOR_INFO_BACKGROUND));

                        GridLayout layout = new GridLayout();
                        layout.numColumns = 1;
                        layout.makeColumnsEqualWidth = true;
                        tip.setLayout (layout);
                        tip.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
                        TableItem itemm = (TableItem) event.item;
                        String text = item.getText(event.index);
                        String[] texts= new String[]{"Link1", "Link2", "Link3"};
                        for(String s: texts){
                            final Hyperlink h=new Hyperlink(tip, SWT.NONE);
                            h.setText(s);
                            h.setHref(s);
                            h.addHyperlinkListener(new HyperlinkAdapter(){
                                public void linkActivated(HyperlinkEvent e){
org.eclipse.swt.program.Program.launch(h.getHref().toString());
                                }
                            });
                            arrHypLnk.add(h);
                        }

                        Point size = tip.computeSize (SWT.DEFAULT, SWT.DEFAULT);
                        Rectangle rect = item.getBounds (0);
                        Point pt = viewer.getTable().toDisplay (rect.x, rect.y);
                        tip.setBounds (pt.x, pt.y, size.x, size.y);
                        tip.setVisible (true);
                    }
                }
            }
            }
        };
        viewer.getTable().addListener(SWT.MouseHover, tooltipListener);
        viewer.getTable().addListener(SWT.MouseMove, tooltipListener);
        viewer.getTable().addListener(SWT.KeyDown, tooltipListener);

        shell.open();
        while(!shell.isDisposed())
        {
            if(!display.readAndDispatch())
            {
                display.sleep();
            }
        }
        display.dispose();
    }


    private static class Person
    {
        String firstName;
        String lastName;

        Person()
        {

        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }
}