eclipse rcp:如何在tableviewer中选择单个单元格?

时间:2011-06-28 07:30:08

标签: eclipse-rcp

我可以更改表格的默认选择行为,我想在用户点击它时选择一个单元格,并在用户双击它时使其可编辑。

在@nonty的帮助下,我得到了我想要的东西 enter image description here
这是我的手机荧光笔实现:

package com.amarsoft.rcputil;

import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;


public class DefaultCellFocusHighlighter extends FocusCellOwnerDrawHighlighter {

    public DefaultCellFocusHighlighter(ColumnViewer viewer) {
        super(viewer);
    }

    protected boolean onlyTextHighlighting(ViewerCell cell) {
        return false;
    }

    protected Color getSelectedCellBackgroundColor(ViewerCell cell) {
        return cell.getControl().getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE);
    }

    protected Color getSelectedCellForegroundColor(ViewerCell cell) {
        return cell.getControl().getDisplay().getSystemColor(SWT.COLOR_WHITE);
    }

    protected Color getSelectedCellForegroundColorNoFocus(ViewerCell cell) {
        return cell.getControl().getDisplay().getSystemColor(SWT.COLOR_WHITE);
    }

    protected Color getSelectedCellBackgroundColorNoFocus(ViewerCell cell) {
        return cell.getControl().getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE);
    }

    protected void focusCellChanged(ViewerCell newCell, ViewerCell oldCell) {
        super.focusCellChanged(newCell, oldCell);
    }



}

使用它的代码:

TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(tv,new DefaultCellFocusHighlighter(tv));
        ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(tv) {
            protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
                return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
                        || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
                        || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
                        || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
            }
        };

        TableViewerEditor.create(tv, focusCellManager, actSupport, ColumnViewerEditor.TABBING_HORIZONTAL
                | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
                | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);

但我遇到了新问题: enter image description here
当我双击单元格来编辑它的值时,单元格左侧有一个小区域仍然以深蓝色突出显示

我知道原因:
使用边框创建文本控件时,操作系统包含围绕控件内容的特定平台。
还在寻找修复......

2 个答案:

答案 0 :(得分:1)

看看这两个JFace Snippets:

答案 1 :(得分:0)

在深入研究代码后,我在ColumnViewer类中找到了以下方法:

/**
 * 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);
            }
        });
    }
}

所以,我在我的TableViewer子类中覆盖了该函数:

@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));
                }
            }
        });
//  }
}

这对我有用。告诉我它是否适合你。