package org.simantics.district.network.ui.table; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.nebula.widgets.nattable.NatTable; import org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration; import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration; import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry; import org.eclipse.nebula.widgets.nattable.copy.command.CopyDataCommandHandler; import org.eclipse.nebula.widgets.nattable.data.IDataProvider; import org.eclipse.nebula.widgets.nattable.freeze.CompositeFreezeLayer; import org.eclipse.nebula.widgets.nattable.freeze.FreezeLayer; import org.eclipse.nebula.widgets.nattable.grid.GridRegion; import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider; import org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer; import org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer; import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer; import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer; import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer; import org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer; import org.eclipse.nebula.widgets.nattable.group.ColumnGroupHeaderLayer; import org.eclipse.nebula.widgets.nattable.group.ColumnGroupModel; import org.eclipse.nebula.widgets.nattable.hideshow.ColumnHideShowLayer; import org.eclipse.nebula.widgets.nattable.hover.HoverLayer; import org.eclipse.nebula.widgets.nattable.hover.config.BodyHoverStylingBindings; import org.eclipse.nebula.widgets.nattable.layer.DataLayer; import org.eclipse.nebula.widgets.nattable.layer.ILayer; import org.eclipse.nebula.widgets.nattable.layer.IUniqueIndexLayer; import org.eclipse.nebula.widgets.nattable.reorder.RowReorderLayer; import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer; import org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry; import org.eclipse.nebula.widgets.nattable.ui.matcher.KeyEventMatcher; import org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer; import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.Clipboard; import org.eclipse.swt.widgets.Composite; public class DistrictCSVTable extends Composite { NatTable table; private TableDataProvider bodyDataProvider; DataLayer bodyDataLayer; private IConfigRegistry summaryConfigRegistry; private IUniqueIndexLayer summaryRowLayer; private ViewportLayer viewportLayer; private CompositeFreezeLayer compositeFreezeLayer; private FreezeLayer freezeLayer; //private TableDataSortModel sortModel; private ColumnHideShowLayer columnHideShowLayer; private ColumnGroupModel columnGroupModel = new ColumnGroupModel(); private ColumnHeaderTableDataProvider columnHeaderDataProvider; Clipboard cpb; public SelectionLayer selectionLayer; public DistrictCSVTable(Composite parent, int style) { super(parent, style); defaultInitializeUI(); } private void defaultInitializeUI() { GridDataFactory.fillDefaults().grab(true, true).applyTo(this); GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this); createTable(this); } private void createTable(Composite parent) { // 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. bodyDataProvider = new TableDataProvider(); bodyDataLayer = new DataLayer(bodyDataProvider); RowReorderLayer rowReorderLayer = new RowReorderLayer(columnHideShowLayer = new ColumnHideShowLayer(bodyDataLayer)); HoverLayer hoverLayer = new HoverLayer(rowReorderLayer, false); // we need to ensure that the hover styling is removed when the mouse // cursor moves out of the cell area hoverLayer.addConfiguration(new BodyHoverStylingBindings(hoverLayer)); selectionLayer = new SelectionLayer(hoverLayer); viewportLayer = new ViewportLayer(selectionLayer); viewportLayer.setRegionName(GridRegion.BODY); freezeLayer = new FreezeLayer(selectionLayer); compositeFreezeLayer = new CompositeFreezeLayer(freezeLayer, viewportLayer, selectionLayer); // build the column header layer columnHeaderDataProvider = new ColumnHeaderTableDataProvider(); DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider); columnHeaderDataLayer.setRowsResizableByDefault(false); columnHeaderDataLayer.setColumnsResizableByDefault(true); ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, compositeFreezeLayer, selectionLayer); ColumnGroupHeaderLayer columnGroupHeaderLayer = new ColumnGroupHeaderLayer(columnHeaderLayer, selectionLayer, columnGroupModel); columnGroupHeaderLayer.setCalculateHeight(true); // build the row header layer IDataProvider rowHeaderDataProvider = new RowHeaderTableDataProvider(bodyDataProvider); DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider); rowHeaderDataLayer.setRowsResizableByDefault(false); rowHeaderDataLayer.setColumnsResizableByDefault(false); RowHeaderLayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, compositeFreezeLayer, selectionLayer); // build the corner layer IDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider); DataLayer cornerDataLayer = new DataLayer(cornerDataProvider); ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnGroupHeaderLayer); // build the grid layer GridLayer gridLayer = new GridLayer(compositeFreezeLayer, columnGroupHeaderLayer, rowHeaderLayer, cornerLayer); table = new NatTable(parent, NatTable.DEFAULT_STYLE_OPTIONS | SWT.BORDER, gridLayer, false); GridDataFactory.fillDefaults().grab(true, true).applyTo(table); // Register a CopyDataCommandHandler that also copies the headers and // uses the configured IDisplayConverters CopyDataCommandHandler copyHandler = new CopyDataCommandHandler( selectionLayer, columnHeaderDataLayer, rowHeaderDataLayer); copyHandler.setCopyFormattedText(true); gridLayer.registerCommandHandler(copyHandler); // initialize paste handler with SWT clipboard cpb = new Clipboard(getDisplay()); PasteDataCommandHandler pasteHandler = new PasteDataCommandHandler(bodyDataProvider, bodyDataLayer, selectionLayer, cpb); bodyDataLayer.registerCommandHandler(pasteHandler); table.addConfiguration(new DefaultNatTableStyleConfiguration()); table.addConfiguration(new EditingSupportConfiguration(bodyDataProvider)); table.addConfiguration(new AbstractRegistryConfiguration() { @Override public void configureRegistry(IConfigRegistry configRegistry) { } @Override public void configureUiBindings(UiBindingRegistry uiBindingRegistry) { super.configureUiBindings(uiBindingRegistry); // ui binding to perform a paste action on pressing CTRL+V uiBindingRegistry.registerFirstKeyBinding(new KeyEventMatcher(SWT.MOD1, 'v'), new CustomPasteDataAction(DistrictCSVTable.this)); } }); table.configure(); } @Override public void dispose() { cpb.dispose(); super.dispose(); } public String[][] getCurrentData() { return bodyDataProvider.getCurrentData(); } }