Search in sources :

Example 1 with NullComparator

use of org.eclipse.nebula.widgets.nattable.config.NullComparator in project nebula.widgets.nattable by eclipse.

the class _602_GlazedListsSortingExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    // property names of the Person class
    String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday" };
    // mapping from property to label, needed for column header labels
    Map<String, String> propertyToLabelMap = new HashMap<>();
    propertyToLabelMap.put("firstName", "Firstname");
    propertyToLabelMap.put("lastName", "Lastname");
    propertyToLabelMap.put("gender", "Gender");
    propertyToLabelMap.put("married", "Married");
    propertyToLabelMap.put("birthday", "Birthday");
    // build the body layer stack
    // Usually you would create a new layer stack by extending
    // AbstractIndexLayerTransform and setting the ViewportLayer as
    // underlying layer. But in this case using the ViewportLayer
    // directly as body layer is also working.
    EventList<Person> persons = GlazedLists.eventList(PersonService.getPersons(10));
    SortedList<Person> sortedList = new SortedList<>(persons, null);
    IColumnPropertyAccessor<Person> accessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
    IDataProvider bodyDataProvider = new ListDataProvider<>(sortedList, accessor);
    DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
    GlazedListsEventLayer<Person> eventLayer = new GlazedListsEventLayer<>(bodyDataLayer, sortedList);
    ColumnReorderLayer columnReorderLayer = new ColumnReorderLayer(eventLayer);
    ColumnHideShowLayer columnHideShowLayer = new ColumnHideShowLayer(columnReorderLayer);
    SelectionLayer selectionLayer = new SelectionLayer(columnHideShowLayer);
    ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
    // build the column header layer
    IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
    DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
    ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, viewportLayer, selectionLayer);
    // add default column labels to the label stack
    // need to be done on the column header data layer, otherwise the label
    // stack does not contain the necessary labels at the time the
    // comparator is searched
    columnHeaderDataLayer.setConfigLabelAccumulator(new ColumnLabelAccumulator());
    ConfigRegistry configRegistry = new ConfigRegistry();
    // add the SortHeaderLayer to the column header layer stack
    // as we use GlazedLists, we use the GlazedListsSortModel which
    // delegates the sorting to the SortedList
    final SortHeaderLayer<Person> sortHeaderLayer = new SortHeaderLayer<>(columnHeaderLayer, new GlazedListsSortModel<>(sortedList, accessor, configRegistry, columnHeaderDataLayer));
    // build the row header layer
    IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
    DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
    ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, viewportLayer, selectionLayer);
    // build the corner layer
    IDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider);
    DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
    ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, sortHeaderLayer);
    // build the grid layer
    GridLayer gridLayer = new GridLayer(viewportLayer, sortHeaderLayer, rowHeaderLayer, cornerLayer);
    // turn the auto configuration off as we want to add our header menu
    // configuration
    NatTable natTable = new NatTable(parent, gridLayer, false);
    natTable.setConfigRegistry(configRegistry);
    // as the autoconfiguration of the NatTable is turned off, we have to
    // add the DefaultNatTableStyleConfiguration manually
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    // override the default sort configuration and change the mouse bindings
    // to sort on a single click
    natTable.addConfiguration(new SingleClickSortConfiguration());
    natTable.addConfiguration(new DebugMenuConfiguration(natTable));
    // add some custom sort configurations regarding comparators
    natTable.addConfiguration(new AbstractRegistryConfiguration() {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            // Register custom comparator for last name column
            // when sorting via last name, Simpson will always win
            configRegistry.registerConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, new Comparator<String>() {

                @Override
                public int compare(String o1, String o2) {
                    // check the sort order
                    boolean sortDesc = sortHeaderLayer.getSortModel().getSortDirection(1).equals(SortDirectionEnum.DESC);
                    if ("Simpson".equals(o1) && !"Simpson".equals(o2)) {
                        return sortDesc ? 1 : -1;
                    } else if (!"Simpson".equals(o1) && "Simpson".equals(o2)) {
                        return sortDesc ? -1 : 1;
                    }
                    return o1.compareToIgnoreCase(o2);
                }
            }, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 1);
            // Register null comparator to disable sorting for gender column
            configRegistry.registerConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, new NullComparator(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 2);
        }
    });
    natTable.configure();
    return natTable;
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) HashMap(java.util.HashMap) ColumnHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer) AbstractRegistryConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration) NullComparator(org.eclipse.nebula.widgets.nattable.config.NullComparator) SortedList(ca.odell.glazedlists.SortedList) ColumnHideShowLayer(org.eclipse.nebula.widgets.nattable.hideshow.ColumnHideShowLayer) DefaultCornerDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) ViewportLayer(org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer) GlazedListsEventLayer(org.eclipse.nebula.widgets.nattable.extension.glazedlists.GlazedListsEventLayer) NullComparator(org.eclipse.nebula.widgets.nattable.config.NullComparator) Comparator(java.util.Comparator) ConfigRegistry(org.eclipse.nebula.widgets.nattable.config.ConfigRegistry) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) DefaultColumnHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) DefaultRowHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) DebugMenuConfiguration(org.eclipse.nebula.widgets.nattable.ui.menu.DebugMenuConfiguration) ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) ColumnReorderLayer(org.eclipse.nebula.widgets.nattable.reorder.ColumnReorderLayer) SortHeaderLayer(org.eclipse.nebula.widgets.nattable.sort.SortHeaderLayer) DefaultColumnHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer) DefaultRowHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider) ReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor) RowHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer) CornerLayer(org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer) SelectionLayer(org.eclipse.nebula.widgets.nattable.selection.SelectionLayer) SingleClickSortConfiguration(org.eclipse.nebula.widgets.nattable.sort.config.SingleClickSortConfiguration) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) GridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer) DefaultColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider) ColumnLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.ColumnLabelAccumulator) Person(org.eclipse.nebula.widgets.nattable.dataset.person.Person)

