eclipse rcp :how to select a single cell in tableviewer?

前提是你 提交于 2019-12-07 15:57:26
Tonny Madsen

Have a look at these two JFace Snippets:

After digging through the code, I found the following method in the ColumnViewer class:

/**
 * Hook up the editing support. Subclasses may override.
 * 
 * @param control
 *      the control you want to hook on
 */
protected void hookEditingSupport(Control control) {
    // Needed for backwards comp with AbstractTreeViewer and TableTreeViewer
    // who are not hooked this way others may already overwrite and provide
    // their
    // own impl
    if (viewerEditor != null) {
        control.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
                // Workaround for bug 185817
                if (e.count != 2) {
                    handleMouseDown(e);
                }
            }

            public void mouseDoubleClick(MouseEvent e) {
                handleMouseDown(e);
            }
        });
    }
}

So, I overrode that function within my TableViewer subclass:

@Override protected void hookEditingSupport(Control control) {
    // We know there should be an editor avaiable
//  if (viewerEditor != null) {
        control.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
                // Workaround for bug 185817
                if (e.count != 2) {
                    // We don't want to edit on single clicks
//                  handleMouseDown(e);
                }
            }

            public void mouseDoubleClick(MouseEvent e) {
                // This method is private, so copy the implementation
//              handleMouseDown(e);
                ViewerCell cell = getCell(new Point(e.x, e.y));
                e.count--; // A hack to make things work - pretend like it's a single click
                if (cell != null) {
                    triggerEditorActivationEvent(new ColumnViewerEditorActivationEvent(
                            cell, e));
                }
            }
        });
//  }
}

This works for me. Tell me if it works for you.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!