use of org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell in project nebula.widgets.nattable by eclipse.
the class TreeExpandCollapseAction method run.
@Override
public void run(NatTable natTable, MouseEvent event) {
int c = natTable.getColumnPositionByX(event.x);
int r = natTable.getRowPositionByY(event.y);
ILayerCell cell = natTable.getCellByPosition(c, r);
int rowIndex = cell.getLayer().getRowIndexByPosition(cell.getOriginRowPosition());
int columnIndex = cell.getLayer().getColumnIndexByPosition(cell.getOriginColumnPosition());
TreeExpandCollapseCommand command = new TreeExpandCollapseCommand(rowIndex, columnIndex);
natTable.doCommand(command);
}
use of org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell in project nebula.widgets.nattable by eclipse.
the class CellPainterMouseEventMatcher method matches.
@Override
public boolean matches(NatTable natTable, MouseEvent event, LabelStack regionLabels) {
if (super.matches(natTable, event, regionLabels)) {
int columnPosition = natTable.getColumnPositionByX(event.x);
int rowPosition = natTable.getRowPositionByY(event.y);
ILayerCell cell = natTable.getCellByPosition(columnPosition, rowPosition);
// contains not enough cells to fill it.
if (cell != null) {
IConfigRegistry configRegistry = natTable.getConfigRegistry();
ICellPainter cellPainter = cell.getLayer().getCellPainter(columnPosition, rowPosition, cell, configRegistry);
GC gc = new GC(natTable.getDisplay());
try {
Rectangle adjustedCellBounds = natTable.getLayerPainter().adjustCellBounds(columnPosition, rowPosition, cell.getBounds());
ICellPainter clickedCellPainter = cellPainter.getCellPainterAt(event.x, event.y, cell, gc, adjustedCellBounds, configRegistry);
if (clickedCellPainter != null) {
if ((this.targetCellPainter != null && this.targetCellPainter == clickedCellPainter) || (this.targetCellPainterClass != null && this.targetCellPainterClass.isInstance(clickedCellPainter))) {
return true;
}
}
} finally {
gc.dispose();
}
}
}
return false;
}
use of org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell in project nebula.widgets.nattable by eclipse.
the class _5044_HorizontalSplitViewportGridExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
// property names of the Person class
String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday", "address.street", "address.housenumber", "address.postalCode", "address.city" };
// 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");
propertyToLabelMap.put("address.street", "Street");
propertyToLabelMap.put("address.housenumber", "Housenumber");
propertyToLabelMap.put("address.postalCode", "Postal Code");
propertyToLabelMap.put("address.city", "City");
IColumnPropertyAccessor<PersonWithAddress> columnPropertyAccessor = new ExtendedReflectiveColumnPropertyAccessor<>(propertyNames);
final BodyLayerStack<PersonWithAddress> bodyLayer = new BodyLayerStack<>(PersonService.getPersonsWithAddress(50), columnPropertyAccessor);
// build the row header layer
IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyLayer.getBodyDataProvider());
DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
final ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayer, bodyLayer.getSelectionLayer());
// build the column header layer
IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
final DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
final AbstractLayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayer, bodyLayer.getSelectionLayer());
// Use this special layer painter that supports rendering of split
// viewports although the ColumnHeaderLayer is not split. Here is some
// custom calculation included that might not work correctly in case
// there are column groups or other spanning involved.
columnHeaderLayer.setLayerPainter(new CellLayerPainter() {
@Override
protected boolean isClipLeft(int position) {
// check position-1 because of the row header column count
// as the body is a composite layer, the default transformation
// for the grid is not working correctly
int index = LayerUtil.convertColumnPosition(columnHeaderLayer, position - 1, columnHeaderDataLayer);
return (index > SPLIT_COLUMN_INDEX);
}
@Override
protected void paintCell(ILayerCell cell, GC gc, IConfigRegistry configRegistry) {
ILayer layer = cell.getLayer();
int columnPosition = cell.getColumnPosition();
int rowPosition = cell.getRowPosition();
ICellPainter cellPainter = layer.getCellPainter(columnPosition, rowPosition, cell, configRegistry);
Rectangle adjustedCellBounds = layer.getLayerPainter().adjustCellBounds(columnPosition, rowPosition, cell.getBounds());
if (cellPainter != null) {
Rectangle originalClipping = gc.getClipping();
int startX = getStartXOfColumnPosition(columnPosition);
int startY = getStartYOfRowPosition(rowPosition);
int endX = getStartXOfColumnPosition(cell.getOriginColumnPosition() + cell.getColumnSpan());
int endY = getStartYOfRowPosition(cell.getOriginRowPosition() + cell.getRowSpan());
// correct position of first column in right region
// find the last visible column in left region
int viewportBorderX = bodyLayer.getViewportLayerLeft().getClientAreaWidth() + rowHeaderLayer.getWidth();
if (isClipLeft(columnPosition) && startX < viewportBorderX) {
startX = viewportBorderX;
}
if (!isClipLeft(columnPosition - 1) && startX > viewportBorderX) {
startX = viewportBorderX;
}
if (isClipLeft(cell.getOriginColumnPosition() + cell.getColumnSpan()) && endX < viewportBorderX) {
endX = viewportBorderX;
}
Rectangle cellClipBounds = originalClipping.intersection(new Rectangle(startX, startY, endX - startX, endY - startY));
gc.setClipping(cellClipBounds.intersection(adjustedCellBounds));
cellPainter.paintCell(cell, gc, adjustedCellBounds, configRegistry);
gc.setClipping(originalClipping);
}
}
});
// build the corner layer
IDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider);
DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
final ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnHeaderLayer);
// build the grid layer
GridLayer gridLayer = new GridLayer(bodyLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer);
// in order to make printing and exporting work correctly you need to
// register the following command handlers
gridLayer.registerCommandHandler(new MultiTurnViewportOnCommandHandler(bodyLayer.getViewportLayerLeft(), bodyLayer.getViewportLayerRight()));
gridLayer.registerCommandHandler(new MultiTurnViewportOffCommandHandler(bodyLayer.getViewportLayerLeft(), bodyLayer.getViewportLayerRight()));
// Wrap NatTable in composite so we can slap on the external horizontal
// sliders
Composite composite = new Composite(parent, SWT.NONE);
GridLayout gridLayout = new GridLayout(1, false);
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
gridLayout.horizontalSpacing = 0;
gridLayout.verticalSpacing = 0;
composite.setLayout(gridLayout);
NatTable natTable = new NatTable(composite, gridLayer, false);
GridData gridData = new GridData();
gridData.horizontalAlignment = GridData.FILL;
gridData.verticalAlignment = GridData.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
natTable.setLayoutData(gridData);
createSplitSliders(composite, rowHeaderLayer, bodyLayer.getViewportLayerLeft(), bodyLayer.getViewportLayerRight());
// add an IOverlayPainter to ensure the right border of the left
// viewport always
// this is necessary because the left border of layer stacks is not
// rendered by default
natTable.addOverlayPainter(new IOverlayPainter() {
@Override
public void paintOverlay(GC gc, ILayer layer) {
Color beforeColor = gc.getForeground();
gc.setForeground(GUIHelper.COLOR_GRAY);
int viewportBorderX = bodyLayer.getViewportLayerLeft().getWidth() + rowHeaderLayer.getWidth() - 1;
gc.drawLine(viewportBorderX, 0, viewportBorderX, layer.getHeight() - 1);
gc.setForeground(beforeColor);
}
});
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
natTable.addConfiguration(new HeaderMenuConfiguration(natTable));
natTable.configure();
return composite;
}
use of org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell in project nebula.widgets.nattable by eclipse.
the class _773_GridExcelExportFormatterExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
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(true, 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");
// 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);
SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer);
ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
bodyDataLayer.setConfigLabelAccumulator(new ColumnLabelAccumulator());
// build the column header layer
IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, viewportLayer, selectionLayer);
// build the row header layer
IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, viewportLayer, selectionLayer);
// build the corner layer
IDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider);
DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnHeaderLayer);
// build the grid layer
GridLayer gridLayer = new GridLayer(viewportLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer);
final NatTable natTable = new NatTable(gridPanel, gridLayer, false);
// adding this configuration adds the styles and the painters to use
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
PoiExcelExporter exporter = new HSSFExcelExporter();
exporter.setApplyVerticalTextConfiguration(true);
exporter.setApplyBackgroundColor(false);
configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORTER, exporter);
configRegistry.registerConfigAttribute(ExportConfigAttributes.DATE_FORMAT, "dd.MM.yyyy");
// register a custom formatter to the body of the grid
// you could also implement different formatter for different
// columns by using the label mechanism
configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORT_FORMATTER, new ExampleExportFormatter(), DisplayMode.NORMAL, GridRegion.BODY);
configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORT_FORMATTER, new IExportFormatter() {
@Override
public Object formatForExport(ILayerCell cell, IConfigRegistry configRegistry) {
// the default conversion to string for export
return cell.getDataValue();
}
}, DisplayMode.NORMAL, GridRegion.ROW_HEADER);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new BeveledBorderDecorator(new VerticalTextPainter(false, true, true)), DisplayMode.NORMAL, GridRegion.COLUMN_HEADER);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
}
});
gridLayer.addConfiguration(new DefaultImageExportBindings());
natTable.configure();
GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
Button addColumnButton = new Button(buttonPanel, SWT.PUSH);
addColumnButton.setText("Export");
addColumnButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
natTable.doCommand(new ExportCommand(natTable.getConfigRegistry(), natTable.getShell()));
}
});
return panel;
}
use of org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell in project nebula.widgets.nattable by eclipse.
the class _774_MultiExportExample method createExampleControl.
@Override
public Control createExampleControl(Composite parent) {
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(new GridLayout());
GridDataFactory.fillDefaults().grab(true, true).applyTo(gridPanel);
Composite buttonPanel = new Composite(panel, SWT.NONE);
buttonPanel.setLayout(new GridLayout());
GridDataFactory.fillDefaults().grab(true, 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");
GridLayer grid = createGrid(propertyNames, propertyToLabelMap, PersonService.getPersons(5));
final NatTable natTable = new NatTable(gridPanel, grid, false);
// add labels to show that alignment configurations are also exported
// correctly
final ColumnOverrideLabelAccumulator columnLabelAccumulator = new ColumnOverrideLabelAccumulator(grid.getBodyLayer());
((AbstractLayer) grid.getBodyLayer()).setConfigLabelAccumulator(columnLabelAccumulator);
columnLabelAccumulator.registerColumnOverrides(0, COLUMN_ONE_LABEL);
columnLabelAccumulator.registerColumnOverrides(1, COLUMN_TWO_LABEL);
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
Style style = new Style();
style.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.LEFT);
style.setAttributeValue(CellStyleAttributes.VERTICAL_ALIGNMENT, VerticalAlignmentEnum.TOP);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, COLUMN_ONE_LABEL);
style = new Style();
style.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.RIGHT);
style.setAttributeValue(CellStyleAttributes.VERTICAL_ALIGNMENT, VerticalAlignmentEnum.BOTTOM);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, COLUMN_TWO_LABEL);
}
});
// adding this configuration adds the styles and the painters to use
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
natTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORTER, new HSSFExcelExporter());
configRegistry.registerConfigAttribute(ExportConfigAttributes.DATE_FORMAT, "dd.MM.yyyy");
// register a custom formatter to the body of the grid
// you could also implement different formatter for different
// columns by using the label mechanism
configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORT_FORMATTER, new ExampleExportFormatter(), DisplayMode.NORMAL, GridRegion.BODY);
configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORT_FORMATTER, new IExportFormatter() {
@Override
public Object formatForExport(ILayerCell cell, IConfigRegistry configRegistry) {
// string for export
return cell.getDataValue();
}
}, DisplayMode.NORMAL, GridRegion.ROW_HEADER);
}
});
natTable.configure();
GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
// property names of the NumberValues class
String[] numberPropertyNames = { "columnOneNumber", "columnTwoNumber", "columnThreeNumber", "columnFourNumber", "columnFiveNumber" };
// mapping from property to label, needed for column header labels
Map<String, String> numberPropertyToLabelMap = new HashMap<>();
numberPropertyToLabelMap.put("columnOneNumber", "Value One");
numberPropertyToLabelMap.put("columnTwoNumber", "Value Two");
numberPropertyToLabelMap.put("columnThreeNumber", "Value Three");
numberPropertyToLabelMap.put("columnFourNumber", "Value Four");
numberPropertyToLabelMap.put("columnFiveNumber", "Value Five");
List<NumberValues> valuesToShow = new ArrayList<>();
valuesToShow.add(createNumberValues());
valuesToShow.add(createNumberValues());
valuesToShow.add(createNumberValues());
valuesToShow.add(createNumberValues());
valuesToShow.add(createNumberValues());
final NatTable numberNatTable = new NatTable(gridPanel, createGrid(numberPropertyNames, numberPropertyToLabelMap, valuesToShow), false);
// adding this configuration adds the styles and the painters to use
numberNatTable.addConfiguration(new DefaultNatTableStyleConfiguration());
numberNatTable.addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
// register a custom formatter to the body of the grid
// you could also implement different formatter for different
// columns by using the label mechanism
configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORT_FORMATTER, new IExportFormatter() {
@Override
public Object formatForExport(ILayerCell cell, IConfigRegistry configRegistry) {
// default conversion to string for export
return cell.getDataValue();
}
}, DisplayMode.NORMAL, GridRegion.BODY);
configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORT_FORMATTER, new IExportFormatter() {
@Override
public Object formatForExport(ILayerCell cell, IConfigRegistry configRegistry) {
// default conversion to string for export
return cell.getDataValue();
}
}, DisplayMode.NORMAL, GridRegion.ROW_HEADER);
}
});
numberNatTable.configure();
GridDataFactory.fillDefaults().grab(true, true).applyTo(numberNatTable);
Button addColumnButton = new Button(buttonPanel, SWT.PUSH);
addColumnButton.setText("Export");
addColumnButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// get the ILayerExporter that is registered for the persons
// table, otherwise the exporter registered there would have no
// effect. For multi table export the ILayerExporter specified
// in the method call is used always
ILayerExporter exporter = natTable.getConfigRegistry().getConfigAttribute(ExportConfigAttributes.EXPORTER, DisplayMode.NORMAL);
Map<String, NatTable> export = new HashMap<>();
export.put("Persons", natTable);
export.put("Numbers", numberNatTable);
new NatExporter(Display.getCurrent().getActiveShell()).exportMultipleNatTables(exporter, export);
}
});
return panel;
}
Aggregations