[英]Adding double-click expansion to the Tree Viewer in SWT
Double-Clicking tree items works completely fine, but when I press CTRL + M on the keyboard then the tree items expand\\collapse, can someone please tell me the reason behind this? 双击树项目可以很好地工作,但是当我在键盘上按CTRL + M时,树项目会展开\\折叠,有人可以告诉我其背后的原因吗? Is this a bug in Eclipse or why does this double-click functionality get triggered when I press CTRL+M. 这是Eclipse中的错误还是为什么当我按CTRL + M时触发了双击功能?
Thanks. 谢谢。
Use TreeViewer.addDoubleClickListener
to listen for tree double clicks not a mouse listener. 使用TreeViewer.addDoubleClickListener
来侦听树双击而不是鼠标侦听器。 You could use something like this: 您可以使用如下形式:
private class DoubleClickListener implements IDoubleClickListener
{
@Override
public void doubleClick(final DoubleClickEvent event)
{
final IStructuredSelection selection = (IStructuredSelection)event.getSelection();
if (selection == null || selection.isEmpty())
return;
final Object sel = selection.getFirstElement();
final ITreeContentProvider provider = (ITreeContentProvider)treeViewer.getContentProvider();
if (!provider.hasChildren(sel))
return;
if (treeViewer.getExpandedState(sel))
treeViewer.collapseToLevel(sel, AbstractTreeViewer.ALL_LEVELS);
else
treeViewer.expandToLevel(sel, 1);
}
}
Update: Using TreeViewer.addDoubleClickListener
is the preferred way to do double click handling for all classes derived from StructuredViewer
. 更新:使用TreeViewer.addDoubleClickListener
是对从StructuredViewer
派生的所有类进行双击处理的首选方法。
Each double click listener is run using SafeRunnable
which deals with any exceptions that the listener may throw, this safeguards the rest of the code for errors in the listeners. 每个双击侦听器都是使用SafeRunnable
运行的,它可以处理侦听器可能抛出的任何异常,这可以保护其余代码,防止侦听器中发生错误。
The DoubleClickEvent
provides direct access to the model object data so it is not necessary to deal with Tree
or TreeItem
objects to work out selections. DoubleClickEvent
提供对模型对象数据的直接访问,因此无需处理Tree
或TreeItem
对象即可得出选择。
The double click code in the TreeViewer
interfaces correctly with the OpenStrategy
single / double click to open code. TreeViewer
的双击代码可以正确地与OpenStrategy
单击/双击打开代码。
I think the following code will be better , cause it will not cause the tree item to reload children and will keep the original state of other tree items. 我认为以下代码会更好,因为它不会导致树项重新加载子项,并保持其他树项的原始状态。
_treeViewer.addDoubleClickListener( new IDoubleClickListener()
{
@Override
public void doubleClick( DoubleClickEvent event )
{
ISelection selection = event.getSelection();
if( selection instanceof ITreeSelection )
{
TreePath[] paths= ((ITreeSelection)selection).getPathsFor(selectedItem);
for (int i= 0; i < paths.length; i++)
{
_treeViewer.setExpandedState(paths[i], !_treeViewer.getExpandedState(paths[i]));
}
}
}
}
} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.