Предотвратить запуск события при вызове expandItem () - PullRequest
0 голосов
/ 24 августа 2011

заголовок говорит почти все.Когда я выполняю функцию expandItem () программным способом, я не хочу, чтобы инициированное событие вызывало вызов nodeExpand ().

Я реализовал ExpandListener:

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

Когда я вызываю expandItem () функция класса Tree, там всегда срабатывает событие.Это код исходного класса Tree:

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

Что я сделал сейчас, чтобы получить эту работу:

m_Tree.removeListener((ExpandListener)this);
m_Tree.expandItem(sItemId);
m_Tree.addListener((ExpandListener)this);

Есть ли более приятный подход?

1 Ответ

0 голосов
/ 25 августа 2011

Вы можете попытаться создать переключатель для вашего слушателя.Например:

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");
}

и отключите прослушиватель при необходимости.

Если имеется более одного прослушивателя, вы можете попробовать создать подкласс Tree и переопределить некоторые методы.

...