0

I'm looking for a way to display a tree in the first column of a table/grid with three other columns, one with a combobox and the others with checkboxes. I've been trying to make this work with a TreeViewer but it doesn't quite fit what I'm looking for. The tree goes together fine. The Combobox column where I used the EditorSupport for the column and return a ComboboxCellEditor in the getCellEditor method but you can only see that there is a combobox in the column when you select a cell in that column. Then when you click out of the cell the selected value goes back to the default blank. The same goes for the checkbox columns where it is is only visible when the cell is selected. I'm looking for something that will display my tree with the combobox column and checkbox columns always visible. I've looked at TableViewer but couldn't find a way to force in a tree in the first column. I've looked at Nebula Grid but that doesn't look like it supports comboboxes. Any tips on how to get one of these to work like what I am looking for or if there is some other tree/table/grid I should be looking at. Thanks.

Edit: Here's the code for the EditingSupport class.

private class ComboBoxEditingSupport extends EditingSupport
{
    private ComboBoxCellEditor cellEditor;

    public ComboBoxEditingSupport(ColumnViewer viewer)
    {
        super(viewer);

        cellEditor =
            new ComboBoxCellEditor(((TreeViewer) viewer).getTree(),
                new String[] {
                        "Some String",
                        "Some other String" }, SWT.READ_ONLY);

    }

    @Override
    protected CellEditor getCellEditor(Object element)
    {
        if (element instanceof MyObject
        {
            return cellEditor;
        }
        return null;
    }

    @Override
    protected boolean canEdit(Object element)
    {
        if (element instanceof MyObject
        {
            return true;
        }
        return false;
    }

    @Override
    protected Object getValue(Object element)
    {
        return 0;
    }

    @Override
    protected void setValue(Object element, Object value)
    {
        TreeItem[] ti = treeViewer.getTree().getSelection();
        CCombo combo = ((CCombo) cellEditor.getControl());
        String str = combo.getItem(combo.getSelectionIndex());
        ti[0].setText(1, str);
    }
}
GamedaySting
  • 107
  • 10
  • I've tired swt TreeViewer with TreeColumnViewers for the columns. I tried Nebula Grid. This was the closes but it doesn't support comboboxes in the columns by the looks of it. I've tried TableViewer but that doesn't support the tree that I need. – GamedaySting Sep 04 '15 at 22:17
  • EditingSupport with a ComboboxCellEditor should work, it sounds like you have a problem in the `setValue` method - show us your code. – greg-449 Sep 05 '15 at 07:15

1 Answers1

1

Your setValue method must update the value in your model data (the data returned by your content provider). The element parameter to setValue is the particular model data object (MyObject) that you should update.

After updating the value call:

getViewer().update(element, null);

to get the tree to update the display from the model.

Trying to update the TreeItem directly will not work.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • That helped a lot and got to going down the right path. I guess a follow up question would be how can I get the down arrow for the combobox to always show. Right now it only shows after the cell has been clicked on and goes away once the cell loses focus. – GamedaySting Sep 08 '15 at 16:00
  • The same kind of goes for a checkbox column too. How do I get the checkbox to actually show up. I have the functionality for it but a checkbox doesn't show up. – GamedaySting Sep 08 '15 at 16:47
  • Unfortunately the EditingSupport code only supports one cell at a time as you have seen. You would probably have to use `OwnerDrawLabelProvider` and draw all the cells yourself to do any better. – greg-449 Sep 08 '15 at 17:58