简体   繁体   English

Java/SWT 鼠标悬停/单击树查看器的一列

[英]Java/SWT mouse hover/click over a column of a tree viewer

I have a TreeViewer where some cells are styled to look like links.我有一个TreeViewer ,其中一些单元格的样式看起来像链接。 The tree is filled with content provider and StyledCellLabelProviders .该树填充了 content provider 和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.有 4 个TreeColumns ,每个TreeViewerColumn The data is filled with a content provider, and 2 types of label providers (for simple text and styled text).数据填充有一个内容提供者和 2 种类型的标签提供者(用于简单文本和样式文本)。 I need to know when the mouse is clicked on an item in the 4th column and when it hovers over it.我需要知道鼠标何时点击第 4 列中的项目以及鼠标悬停在它上面的时间。 When that happens - I need to know the cell it is clicked on, which TreeItem it belongs to, the data in this item, etc.发生这种情况时 - 我需要知道它被单击的单元格、它属于哪个 TreeItem、该项目中的数据等。

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?任何的想法?

I would follow this approach:我会遵循这种方法:

  1. attach a MouseMoveListener to the TreeMouseMoveListener附加到树

  2. determine which TreeItem the mouse pointer is over with Tree.getItem(Point) .使用Tree.getItem(Point)确定鼠标指针在哪个TreeItem上。

    For a multi-column tree, getItem() will only return an item if the mouse position is within the first column.对于多列树,如果鼠标位置在第一列内, getItem()只会返回一个项目。 Either create the tree with the SWT.FULL_SELECTION style flag, or use a workaround like this to detmine the item:要么使用SWT.FULL_SELECTION样式标志创建树,要么使用这样的解决方法来确定项目:

     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 (eg x < tree.getBounds().x ) to prevent an endless loop, if no item can be found.如果找不到项目,可能需要添加进一步的条件(例如x < tree.getBounds().x )以防止无限循环。

  3. once you have the TreeItem , you can obtain the element that it shows with TreeItem::getData .一旦你有了TreeItem ,你就可以获取它显示的元素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 .请注意,这是TreeViewer的实现细节 - 但我不知道还有另一种方法可以获取给定TreeItem的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM