具有复选框列的NatTable - 选中/取消选中不起作用

时间:2017-04-28 20:44:40

标签: java eclipse checkbox swt nattable

使用EditableGrid示例在我的NatTable中创建一个复选框列,但是检查和取消选中的鼠标事件没有被触发,因此复选框的状态不会改变。以下是代码段:

private Control exampleNatTableWithCheckBox(Composite parent) {

        final String[] propertyNames = PLAN_PROPERTY_NAMES;
        final Map<String, String> propertyToLabelMap = getPropertyToLabelMap();

        IRowIdAccessor<ConsoleEntry> rowIdAccessor = new IRowIdAccessor<T>() {
            @Override
            public Serializable getRowId(T rowObject) {
                return rowObject.getRcdIdx();
            }
        };
        ConfigRegistry configRegistry = new ConfigRegistry();

        // Body
        this.baseEventList = getBaseEventList();
        FilterList<T> filterList = new FilterList<T>(baseEventList);
        SortedList<T> sortedList = new SortedList<T>(filterList, null);

        bodyLayer = new FullFeaturedBodyStackLayer<ConsoleEntry>(sortedList, rowIdAccessor, propertyNames,configRegistry);

        this.bodyDataProvider = bodyLayer.getBodyDataProvider();

        registerConfigCells(configRegistry);

        registerCheckBoxEditor(configRegistry);

        // Column header
        FullFeaturedColumnHeaderLayerStack<T> columnHeaderLayer = new FullFeaturedColumnHeaderLayerStack<T>(sortedList, filterList, propertyNames, propertyToLabelMap, bodyLayer, bodyLayer.getSelectionLayer(),
                configRegistry);

        // Row header
        final DefaultRowHeaderDataProvider rowHeaderDataProvider = new DefaultSummaryRowHeaderDataProvider(
                this.bodyDataProvider);
        DefaultRowHeaderDataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
        rowHeaderDataLayer.setDefaultColumnWidth(50);
        ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayer, bodyLayer.getSelectionLayer());

        // Corner
        final DefaultCornerDataProvider cornerDataProvider = new DefaultCornerDataProvider(
                columnHeaderLayer.getColumnHeaderDataProvider(), rowHeaderDataProvider);
        DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
        ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnHeaderLayer);

        // Grid
        GridLayer gridLayer = new GridLayer(bodyLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer);

        this.natTable = new NatTable(parent, gridLayer, false);

        this.natTable.setConfigRegistry(configRegistry);

        this.natTable.addConfiguration(new StyledRowHeaderConfiguration());
        this.natTable.addConfiguration(new StyledColumnHeaderConfiguration());
        this.natTable.addConfiguration(new DefaultNatTableStyleConfiguration());

        // Popup menu
        this.natTable.addConfiguration(new HeaderMenuConfiguration(this.natTable) {
            @Override
            protected PopupMenuBuilder createColumnHeaderMenu(NatTable natTable) {
                return super.createColumnHeaderMenu(natTable).withColumnChooserMenuItem();
            }
        });

        this.natTable.addConfiguration(new SingleClickSortConfiguration());

        // Editing
        ColumnOverrideLabelAccumulator columnLabelAccumulator = new ColumnOverrideLabelAccumulator(
                bodyLayer.getBodyDataLayer());
        bodyLayer.getBodyDataLayer().setConfigLabelAccumulator(columnLabelAccumulator);
        columnLabelAccumulator.registerColumnOverrides(0, COLUMN_BOOKMARK_LABEL);

        this.natTable.addConfiguration(editableGridConfiguration(columnLabelAccumulator, this.bodyDataProvider));
        this.natTable.addConfiguration(filterRowConfiguration());

        bodyLayer.getBodyDataLayer().setConfigLabelAccumulator(getConfigLabelAccumulator(bodyLayer.getBodyDataLayer()));

        // Preserve selection on updates and sort
        final SelectionLayer selectionLayer = bodyLayer.getSelectionLayer();
        final RowSelectionModel<ConsoleEntry> rowSelectionModel = new RowSelectionModel<ConsoleEntry>(selectionLayer,
                this.bodyDataProvider, rowIdAccessor);
        selectionLayer.setSelectionModel(rowSelectionModel);

        // Select complete rows
        RowOnlySelectionConfiguration<ConsoleEntry> selectionConfig = new RowOnlySelectionConfiguration<ConsoleEntry>();
        selectionLayer.addConfiguration(selectionConfig);
//      this.natTable.addConfiguration(new RowOnlySelectionBindings());

         rowSelectionProviderNatTable = new RowSelectionProvider<ConsoleEntry>(selectionLayer,
                this.bodyDataProvider);
        if (this.bodyDataProvider.getList() != null && this.bodyDataProvider.getList().size() > 0) {
            rowSelectionProviderNatTable.setSelection(new StructuredSelection(this.bodyDataProvider.getList().get(0)));
        }

        LayerListenerFixture listener = new LayerListenerFixture();
        // we register the listener to the SelectionLayer because for some cases
        // like clearing a collection, the selection change is not propagated
        // the layer stack upwards as it gets stopped on layer conversion
        selectionLayer.addLayerListener(listener);

        // Column chooser
        DisplayColumnChooserCommandHandler columnChooserCommandHandler = new DisplayColumnChooserCommandHandler(
                selectionLayer, bodyLayer.getColumnHideShowLayer(), columnHeaderLayer.getColumnHeaderLayer(),
                columnHeaderLayer.getColumnHeaderDataLayer(), null, null);
        bodyLayer.registerCommandHandler(columnChooserCommandHandler);

        this.natTable.configure();

        return this.natTable;
    }

以下是registeryCheckBoxEditor&amp; editableGridConfiguration

private void registerCheckBoxEditor(IConfigRegistry configRegistry) {

        // make checkbox cells editable
        configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE,
                DisplayMode.EDIT, COLUMN_BOOKMARK_LABEL);

        configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(),
                DisplayMode.NORMAL, COLUMN_BOOKMARK_LABEL);

        final CheckBoxPainter checkBoxCellPainter = new CheckBoxPainter();

        configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, checkBoxCellPainter,
                DisplayMode.NORMAL, COLUMN_BOOKMARK_LABEL);

        configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER,
                new DefaultBooleanDisplayConverter(), DisplayMode.NORMAL, COLUMN_BOOKMARK_LABEL);
    }

public AbstractRegistryConfiguration editableGridConfiguration(
            final ColumnOverrideLabelAccumulator columnLabelAccumulator, final IDataProvider dataProvider) {

        return new AbstractRegistryConfiguration() {

            @Override
            public void configureRegistry(IConfigRegistry configRegistry) {

                columnLabelAccumulator.registerColumnOverrides(0, COLUMN_BOOKMARK_LABEL);

                registerCheckBoxEditor(configRegistry, new CheckBoxPainter(), new CheckBoxCellEditor());

                configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE,
                        IEditableRule.ALWAYS_EDITABLE, DisplayMode.EDIT, COLUMN_BOOKMARK_LABEL);
            }

        };
    }

我已完成与示例代码类似的所有设置,但触发鼠标事件无法正常工作。任何指针或帮助真的很感激。 TIA。

0 个答案:

没有答案