Example 2 with NullComparator

use of org.eclipse.nebula.widgets.nattable.config.NullComparator in project nebula.widgets.nattable by eclipse.

the class NatTableBuilder method configureSorting.

protected void configureSorting() {
    ColumnOverrideLabelAccumulator columnHeaderLabelAccumulator = new ColumnOverrideLabelAccumulator(columnHeaderLayer.getDataLayer());
    columnHeaderLayer.addLabelAccumulator(columnHeaderLabelAccumulator);
    for (int colIndex = 0; colIndex < columns.length; colIndex++) {
        String columnHeaderLabel = COLUMN_HEADER_COLUMN_LABEL_PREFIX + colIndex;
        // Register an accumulator on the Column Header layer (since it
        // triggers sort)
        columnHeaderLabelAccumulator.registerColumnOverrides(colIndex, columnHeaderLabel);
        if (columns[colIndex].isSortable) {
            Comparator<?> comparator = columns[colIndex].comparator;
            configRegistry.registerConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, comparator, DisplayMode.NORMAL, columnHeaderLabel);
        } else {
            configRegistry.registerConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, new NullComparator(), DisplayMode.NORMAL, columnHeaderLabel);
        }
    }
}
Also used : NullComparator(org.eclipse.nebula.widgets.nattable.config.NullComparator) ColumnOverrideLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.ColumnOverrideLabelAccumulator)

Example 3 with NullComparator

use of org.eclipse.nebula.widgets.nattable.config.NullComparator in project nebula.widgets.nattable by eclipse.

the class SortableGridExample method getCustomComparatorConfiguration.

/**
 * NOTE: The labels for the the custom comparators must go on the
 * columnHeaderDataLayer - since, the SortHeaderLayer will resolve cell
 * labels with respect to its underlying layer i.e columnHeaderDataLayer
 */
