简体   繁体   English

SWT Tree和TreeItem:删除或更改展开/折叠指示器的绘图行为

[英]SWT Tree and TreeItem: Remove or change the drawing behavior of the expand/collapse indicator

I'm working in SWT (no JFace), and I'm attempting to customize the behavior of a Tree that I'm using a sidebar. 我在SWT(没有JFace)工作,我正在尝试自定义我正在使用侧边栏的树的行为。

The top-level items in the tree shouldn't be selectable; 树中的顶级项目不应该是可选择的; they're basically headers. 他们基本上是标题。 Only the children of these items should be selectable. 只能选择这些项目的子项。 As a result, I would like the UI to behave in a way that indicates this; 因此,我希望UI的行为能够表明这一点; clicking on one of these top-level items should expand or collapse but shouldn't provide any sort of visual feedback outside of the indicator changing its state and the children's visibility changing. 点击其中一个顶级项目应该展开或折叠,但不应在指标之外提供任何类型的视觉反馈,以改变其状态和孩子的可见性变化。

The problem seems to be that on OS X, the expand/collapse indicator is a triangle (don't know if it's an image or a unicode character) that either points right or down, but is also colored based on the "selection" state. 问题似乎是在OS X上,展开/折叠指示器是一个三角形(不知道它是图像还是unicode字符),它指向右或向下,但也基于“选择”状态着色。 I've managed to override all of the relevant behavior for the top-level items except for the arrow changing color. 我已设法覆盖顶级项目的所有相关行为,但箭头更改颜色除外。

  • I've used an SWT.EraseItem listener to hook in to the background drawing so that the background doesn't change color. 我使用了一个SWT.EraseItem监听器来挂钩背景图,这样背景就不会改变颜色。 This works as expected. 这按预期工作。
  • I've used an SWT.PaintItem listener to make sure that the text in the top-level item doesn't change color. 我使用了SWT.PaintItem监听器来确保顶级项目中的文本不会改变颜色。 This works as expected, but doesn't seem to have any influence over the indicator; 这符合预期,但似乎对指标没有任何影响; I've even tried not resetting the GC's foreground color, but the color of the indicator still changes: 我甚至尝试过重置GC的前景色,但指示灯的颜色仍然会改变:

Not Selected : 未选中

未选中的

Selected

选择和丑陋

Some of the things that I've attempted to do, all of which have failed: 我试图做的一些事情,都失败了:

  • Just drawing a rectangle on top of indicator. 只需在指标顶部绘制一个矩形。 The indicator seems to always be on top, no matter what I attach the PaintListener to. 无论我将PaintListener附加到什么位置,指标似乎始终位于顶部。

  • Using a Listener on the Selection event type, checking to see if the event.item is one of the top-level items, and then fiddling with the event bits and redraw flags. 在Selection事件类型上使用Listener,检查event.item是否是顶级项之一,然后摆弄事件位和重绘标志。 This doesn't seem to work well at all; 这似乎根本不起作用; the behavior is completely unpredictable. 这种行为完全不可预测。 The code looks something like this: 代码看起来像这样:

     sideBar.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { TreeItem eventItem = (TreeItem) event.item; if(sideBarRoots.contains(event.item)) { event.detail = 0; event.type = 0; event.doit = false; sideBar.setRedraw(false); } else { sideBar.setRedraw(true); event.type = SWT.Selection; event.doit = true; } } }); 

    Since the redraw flag is just a hint, sometimes it gets set properly and others it doesn't. 由于重绘标志只是一个提示,有时它被正确设置而其他人则没有。 It can have either no effect, or can get locked in to a state where nothing redraws in the sidebar. 它可以没有效果,也可以锁定到侧边栏中没有重绘的状态。

I'm aware that a lot of this behavior is highly coupled to the behavior of the underlying native widgets, but I was wondering if there was any way to get the behavior that I'm looking for without roll a custom widget from scratch? 我知道很多这种行为高度耦合到底层本机小部件的行为,但我想知道是否有任何方法可以获得我正在寻找的行为而无需从头开始自定义小部件?

I think in a situation as yours you should ideally not allow selection on a tree header. 我认为在你的情况下,理想情况下你不应该允许在树头上进行选择。

You can either cancel the selection event, by not allowing it to be clickable. 您可以取消选择事件,不允许它被点击。 But that might be a little kludgy implementation, especially for key navigation. 但这可能是一个小小的kludgy实现,特别是对于键导航。

The safer approach, if possible with your data, would be to just move the selection whenever a tree parent node is selected. 如果可能的话,更安全的方法是在选择树父节点时移动选择。 So it should work like this that if a parent node is selected, either by keyboard or mouse: you expand the node and move the selection to the first child. 所以它应该像这样工作,如果通过键盘或鼠标选择父节点:您展开节点并将选择移动到第一个子节点。

This way you can avoid all the paint trickery to hide the selection state. 这样你就可以避免所有的绘画技巧来隐藏选择状态。

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

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