本文介绍了SWT Tree和TreeItem:删除或更改展开/折叠指示器的绘图行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在SWT(没有JFace)中工作,并且试图自定义使用边栏的Tree的行为.

I'm working in SWT (no JFace), and I'm attempting to customize the behavior of a Tree that I'm using a sidebar.

树中的顶级项目不应该是可选的;他们基本上是标题.仅这些项目的子项应该是可选的.结果,我希望用户界面的行为可以表明这一点.单击这些顶级项目之一应会展开或折叠,但不应在指示器状态改变和儿童可见度改变之外提供任何形式的视觉反馈.

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; 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.

问题似乎是在OS X上,展开/折叠指示器是一个三角形(不知道它是图像还是unicode字符),该三角形要么指向右要么指向下,但也根据选择"状态.我设法覆盖了顶层项目的所有相关行为,除了箭头改变了颜色.

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. I've managed to override all of the relevant behavior for the top-level items except for the arrow changing color.

  • 我使用了SWT.EraseItem侦听器来挂接到背景图形,以使背景不会改变颜色.这按预期工作.
  • 我使用了SWT.PaintItem侦听器,以确保顶级项目中的文本不会更改颜色.这可以按预期工作,但似乎对指标没有任何影响.我什至尝试 not 重置GC的前景色,但是指示器的颜色仍然会更改:
  • I've used an SWT.EraseItem listener to hook in to the background drawing so that the background doesn't change color. 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. 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:

未选中:

已选择

我尝试做的一些事情都失败了:

Some of the things that I've attempted to do, all of which have failed:

  • 只需在指示器顶部绘制一个矩形即可.无论我将PaintListener附加到什么上,该指标似乎始终位于最上方.

  • Just drawing a rectangle on top of indicator. The indicator seems to always be on top, no matter what I attach the PaintListener to.

在Selection事件类型上使用侦听器,检查event.item是否是顶级项目之一,然后摆弄事件位和重绘标志.这似乎一点也不奏效.该行为是完全无法预测的.代码看起来像这样:

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. 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.

如果可能的话,使用数据的更安全方法是在选择树的父节点时仅移动选择.因此,应该这样工作:如果通过键盘或鼠标选择了父节点,请展开该节点并将所选内容移动到第一个子节点.

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.

这篇关于SWT Tree和TreeItem:删除或更改展开/折叠指示器的绘图行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:03