]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/DistrictCSVTable.java
Add CSV table view for copy/pasting consumer information before creation
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / table / DistrictCSVTable.java
1 package org.simantics.district.network.ui.table;
2
3 import org.eclipse.jface.layout.GridDataFactory;
4 import org.eclipse.jface.layout.GridLayoutFactory;
5 import org.eclipse.nebula.widgets.nattable.NatTable;
6 import org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration;
7 import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration;
8 import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
9 import org.eclipse.nebula.widgets.nattable.copy.command.CopyDataCommandHandler;
10 import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
11 import org.eclipse.nebula.widgets.nattable.freeze.CompositeFreezeLayer;
12 import org.eclipse.nebula.widgets.nattable.freeze.FreezeLayer;
13 import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
14 import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider;
15 import org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer;
16 import org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer;
17 import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer;
18 import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer;
19 import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer;
20 import org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer;
21 import org.eclipse.nebula.widgets.nattable.group.ColumnGroupHeaderLayer;
22 import org.eclipse.nebula.widgets.nattable.group.ColumnGroupModel;
23 import org.eclipse.nebula.widgets.nattable.hideshow.ColumnHideShowLayer;
24 import org.eclipse.nebula.widgets.nattable.hover.HoverLayer;
25 import org.eclipse.nebula.widgets.nattable.hover.config.BodyHoverStylingBindings;
26 import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
27 import org.eclipse.nebula.widgets.nattable.layer.ILayer;
28 import org.eclipse.nebula.widgets.nattable.layer.IUniqueIndexLayer;
29 import org.eclipse.nebula.widgets.nattable.reorder.RowReorderLayer;
30 import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
31 import org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry;
32 import org.eclipse.nebula.widgets.nattable.ui.matcher.KeyEventMatcher;
33 import org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.dnd.Clipboard;
36 import org.eclipse.swt.widgets.Composite;
37
38 public class DistrictCSVTable extends Composite {
39
40         NatTable table;
41         private TableDataProvider bodyDataProvider;
42         DataLayer bodyDataLayer;
43         private IConfigRegistry summaryConfigRegistry;
44         private IUniqueIndexLayer summaryRowLayer;
45         private ViewportLayer viewportLayer;
46         private CompositeFreezeLayer compositeFreezeLayer;
47         private FreezeLayer freezeLayer;
48         //private TableDataSortModel sortModel;
49         private ColumnHideShowLayer columnHideShowLayer;
50         private ColumnGroupModel columnGroupModel = new ColumnGroupModel();
51         private ColumnHeaderTableDataProvider columnHeaderDataProvider;
52         Clipboard cpb;
53         public SelectionLayer selectionLayer;
54
55         public DistrictCSVTable(Composite parent, int style) {
56                 super(parent, style);
57                 defaultInitializeUI();
58         }
59
60         private void defaultInitializeUI() {
61                 GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
62                 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this);
63                 createTable(this);
64         }
65
66         private void createTable(Composite parent) {
67
68                 // build the body layer stack 
69                 // Usually you would create a new layer stack by extending AbstractIndexLayerTransform and
70                 // setting the ViewportLayer as underlying layer. But in this case using the ViewportLayer
71                 // directly as body layer is also working.
72                 bodyDataProvider = new TableDataProvider();
73                 bodyDataLayer = new DataLayer(bodyDataProvider);
74
75                 RowReorderLayer rowReorderLayer =
76                                 new RowReorderLayer(columnHideShowLayer = new ColumnHideShowLayer(bodyDataLayer));
77
78                 HoverLayer hoverLayer = new HoverLayer(rowReorderLayer, false);
79                 // we need to ensure that the hover styling is removed when the mouse
80                 // cursor moves out of the cell area
81                 hoverLayer.addConfiguration(new BodyHoverStylingBindings(hoverLayer));
82
83                 selectionLayer = new SelectionLayer(hoverLayer);
84                 viewportLayer = new ViewportLayer(selectionLayer);
85                 viewportLayer.setRegionName(GridRegion.BODY);
86                 freezeLayer = new FreezeLayer(selectionLayer);
87                 compositeFreezeLayer = new CompositeFreezeLayer(freezeLayer, viewportLayer, selectionLayer);
88
89                 // build the column header layer
90                 columnHeaderDataProvider = new ColumnHeaderTableDataProvider();
91                 DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
92                 columnHeaderDataLayer.setRowsResizableByDefault(false);
93                 columnHeaderDataLayer.setColumnsResizableByDefault(true);
94                 ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, compositeFreezeLayer, selectionLayer);
95                 ColumnGroupHeaderLayer columnGroupHeaderLayer = new ColumnGroupHeaderLayer(columnHeaderLayer, selectionLayer, columnGroupModel);
96                 columnGroupHeaderLayer.setCalculateHeight(true);
97
98                 // build the row header layer
99                 IDataProvider rowHeaderDataProvider = new RowHeaderTableDataProvider(bodyDataProvider);
100                 DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
101                 rowHeaderDataLayer.setRowsResizableByDefault(false);
102                 rowHeaderDataLayer.setColumnsResizableByDefault(false);
103                 RowHeaderLayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, compositeFreezeLayer, selectionLayer);
104
105                 // build the corner layer
106                 IDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider);
107                 DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
108                 ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnGroupHeaderLayer);
109
110                 // build the grid layer
111                 GridLayer gridLayer = new GridLayer(compositeFreezeLayer, columnGroupHeaderLayer, rowHeaderLayer, cornerLayer);
112                 
113                 table = new NatTable(parent, NatTable.DEFAULT_STYLE_OPTIONS | SWT.BORDER, gridLayer, false);
114                 GridDataFactory.fillDefaults().grab(true, true).applyTo(table);
115                 
116                 // Register a CopyDataCommandHandler that also copies the headers and
117                 // uses the configured IDisplayConverters
118                 CopyDataCommandHandler copyHandler = new CopyDataCommandHandler(
119                                 selectionLayer,
120                                 columnHeaderDataLayer,
121                                 rowHeaderDataLayer);
122                 copyHandler.setCopyFormattedText(true);
123                 gridLayer.registerCommandHandler(copyHandler);
124                 
125                 // initialize paste handler with SWT clipboard
126                 cpb = new Clipboard(getDisplay());
127                 PasteDataCommandHandler pasteHandler = new PasteDataCommandHandler(bodyDataProvider, bodyDataLayer, selectionLayer, cpb);
128                 bodyDataLayer.registerCommandHandler(pasteHandler);
129                 
130                 table.addConfiguration(new DefaultNatTableStyleConfiguration());
131                 table.addConfiguration(new EditingSupportConfiguration(bodyDataProvider));
132                 
133                 table.addConfiguration(new AbstractRegistryConfiguration() {
134                         
135                         @Override
136                         public void configureRegistry(IConfigRegistry configRegistry) {
137                         }
138
139                         @Override
140                         public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
141                                 super.configureUiBindings(uiBindingRegistry);
142                                 // ui binding to perform a paste action on pressing CTRL+V
143                                 uiBindingRegistry.registerFirstKeyBinding(new KeyEventMatcher(SWT.MOD1, 'v'), new CustomPasteDataAction(DistrictCSVTable.this));
144                         }
145                 });
146                 
147                 table.configure();
148         }
149         
150         @Override
151         public void dispose() {
152                 cpb.dispose();
153                 super.dispose();
154         }
155
156         public String[][] getCurrentData() {
157                 return bodyDataProvider.getCurrentData();
158         }
159
160 }