为什么我的natable无法编辑

时间:2016-07-13 07:12:48

标签: nattable

我使用以下代码创建一个natable并通过配置注册编辑功能。为什么这个表格的单元格无法编辑?

public class NattableFactory {

  public static Control createExampleControl(Composite parent) {
    ColumnGroupModel columnGroupModel = new ColumnGroupModel();
    ColumnHeaderLayer columnHeaderLayer;
    parent.setLayout(new GridLayout());

    // property names of the Person class
    String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday", "street", "status" };

    // mapping from property to label, needed for column header labels
    Map<String, String> propertyToLabelMap = new HashMap<String, String>();
    propertyToLabelMap.put("firstName", "Firstname");
    propertyToLabelMap.put("lastName", "Lastname");
    propertyToLabelMap.put("gender", "Gender");
    propertyToLabelMap.put("married", "Married");
    propertyToLabelMap.put("birthday", "Birthday");
    propertyToLabelMap.put("street", "Street");
    propertyToLabelMap.put("status", "Status");

    // Body
    DefaultBodyDataProvider<Person> bodyDataProvider = new DefaultBodyDataProvider<Person>(PersonService.getPersons(50),
        propertyNames);

    ColumnGroupBodyLayerStack bodyLayer = new ColumnGroupBodyLayerStack(new DataLayer(bodyDataProvider),
        columnGroupModel);

    // Column header

    DefaultColumnHeaderDataProvider defaultColumnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames,
        propertyToLabelMap);
    DefaultColumnHeaderDataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(
        defaultColumnHeaderDataProvider);
    columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayer, bodyLayer.getSelectionLayer());
    ColumnGroupHeaderLayer columnGroupHeaderLayer = new ColumnGroupHeaderLayer(columnHeaderLayer,
        bodyLayer.getSelectionLayer(), columnGroupModel);

    columnGroupHeaderLayer.addColumnsIndexesToGroup("name", 0, 1);
    columnGroupModel.getColumnGroupByIndex(0).setCollapseable(false);
    columnGroupHeaderLayer.setGroupUnbreakable(0);

    CompositeLayer compositeLayer = new CompositeLayer(1, 2);
    compositeLayer.setChildLayer(GridRegion.COLUMN_HEADER, columnGroupHeaderLayer, 0, 0);
    compositeLayer.setChildLayer(GridRegion.BODY, bodyLayer, 0, 1);

    // register column label accumulator
    final ColumnOverrideLabelAccumulator columnLabelAccumulator = new ColumnOverrideLabelAccumulator(bodyLayer);
    bodyLayer.setConfigLabelAccumulator(columnLabelAccumulator);
    columnLabelAccumulator.registerColumnOverrides(5, "street");
    columnLabelAccumulator.registerColumnOverrides(2, "gender");

    // Register create column group command handler

    // Register column chooser
    DisplayColumnChooserCommandHandler columnChooserCommandHandler = new DisplayColumnChooserCommandHandler(
        bodyLayer.getSelectionLayer(), bodyLayer.getColumnHideShowLayer(), columnHeaderLayer, columnHeaderDataLayer,
        columnGroupHeaderLayer, columnGroupModel);
    bodyLayer.registerCommandHandler(columnChooserCommandHandler);

    NatTable natTable = new NatTable(parent, compositeLayer, false);
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    natTable.addConfiguration(new EditorConfiguration());
    natTable.addConfiguration(new DefaultColumnHeaderStyleConfiguration());
    natTable.addConfiguration(new DefaultSelectionStyleConfiguration());
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    natTable.configure();
    return natTable;
  }

}

class EditorConfiguration extends AbstractRegistryConfiguration {

  @Override
  public void configureRegistry(IConfigRegistry configRegistry) {
    configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE);
    registerEditors(configRegistry);

  }

  private void registerEditors(IConfigRegistry configRegistry) {
    registerColumnStreetComboBox(configRegistry);
    registerColumnGenderIcon(configRegistry);
  }

  private static void registerColumnStreetComboBox(IConfigRegistry configRegistry) {
    // register a combobox editor for the street names
    ComboBoxCellEditor comboBoxCellEditor = new ComboBoxCellEditor(Arrays.asList(PersonService.getStreetNames()));
    configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, comboBoxCellEditor, DisplayMode.NORMAL,
        "street");
  }

  /**
   * The following will register a CheckBoxCellEditor with custom icons for the column that carries the gender
   * information. As a Gender is not a Boolean, there need to be a special converter registered. Note that such a
   * converter needs to create a Boolean display value and create the canonical value out of a Boolean value again.
   * 
   * To register a CheckBoxCellEditor, you need to
   * 
   * 
   * 
   * 1.Register the editor
   * 
   * 2.Register the painter corresponding to that editor
   * 
   * 3.Register the needed converter
   * 
   *
   * 
   * @param configRegistry
   */
  private void registerColumnGenderIcon(IConfigRegistry configRegistry) {
    // register a CheckBoxCellEditor for column four
    configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(),
        DisplayMode.NORMAL, "gender");

    // if you want to use the CheckBoxCellEditor, you should also consider
    // using the corresponding CheckBoxPainter to show the content like a
    // checkbox in your NatTable
    // in this case we use different icons to show how this works
    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER,
        new CheckBoxPainter(GUIHelper.getImage("arrow_up"), GUIHelper.getImage("arrow_down")), DisplayMode.NORMAL,
        "gender");

    // using a CheckBoxCellEditor also needs a Boolean conversion to work
    // correctly
    configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, getGenderBooleanConverter(),
        DisplayMode.NORMAL, "gender");
  }

  /**
   * @return Returns a simple converter for the gender of a Person. {@link Gender#MALE} will be interpreted as true
   *         while {@link Gender#FEMALE} will be interpreted as false
   */
  private IDisplayConverter getGenderBooleanConverter() {
    return new DisplayConverter() {

      @Override
      public Object canonicalToDisplayValue(Object canonicalValue) {
        if (canonicalValue instanceof Gender) {
          return ((Gender) canonicalValue) == Gender.MALE;
        }
        return null;
      }

      @Override
      public Object displayToCanonicalValue(Object displayValue) {
        Boolean displayBoolean = Boolean.valueOf(displayValue.toString());
        return displayBoolean ? Gender.MALE : Gender.FEMALE;
      }

    };
  }

}

你可以看到我通过“natTable.addConfiguration(new EditorConfiguration())注册编辑功能;”我的意思是第2列(性别)和第5列(街道)

1 个答案:

答案 0 :(得分:0)

在实例化下面添加以下2行,CompositeLayer将起作用。

compositeLayer.addConfiguration(new DefaultEditConfiguration());
compositeLayer.addConfiguration(new DefaultEditBindings());