use of org.eclipse.nebula.widgets.nattable.data.IDataProvider in project nebula.widgets.nattable by eclipse.
the class _5052_RowSelectionExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
// property names of the Person class
String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday" };
// mapping from property to label, needed for column header labels
Map<String, String> propertyToLabelMap = new HashMap<>();
propertyToLabelMap.put("firstName", "Firstname");
propertyToLabelMap.put("lastName", "Lastname");
propertyToLabelMap.put("gender", "Gender");
propertyToLabelMap.put("married", "Married");
propertyToLabelMap.put("birthday", "Birthday");
IColumnPropertyAccessor<Person> columnPropertyAccessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
final List<Person> data = PersonService.getPersons(10);
// create the body layer stack
IRowDataProvider<Person> bodyDataProvider = new ListDataProvider<>(data, columnPropertyAccessor);
final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
// create a SelectionLayer without using the default configuration
// this enables us to add the row selection configuration cleanly
// afterwards
final SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer, false);
ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
// use a RowSelectionModel that will perform row selections and is able
// to identify a row via unique ID
selectionLayer.setSelectionModel(new RowSelectionModel<>(selectionLayer, bodyDataProvider, new IRowIdAccessor<Person>() {
@Override
public Serializable getRowId(Person rowObject) {
return rowObject.getId();
}
}));
// register the DefaultRowSelectionLayerConfiguration that contains the
// default styling and functionality bindings (search, tick update)
// and different configurations for a move command handler that always
// moves by a row and row only selection bindings
selectionLayer.addConfiguration(new DefaultRowSelectionLayerConfiguration());
// create the column header layer stack
IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
ILayer columnHeaderLayer = new ColumnHeaderLayer(new DataLayer(columnHeaderDataProvider), viewportLayer, selectionLayer);
// create the row header layer stack
IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
ILayer rowHeaderLayer = new RowHeaderLayer(new DefaultRowHeaderDataLayer(new DefaultRowHeaderDataProvider(bodyDataProvider)), viewportLayer, selectionLayer);
// create the corner layer stack
ILayer cornerLayer = new CornerLayer(new DataLayer(new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider)), rowHeaderLayer, columnHeaderLayer);
// create the grid layer composed with the prior created layer stacks
GridLayer gridLayer = new GridLayer(viewportLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer);
return new NatTable(parent, gridLayer);
}
use of org.eclipse.nebula.widgets.nattable.data.IDataProvider 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.data.IDataProvider in project nebula.widgets.nattable by eclipse.
the class _5061_SimpleHoverStylingExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
// property names of the Person class
String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday" };
// build the body layer stack
// Usually you would create a new layer stack by extending
// AbstractIndexLayerTransform and setting the ViewportLayer as
// underlying layer. But in this case using the ViewportLayer directly
// as body layer is also working.
IDataProvider bodyDataProvider = new DefaultBodyDataProvider<>(PersonService.getPersons(10), propertyNames);
DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
HoverLayer hoverLayer = new HoverLayer(bodyDataLayer, false);
// we need to ensure that the hover styling is removed when the mouse
// cursor moves out of the cell area
hoverLayer.addConfiguration(new SimpleHoverStylingBindings(hoverLayer));
SelectionLayer selectionLayer = new SelectionLayer(hoverLayer);
ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
viewportLayer.setRegionName(GridRegion.BODY);
// turn the auto configuration off as we want to add our hover styling
// configuration
NatTable natTable = new NatTable(parent, viewportLayer, false);
// as the autoconfiguration of the NatTable is turned off, we have to
// add the DefaultNatTableStyleConfiguration manually
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
// add the style configuration for hover
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
// style that is applied when cells are hovered
Style style = new Style();
style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.COLOR_YELLOW);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.HOVER);
// style that is applied when selected cells are hovered
style = new Style();
style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.COLOR_GREEN);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.SELECT_HOVER);
}
});
natTable.configure();
return natTable;
}
use of org.eclipse.nebula.widgets.nattable.data.IDataProvider in project nebula.widgets.nattable by eclipse.
the class _304_DynamicColumnExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
// start with 3 columns
this.columns.add("Column_0");
this.columns.add("Column_1");
this.columns.add("Column_2");
this.values.add(createValueRow("Homer"));
this.values.add(createValueRow("Marge"));
this.values.add(createValueRow("Bart"));
this.values.add(createValueRow("Lisa"));
this.values.add(createValueRow("Maggie"));
Composite panel = new Composite(parent, SWT.NONE);
panel.setLayout(new GridLayout());
GridDataFactory.fillDefaults().grab(true, true).applyTo(panel);
Composite gridPanel = new Composite(panel, SWT.NONE);
gridPanel.setLayout(new GridLayout());
GridDataFactory.fillDefaults().grab(true, true).applyTo(gridPanel);
Composite buttonPanel = new Composite(panel, SWT.NONE);
buttonPanel.setLayout(new GridLayout());
GridDataFactory.fillDefaults().grab(true, true).applyTo(buttonPanel);
ConfigRegistry configRegistry = new ConfigRegistry();
// create the body layer stack
IDataProvider bodyDataProvider = new ListDataProvider<>(this.values, new MyColumnPropertyAccessor());
final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
DefaultBodyLayerStack bodyLayerStack = new DefaultBodyLayerStack(bodyDataLayer);
// create the column header layer stack
IDataProvider columnHeaderDataProvider = new SimpleColumnHeaderDataProvider();
ILayer columnHeaderLayer = new ColumnHeaderLayer(new DataLayer(columnHeaderDataProvider), bodyLayerStack.getViewportLayer(), bodyLayerStack.getSelectionLayer());
// create the row header layer stack
IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
ILayer rowHeaderLayer = new RowHeaderLayer(new DefaultRowHeaderDataLayer(new DefaultRowHeaderDataProvider(bodyDataProvider)), bodyLayerStack.getViewportLayer(), bodyLayerStack.getSelectionLayer());
// create the corner layer stack
ILayer cornerLayer = new CornerLayer(new DataLayer(new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider)), rowHeaderLayer, columnHeaderLayer);
// create the grid layer composed with the prior created layer stacks
GridLayer gridLayer = new GridLayer(bodyLayerStack, columnHeaderLayer, rowHeaderLayer, cornerLayer);
final NatTable natTable = new NatTable(gridPanel, gridLayer, false);
natTable.setConfigRegistry(configRegistry);
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
natTable.addConfiguration(new HeaderMenuConfiguration(natTable));
natTable.addConfiguration(new SingleClickSortConfiguration());
natTable.configure();
GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
Button addColumnButton = new Button(buttonPanel, SWT.PUSH);
addColumnButton.setText("Add Column");
addColumnButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String newColumn = "Column_" + _304_DynamicColumnExample.this.columns.size();
_304_DynamicColumnExample.this.columns.add(newColumn);
for (Map<String, String> value : _304_DynamicColumnExample.this.values) {
String prefix = value.get("Column_0");
prefix = prefix.substring(0, prefix.indexOf("_"));
value.put(newColumn, prefix + "_" + (_304_DynamicColumnExample.this.columns.size() - 1));
}
bodyDataLayer.fireLayerEvent(new ColumnInsertEvent(bodyDataLayer, _304_DynamicColumnExample.this.columns.size() - 1));
}
});
return panel;
}
use of org.eclipse.nebula.widgets.nattable.data.IDataProvider in project nebula.widgets.nattable by eclipse.
the class _308_DataModificationExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
// set the GridLayout because the FillLayout seems to introduce a
// scrollbar rendering issue on changing the content
parent.setLayout(new GridLayout());
// property names of the Person class
String[] personPropertyNames = { "firstName", "lastName", "gender", "married", "birthday" };
// mapping from property to label, needed for column header labels
Map<String, String> personPropertyToLabelMap = new HashMap<>();
personPropertyToLabelMap.put("firstName", "Firstname");
personPropertyToLabelMap.put("lastName", "Lastname");
personPropertyToLabelMap.put("gender", "Gender");
personPropertyToLabelMap.put("married", "Married");
personPropertyToLabelMap.put("birthday", "Birthday");
ListDataProvider<Person> bodyDataProvider = new ListDataProvider<>(PersonService.getPersons(10), new ReflectiveColumnPropertyAccessor<Person>(personPropertyNames));
IDataProvider personColumnHeaderDataProvider = new DefaultColumnHeaderDataProvider(personPropertyNames, personPropertyToLabelMap);
IDataProvider personRowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
// mapping from property to label, needed for column header labels
Map<String, String> addressPropertyToLabelMap = new HashMap<>();
addressPropertyToLabelMap.put("street", "Street");
addressPropertyToLabelMap.put("housenumber", "Housenumber");
addressPropertyToLabelMap.put("postalCode", "Postal Code");
addressPropertyToLabelMap.put("city", "City");
Composite panel = new Composite(parent, SWT.NONE);
panel.setLayout(new GridLayout());
GridDataFactory.fillDefaults().grab(true, true).applyTo(panel);
Composite gridPanel = new Composite(panel, SWT.NONE);
gridPanel.setLayout(new GridLayout());
GridDataFactory.fillDefaults().grab(true, true).applyTo(gridPanel);
Composite buttonPanel = new Composite(panel, SWT.NONE);
buttonPanel.setLayout(new GridLayout());
GridDataFactory.fillDefaults().grab(true, true).applyTo(buttonPanel);
ConfigRegistry configRegistry = new ConfigRegistry();
// create the body layer stack
DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
bodyDataLayer.setConfigLabelAccumulator(new IConfigLabelAccumulator() {
@Override
public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
switch(columnPosition) {
case 3:
configLabels.addLabel("MARRIED");
break;
case 4:
configLabels.addLabel("DATE");
break;
}
}
});
DefaultBodyLayerStack bodyLayerStack = new DefaultBodyLayerStack(bodyDataLayer);
bodyDataLayer.registerCommandHandler(new DeleteRowCommandHandler<>(bodyDataProvider.getList()));
bodyDataLayer.registerCommandHandler(new InsertRowCommandHandler<>(bodyDataProvider.getList()));
// create the column header layer stack
DataLayer columnHeaderDataLayer = new DataLayer(personColumnHeaderDataProvider);
ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayerStack.getViewportLayer(), bodyLayerStack.getSelectionLayer());
// create the row header layer stack
DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(personRowHeaderDataProvider);
ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayerStack.getViewportLayer(), bodyLayerStack.getSelectionLayer());
// create the corner layer stack
ILayer cornerLayer = new CornerLayer(new DataLayer(new DefaultCornerDataProvider(personColumnHeaderDataProvider, personRowHeaderDataProvider)), rowHeaderLayer, columnHeaderLayer);
// create the grid layer composed with the prior created layer stacks
GridLayer gridLayer = new GridLayer(bodyLayerStack, columnHeaderLayer, rowHeaderLayer, cornerLayer);
final NatTable natTable = new NatTable(gridPanel, gridLayer, false);
natTable.setConfigRegistry(configRegistry);
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
natTable.addConfiguration(new HeaderMenuConfiguration(natTable));
natTable.addConfiguration(new SingleClickSortConfiguration());
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(), DisplayMode.NORMAL, "MARRIED");
configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultDateDisplayConverter("yyyy-MM-dd"), DisplayMode.NORMAL, "DATE");
}
});
natTable.addConfiguration(new AbstractUiBindingConfiguration() {
private final Menu bodyMenu = new PopupMenuBuilder(natTable).withMenuItemProvider(new IMenuItemProvider() {
@Override
public void addMenuItem(NatTable natTable, Menu popupMenu) {
MenuItem deleteRow = new MenuItem(popupMenu, SWT.PUSH);
deleteRow.setText("Insert below");
deleteRow.setEnabled(true);
deleteRow.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
int rowPosition = MenuItemProviders.getNatEventData(event).getRowPosition();
natTable.doCommand(new InsertRowCommand<>(bodyDataLayer, rowPosition, PersonService.getPersons(1).get(0)));
}
});
}
}).withMenuItemProvider(new IMenuItemProvider() {
@Override
public void addMenuItem(NatTable natTable, Menu popupMenu) {
MenuItem deleteRow = new MenuItem(popupMenu, SWT.PUSH);
deleteRow.setText("Delete");
deleteRow.setEnabled(true);
deleteRow.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
int rowPosition = MenuItemProviders.getNatEventData(event).getRowPosition();
natTable.doCommand(new DeleteRowCommand(natTable, rowPosition));
}
});
}
}).build();
@Override
public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
uiBindingRegistry.registerMouseDownBinding(new MouseEventMatcher(SWT.NONE, GridRegion.BODY, MouseEventMatcher.RIGHT_BUTTON), new PopupMenuAction(this.bodyMenu) {
@Override
public void run(NatTable natTable, MouseEvent event) {
int columnPosition = natTable.getColumnPositionByX(event.x);
int rowPosition = natTable.getRowPositionByY(event.y);
if (!bodyLayerStack.getSelectionLayer().isRowPositionFullySelected(rowPosition)) {
natTable.doCommand(new SelectRowsCommand(natTable, columnPosition, rowPosition, false, false));
}
super.run(natTable, event);
}
});
}
});
natTable.configure();
GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
return panel;
}
Aggregations