use of org.eclipse.nebula.widgets.nattable.selection.RowSelectionProvider in project nebula.widgets.nattable by eclipse.
the class _801_VerticalCompositionWithFeaturesExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
ConfigRegistry configRegistry = new ConfigRegistry();
Composite panel = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 0;
panel.setLayout(layout);
GridDataFactory.fillDefaults().grab(true, true).applyTo(panel);
Composite gridPanel = new Composite(panel, SWT.NONE);
gridPanel.setLayout(layout);
GridDataFactory.fillDefaults().grab(true, true).applyTo(gridPanel);
Composite buttonPanel = new Composite(panel, SWT.NONE);
buttonPanel.setLayout(new GridLayout());
GridDataFactory.fillDefaults().grab(false, false).applyTo(buttonPanel);
// 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");
IColumnPropertyAccessor<Person> columnPropertyAccessor = new ExtendedReflectiveColumnPropertyAccessor<>(propertyNames);
List<Person> values = PersonService.getPersons(10);
final EventList<Person> eventList = GlazedLists.eventList(values);
TransformedList<Person, Person> rowObjectsGlazedList = GlazedLists.threadSafeList(eventList);
// use the SortedList constructor with 'null' for the Comparator because
// the Comparator will be set by configuration
SortedList<Person> sortedList = new SortedList<>(rowObjectsGlazedList, null);
// wrap the SortedList with the FilterList
FilterList<Person> filterList = new FilterList<>(sortedList);
IRowDataProvider<Person> bodyDataProvider = new ListDataProvider<>(filterList, columnPropertyAccessor);
final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
bodyDataLayer.setConfigLabelAccumulator(new ColumnLabelAccumulator());
GlazedListsEventLayer<Person> eventLayer = new GlazedListsEventLayer<>(bodyDataLayer, filterList);
final SelectionLayer selectionLayer = new SelectionLayer(eventLayer);
ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
DataLayer columnHeaderDataLayer = new DataLayer(columnHeaderDataProvider);
ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, viewportLayer, selectionLayer);
// add sorting
SortHeaderLayer<Person> sortHeaderLayer = new SortHeaderLayer<>(columnHeaderLayer, new GlazedListsSortModel<>(sortedList, columnPropertyAccessor, configRegistry, columnHeaderDataLayer), false);
// add the filter row functionality
final FilterRowHeaderComposite<Person> filterRowHeaderLayer = new FilterRowHeaderComposite<>(new DefaultGlazedListsFilterStrategy<>(filterList, columnPropertyAccessor, configRegistry), sortHeaderLayer, columnHeaderDataProvider, configRegistry);
// set the region labels to make default configurations work, e.g.
// editing, selection
CompositeLayer compositeLayer = new CompositeLayer(1, 2);
compositeLayer.setChildLayer(GridRegion.COLUMN_HEADER, filterRowHeaderLayer, 0, 0);
compositeLayer.setChildLayer(GridRegion.BODY, viewportLayer, 0, 1);
// add edit configurations
compositeLayer.addConfiguration(new DefaultEditConfiguration());
compositeLayer.addConfiguration(new DefaultEditBindings());
// add print support
compositeLayer.registerCommandHandler(new PrintCommandHandler(compositeLayer));
compositeLayer.addConfiguration(new DefaultPrintBindings());
// add excel export support
compositeLayer.registerCommandHandler(new ExportCommandHandler(compositeLayer));
compositeLayer.addConfiguration(new DefaultExportBindings());
final NatTable natTable = new NatTable(gridPanel, compositeLayer, false);
natTable.setConfigRegistry(configRegistry);
// adding this configuration adds the styles and the painters to use
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
// add sorting configuration
natTable.addConfiguration(new SingleClickSortConfiguration());
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE);
// birthday is never editable
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.NEVER_EDITABLE, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new ComboBoxCellEditor(Arrays.asList(Gender.FEMALE, Gender.MALE)), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 2);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, getGenderBooleanConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 2);
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new ComboBoxCellEditor(Arrays.asList(Boolean.TRUE, Boolean.FALSE)), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultBooleanDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
}
});
natTable.configure();
final RowSelectionProvider<Person> selectionProvider = new RowSelectionProvider<>(selectionLayer, bodyDataProvider, false);
GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
Button button = new Button(buttonPanel, SWT.PUSH);
button.setText("Remove selected item");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// the NatTable context
if (natTable.getActiveCellEditor() != null) {
natTable.commitAndCloseActiveCellEditor();
}
Person item = (Person) ((IStructuredSelection) selectionProvider.getSelection()).getFirstElement();
eventList.remove(item);
}
});
return panel;
}
use of org.eclipse.nebula.widgets.nattable.selection.RowSelectionProvider in project nebula.widgets.nattable by eclipse.
the class Get_and_set_selected_objects method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
Person homer = new Person("Homer", "Simpson", "Sargeant", 1234567890L);
Person smithers = new Person("Waylon", "Smithers", "Admiral", 6666666666L);
Person bart = new Person("Bart", "Smithers", "General", 9125798342L);
Person nelson = new Person("Nelson", "Muntz", "Private", 0000000001L);
Person frink = new Person("John", "Frink", "Lieutenant", 3141592654L);
List<Person> myList = new ArrayList<>();
myList.add(homer);
myList.add(smithers);
myList.add(bart);
myList.add(nelson);
myList.add(frink);
String[] propertyNames = { "firstName", "lastName", "rank", "serialNumber" };
IColumnPropertyAccessor<Person> columnPropertyAccessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
IRowDataProvider<Person> bodyDataProvider = new ListDataProvider<>(myList, columnPropertyAccessor);
DefaultGridLayer gridLayer = new DefaultGridLayer(bodyDataProvider, new DefaultColumnHeaderDataProvider(propertyNames));
NatTable natTable = new NatTable(parent, gridLayer);
ISelectionProvider selectionProvider = new RowSelectionProvider<>(gridLayer.getBodyLayer().getSelectionLayer(), bodyDataProvider, // Provides rows where any cell in the row is selected
false);
selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
System.out.println("Selection changed:");
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
@SuppressWarnings("rawtypes") Iterator it = selection.iterator();
while (it.hasNext()) {
System.out.println(" " + it.next());
}
}
});
// Programmatically select a few rows
selectionProvider.setSelection(new StructuredSelection(new Person[] { homer, smithers, nelson }));
// I changed my mind. Select a few other rows
selectionProvider.setSelection(new StructuredSelection(new Person[] { bart, frink }));
return natTable;
}
use of org.eclipse.nebula.widgets.nattable.selection.RowSelectionProvider in project nebula.widgets.nattable by eclipse.
the class _5054_SelectionProviderExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
Composite panel = new Composite(parent, SWT.NONE);
panel.setLayout(new GridLayout(2, true));
// property names of the Person class
String[] propertyNames = { "lastName", "firstName" };
// mapping from property to label, needed for column header labels
Map<String, String> propertyToLabelMap = new HashMap<>();
propertyToLabelMap.put("lastName", "Lastname");
propertyToLabelMap.put("firstName", "Firstname");
IColumnPropertyAccessor<Person> columnPropertyAccessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
IRowIdAccessor<Person> rowIdAccessor = new IRowIdAccessor<Person>() {
@Override
public Serializable getRowId(Person rowObject) {
return rowObject.getId();
}
};
// create the first table
// create the body layer stack
final IRowDataProvider<Person> firstBodyDataProvider = new ListDataProvider<>(getSimpsonsList(), columnPropertyAccessor);
final DataLayer firstBodyDataLayer = new DataLayer(firstBodyDataProvider);
final SelectionLayer firstSelectionLayer = new SelectionLayer(firstBodyDataLayer);
ViewportLayer firstViewportLayer = new ViewportLayer(firstSelectionLayer);
// use a RowSelectionModel that will perform row selections and is able
// to identify a row via unique ID
firstSelectionLayer.setSelectionModel(new RowSelectionModel<>(firstSelectionLayer, firstBodyDataProvider, rowIdAccessor));
// create the column header layer stack
IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
DataLayer firstColumnHeaderDataLayer = new DataLayer(columnHeaderDataProvider);
ColumnHeaderLayer firstColumnHeaderLayer = new ColumnHeaderLayer(firstColumnHeaderDataLayer, firstViewportLayer, firstSelectionLayer);
// register custom label styling to indicate if the table is active
firstColumnHeaderDataLayer.setConfigLabelAccumulator(new IConfigLabelAccumulator() {
@Override
public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
if (_5054_SelectionProviderExample.this.isFirstSelectionProvider) {
configLabels.addLabelOnTop(ACTIVE_LABEL);
}
}
});
// set the region labels to make default configurations work, e.g.
// selection
CompositeLayer firstCompositeLayer = new CompositeLayer(1, 2);
firstCompositeLayer.setChildLayer(GridRegion.COLUMN_HEADER, firstColumnHeaderLayer, 0, 0);
firstCompositeLayer.setChildLayer(GridRegion.BODY, firstViewportLayer, 0, 1);
final NatTable firstNatTable = new NatTable(panel, firstCompositeLayer, false);
firstNatTable.addConfiguration(new DefaultNatTableStyleConfiguration());
firstNatTable.addConfiguration(new ActiveTableStyleConfiguration());
firstNatTable.configure();
// set the modern theme
firstNatTable.setTheme(new ModernNatTableThemeConfiguration());
// add overlay painter for full borders
firstNatTable.addOverlayPainter(new NatTableBorderOverlayPainter());
// create the second table
// create the body layer stack
final IRowDataProvider<Person> secondBodyDataProvider = new ListDataProvider<>(getFlandersList(), columnPropertyAccessor);
final DataLayer secondBodyDataLayer = new DataLayer(secondBodyDataProvider);
final SelectionLayer secondSelectionLayer = new SelectionLayer(secondBodyDataLayer);
ViewportLayer secondViewportLayer = new ViewportLayer(secondSelectionLayer);
// use a RowSelectionModel that will perform row selections and is able
// to identify a row via unique ID
secondSelectionLayer.setSelectionModel(new RowSelectionModel<>(secondSelectionLayer, secondBodyDataProvider, rowIdAccessor));
// create the column header layer stack
DataLayer secondColumnHeaderDataLayer = new DataLayer(columnHeaderDataProvider);
ILayer secondColumnHeaderLayer = new ColumnHeaderLayer(secondColumnHeaderDataLayer, secondViewportLayer, secondSelectionLayer);
// register custom label styling to indicate if the table is active
secondColumnHeaderDataLayer.setConfigLabelAccumulator(new IConfigLabelAccumulator() {
@Override
public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
if (!_5054_SelectionProviderExample.this.isFirstSelectionProvider) {
configLabels.addLabelOnTop(ACTIVE_LABEL);
}
}
});
// set the region labels to make default configurations work, e.g.
// selection
CompositeLayer secondCompositeLayer = new CompositeLayer(1, 2);
secondCompositeLayer.setChildLayer(GridRegion.COLUMN_HEADER, secondColumnHeaderLayer, 0, 0);
secondCompositeLayer.setChildLayer(GridRegion.BODY, secondViewportLayer, 0, 1);
final NatTable secondNatTable = new NatTable(panel, secondCompositeLayer, false);
secondNatTable.addConfiguration(new DefaultNatTableStyleConfiguration());
secondNatTable.addConfiguration(new ActiveTableStyleConfiguration());
secondNatTable.configure();
// set the modern theme
secondNatTable.setTheme(new ModernNatTableThemeConfiguration());
// add overlay painter for full borders
secondNatTable.addOverlayPainter(new NatTableBorderOverlayPainter());
// set ISelectionProvider
final RowSelectionProvider<Person> selectionProvider = new RowSelectionProvider<>(firstSelectionLayer, firstBodyDataProvider);
// add a listener to the selection provider, in an Eclipse application
// you would do this e.g. getSite().getPage().addSelectionListener()
selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
log("Selection changed:");
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
@SuppressWarnings("rawtypes") Iterator it = selection.iterator();
while (it.hasNext()) {
Person selected = (Person) it.next();
log(" " + selected.getFirstName() + " " + selected.getLastName());
}
}
});
// layout widgets
GridDataFactory.fillDefaults().grab(true, true).applyTo(firstNatTable);
GridDataFactory.fillDefaults().grab(true, true).applyTo(secondNatTable);
// add a region for buttons
Composite buttonArea = new Composite(panel, SWT.NONE);
buttonArea.setLayout(new RowLayout());
GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(buttonArea);
// create a button to enable selection provider change
Button button = new Button(buttonArea, SWT.PUSH);
button.setText("Change selection provider");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
_5054_SelectionProviderExample.this.isFirstSelectionProvider = !_5054_SelectionProviderExample.this.isFirstSelectionProvider;
if (_5054_SelectionProviderExample.this.isFirstSelectionProvider) {
selectionProvider.updateSelectionProvider(firstSelectionLayer, firstBodyDataProvider);
} else {
selectionProvider.updateSelectionProvider(secondSelectionLayer, secondBodyDataProvider);
}
// refresh both tables to update the active rendering in the
// column header/ this is not necessary for updating the
// selection provider
firstNatTable.doCommand(new VisualRefreshCommand());
secondNatTable.doCommand(new VisualRefreshCommand());
}
});
// add a log area to the example to show the log entries
Text output = setupTextArea(panel);
GridDataFactory.fillDefaults().grab(true, true).span(2, 1).applyTo(output);
return panel;
}
use of org.eclipse.nebula.widgets.nattable.selection.RowSelectionProvider 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;
}
Aggregations