在节点扩展/折叠时更改JFace TreeViewer中的标签

时间:2010-06-30 09:51:08

标签: swt jface

我有一个TreeViewer,其中一些节点代表文件夹,因此我希望在展开节点时显示打开文件夹的图标,在折叠时显示关闭的文件夹。听起来很简单,我只需要添加一个树监听器(代码在Scala中,但Java等价物应该很容易想象):

val tree = new TreeViewer(contents)
tree.addTreeListener(folderIconFixingListener)

private def folderIconFixingListener = new ITreeViewerListener {
  def treeCollapsed(event: TreeExpansionEvent) {
    tree.update(event.getElement, null)
  }

  def treeExpanded(event: TreeExpansionEvent) {
    tree.update(event.getElement, null)      
  }
}

不幸的是,这给了一个例外:

Ignored reentrant call while viewer is busy. This is only logged once per viewer instance, but similar calls will still be ignored.
java.lang.RuntimeException
    at org.eclipse.jface.viewers.ColumnViewer.checkBusy(ColumnViewer.java:763)
    at org.eclipse.jface.viewers.ColumnViewer.update(ColumnViewer.java:552)
    at scsadmin.NodesTreeScreen$$anon$1.treeExpanded(NodesTreeScreen.scala:64)
    at org.eclipse.jface.viewers.AbstractTreeViewer$3.run(AbstractTreeViewer.java:1101)
    at org.eclipse.jface.util.SafeRunnable$1.run(SafeRunnable.java:128)
    at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
    at org.eclipse.jface.viewers.AbstractTreeViewer.fireTreeExpanded(AbstractTreeViewer.java:1099)
    at org.eclipse.jface.viewers.AbstractTreeViewer.handleTreeExpand(AbstractTreeViewer.java:1444)
    at org.eclipse.jface.viewers.TreeViewer.handleTreeExpand(TreeViewer.java:937)
    at org.eclipse.jface.viewers.AbstractTreeViewer$4.treeExpanded(AbstractTreeViewer.java:1453)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
    at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
    at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
    at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
    at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
    at org.eclipse.swt.widgets.Tree.gtk_test_expand_row(Unknown Source)
    at org.eclipse.swt.widgets.Widget.windowProc(Unknown Source)
    at org.eclipse.swt.widgets.Display.windowProc(Unknown Source)
    at org.eclipse.swt.internal.gtk.OS._gtk_main_do_event(Native Method)
    at org.eclipse.swt.internal.gtk.OS.gtk_main_do_event(Unknown Source)
    at org.eclipse.swt.widgets.Display.eventProc(Unknown Source)
    at org.eclipse.swt.internal.gtk.OS._g_main_context_iteration(Native Method)
    at org.eclipse.swt.internal.gtk.OS.g_main_context_iteration(Unknown Source)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
    at org.eclipse.jface.window.Window.runEventLoop(Window.java:825)
    at org.eclipse.jface.window.Window.open(Window.java:801)
    at scsadmin.BootStrapper$.main(BootStrapper.scala:64)
    at scsadmin.BootStrapper.main(BootStrapper.scala)

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

你可能会尝试将你的代码放在你的监听器中的asyncExec中,以推迟它的执行:

display.asyncExec(new Runnable(){
   public void run(){
      tree.update(.);
   }
});

答案 1 :(得分:0)

我最终使用的解决方案是在TreeListener控件本身上使用Tree,这样做效果很好。