0

I have a TreeViewer where some cells are styled to look like links. The tree is filled with content provider and StyledCellLabelProviders. I need to know when those specific "link" cells are hovered (so I change the mouse to the hand cursor) and clicked (so I actually do something with those "links").

I failed to find a solution, so any help will be highly appreciated.

Thanks, Oren

EDIT: More explanation I have a tree and a treeviewer. There are 4 TreeColumns with a TreeViewerColumn for each. The data is filled with a content provider, and 2 types of label providers (for simple text and styled text). I need to know when the mouse is clicked on an item in the 4th column and when it hovers over it. When that happens - I need to know the cell it is clicked on, which TreeItem it belongs to, the data in this item, etc.

My problem is I can't figure this out. When I use a mouse listener, or a selection listener, they only work on the first column. I can't "reach" the 4th one.

I cannot change the column order or it won't make sense.

Any idea?

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
Oren Sarid
  • 143
  • 1
  • 12

1 Answers1

1

I would follow this approach:

  1. attach a MouseMoveListener to the Tree

  2. determine which TreeItem the mouse pointer is over with Tree.getItem(Point).

    For a multi-column tree, getItem() will only return an item if the mouse position is within the first column. Either create the tree with the SWT.FULL_SELECTION style flag, or use a workaround like this to detmine the item:

    TreeItem item = tree.getItem(new Point(event.x, event.y));
    int x = 0;
    while (item == null) {
      item = tree.getItem(new Point(x, event.y));
      x += 5;
    }
    

    It might be necessary to add a further condition (e.g. x < tree.getBounds().x) to prevent an endless loop, if no item can be found.

  3. once you have the TreeItem, you can obtain the element that it shows with TreeItem::getData. Be aware that this is an implementation detail of the TreeViewer - but I am not aware that there is another way to obtain the element for a given TreeItem.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • My TreeItem have 4 columns each, and I don't why, but I create them with a content viewer and I believe each TreeItem in the top level should have a few children (as the content provider shows), but when iterating on them the don't show them. So I am not sure the TreeItem I'll get is correct. I'll try that and update back here. – Oren Sarid Oct 07 '14 at 05:21
  • Update: I tried it just now, I can get the TreeItem only when hovering over the first column. I don't get it when hovering over the other columns. Any idea? – Oren Sarid Oct 07 '14 at 06:17
  • @OrenSarid I have updated the answer with what I found out about multi-column trees HTH Rüdiger – Rüdiger Herrmann Oct 07 '14 at 10:03