3

I've currently implemented a Table with a TableEditor in my Eclipse plugin to support cell-level editing with keyboard support (to traverse cells with the editor).

I also need a way to delete rows, and I didn't want to go with the practice of adding a delete button next to the table as it requires 2 clicks to delete a row (1 to select a row, and 1 to delete it). Instead I want a separate column which is populated with delete icons. I've thought of 2 ways to accomplish this and have run into issues with both:

  1. Add another column to the Table, set the icon with TableItem.setImage(). There are multiple issues with this approach, and you can see them below:

    • When selecting a row, the icon gets selected too
    • When hovering over the icon, it gets a tooltip of the image which apparently can't be disabled
    • Can't seem to vertically center the image inside the cell
       

    Delete column approach #1

  2. Add a ScrolledComposite next to the table and fill it with delete icons. This sounds a little insane, but I've actually made it pretty far with this one. The idea is to fill a ScrolledComposite with delete icons, force it to scroll with the table's scrollbar, and delete the corresponding row when an icon is clicked. I've only run into one blocking issue with this approach:

    • Can't seem to hide the scrollbar
       

    Delete column approach #2

So my questions are:

  • How I can solve the issues mentioned for either of these approaches?
  • Is there some other better approach?
seand
  • 468
  • 6
  • 23

1 Answers1

3

I found a way to hide the scrollbar for my 2nd approach. Basically all you need to do is:

// ScrolledComposite sc;
sc.setAlwaysShowScrollBars(true);
sc.getVerticalBar().setVisible(false);

And then set the width of the ScrolledComposite to 1 to get rid of the extra space the invisible ScrollBar takes up.

And to keep the scrollbars in sync:

// Table table;
// ScrolledComposite sc;
// int tableRowHeight;

protected void createTable() {

  ...

  // Set the listener that dictates the table row height.
  table.addListener(SWT.MeasureItem, new Listener() {
    @Override
    public void handleEvent(Event event) {
      event.height = tableRowHeight;
    }
  });

  // Set the listener for keeping the scrollbars in sync.
  table.getVerticalBar().addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
      syncDeleteColumnScrollBar();
    }
  });
}

// This is extracted out into a method so it can also be called
// when removing a table row.
protected void syncDeleteColumnScrollBar() {
  sc.setOrigin(0, table.getVerticalBar().getSelection() * tableRowHeight);
}

The result:

Delete column image

seand
  • 468
  • 6
  • 23