]> gerrit.simantics Code Review - simantics/district.git/blob
3b3d4a7014f13cbdc924ef46368f3111db828dfa
[simantics/district.git] /
1 package org.simantics.district.network.ui.techtype.table;
2
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;
8
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;
50
51 public class TechTypeTable extends Composite {
52
53         private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTable.class);
54
55         NatTable table;
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;
67         Clipboard cpb;
68         public SelectionLayer selectionLayer;
69         private TechTypeTableSortModel sortModel;
70
71         private Resource componentType;
72
73         public TechTypeTable(Composite parent, int style, Resource componentType, String data) {
74                 super(parent, style);
75                 this.componentType = componentType;
76
77                 defaultInitializeUI(data);
78         }
79
80         private void defaultInitializeUI(String data) {
81                 GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
82                 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this);
83
84                 Composite filterComposite = new Composite(this, SWT.NONE);
85                 GridDataFactory.fillDefaults().grab(true, false).applyTo(filterComposite);
86                 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(filterComposite);
87
88                 createFilterBar(filterComposite);
89
90                 Composite tableComposite = new Composite(this, SWT.NONE);
91                 GridDataFactory.fillDefaults().grab(true, true).applyTo(tableComposite);
92                 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(tableComposite);
93
94                 createTable(tableComposite, data);
95         }
96
97         private void createFilterBar(Composite filterComposite) {
98
99                 Text filterText = new Text(filterComposite, SWT.BORDER);
100                 GridDataFactory.fillDefaults().grab(true, true).applyTo(filterText);
101                 filterText.addModifyListener(new ModifyListener() {
102
103                         @Override
104                         public void modifyText(ModifyEvent e) {
105                                 System.out.println("text modified");
106                                 bodyDataProvider.setFilter(filterText.getText());
107                                 table.refresh(true);
108                         }
109                 });
110
111         }
112
113         private void createTable(Composite parent, String data) {
114
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
119                 // ViewportLayer
120                 // directly as body layer is also working.
121                 bodyDataProvider = new TechTypeTableDataProvider(data);
122                 bodyDataLayer = new DataLayer(bodyDataProvider);
123
124                 RowReorderLayer rowReorderLayer = new RowReorderLayer(
125                                 columnHideShowLayer = new ColumnHideShowLayer(bodyDataLayer));
126
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));
131
132                 selectionLayer = new SelectionLayer(hoverLayer);
133
134                 viewportLayer = new ViewportLayer(selectionLayer);
135                 viewportLayer.setRegionName(GridRegion.BODY);
136                 freezeLayer = new FreezeLayer(selectionLayer);
137                 compositeFreezeLayer = new CompositeFreezeLayer(freezeLayer, viewportLayer, selectionLayer);
138
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,
145                                 selectionLayer);
146                 ColumnGroupHeaderLayer columnGroupHeaderLayer = new ColumnGroupHeaderLayer(columnHeaderLayer, selectionLayer,
147                                 columnGroupModel);
148                 columnGroupHeaderLayer.setCalculateHeight(true);
149                 SortHeaderLayer<String> columnSortHeaderLayer = new SortHeaderLayer<>(columnGroupHeaderLayer,
150                                 sortModel = new TechTypeTableSortModel(bodyDataProvider));
151
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);
158
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);
164
165                 // build the grid layer
166                 GridLayer gridLayer = new GridLayer(compositeFreezeLayer, columnSortHeaderLayer, rowHeaderLayer, cornerLayer);
167
168                 table = new NatTable(parent, NatTable.DEFAULT_STYLE_OPTIONS | SWT.BORDER, gridLayer, false);
169                 GridDataFactory.fillDefaults().grab(true, true).applyTo(table);
170
171                 // Register a CopyDataCommandHandler that also copies the headers and
172                 // uses the configured IDisplayConverters
173                 CopyDataCommandHandler copyHandler = new CopyDataCommandHandler(selectionLayer, columnHeaderDataLayer,
174                                 rowHeaderDataLayer);
175                 copyHandler.setCopyFormattedText(true);
176                 gridLayer.registerCommandHandler(copyHandler);
177
178                 // initialize paste handler with SWT clipboard
179                 cpb = new Clipboard(getDisplay());
180                 // PasteDataCommandHandler pasteHandler = new
181                 // PasteDataCommandHandler(bodyDataProvider, bodyDataLayer, selectionLayer,
182                 // cpb);
183                 // bodyDataLayer.registerCommandHandler(pasteHandler);
184
185                 table.addConfiguration(new DefaultNatTableStyleConfiguration());
186                 // table.addConfiguration(new EditingSupportConfiguration(bodyDataProvider));
187                 table.configure();
188         }
189
190         @Override
191         public void dispose() {
192                 cpb.dispose();
193                 super.dispose();
194         }
195
196         public void setTechTypePath(String path) {
197                 String data;
198                 try {
199                         data = Files.lines(Paths.get(path), Charset.defaultCharset()).collect(Collectors.joining("\n"));
200                 } catch (IOException e) {
201                         LOGGER.error("Failed to read contents of file '{}' as {}", path, Charset.defaultCharset(), e);
202                         return;
203                 }
204
205                 try {
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);
209                 }
210
211                 bodyDataProvider.setData(data);
212                 table.refresh(true);
213         }
214
215 }