SWT StyledCellLabelProvider崩溃表格外观和选择

时间:2014-02-13 17:16:08

标签: java swt jface

在使用StyledCellLabelProvider之后,我的表格看起来有问题。

以下是快速代码段代码:

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.CoolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class SwtIssueWithTableSelectionAndBorders extends ApplicationWindow {

    private Table table;
    private TableViewer tableViewer;
    /**
     * Create the application window,
     */
    public SwtIssueWithTableSelectionAndBorders() {
        super(null);
        addCoolBar(SWT.FLAT);
        addMenuBar();
        addStatusLine();
    }

    /**
     * Create contents of the application window.
     * @param parent
     */
    @Override
    protected Control createContents(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        container.setLayout(new GridLayout(1, false));

        Composite composite = new Composite(container, SWT.NONE);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

        TableColumnLayout layout = new TableColumnLayout();
        composite.setLayout(layout);

        tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
        table = tableViewer.getTable();
        table.setHeaderVisible(true);
        table.setLinesVisible(false);

        TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
        TableColumn tableColumn = tableViewerColumn.getColumn();
        layout.setColumnData(tableColumn, new ColumnWeightData(2, ColumnWeightData.MINIMUM_WIDTH, true));
        tableColumn.setText("Test1");

        tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
        tableColumn = tableViewerColumn.getColumn();
        layout.setColumnData(tableColumn, new ColumnWeightData(2, ColumnWeightData.MINIMUM_WIDTH, true));
        tableColumn.setText("Test2");

        tableViewer.setLabelProvider(new SampleLabelProvider());
        tableViewer.setContentProvider(new ContentProvider());

        StyledCellLabelProvider cellLabelProvider = new StyledCellLabelProvider() {
            @Override
            public void update(ViewerCell cell)
            {
                StyledString text = new StyledString();
                StyleRange myStyledRange = new StyleRange(0, "test".length(), Display.getDefault().getSystemColor(SWT.COLOR_BLACK), null);
                myStyledRange.underline = true;
                text.append("test", StyledString.DECORATIONS_STYLER);
                cell.setText(text.toString());

                StyleRange[] range = { myStyledRange };
                cell.setStyleRanges(range);
                super.update(cell);
            }
        };

        //tableViewerColumn.setLabelProvider(cellLabelProvider);

        initModel();
        initListener();
        return container;
    }

    private void initListener() {
        table.addListener(SWT.MouseDown, new Listener() {

            @Override
            public void handleEvent(Event event) {
                table.setEnabled(false);
            }
        });
    }

    private void initModel() {
        List<SampleModel> sampleModelElements = new ArrayList<SampleModel>();
        sampleModelElements.add(new SampleModel("test1_1", "test1_2"));
        sampleModelElements.add(new SampleModel("test2_1", "test2_2"));
        sampleModelElements.add(new SampleModel("test3_1", "test3_2"));
        tableViewer.setInput(sampleModelElements);
    }

    @Override
    protected MenuManager createMenuManager() {
        return new MenuManager("menu");
    }

    @Override
    protected CoolBarManager createCoolBarManager(int style) {
        return new CoolBarManager(style);
    }

    @Override
    protected StatusLineManager createStatusLineManager() {
        return new StatusLineManager();
    }

    public static void main(String args[]) {
        try {
            SwtIssueWithTableSelectionAndBorders window = new SwtIssueWithTableSelectionAndBorders();
            window.setBlockOnOpen(true);
            window.open();
            Display.getCurrent().dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("TableApp");
    }

    @Override
    protected Point getInitialSize() {
        return new Point(400,300);
    }

    private class SampleLabelProvider extends LabelProvider implements ITableLabelProvider {
        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }
        public String getColumnText(Object element, int columnIndex) {
            SampleModel sampleModel = (SampleModel) element;
            String result = "";
            switch(columnIndex){
                case 0:
                    result = sampleModel.getTest1();
                    break;
                case 1:
                    result = sampleModel.getTest2();
                    break;
            }
            return result;
        }
    }
    private class ContentProvider implements IStructuredContentProvider {
        public Object[] getElements(Object inputElement) {
            List<SampleModel> sampleModelElements = (List<SampleModel>)inputElement;
            return sampleModelElements.toArray();
        }
        public void dispose() {
        }
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }
    }

    private class SampleModel {
        private String test1;
        private String test2;

        public SampleModel(String test1, String test2) {
            super();
            this.test1 = test1;
            this.test2 = test2;
        }

        public String getTest1() {
            return test1;
        }

        public String getTest2() {
            return test2;
        }
   }
}

当我运行此代码时,一切都按预期工作。 表在列之间有分隔符(因为table.setLinesVisible(false)而不在行之间): enter image description here

当我点击一行时,我有可见的禁用选择: enter image description here

当我取消注释行时出现问题:

tableViewerColumn.setLabelProvider(cellLabelProvider);

比列之间没有分隔符:

enter image description here

如果我在一行上按下鼠标按钮,则禁用表中没有选择:

enter image description here

当它被禁用并且使用StyledCellLabelProvider时,有没有办法以某种方式将选择带回到表中?

0 个答案:

没有答案