开发者

Prevent event from being fired when calling expandItem()

开发者 https://www.devze.com 2023-03-30 02:52 出处:网络
the title says almost everything. When I execute the expandItem() function programmatically I do not want the fired event causing a nodeExpand() call.

the title says almost everything. When I execute the expandItem() function programmatically I do not want the fired event causing a nodeExpand() call.

I have implemented the ExpandListener:

@Override
public void nodeExpand(ExpandEvent event)
{
    System.out.println("This should only appear when the user clicks the node on the UI");
}

When I call the expandItem() function of the Tree class, there is always an event fired. This is the code of the original Tree class:

public boolean expandItem(Object itemId) {
    boolean success = expandItem(itemId, true);
    requestRepaint();
    return success;
}

private boolean expandItem(Object itemId, boolean sendChildTree) {

    // Succeeds if the node is already expanded
    if (isExpanded(itemId)) {
        return true;
    }

    // Nodes that can not have children are not expandable
    if (!areChildrenAllowed(itemId)) {
        return false;
    }

    // Expands
    expanded.add(itemId);

    expandedItemId = itemId;
    if (initialPaint) {
        requestRepaint();
    } else if (sendChildTree) {
        requestPartialRepaint();
    }
    fireExpandEvent(itemId);

    return true;
}

What I did now to get this work is:

m_Tree.removeListener((ExpandListener)this);
m_Tree.expandItem(sItemId);
m_Tree.addListener((ExpandListener)this);
开发者_Go百科

Is there any nicer approach?


You could try to create a switch to your listener. For example:

private boolean listenerDisabled;

void setListenerDisabled(boolean disabled) 
{
    listenerDisabled = disabled;
}

@Override
public void nodeExpand(ExpandEvent event)
{
    if(listenerDisabled) return;
    System.out.println("This should only appear when the user clicks the node on the UI");
}

and disable the listener when needed.

If there are more than one listener, you could try creating a subclass of Tree and overriding some methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消