private IConfiguration getCustomComparatorConfiguration(final AbstractLayer columnHeaderDataLayer) {
    return new AbstractRegistryConfiguration() {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            // Add label accumulator
            ColumnOverrideLabelAccumulator labelAccumulator = new ColumnOverrideLabelAccumulator(columnHeaderDataLayer);
            columnHeaderDataLayer.setConfigLabelAccumulator(labelAccumulator);
            // Register labels
            labelAccumulator.registerColumnOverrides(RowDataListFixture.getColumnIndexOfProperty(RowDataListFixture.RATING_PROP_NAME), CUSTOM_COMPARATOR_LABEL);
            labelAccumulator.registerColumnOverrides(RowDataListFixture.getColumnIndexOfProperty(RowDataListFixture.ASK_PRICE_PROP_NAME), NO_SORT_LABEL);
            // Register custom comparator
            configRegistry.registerConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, getCustomComparator(), DisplayMode.NORMAL, CUSTOM_COMPARATOR_LABEL);
            // Register null comparator to disable sort
            configRegistry.registerConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, new NullComparator(), DisplayMode.NORMAL, NO_SORT_LABEL);
        }
    };
}
Also used : AbstractRegistryConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration) NullComparator(org.eclipse.nebula.widgets.nattable.config.NullComparator) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) ColumnOverrideLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.ColumnOverrideLabelAccumulator)

Example 4 with NullComparator

use of org.eclipse.nebula.widgets.nattable.config.NullComparator in project nebula.widgets.nattable by eclipse.

the class NatColumnTableFormat method getColumnComparator.

@Override
public Comparator<?> getColumnComparator(final int col) {
    ILayerCell cell = this.columnHeaderDataLayer.getCellByPosition(col, 0);
    if (cell == null) {
        return null;
    }
    Comparator<?> comparator = this.configRegistry.getConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, cell.getDisplayMode(), cell.getConfigLabels().getLabels());
    return (comparator instanceof NullComparator) ? null : comparator;
}
Also used : NullComparator(org.eclipse.nebula.widgets.nattable.config.NullComparator) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)

Example 5 with NullComparator

use of org.eclipse.nebula.widgets.nattable.config.NullComparator in project nebula.widgets.nattable by eclipse.

the class HierarchicalWrapperSortModel method getColumnComparator.

@Override
public Comparator<?> getColumnComparator(int columnIndex) {
    ILayerCell cell = this.columnHeaderDataLayer.getCellByPosition(columnIndex, 0);
    if (cell == null) {
        return null;
    }
    Comparator<?> comparator = this.configRegistry.getConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, cell.getDisplayMode(), cell.getConfigLabels().getLabels());
    return (comparator instanceof NullComparator) ? null : comparator;
}
Also used : NullComparator(org.eclipse.nebula.widgets.nattable.config.NullComparator) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)

Aggregations

NullComparator (org.eclipse.nebula.widgets.nattable.config.NullComparator)6 AbstractRegistryConfiguration (org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration)2 IConfigRegistry (org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)2 DataLayer (org.eclipse.nebula.widgets.nattable.layer.DataLayer)2 ColumnOverrideLabelAccumulator (org.eclipse.nebula.widgets.nattable.layer.cell.ColumnOverrideLabelAccumulator)2 ILayerCell (org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)2 SortedList (ca.odell.glazedlists.SortedList)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)1 ConfigRegistry (org.eclipse.nebula.widgets.nattable.config.ConfigRegistry)1 DefaultNatTableStyleConfiguration (org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration)1 IDataProvider (org.eclipse.nebula.widgets.nattable.data.IDataProvider)1 ListDataProvider (org.eclipse.nebula.widgets.nattable.data.ListDataProvider)1 ReflectiveColumnPropertyAccessor (org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor)1 Person (org.eclipse.nebula.widgets.nattable.dataset.person.Person)1 GlazedListsEventLayer (org.eclipse.nebula.widgets.nattable.extension.glazedlists.GlazedListsEventLayer)1 DefaultColumnHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider)1 DefaultCornerDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider)1 DefaultRowHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider)1