在org.eclipse.swt.widgets.Table中启用单列选择

时间:2013-07-24 05:28:08

标签: eclipse-rcp

我使用了org.eclipse.swt.widgets.Table 代码实现将如下所示

表table = new Table(shell,SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);

out put will look like this

但我需要的行为就像鼠标点击了' Sec_1'我只想要' Sec_1'选择不是整行,如果鼠标点击' First_1'我不希望它突出显示(FirstColumn没有选择)。  任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

请参阅此代码段(例如http://git.eclipse.org/c/platform/eclipse.platform.ui.git/tree/examples/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet036FocusBorderCellHighlighter.java):

public class Test {

private class MyContentProvider implements IStructuredContentProvider {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
     */
    public Object[] getElements(Object inputElement) {
        return (MyModel[]) inputElement;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IContentProvider#dispose()
     */
    public void dispose() {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
     *      java.lang.Object, java.lang.Object)
     */
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

    }

}

public static boolean flag = true;

public class MyModel {
    public int counter;

    public MyModel(int counter) {
        this.counter = counter;
    }

    public String toString() {
        return "Item " + this.counter;
    }
}

public class MyLabelProvider extends LabelProvider implements
        ITableLabelProvider, ITableFontProvider, ITableColorProvider {
    FontRegistry registry = new FontRegistry();

    public Image getColumnImage(Object element, int columnIndex) {
        return null;
    }

    public String getColumnText(Object element, int columnIndex) {
        return "Column " + columnIndex + " => " + element.toString();
    }

    public Font getFont(Object element, int columnIndex) {
        return null;
    }

    public Color getBackground(Object element, int columnIndex) {
        return null;
    }

    public Color getForeground(Object element, int columnIndex) {
        return null;
    }

}

public Test(Shell shell) {
    final TableViewer v = new TableViewer(shell, SWT.BORDER|SWT.FULL_SELECTION);
    v.setLabelProvider(new MyLabelProvider());
    v.setContentProvider(new MyContentProvider());

    v.setCellEditors(new CellEditor[] { new TextCellEditor(v.getTable()), new TextCellEditor(v.getTable()), new TextCellEditor(v.getTable()) });
    v.setCellModifier(new ICellModifier() {

        public boolean canModify(Object element, String property) {
            return true;
        }

        public Object getValue(Object element, String property) {
            return "Column " + property + " => " + element.toString();
        }

        public void modify(Object element, String property, Object value) {

        }

    });

    v.setColumnProperties(new String[] {"1","2","3"});

    TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(v,new FocusCellOwnerDrawHighlighter(v));
    ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(v) {
        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(v, focusCellManager, actSupport, ColumnViewerEditor.TABBING_HORIZONTAL
            | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
            | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);


    TableColumn column = new TableColumn(v.getTable(), SWT.NONE);
    column.setWidth(200);
    column.setMoveable(true);
    column.setText("Column 1");

    column = new TableColumn(v.getTable(), SWT.NONE);
    column.setWidth(200);
    column.setMoveable(true);
    column.setText("Column 2");

    column = new TableColumn(v.getTable(), SWT.NONE);
    column.setWidth(200);
    column.setMoveable(true);
    column.setText("Column 3");


    MyModel[] model = createModel();
    v.setInput(model);
    v.getTable().setLinesVisible(true);
    v.getTable().setHeaderVisible(true);
}

private MyModel[] createModel() {
    MyModel[] elements = new MyModel[10];

    for (int i = 0; i < 10; i++) {
        elements[i] = new MyModel(i);
    }

    return elements;
}

/**
 * @param args
 */
public static void main(String[] args) {
    Display display = new Display();

    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    new Test(shell);
    shell.open();

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

    display.dispose();

}

}