Home » Eclipse Projects » NatTable » Create nattable with two column ,the second column has multiple dynamic rows inside.(Creating a customise nattable)
| | |
Re: Create nattable with two column ,the second column has multiple dynamic rows inside. [message #1764100 is a reply to message #1763898] |
Wed, 24 May 2017 20:31 |
Andreea Sonda Messages: 11 Registered: May 2017 |
Junior Member |
|
|
I tried the example from https://eclipse.org/nattable/nandn/nandn_110.php but it seems I am doing something wrong because the cell in`s not splitted. :(.
@Creatable
public class Part {
public void createNatTable(Composite parent) {
String[] propertyNames = {"firstName", "lastName", "password", "description", "age", "money", "married",
"gender", "address.street", "address.city", "favouriteFood", "favouriteDrinks" };
// 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("password", "Password");
propertyToLabelMap.put("description", "Description");
propertyToLabelMap.put("age", "Age");
propertyToLabelMap.put("money", "Money");
propertyToLabelMap.put("married", "Married");
propertyToLabelMap.put("gender", "Gender");
propertyToLabelMap.put("address.street", "Street");
propertyToLabelMap.put("address.city", "City");
propertyToLabelMap.put("favouriteFood", "Food");
propertyToLabelMap.put("favouriteDrinks", "Drinks");
IDataProvider bodyDataProvider = new ListDataProvider<>(PersonService.getExtendedPersonsWithAddress(10),
new ExtendedReflectiveColumnPropertyAccessor<ExtendedPersonWithAddress>(propertyNames));
DefaultGridLayer gridLayer = new DefaultGridLayer(bodyDataProvider,
new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap));
final DataLayer bodyDataLayer = (DataLayer)gridLayer.getBodyDataLayer();
final ColumnOverrideLabelAccumulator columnLabelAccumulator = new ColumnOverrideLabelAccumulator(bodyDataLayer);
bodyDataLayer.setConfigLabelAccumulator(columnLabelAccumulator);
registerColumnLabels(columnLabelAccumulator);
this.natTable = new NatTable(parent, gridLayer, false);
this.natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
this.natTable.addConfiguration(new PainterConfiguration());
this.natTable.configure();
new NatTableContentTooltip(this.natTable, GridRegion.BODY);
}
private void registerColumnLabels(ColumnOverrideLabelAccumulator columnLabelAccumulator) {
columnLabelAccumulator.registerColumnOverrides(0, COLUMN_ELEVEN_LABEL);
}
public class PainterConfiguration extends AbstractRegistryConfiguration {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
registerPainters(configRegistry);
}
private void registerPainters(IConfigRegistry configRegistry) {
registerColumnElevenTablePainter(configRegistry);
}
private void registerColumnElevenTablePainter(IConfigRegistry configRegistry) {
//register the TableCellPainter for the food collection in the data model
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new TableCellPainter(),
DisplayMode.NORMAL, COLUMN_ELEVEN_LABEL);
//register the TableCellEditor for the food collection in the data model
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new TableCellEditor(),
DisplayMode.NORMAL, COLUMN_ELEVEN_LABEL);
// uncomment this to get an idea on how the TableCellEditor works
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE,
IEditableRule.ALWAYS_EDITABLE, DisplayMode.EDIT, COLUMN_ELEVEN_LABEL);
//
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new TableCellEditor(),
DisplayMode.NORMAL, COLUMN_ELEVEN_LABEL);
}
}
|
|
| | |
Re: Create nattable with two column ,the second column has multiple dynamic rows inside. [message #1764366 is a reply to message #1764319] |
Mon, 29 May 2017 08:55 |
Andreea Sonda Messages: 11 Registered: May 2017 |
Junior Member |
|
|
Hello Dirk,
I have two question:
1.is it possible to have two different column providers for each column in the same table?
2.can I show two attributes of an object in the same cell (as it can be seen in the resulted_nattable, second column)
The problem that I have, is that the object that works as a provider for the first column has a list of objects that I want to use for the cell painter in the second column ,just as in the UML.
I need a second provider because I will also have to implement a ISelection provider for the both columns, enabling the user to modify the data in the both columns and keeping the back end model up to date.
I saw that the constructor of DefaultGridLayer does not provide a constructor for two column providers.
The wanted result can be found in the resulted_nattable.png
Thank you!
Have a nice day!
[Updated on: Mon, 29 May 2017 09:05] Report message to a moderator
|
|
| | |
Re: Create nattable with two column ,the second column has multiple dynamic rows inside. [message #1765599 is a reply to message #1764386] |
Mon, 12 June 2017 17:07 |
Andreea Sonda Messages: 11 Registered: May 2017 |
Junior Member |
|
|
Hello Dirk,
I am trying to add a context menu with delete and add row menus.
I tried to copy the code from _308_DataModificationExample into my workspace but I have some problems with DeleteRowCommandHandeler at doCommand() method.
Here is the errors I get:Quote:
"The method doCommand(ILayer, ExamplePart.InsertRowCommand) of type ExamplePart.InsertRowCommandHandler must override or implement a supertype method"
public class ExamplePart {
public void createNatTable(Composite parent) {
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 personPropertyToLabelMap = new HashMap();
personPropertyToLabelMap.put("firstName", "Firstname");
personPropertyToLabelMap.put("lastName", "Lastname");
personPropertyToLabelMap.put("gender", "Gender");
personPropertyToLabelMap.put("married", "Married");
personPropertyToLabelMap.put("birthday", "Birthday");
ListDataProvider bodyDataProvider = new ListDataProvider<>(
PersonService.getPersons(10),
new ReflectiveColumnPropertyAccessor(personPropertyNames));
IDataProvider personColumnHeaderDataProvider =
new DefaultColumnHeaderDataProvider(personPropertyNames, personPropertyToLabelMap);
IDataProvider personRowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
// mapping from property to label, needed for column header labels
Map 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());
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);
}
class InsertRowCommand extends AbstractRowCommand {
private final T object;
public InsertRowCommand(ILayer layer, int rowPosition, T object) {
super(layer, rowPosition);
this.object = object;
}
protected InsertRowCommand(InsertRowCommand command) {
super(command);
this.object = command.object;
}
@Override
public ILayerCommand cloneCommand() {
return new InsertRowCommand(this);
}
public T getObject() {
return this.object;
}
}
/**
* The command handler for inserting a row.
*
* @param
*/
@SuppressWarnings ("rawtypes")
class InsertRowCommandHandler implements ILayerCommandHandler {
private List bodyData;
public InsertRowCommandHandler(List bodyData) {
this.bodyData = bodyData;
}
@Override
public Class getCommandClass() {
return InsertRowCommand.class;
}
@SuppressWarnings ("unchecked")
@Override
public boolean doCommand(ILayer targetLayer, InsertRowCommand command) {
// convert the transported position to the target layer
if (command.convertToTargetLayer(targetLayer)) {
// remove the element
this.bodyData.add(command.getRowPosition(), (T)command.getObject());
// fire the event to refresh
targetLayer.fireLayerEvent(new RowInsertEvent(targetLayer, command.getRowPosition()));
return true;
}
return false;
}
@Override
public boolean doCommand(ILayer targetLayer, ILayerCommand command) {
// TODO Auto-generated method stub
return false;
}
}
/**
* Command to delete a row.
*/
class DeleteRowCommand extends AbstractRowCommand {
public DeleteRowCommand(ILayer layer, int rowPosition) {
super(layer, rowPosition);
}
protected DeleteRowCommand(DeleteRowCommand command) {
super(command);
}
@Override
public ILayerCommand cloneCommand() {
return new DeleteRowCommand(this);
}
}
/**
* The command handler for deleting a row.
*
* @param
*/
class DeleteRowCommandHandler implements ILayerCommandHandler {
private List bodyData;
public DeleteRowCommandHandler(List bodyData) {
this.bodyData = bodyData;
}
@Override
public Class getCommandClass() {
return DeleteRowCommand.class;
}
@Override
public boolean doCommand(ILayer targetLayer, DeleteRowCommand command) {
// convert the transported position to the target layer
if (command.convertToTargetLayer(targetLayer)) {
// remove the element
this.bodyData.remove(command.getRowPosition());
// fire the event to refresh
targetLayer.fireLayerEvent(new RowDeleteEvent(targetLayer, command.getRowPosition()));
return true;
}
return false;
}
@Override
public boolean doCommand(ILayer targetLayer, ILayerCommand command) {
// TODO Auto-generated method stub
return false;
}
}
}
If I tried to fix the problem , the menu appears in the context menu but does nothing.
-
Attachment: error.PNG
(Size: 46.78KB, Downloaded 230 times)
|
|
| | | | | | |
Goto Forum:
Current Time: Sun Sep 22 19:08:28 GMT 2024
Powered by FUDForum. Page generated in 0.06329 seconds
|