选择TableViewer

时间:2017-06-12 05:47:43

标签: java eclipse-rcp jface

我使用TableViewer来显示元素矩阵(而不是像往常一样每行一个元素)。有点尴尬的是,JFace无法开箱即用,但我终于得到了允许选择单元格的表格:

public class TableCellSelection {

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        final TableViewer viewer = new TableViewer(shell,
                SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
        viewer.setContentProvider(ArrayContentProvider.getInstance());
        Arrays.asList("Tic", "Tac", "Toe").forEach(c -> createTableViewerColumn(viewer, c));
        viewer.setInput(Arrays.asList("XOX", "OXX", "OXO"));

        hookCellSelection(viewer);

        shell.pack();
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    private static <T extends Comparable<T>> void createTableViewerColumn(TableViewer viewer,
            String name) {
        final int columnIndex = viewer.getTable().getColumnCount();
        final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
        viewerColumn.setLabelProvider(new ColumnLabelProvider() {
            @Override
            public String getText(Object element) {
                return String.valueOf(((String) element).charAt(columnIndex));
            }
        });
        viewerColumn.getColumn().setText(name);
        viewerColumn.getColumn().setWidth(100);
    }

    private static void hookCellSelection(TableViewer viewer) {
        final TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(viewer,
                new FocusCellOwnerDrawHighlighter(viewer), new CellNavigationStrategy());
        final ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(viewer);
        final int feature = ColumnViewerEditor.TABBING_HORIZONTAL | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
                | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION;
        TableViewerEditor.create(viewer, focusCellManager, actSupport, feature);
    }
}

现在客户端想要选择多个单元格,但我无法确定哪个类hookCellSelection方法可能负责取消选择上一个单元格(表格为SWT.MULTI,因此选择多个行应该是可能的)。

搜索问题以断开的链接结束并移动教程。

如何在TableViewer

中选择多个单元格

0 个答案:

没有答案