use of org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.DefaultGroupByThemeExtension in project nebula.widgets.nattable by eclipse.
the class _811_GroupBySummaryFixedSummaryRowExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout());
// create a new ConfigRegistry which will be needed for GlazedLists
// handling
final ConfigRegistry configRegistry = new ConfigRegistry();
// property names of the ExtendedPersonWithAddress class
String[] propertyNames = { "firstName", "lastName", "age", "money", "married", "gender", "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("age", "Age");
propertyToLabelMap.put("money", "Money");
propertyToLabelMap.put("married", "Married");
propertyToLabelMap.put("gender", "Gender");
propertyToLabelMap.put("birthday", "Birthday");
final IColumnPropertyAccessor<ExtendedPersonWithAddress> columnPropertyAccessor = new ExtendedReflectiveColumnPropertyAccessor<>(propertyNames);
// to enable the group by summary feature, the GroupByDataLayer needs to
// know the ConfigRegistry
List<ExtendedPersonWithAddress> persons = PersonService.getExtendedPersonsWithAddress(10);
final BodyLayerStack<ExtendedPersonWithAddress> bodyLayerStack = new BodyLayerStack<>(persons, columnPropertyAccessor, configRegistry);
bodyLayerStack.getBodyDataLayer().setConfigLabelAccumulator(new ColumnLabelAccumulator());
// build the column header layer
IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer());
// add sorting
SortHeaderLayer<ExtendedPersonWithAddress> sortHeaderLayer = new SortHeaderLayer<>(columnHeaderLayer, new GlazedListsSortModel<>(bodyLayerStack.getSortedList(), columnPropertyAccessor, configRegistry, columnHeaderDataLayer), false);
// connect sortModel to GroupByDataLayer to support sorting by group by
// summary values
bodyLayerStack.getBodyDataLayer().initializeTreeComparator(sortHeaderLayer.getSortModel(), bodyLayerStack.getTreeLayer(), true);
// build the row header layer
// Adding the specialized DefaultSummaryRowHeaderDataProvider to
// indicate the summary row in the row header
IDataProvider rowHeaderDataProvider = new DefaultSummaryRowHeaderDataProvider(bodyLayerStack.getBodyDataProvider(), "\u2211");
final DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer());
// 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(bodyLayerStack, sortHeaderLayer, rowHeaderLayer, cornerLayer, false);
FixedSummaryRowLayer summaryRowLayer = new FixedSummaryRowLayer(bodyLayerStack.getGlazedListsEventLayer(), gridLayer, configRegistry, false);
summaryRowLayer.setSummaryRowLabel("\u2211");
summaryRowLayer.setConfigLabelAccumulator(new AbstractOverrider() {
@Override
public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
if (columnPosition == 0) {
// our label is more important regarding styling than the
// summary row labels
configLabels.addLabelOnTop(ROW_HEADER_SUMMARY_ROW);
}
}
});
// ensure the body data layer uses a layer painter with correct
// configured clipping
bodyLayerStack.getBodyDataLayer().setLayerPainter(new GridLineCellLayerPainter(false, true));
// set the group by header on top of the grid
CompositeLayer compositeGridLayer = new CompositeLayer(1, 3);
final GroupByHeaderLayer groupByHeaderLayer = new GroupByHeaderLayer(bodyLayerStack.getGroupByModel(), gridLayer, columnHeaderDataProvider);
compositeGridLayer.setChildLayer(GroupByHeaderLayer.GROUP_BY_REGION, groupByHeaderLayer, 0, 0);
compositeGridLayer.setChildLayer("Grid", gridLayer, 0, 1);
compositeGridLayer.setChildLayer("Summary", summaryRowLayer, 0, 2);
// add the export configuration to the composite layer
compositeGridLayer.registerCommandHandler(new ExportCommandHandler(compositeGridLayer));
compositeGridLayer.addConfiguration(new IConfiguration() {
@Override
public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
uiBindingRegistry.registerKeyBinding(new KeyEventMatcher(SWT.MOD1, 'e'), new ExportAction());
}
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
PoiExcelExporter exporter = new HSSFExcelExporter();
exporter.setApplyBackgroundColor(false);
configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORTER, exporter);
configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORT_FORMATTER, new DefaultExportFormatter());
configRegistry.registerConfigAttribute(ExportConfigAttributes.DATE_FORMAT, // $NON-NLS-1$
"m/d/yy h:mm");
}
@Override
public void configureLayer(ILayer layer) {
}
});
// turn the auto configuration off as we want to add our header menu
// configuration
final NatTable natTable = new NatTable(container, compositeGridLayer, false);
// as the autoconfiguration of the NatTable is turned off, we have to
// add the DefaultNatTableStyleConfiguration and the ConfigRegistry
// manually
natTable.setConfigRegistry(configRegistry);
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
// add some additional styling
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
IStyle style = new Style();
style.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.RIGHT);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 2);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultDoubleDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
// the main styling of the summary row cell in the row header is
// done via summary row default style, but we need to override
// the alignment
style = new Style();
style.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.CENTER);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, ROW_HEADER_SUMMARY_ROW);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.SELECT, ROW_HEADER_SUMMARY_ROW);
}
});
// add sorting configuration
natTable.addConfiguration(new SingleClickSortConfiguration());
this.sumMoneyGroupBySummaryProvider = new SummationGroupBySummaryProvider<>(columnPropertyAccessor);
this.avgMoneyGroupBySummaryProvider = new AverageMoneyGroupBySummaryProvider();
// create a new IDataProvider that operates on the basic underlying list
// this is necessary because the IDataProvider in the body layer stack
// is operating on the TreeList, and on collapsing a node, the children
// will be not visible, which has effect on the summary value.
final IDataProvider summaryDataProvider = new ListDataProvider<>(bodyLayerStack.getSortedList(), columnPropertyAccessor);
this.sumMoneySummaryProvider = new SummationSummaryProvider(summaryDataProvider, false);
this.avgMoneySummaryProvider = new AverageMoneySummaryProvider(summaryDataProvider);
// add group by summary configuration
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
// GroupBy summary configuration
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _811_GroupBySummaryFixedSummaryRowExample.this.sumMoneyGroupBySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, new AverageAgeGroupBySummaryProvider(), DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 2);
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_CHILD_COUNT_PATTERN, "[{0}] - ({1})");
// SummaryRow configuration
configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, _811_GroupBySummaryFixedSummaryRowExample.this.sumMoneySummaryProvider, DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, new AverageAgeSummaryProvider(summaryDataProvider), DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 2);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new SummaryDisplayConverter(new DefaultDoubleDisplayConverter()), DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
}
});
// add group by header configuration
natTable.addConfiguration(new GroupByHeaderMenuConfiguration(natTable, groupByHeaderLayer));
natTable.addConfiguration(new AbstractHeaderMenuConfiguration(natTable) {
@Override
protected PopupMenuBuilder createColumnHeaderMenu(NatTable natTable) {
return super.createColumnHeaderMenu(natTable).withHideColumnMenuItem().withShowAllColumnsMenuItem().withStateManagerMenuItemProvider();
}
@Override
protected PopupMenuBuilder createCornerMenu(NatTable natTable) {
return super.createCornerMenu(natTable).withShowAllColumnsMenuItem().withStateManagerMenuItemProvider();
}
});
// adds the key bindings that allow space bar to be pressed to
// expand/collapse tree nodes
natTable.addConfiguration(new TreeLayerExpandCollapseKeyBindings(bodyLayerStack.getTreeLayer(), bodyLayerStack.getSelectionLayer()));
natTable.configure();
// set the modern theme to visualize the summary better
final ThemeConfiguration defaultTheme = new DefaultNatTableThemeConfiguration();
defaultTheme.addThemeExtension(new DefaultGroupByThemeExtension());
final ThemeConfiguration modernTheme = new ModernNatTableThemeConfiguration();
modernTheme.addThemeExtension(new ModernGroupByThemeExtension());
final ThemeConfiguration darkTheme = new DarkNatTableThemeConfiguration();
darkTheme.addThemeExtension(new DarkGroupByThemeExtension());
natTable.setTheme(modernTheme);
// add a border on every side of the table
natTable.addOverlayPainter(new NatTableBorderOverlayPainter());
natTable.registerCommandHandler(new DisplayPersistenceDialogCommandHandler(natTable));
GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
Composite buttonPanel = new Composite(container, SWT.NONE);
buttonPanel.setLayout(new RowLayout());
GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonPanel);
Button toggleHeaderButton = new Button(buttonPanel, SWT.PUSH);
toggleHeaderButton.setText("Toggle Group By Header");
toggleHeaderButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
groupByHeaderLayer.setVisible(!groupByHeaderLayer.isVisible());
}
});
Button collapseAllButton = new Button(buttonPanel, SWT.PUSH);
collapseAllButton.setText("Collapse All");
collapseAllButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(new TreeCollapseAllCommand());
}
});
Button expandAllButton = new Button(buttonPanel, SWT.PUSH);
expandAllButton.setText("Expand All");
expandAllButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(new TreeExpandAllCommand());
}
});
Button toggleMoneySummaryButton = new Button(buttonPanel, SWT.PUSH);
toggleMoneySummaryButton.setText("Toggle Money Group Summary (SUM/AVG)");
toggleMoneySummaryButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// clear the group by summary cache so the new summary
// calculation gets triggered
bodyLayerStack.getBodyDataLayer().clearCache();
_811_GroupBySummaryFixedSummaryRowExample.this.useMoneySum = !_811_GroupBySummaryFixedSummaryRowExample.this.useMoneySum;
if (_811_GroupBySummaryFixedSummaryRowExample.this.useMoneySum) {
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _811_GroupBySummaryFixedSummaryRowExample.this.sumMoneyGroupBySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, _811_GroupBySummaryFixedSummaryRowExample.this.sumMoneySummaryProvider, DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
} else {
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _811_GroupBySummaryFixedSummaryRowExample.this.avgMoneyGroupBySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, _811_GroupBySummaryFixedSummaryRowExample.this.avgMoneySummaryProvider, DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
}
natTable.doCommand(new VisualRefreshCommand());
}
});
Button toggleThemeButton = new Button(buttonPanel, SWT.PUSH);
toggleThemeButton.setText("Toggle Theme");
toggleThemeButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (_811_GroupBySummaryFixedSummaryRowExample.this.currentTheme == 0) {
natTable.setTheme(modernTheme);
_811_GroupBySummaryFixedSummaryRowExample.this.currentTheme++;
} else if (_811_GroupBySummaryFixedSummaryRowExample.this.currentTheme == 1) {
natTable.setTheme(darkTheme);
_811_GroupBySummaryFixedSummaryRowExample.this.currentTheme++;
} else if (_811_GroupBySummaryFixedSummaryRowExample.this.currentTheme == 2) {
natTable.setTheme(defaultTheme);
_811_GroupBySummaryFixedSummaryRowExample.this.currentTheme = 0;
}
}
});
Button button = new Button(buttonPanel, SWT.PUSH);
button.setText("Add Row");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
bodyLayerStack.getSortedList().add(PersonService.createExtendedPersonWithAddress(bodyLayerStack.getSortedList().size()));
}
});
return container;
}
use of org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.DefaultGroupByThemeExtension in project nebula.widgets.nattable by eclipse.
the class _814_EditableSortableGroupByWithFilterExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout());
// create a new ConfigRegistry which will be needed for GlazedLists
// handling
final ConfigRegistry configRegistry = new ConfigRegistry();
// property names of the ExtendedPersonWithAddress class
String[] propertyNames = { "firstName", "lastName", "age", "money", "married", "gender", "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("age", "Age");
propertyToLabelMap.put("money", "Money");
propertyToLabelMap.put("married", "Married");
propertyToLabelMap.put("gender", "Gender");
propertyToLabelMap.put("birthday", "Birthday");
final IColumnPropertyAccessor<ExtendedPersonWithAddress> columnPropertyAccessor = new ExtendedReflectiveColumnPropertyAccessor<>(propertyNames);
// to enable the group by summary feature, the GroupByDataLayer needs to
// know the ConfigRegistry
final BodyLayerStack<ExtendedPersonWithAddress> bodyLayerStack = new BodyLayerStack<>(PersonService.getExtendedPersonsWithAddress(20), columnPropertyAccessor, new IRowIdAccessor<ExtendedPersonWithAddress>() {
@Override
public Serializable getRowId(ExtendedPersonWithAddress rowObject) {
return rowObject.getId();
}
}, configRegistry, (rowObject, columnIndex) -> {
System.out.println("Save person with ID " + rowObject.getId() + " changing property " + columnPropertyAccessor.getColumnProperty(columnIndex) + " to " + columnPropertyAccessor.getDataValue(rowObject, columnIndex));
});
bodyLayerStack.getBodyDataLayer().setConfigLabelAccumulator(new ColumnLabelAccumulator());
// build the column header layer
IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer());
// add sorting
SortHeaderLayer<ExtendedPersonWithAddress> sortHeaderLayer = new SortHeaderLayer<>(columnHeaderLayer, new GlazedListsSortModel<>(bodyLayerStack.getSortedList(), columnPropertyAccessor, configRegistry, columnHeaderDataLayer), false);
// connect sortModel to GroupByDataLayer to support sorting by group by
// summary values
bodyLayerStack.getBodyDataLayer().initializeTreeComparator(sortHeaderLayer.getSortModel(), bodyLayerStack.getTreeLayer(), true);
// add the filter row functionality
final FilterRowHeaderComposite<ExtendedPersonWithAddress> filterRowHeaderLayer = new FilterRowHeaderComposite<>(new DefaultGlazedListsFilterStrategy<>(bodyLayerStack.getFilterList(), columnPropertyAccessor, configRegistry), sortHeaderLayer, columnHeaderDataLayer.getDataProvider(), configRegistry);
// build the row header layer
IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyLayerStack.getBodyDataProvider());
DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer());
// build the corner layer
IDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider);
DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, filterRowHeaderLayer);
// build the grid layer
GridLayer gridLayer = new GridLayer(bodyLayerStack, filterRowHeaderLayer, rowHeaderLayer, cornerLayer, false);
// set the group by header on top of the grid
CompositeLayer compositeGridLayer = new CompositeLayer(1, 2);
final GroupByHeaderLayer groupByHeaderLayer = new GroupByHeaderLayer(bodyLayerStack.getGroupByModel(), gridLayer, columnHeaderDataProvider);
compositeGridLayer.setChildLayer(GroupByHeaderLayer.GROUP_BY_REGION, groupByHeaderLayer, 0, 0);
compositeGridLayer.setChildLayer("Grid", gridLayer, 0, 1);
// add editing capability
compositeGridLayer.addConfiguration(new AbstractLayerConfiguration<AbstractLayer>() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE);
configRegistry.registerConfigAttribute(EditConfigAttributes.DATA_VALIDATOR, new DefaultDataValidator());
// register matching editors
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new TextCellEditor());
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 5);
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new DateCellEditor(), ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 6);
// register the correct converters
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultIntegerDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 2);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultDoubleDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultBooleanDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, 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;
}
}, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 5);
DateFormat formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
String pattern = ((SimpleDateFormat) formatter).toPattern();
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultDateDisplayConverter(pattern), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 6);
}
@Override
public void configureTypedLayer(AbstractLayer layer) {
layer.registerCommandHandler(new EditCellCommandHandler());
layer.registerEventHandler(new InlineCellEditEventHandler(layer));
}
});
compositeGridLayer.addConfiguration(new DefaultEditBindings());
// turn the auto configuration off as we want to add our header menu
// configuration
final NatTable natTable = new NatTable(container, compositeGridLayer, false);
// as the autoconfiguration of the NatTable is turned off, we have to
// add the DefaultNatTableStyleConfiguration and the ConfigRegistry
// manually
natTable.setConfigRegistry(configRegistry);
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
// add some additional styling
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(GUIHelper.getImage("arrow_up"), GUIHelper.getImage("arrow_down")), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 5);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultDoubleDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
}
});
// add sorting configuration
natTable.addConfiguration(new SingleClickSortConfiguration());
this.sumMoneySummaryProvider = new SummationGroupBySummaryProvider<>(columnPropertyAccessor);
this.avgMoneySummaryProvider = new AverageMoneyGroupBySummaryProvider();
// add group by summary configuration
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _814_EditableSortableGroupByWithFilterExample.this.sumMoneySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, new AverageAgeGroupBySummaryProvider(), DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 2);
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_CHILD_COUNT_PATTERN, "[{0}] - ({1})");
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_HINT, "Drag columns here");
Style hintStyle = new Style();
hintStyle.setAttributeValue(CellStyleAttributes.FONT, GUIHelper.getFont(new FontData("Arial", 10, SWT.ITALIC)));
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_HINT_STYLE, hintStyle);
}
});
// add group by header configuration
natTable.addConfiguration(new GroupByHeaderMenuConfiguration(natTable, groupByHeaderLayer));
natTable.addConfiguration(new AbstractHeaderMenuConfiguration(natTable) {
@Override
protected PopupMenuBuilder createColumnHeaderMenu(NatTable natTable) {
return super.createColumnHeaderMenu(natTable).withHideColumnMenuItem().withShowAllColumnsMenuItem().withStateManagerMenuItemProvider();
}
@Override
protected PopupMenuBuilder createCornerMenu(NatTable natTable) {
return super.createCornerMenu(natTable).withShowAllColumnsMenuItem().withStateManagerMenuItemProvider().withMenuItemProvider(new IMenuItemProvider() {
@Override
public void addMenuItem(NatTable natTable, Menu popupMenu) {
MenuItem export = new MenuItem(popupMenu, SWT.PUSH);
export.setText("Discard changes");
export.setEnabled(true);
export.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(new DiscardDataChangesCommand());
}
});
}
}).withMenuItemProvider(new IMenuItemProvider() {
@Override
public void addMenuItem(NatTable natTable, Menu popupMenu) {
MenuItem export = new MenuItem(popupMenu, SWT.PUSH);
export.setText("Save changes");
export.setEnabled(true);
export.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(new SaveDataChangesCommand());
}
});
}
});
}
});
natTable.configure();
// set the modern theme to visualize the summary better
final ThemeConfiguration defaultTheme = new DefaultNatTableThemeConfiguration();
defaultTheme.addThemeExtension(new DefaultGroupByThemeExtension());
final ThemeConfiguration modernTheme = new ModernNatTableThemeConfiguration();
modernTheme.addThemeExtension(new ModernGroupByThemeExtension());
final ThemeConfiguration darkTheme = new DarkNatTableThemeConfiguration();
darkTheme.addThemeExtension(new DarkGroupByThemeExtension());
natTable.setTheme(modernTheme);
natTable.registerCommandHandler(new DisplayPersistenceDialogCommandHandler(natTable));
GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
Composite buttonPanel = new Composite(container, SWT.NONE);
buttonPanel.setLayout(new RowLayout());
GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonPanel);
Button toggleHeaderButton = new Button(buttonPanel, SWT.PUSH);
toggleHeaderButton.setText("Toggle Group By Header");
toggleHeaderButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
groupByHeaderLayer.setVisible(!groupByHeaderLayer.isVisible());
}
});
Button toggleFilterButton = new Button(buttonPanel, SWT.PUSH);
toggleFilterButton.setText("Toggle Filter Row");
toggleFilterButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
filterRowHeaderLayer.setFilterRowVisible(!filterRowHeaderLayer.isFilterRowVisible());
}
});
Button collapseAllButton = new Button(buttonPanel, SWT.PUSH);
collapseAllButton.setText("Collapse All");
collapseAllButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(new TreeCollapseAllCommand());
}
});
Button expandAllButton = new Button(buttonPanel, SWT.PUSH);
expandAllButton.setText("Expand All");
expandAllButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(new TreeExpandAllCommand());
}
});
Button toggleMoneySummaryButton = new Button(buttonPanel, SWT.PUSH);
toggleMoneySummaryButton.setText("Toggle Money Group Summary (SUM/AVG)");
toggleMoneySummaryButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// clear the group by summary cache so the new summary
// calculation gets triggered
bodyLayerStack.getBodyDataLayer().clearCache();
_814_EditableSortableGroupByWithFilterExample.this.useMoneySum = !_814_EditableSortableGroupByWithFilterExample.this.useMoneySum;
if (_814_EditableSortableGroupByWithFilterExample.this.useMoneySum) {
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _814_EditableSortableGroupByWithFilterExample.this.sumMoneySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
} else {
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _814_EditableSortableGroupByWithFilterExample.this.avgMoneySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
}
natTable.doCommand(new VisualRefreshCommand());
}
});
// this button adds data to the grid
// try to group by last name, sort by last name desc and then add
// dynamic data for verification
Button addDynamicDataButton = new Button(buttonPanel, SWT.PUSH);
addDynamicDataButton.setText("Add Data");
addDynamicDataButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Address address = new Address();
address.setStreet("Some Street");
address.setHousenumber(42);
address.setPostalCode(12345);
address.setCity("In the clouds");
Person person = new Person(42, "Ralph", "Wiggum", Gender.MALE, false, new Date());
ExtendedPersonWithAddress entry = new ExtendedPersonWithAddress(person, address, "0000", "The little Ralphy", 0, new ArrayList<String>(), new ArrayList<String>());
bodyLayerStack.getEventList().add(entry);
person = new Person(42, "Clancy", "Wiggum", Gender.MALE, true, new Date());
entry = new ExtendedPersonWithAddress(person, address, "XXXL", "It is Chief Wiggum", 0, new ArrayList<String>(), new ArrayList<String>());
bodyLayerStack.getEventList().add(entry);
person = new Person(42, "Sarah", "Wiggum", Gender.FEMALE, true, new Date());
entry = new ExtendedPersonWithAddress(person, address, "mommy", "Little Ralphy's mother", 0, new ArrayList<String>(), new ArrayList<String>());
bodyLayerStack.getEventList().add(entry);
}
});
return container;
}
use of org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.DefaultGroupByThemeExtension in project nebula.widgets.nattable by eclipse.
the class _812_EditableGroupBySummarySummaryRowExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout());
// create a new ConfigRegistry which will be needed for GlazedLists
// handling
final ConfigRegistry configRegistry = new ConfigRegistry();
// property names of the ExtendedPersonWithAddress class
String[] propertyNames = { "firstName", "lastName", "age", "money", "married", "gender", "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("age", "Age");
propertyToLabelMap.put("money", "Money");
propertyToLabelMap.put("married", "Married");
propertyToLabelMap.put("gender", "Gender");
propertyToLabelMap.put("birthday", "Birthday");
final IColumnPropertyAccessor<ExtendedPersonWithAddress> columnPropertyAccessor = new ExtendedReflectiveColumnPropertyAccessor<>(propertyNames);
// to enable the group by summary feature, the GroupByDataLayer needs to
// know the ConfigRegistry
List<ExtendedPersonWithAddress> persons = PersonService.getExtendedPersonsWithAddress(10);
final BodyLayerStack<ExtendedPersonWithAddress> bodyLayerStack = new BodyLayerStack<>(persons, columnPropertyAccessor, configRegistry);
bodyLayerStack.getBodyDataLayer().setConfigLabelAccumulator(new ColumnLabelAccumulator());
// build the column header layer
IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer());
// add sorting
SortHeaderLayer<ExtendedPersonWithAddress> sortHeaderLayer = new SortHeaderLayer<>(columnHeaderLayer, new GlazedListsSortModel<>(bodyLayerStack.getSortedList(), columnPropertyAccessor, configRegistry, columnHeaderDataLayer), false);
// connect sortModel to GroupByDataLayer to support sorting by group by
// summary values
bodyLayerStack.getBodyDataLayer().initializeTreeComparator(sortHeaderLayer.getSortModel(), bodyLayerStack.getTreeLayer(), true);
// build the row header layer
// Adding the specialized DefaultSummaryRowHeaderDataProvider to
// indicate the summary row in the row header
IDataProvider rowHeaderDataProvider = new DefaultSummaryRowHeaderDataProvider(bodyLayerStack.getBodyDataProvider(), "\u2211");
final DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
// add a label to the row header summary row cell aswell, so it can be
// styled differently too
// in this case it will simply use the same styling as the summary row
// in the body
rowHeaderDataLayer.setConfigLabelAccumulator(new AbstractOverrider() {
@Override
public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
if ((rowPosition + 1) == rowHeaderDataLayer.getRowCount()) {
configLabels.addLabel(ROW_HEADER_SUMMARY_ROW);
configLabels.addLabel(SummaryRowLayer.DEFAULT_SUMMARY_ROW_CONFIG_LABEL);
}
}
});
ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer());
// 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(bodyLayerStack, sortHeaderLayer, rowHeaderLayer, cornerLayer, false);
// set the group by header on top of the grid
CompositeLayer compositeGridLayer = new CompositeLayer(1, 2);
final GroupByHeaderLayer groupByHeaderLayer = new GroupByHeaderLayer(bodyLayerStack.getGroupByModel(), gridLayer, columnHeaderDataProvider);
compositeGridLayer.setChildLayer(GroupByHeaderLayer.GROUP_BY_REGION, groupByHeaderLayer, 0, 0);
compositeGridLayer.setChildLayer("Grid", gridLayer, 0, 1);
// add editing capability
compositeGridLayer.addConfiguration(new AbstractLayerConfiguration<AbstractLayer>() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE);
configRegistry.registerConfigAttribute(EditConfigAttributes.DATA_VALIDATOR, new DefaultDataValidator());
// register matching editors
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new TextCellEditor());
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 5);
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new DateCellEditor(), ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 6);
// register the correct converters
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultIntegerDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 2);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultDoubleDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultBooleanDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, 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;
}
}, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 5);
DateFormat formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
String pattern = ((SimpleDateFormat) formatter).toPattern();
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultDateDisplayConverter(pattern), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 6);
}
@Override
public void configureTypedLayer(AbstractLayer layer) {
layer.registerCommandHandler(new EditCellCommandHandler());
layer.registerEventHandler(new InlineCellEditEventHandler(layer));
}
});
compositeGridLayer.addConfiguration(new DefaultEditBindings());
compositeGridLayer.addConfiguration(new DefaultExportBindings());
compositeGridLayer.addConfiguration(new DefaultPrintBindings());
// turn the auto configuration off as we want to add our header menu
// configuration
final NatTable natTable = new NatTable(container, compositeGridLayer, false);
// as the autoconfiguration of the NatTable is turned off, we have to
// add the DefaultNatTableStyleConfiguration and the ConfigRegistry
// manually
natTable.setConfigRegistry(configRegistry);
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
// add some additional styling
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(GUIHelper.getImage("arrow_up"), GUIHelper.getImage("arrow_down")), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 5);
IStyle style = new Style();
style.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.RIGHT);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 2);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
// the main styling of the summary row cell in the row header is
// done via summary row default style, but we need to override
// the alignment
style = new Style();
style.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.CENTER);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, ROW_HEADER_SUMMARY_ROW);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.SELECT, ROW_HEADER_SUMMARY_ROW);
}
});
// add sorting configuration
natTable.addConfiguration(new SingleClickSortConfiguration());
this.sumMoneyGroupBySummaryProvider = new SummationGroupBySummaryProvider<>(columnPropertyAccessor);
this.avgMoneyGroupBySummaryProvider = new AverageMoneyGroupBySummaryProvider();
// create a new IDataProvider that operates on the basic underlying list
// this is necessary because the IDataProvider in the body layer stack
// is operating on the TreeList, and on collapsing a node, the children
// will be not visible, which has effect on the summary value.
final IDataProvider summaryDataProvider = new ListDataProvider<>(bodyLayerStack.getSortedList(), columnPropertyAccessor);
this.sumMoneySummaryProvider = new SummationSummaryProvider(summaryDataProvider, false);
this.avgMoneySummaryProvider = new AverageMoneySummaryProvider(summaryDataProvider);
// add group by summary configuration
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
// GroupBy summary configuration
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _812_EditableGroupBySummarySummaryRowExample.this.sumMoneyGroupBySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, new AverageAgeGroupBySummaryProvider(), DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 2);
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_CHILD_COUNT_PATTERN, "[{0}] - ({1})");
// SummaryRow configuration
configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, _812_EditableGroupBySummarySummaryRowExample.this.sumMoneySummaryProvider, DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, new AverageAgeSummaryProvider(summaryDataProvider), DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 2);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new SummaryDisplayConverter(new DefaultDoubleDisplayConverter()), DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
}
});
// add group by header configuration
natTable.addConfiguration(new GroupByHeaderMenuConfiguration(natTable, groupByHeaderLayer));
natTable.addConfiguration(new AbstractHeaderMenuConfiguration(natTable) {
@Override
protected PopupMenuBuilder createColumnHeaderMenu(NatTable natTable) {
return super.createColumnHeaderMenu(natTable).withHideColumnMenuItem().withShowAllColumnsMenuItem().withStateManagerMenuItemProvider();
}
@Override
protected PopupMenuBuilder createCornerMenu(NatTable natTable) {
return super.createCornerMenu(natTable).withShowAllColumnsMenuItem().withStateManagerMenuItemProvider();
}
});
// adds the key bindings that allow space bar to be pressed to
// expand/collapse tree nodes
natTable.addConfiguration(new TreeLayerExpandCollapseKeyBindings(bodyLayerStack.getTreeLayer(), bodyLayerStack.getSelectionLayer()));
natTable.addConfiguration(new TreeDebugMenuConfiguration(natTable));
natTable.configure();
// set the modern theme to visualize the summary better
final ThemeConfiguration defaultTheme = new DefaultNatTableThemeConfiguration();
defaultTheme.addThemeExtension(new DefaultGroupByThemeExtension());
final ThemeConfiguration modernTheme = new ModernNatTableThemeConfiguration();
modernTheme.addThemeExtension(new ModernGroupByThemeExtension());
final ThemeConfiguration darkTheme = new DarkNatTableThemeConfiguration();
darkTheme.addThemeExtension(new DarkGroupByThemeExtension());
natTable.setTheme(modernTheme);
// add a border on every side of the table
natTable.addOverlayPainter(new NatTableBorderOverlayPainter());
natTable.registerCommandHandler(new DisplayPersistenceDialogCommandHandler(natTable));
GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
Composite buttonPanel = new Composite(container, SWT.NONE);
buttonPanel.setLayout(new RowLayout());
GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonPanel);
Button toggleHeaderButton = new Button(buttonPanel, SWT.PUSH);
toggleHeaderButton.setText("Toggle Group By Header");
toggleHeaderButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
groupByHeaderLayer.setVisible(!groupByHeaderLayer.isVisible());
}
});
Button collapseAllButton = new Button(buttonPanel, SWT.PUSH);
collapseAllButton.setText("Collapse All");
collapseAllButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(new TreeCollapseAllCommand());
}
});
Button expandAllButton = new Button(buttonPanel, SWT.PUSH);
expandAllButton.setText("Expand All");
expandAllButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(new TreeExpandAllCommand());
}
});
Button toggleMoneySummaryButton = new Button(buttonPanel, SWT.PUSH);
toggleMoneySummaryButton.setText("Toggle Money Group Summary (SUM/AVG)");
toggleMoneySummaryButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// clear the group by summary cache so the new summary
// calculation gets triggered
bodyLayerStack.getBodyDataLayer().clearCache();
_812_EditableGroupBySummarySummaryRowExample.this.useMoneySum = !_812_EditableGroupBySummarySummaryRowExample.this.useMoneySum;
if (_812_EditableGroupBySummarySummaryRowExample.this.useMoneySum) {
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _812_EditableGroupBySummarySummaryRowExample.this.sumMoneyGroupBySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, _812_EditableGroupBySummarySummaryRowExample.this.sumMoneySummaryProvider, DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
} else {
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _812_EditableGroupBySummarySummaryRowExample.this.avgMoneyGroupBySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, _812_EditableGroupBySummarySummaryRowExample.this.avgMoneySummaryProvider, DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
}
natTable.doCommand(new VisualRefreshCommand());
}
});
Button toggleThemeButton = new Button(buttonPanel, SWT.PUSH);
toggleThemeButton.setText("Toggle Theme");
toggleThemeButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (_812_EditableGroupBySummarySummaryRowExample.this.currentTheme == 0) {
natTable.setTheme(modernTheme);
_812_EditableGroupBySummarySummaryRowExample.this.currentTheme++;
} else if (_812_EditableGroupBySummarySummaryRowExample.this.currentTheme == 1) {
natTable.setTheme(darkTheme);
_812_EditableGroupBySummarySummaryRowExample.this.currentTheme++;
} else if (_812_EditableGroupBySummarySummaryRowExample.this.currentTheme == 2) {
natTable.setTheme(defaultTheme);
_812_EditableGroupBySummarySummaryRowExample.this.currentTheme = 0;
}
}
});
Button button = new Button(buttonPanel, SWT.PUSH);
button.setText("Add Row");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
bodyLayerStack.getSortedList().add(PersonService.createExtendedPersonWithAddress(bodyLayerStack.getSortedList().size()));
}
});
@SuppressWarnings("unchecked") final RowSelectionProvider<ExtendedPersonWithAddress> provider = new RowSelectionProvider<>(bodyLayerStack.selectionLayer, (IRowDataProvider<ExtendedPersonWithAddress>) bodyLayerStack.bodyDataProvider);
Button updateButton = new Button(buttonPanel, SWT.PUSH);
updateButton.setText("Update Row");
updateButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
IStructuredSelection selection = (IStructuredSelection) provider.getSelection();
@SuppressWarnings("rawtypes") Iterator it = selection.iterator();
while (it.hasNext()) {
ExtendedPersonWithAddress selected = (ExtendedPersonWithAddress) it.next();
int index = bodyLayerStack.getSortedList().indexOf(selected);
selected.setLastName("Simpson");
bodyLayerStack.getSortedList().set(index, selected);
}
}
});
return container;
}
use of org.eclipse.nebula.widgets.nattable.extension.glazedlists.groupBy.DefaultGroupByThemeExtension in project nebula.widgets.nattable by eclipse.
the class _809_GroupBySummarySummaryRowExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout());
// create a new ConfigRegistry which will be needed for GlazedLists
// handling
final ConfigRegistry configRegistry = new ConfigRegistry();
// property names of the ExtendedPersonWithAddress class
String[] propertyNames = { "firstName", "lastName", "age", "money", "married", "gender", "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("age", "Age");
propertyToLabelMap.put("money", "Money");
propertyToLabelMap.put("married", "Married");
propertyToLabelMap.put("gender", "Gender");
propertyToLabelMap.put("birthday", "Birthday");
final IColumnPropertyAccessor<ExtendedPersonWithAddress> columnPropertyAccessor = new ExtendedReflectiveColumnPropertyAccessor<>(propertyNames);
// to enable the group by summary feature, the GroupByDataLayer needs to
// know the ConfigRegistry
List<ExtendedPersonWithAddress> persons = PersonService.getExtendedPersonsWithAddress(10000);
final BodyLayerStack<ExtendedPersonWithAddress> bodyLayerStack = new BodyLayerStack<>(persons, columnPropertyAccessor, configRegistry);
bodyLayerStack.getBodyDataLayer().setConfigLabelAccumulator(new ColumnLabelAccumulator());
// build the column header layer
IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer());
// add sorting
SortHeaderLayer<ExtendedPersonWithAddress> sortHeaderLayer = new SortHeaderLayer<>(columnHeaderLayer, new GlazedListsSortModel<>(bodyLayerStack.getSortedList(), columnPropertyAccessor, configRegistry, columnHeaderDataLayer), false);
// connect sortModel to GroupByDataLayer to support sorting by group by
// summary values
bodyLayerStack.getBodyDataLayer().initializeTreeComparator(sortHeaderLayer.getSortModel(), bodyLayerStack.getTreeLayer(), true);
// build the row header layer
// Adding the specialized DefaultSummaryRowHeaderDataProvider to
// indicate the summary row in the row header
IDataProvider rowHeaderDataProvider = new DefaultSummaryRowHeaderDataProvider(bodyLayerStack.getBodyDataProvider(), "\u2211");
final DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
// add a label to the row header summary row cell aswell, so it can be
// styled differently too
// in this case it will simply use the same styling as the summary row
// in the body
rowHeaderDataLayer.setConfigLabelAccumulator(new AbstractOverrider() {
@Override
public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
if ((rowPosition + 1) == rowHeaderDataLayer.getRowCount()) {
configLabels.addLabel(ROW_HEADER_SUMMARY_ROW);
configLabels.addLabel(SummaryRowLayer.DEFAULT_SUMMARY_ROW_CONFIG_LABEL);
}
}
});
ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer());
// 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(bodyLayerStack, sortHeaderLayer, rowHeaderLayer, cornerLayer, false);
// set the group by header on top of the grid
CompositeLayer compositeGridLayer = new CompositeLayer(1, 2);
final GroupByHeaderLayer groupByHeaderLayer = new GroupByHeaderLayer(bodyLayerStack.getGroupByModel(), gridLayer, columnHeaderDataProvider);
compositeGridLayer.setChildLayer(GroupByHeaderLayer.GROUP_BY_REGION, groupByHeaderLayer, 0, 0);
compositeGridLayer.setChildLayer("Grid", gridLayer, 0, 1);
// turn the auto configuration off as we want to add our header menu
// configuration
final NatTable natTable = new NatTable(container, compositeGridLayer, false);
// as the autoconfiguration of the NatTable is turned off, we have to
// add the DefaultNatTableStyleConfiguration and the ConfigRegistry
// manually
natTable.setConfigRegistry(configRegistry);
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
// add some additional styling
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
IStyle style = new Style();
style.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.RIGHT);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 2);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultDoubleDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
// the main styling of the summary row cell in the row header is
// done via summary row default style, but we need to override
// the alignment
style = new Style();
style.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.CENTER);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, ROW_HEADER_SUMMARY_ROW);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.SELECT, ROW_HEADER_SUMMARY_ROW);
}
});
// add sorting configuration
natTable.addConfiguration(new SingleClickSortConfiguration());
this.sumMoneyGroupBySummaryProvider = new SummationGroupBySummaryProvider<>(columnPropertyAccessor);
this.avgMoneyGroupBySummaryProvider = new AverageMoneyGroupBySummaryProvider();
// create a new IDataProvider that operates on the basic underlying list
// this is necessary because the IDataProvider in the body layer stack
// is operating on the TreeList, and on collapsing a node, the children
// will be not visible, which has effect on the summary value.
final IDataProvider summaryDataProvider = new ListDataProvider<>(bodyLayerStack.getSortedList(), columnPropertyAccessor);
this.sumMoneySummaryProvider = new SummationSummaryProvider(summaryDataProvider, false);
this.avgMoneySummaryProvider = new AverageMoneySummaryProvider(summaryDataProvider);
// add group by summary configuration
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
// GroupBy summary configuration
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _809_GroupBySummarySummaryRowExample.this.sumMoneyGroupBySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, new AverageAgeGroupBySummaryProvider(), DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 2);
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_CHILD_COUNT_PATTERN, "[{0}] - ({1})");
// SummaryRow configuration
configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, _809_GroupBySummarySummaryRowExample.this.sumMoneySummaryProvider, DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, new AverageAgeSummaryProvider(summaryDataProvider), DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 2);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new SummaryDisplayConverter(new DefaultDoubleDisplayConverter()), DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
}
});
// add group by header configuration
natTable.addConfiguration(new GroupByHeaderMenuConfiguration(natTable, groupByHeaderLayer));
natTable.addConfiguration(new AbstractHeaderMenuConfiguration(natTable) {
@Override
protected PopupMenuBuilder createColumnHeaderMenu(NatTable natTable) {
return super.createColumnHeaderMenu(natTable).withHideColumnMenuItem().withShowAllColumnsMenuItem().withStateManagerMenuItemProvider();
}
@Override
protected PopupMenuBuilder createCornerMenu(NatTable natTable) {
return super.createCornerMenu(natTable).withShowAllColumnsMenuItem().withStateManagerMenuItemProvider();
}
});
// adds the key bindings that allow space bar to be pressed to
// expand/collapse tree nodes
natTable.addConfiguration(new TreeLayerExpandCollapseKeyBindings(bodyLayerStack.getTreeLayer(), bodyLayerStack.getSelectionLayer()));
natTable.addConfiguration(new TreeDebugMenuConfiguration(natTable));
natTable.configure();
// set the modern theme to visualize the summary better
final ThemeConfiguration defaultTheme = new DefaultNatTableThemeConfiguration();
defaultTheme.addThemeExtension(new DefaultGroupByThemeExtension());
final ThemeConfiguration modernTheme = new ModernNatTableThemeConfiguration();
modernTheme.addThemeExtension(new ModernGroupByThemeExtension());
final ThemeConfiguration darkTheme = new DarkNatTableThemeConfiguration();
darkTheme.addThemeExtension(new DarkGroupByThemeExtension());
natTable.setTheme(modernTheme);
// add a border on every side of the table
natTable.addOverlayPainter(new NatTableBorderOverlayPainter());
natTable.registerCommandHandler(new DisplayPersistenceDialogCommandHandler(natTable));
GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
Composite buttonPanel = new Composite(container, SWT.NONE);
buttonPanel.setLayout(new RowLayout());
GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonPanel);
Button toggleHeaderButton = new Button(buttonPanel, SWT.PUSH);
toggleHeaderButton.setText("Toggle Group By Header");
toggleHeaderButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
groupByHeaderLayer.setVisible(!groupByHeaderLayer.isVisible());
}
});
Button collapseAllButton = new Button(buttonPanel, SWT.PUSH);
collapseAllButton.setText("Collapse All");
collapseAllButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(new TreeCollapseAllCommand());
}
});
Button expandAllButton = new Button(buttonPanel, SWT.PUSH);
expandAllButton.setText("Expand All");
expandAllButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(new TreeExpandAllCommand());
}
});
Button toggleMoneySummaryButton = new Button(buttonPanel, SWT.PUSH);
toggleMoneySummaryButton.setText("Toggle Money Group Summary (SUM/AVG)");
toggleMoneySummaryButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// clear the group by summary cache so the new summary
// calculation gets triggered
bodyLayerStack.getBodyDataLayer().clearCache();
_809_GroupBySummarySummaryRowExample.this.useMoneySum = !_809_GroupBySummarySummaryRowExample.this.useMoneySum;
if (_809_GroupBySummarySummaryRowExample.this.useMoneySum) {
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _809_GroupBySummarySummaryRowExample.this.sumMoneyGroupBySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, _809_GroupBySummarySummaryRowExample.this.sumMoneySummaryProvider, DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
} else {
configRegistry.registerConfigAttribute(GroupByConfigAttributes.GROUP_BY_SUMMARY_PROVIDER, _809_GroupBySummarySummaryRowExample.this.avgMoneyGroupBySummaryProvider, DisplayMode.NORMAL, GroupByDataLayer.GROUP_BY_COLUMN_PREFIX + 3);
configRegistry.registerConfigAttribute(SummaryRowConfigAttributes.SUMMARY_PROVIDER, _809_GroupBySummarySummaryRowExample.this.avgMoneySummaryProvider, DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 3);
}
natTable.doCommand(new VisualRefreshCommand());
}
});
Button toggleThemeButton = new Button(buttonPanel, SWT.PUSH);
toggleThemeButton.setText("Toggle Theme");
toggleThemeButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (_809_GroupBySummarySummaryRowExample.this.currentTheme == 0) {
natTable.setTheme(modernTheme);
_809_GroupBySummarySummaryRowExample.this.currentTheme++;
} else if (_809_GroupBySummarySummaryRowExample.this.currentTheme == 1) {
natTable.setTheme(darkTheme);
_809_GroupBySummarySummaryRowExample.this.currentTheme++;
} else if (_809_GroupBySummarySummaryRowExample.this.currentTheme == 2) {
natTable.setTheme(defaultTheme);
_809_GroupBySummarySummaryRowExample.this.currentTheme = 0;
}
}
});
Button button = new Button(buttonPanel, SWT.PUSH);
button.setText("Add Row");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
bodyLayerStack.getSortedList().add(PersonService.createExtendedPersonWithAddress(bodyLayerStack.getSortedList().size()));
}
});
Button change = new Button(buttonPanel, SWT.PUSH);
change.setText("Change Content");
change.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
bodyLayerStack.getSortedList().getReadWriteLock().writeLock().lock();
try {
// deactivate
bodyLayerStack.getGlazedListsEventLayer().deactivate();
// clear
bodyLayerStack.getSortedList().clear();
// addall
bodyLayerStack.getSortedList().addAll(PersonService.getExtendedPersonsWithAddress(1000));
} finally {
bodyLayerStack.getSortedList().getReadWriteLock().writeLock().unlock();
// activate
bodyLayerStack.getGlazedListsEventLayer().activate();
}
}
});
return container;
}
Aggregations