2

I would like to have multiple hyperlinks on each cell of a SWT table column. I followed the thread SWT Table and Hyperlink widget integration and modified Sambi's answer to my requirements.

I was able to get one hyperlink in each cell and invoke browser using Hyperlinklistener. I now want to have more than one hyperlink in each cell.

Below is the modified code for your reference:

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;
        }
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sree Harsha
  • 108
  • 6

2 Answers2

0

Approach1:

You can set a composite with multiple links on it and set that composite in in editor and its done.

enter image description here

The problem with this approach is you can not have different size rows in SWT/JFACE. Its known bug. Again you can not decrease the size of rows if you set(It is also known bug-platform specific).

Approach2:

You can manage to show a small custom tooltip on your Action Column, which will have all your links, The ideas is initially cell will show only one link and when you move mouse on it will pop up a Custom tooltip with all your links.

As I am in office, I can not share sample code snippet right now. But if you need I will share it in the evening.

Shrirang Kumbhar
  • 363
  • 4
  • 17
  • thanks for the ideas, yeah, I too thought of approach 1 and because of limitations, could not implement it. Please do share a sample snippet for approach 2. I will also try that meanwhile – Sree Harsha Jun 12 '17 at 11:52
  • tried approach 2 and posted the code in answers section for the sake of others. Accepting your answer as it has meaningful solutions – Sree Harsha Jun 14 '17 at 05:34
0

I've wasted 1 week on this already. I have moved on to another solution that better fits my RCP application.

Below is the updated code if anyone needs it.

Managed to get tooltip as per Approach2 of Shrirang's answer. However, the tooltip comes only for the first column. If anyone managed to get the tooltip on any other column except the first one, please post the snippet here for the sake of other users.

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;
        }
    }
}
Sree Harsha
  • 108
  • 6