1 package org.simantics.district.network.ui.techtype.table;
3 import java.io.IOException;
4 import java.nio.charset.Charset;
5 import java.nio.file.Files;
6 import java.nio.file.Paths;
7 import java.util.stream.Collectors;
9 import org.eclipse.jface.layout.GridDataFactory;
10 import org.eclipse.jface.layout.GridLayoutFactory;
11 import org.eclipse.nebula.widgets.nattable.NatTable;
12 import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration;
13 import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
14 import org.eclipse.nebula.widgets.nattable.copy.command.CopyDataCommandHandler;
15 import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
16 import org.eclipse.nebula.widgets.nattable.freeze.CompositeFreezeLayer;
17 import org.eclipse.nebula.widgets.nattable.freeze.FreezeLayer;
18 import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
19 import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider;
20 import org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer;
21 import org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer;
22 import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer;
23 import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer;
24 import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer;
25 import org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer;
26 import org.eclipse.nebula.widgets.nattable.group.ColumnGroupHeaderLayer;
27 import org.eclipse.nebula.widgets.nattable.group.ColumnGroupModel;
28 import org.eclipse.nebula.widgets.nattable.hideshow.ColumnHideShowLayer;
29 import org.eclipse.nebula.widgets.nattable.hover.HoverLayer;
30 import org.eclipse.nebula.widgets.nattable.hover.config.BodyHoverStylingBindings;
31 import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
32 import org.eclipse.nebula.widgets.nattable.layer.ILayer;
33 import org.eclipse.nebula.widgets.nattable.layer.IUniqueIndexLayer;
34 import org.eclipse.nebula.widgets.nattable.reorder.RowReorderLayer;
35 import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
36 import org.eclipse.nebula.widgets.nattable.sort.SortHeaderLayer;
37 import org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.dnd.Clipboard;
40 import org.eclipse.swt.events.ModifyEvent;
41 import org.eclipse.swt.events.ModifyListener;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Text;
44 import org.simantics.Simantics;
45 import org.simantics.db.Resource;
46 import org.simantics.db.exception.DatabaseException;
47 import org.simantics.district.network.ui.techtype.requests.WriteTechTypeTableRequest;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class TechTypeTable extends Composite {
53 private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTable.class);
56 private TechTypeTableDataProvider bodyDataProvider;
57 DataLayer bodyDataLayer;
58 private IConfigRegistry summaryConfigRegistry;
59 private IUniqueIndexLayer summaryRowLayer;
60 private ViewportLayer viewportLayer;
61 private CompositeFreezeLayer compositeFreezeLayer;
62 private FreezeLayer freezeLayer;
63 // private TableDataSortModel sortModel;
64 private ColumnHideShowLayer columnHideShowLayer;
65 private ColumnGroupModel columnGroupModel = new ColumnGroupModel();
66 private TechTypeColumnHeaderTableDataProvider columnHeaderDataProvider;
68 public SelectionLayer selectionLayer;
69 private TechTypeTableSortModel sortModel;
71 private Resource componentType;
73 public TechTypeTable(Composite parent, int style, Resource componentType, String data) {
75 this.componentType = componentType;
77 defaultInitializeUI(data);
80 private void defaultInitializeUI(String data) {
81 GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
82 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this);
84 Composite filterComposite = new Composite(this, SWT.NONE);
85 GridDataFactory.fillDefaults().grab(true, false).applyTo(filterComposite);
86 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(filterComposite);
88 createFilterBar(filterComposite);
90 Composite tableComposite = new Composite(this, SWT.NONE);
91 GridDataFactory.fillDefaults().grab(true, true).applyTo(tableComposite);
92 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(tableComposite);
94 createTable(tableComposite, data);
97 private void createFilterBar(Composite filterComposite) {
99 Text filterText = new Text(filterComposite, SWT.BORDER);
100 GridDataFactory.fillDefaults().grab(true, true).applyTo(filterText);
101 filterText.addModifyListener(new ModifyListener() {
104 public void modifyText(ModifyEvent e) {
105 System.out.println("text modified");
106 bodyDataProvider.setFilter(filterText.getText());
113 private void createTable(Composite parent, String data) {
115 // build the body layer stack
116 // Usually you would create a new layer stack by extending
117 // AbstractIndexLayerTransform and
118 // setting the ViewportLayer as underlying layer. But in this case using the
120 // directly as body layer is also working.
121 bodyDataProvider = new TechTypeTableDataProvider(data);
122 bodyDataLayer = new DataLayer(bodyDataProvider);
124 RowReorderLayer rowReorderLayer = new RowReorderLayer(
125 columnHideShowLayer = new ColumnHideShowLayer(bodyDataLayer));
127 HoverLayer hoverLayer = new HoverLayer(rowReorderLayer, false);
128 // we need to ensure that the hover styling is removed when the mouse
129 // cursor moves out of the cell area
130 hoverLayer.addConfiguration(new BodyHoverStylingBindings(hoverLayer));
132 selectionLayer = new SelectionLayer(hoverLayer);
134 viewportLayer = new ViewportLayer(selectionLayer);
135 viewportLayer.setRegionName(GridRegion.BODY);
136 freezeLayer = new FreezeLayer(selectionLayer);
137 compositeFreezeLayer = new CompositeFreezeLayer(freezeLayer, viewportLayer, selectionLayer);
139 // build the column header layer
140 columnHeaderDataProvider = new TechTypeColumnHeaderTableDataProvider(bodyDataProvider);
141 DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
142 columnHeaderDataLayer.setRowsResizableByDefault(false);
143 columnHeaderDataLayer.setColumnsResizableByDefault(true);
144 ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, compositeFreezeLayer,
146 ColumnGroupHeaderLayer columnGroupHeaderLayer = new ColumnGroupHeaderLayer(columnHeaderLayer, selectionLayer,
148 columnGroupHeaderLayer.setCalculateHeight(true);
149 SortHeaderLayer<String> columnSortHeaderLayer = new SortHeaderLayer<>(columnGroupHeaderLayer,
150 sortModel = new TechTypeTableSortModel(bodyDataProvider));
152 // build the row header layer
153 IDataProvider rowHeaderDataProvider = new TechTypeRowHeaderTableDataProvider(bodyDataProvider);
154 DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
155 rowHeaderDataLayer.setRowsResizableByDefault(false);
156 rowHeaderDataLayer.setColumnsResizableByDefault(false);
157 RowHeaderLayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, compositeFreezeLayer, selectionLayer);
159 // build the corner layer
160 IDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider,
161 rowHeaderDataProvider);
162 DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
163 ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnSortHeaderLayer);
165 // build the grid layer
166 GridLayer gridLayer = new GridLayer(compositeFreezeLayer, columnSortHeaderLayer, rowHeaderLayer, cornerLayer);
168 table = new NatTable(parent, NatTable.DEFAULT_STYLE_OPTIONS | SWT.BORDER, gridLayer, false);
169 GridDataFactory.fillDefaults().grab(true, true).applyTo(table);
171 // Register a CopyDataCommandHandler that also copies the headers and
172 // uses the configured IDisplayConverters
173 CopyDataCommandHandler copyHandler = new CopyDataCommandHandler(selectionLayer, columnHeaderDataLayer,
175 copyHandler.setCopyFormattedText(true);
176 gridLayer.registerCommandHandler(copyHandler);
178 // initialize paste handler with SWT clipboard
179 cpb = new Clipboard(getDisplay());
180 // PasteDataCommandHandler pasteHandler = new
181 // PasteDataCommandHandler(bodyDataProvider, bodyDataLayer, selectionLayer,
183 // bodyDataLayer.registerCommandHandler(pasteHandler);
185 table.addConfiguration(new DefaultNatTableStyleConfiguration());
186 // table.addConfiguration(new EditingSupportConfiguration(bodyDataProvider));
191 public void dispose() {
196 public void setTechTypePath(String path) {
199 data = Files.lines(Paths.get(path)).collect(Collectors.joining("\n"));
200 } catch (IOException e) {
201 LOGGER.error("Failed to read contents of file '{}' as {}", path, Charset.defaultCharset(), e);
206 Simantics.getSession().syncRequest(new WriteTechTypeTableRequest(componentType, data));
207 } catch (DatabaseException e) {
208 LOGGER.error("Failed to write tech type table data to model", e);
211 bodyDataProvider.setData(data);