]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTable.java
First testing version of TechTypeTable
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / techtype / table / TechTypeTable.java
1 package org.simantics.district.network.ui.techtype.table;
2
3 import java.io.Serializable;
4
5 import org.eclipse.jface.layout.GridDataFactory;
6 import org.eclipse.jface.layout.GridLayoutFactory;
7 import org.eclipse.nebula.widgets.nattable.NatTable;
8 import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration;
9 import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
10 import org.eclipse.nebula.widgets.nattable.copy.command.CopyDataCommandHandler;
11 import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
12 import org.eclipse.nebula.widgets.nattable.data.IRowIdAccessor;
13 import org.eclipse.nebula.widgets.nattable.freeze.CompositeFreezeLayer;
14 import org.eclipse.nebula.widgets.nattable.freeze.FreezeLayer;
15 import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
16 import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider;
17 import org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer;
18 import org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer;
19 import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer;
20 import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer;
21 import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer;
22 import org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer;
23 import org.eclipse.nebula.widgets.nattable.group.ColumnGroupHeaderLayer;
24 import org.eclipse.nebula.widgets.nattable.group.ColumnGroupModel;
25 import org.eclipse.nebula.widgets.nattable.hideshow.ColumnHideShowLayer;
26 import org.eclipse.nebula.widgets.nattable.hover.HoverLayer;
27 import org.eclipse.nebula.widgets.nattable.hover.config.BodyHoverStylingBindings;
28 import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
29 import org.eclipse.nebula.widgets.nattable.layer.ILayer;
30 import org.eclipse.nebula.widgets.nattable.layer.IUniqueIndexLayer;
31 import org.eclipse.nebula.widgets.nattable.reorder.RowReorderLayer;
32 import org.eclipse.nebula.widgets.nattable.selection.RowSelectionModel;
33 import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
34 import org.eclipse.nebula.widgets.nattable.sort.SortHeaderLayer;
35 import org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.dnd.Clipboard;
38 import org.eclipse.swt.events.ModifyEvent;
39 import org.eclipse.swt.events.ModifyListener;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Text;
42
43 public class TechTypeTable extends Composite {
44
45         NatTable table;
46         private TechTypeTableDataProvider bodyDataProvider;
47         DataLayer bodyDataLayer;
48         private IConfigRegistry summaryConfigRegistry;
49         private IUniqueIndexLayer summaryRowLayer;
50         private ViewportLayer viewportLayer;
51         private CompositeFreezeLayer compositeFreezeLayer;
52         private FreezeLayer freezeLayer;
53         //private TableDataSortModel sortModel;
54         private ColumnHideShowLayer columnHideShowLayer;
55         private ColumnGroupModel columnGroupModel = new ColumnGroupModel();
56         private TechTypeColumnHeaderTableDataProvider columnHeaderDataProvider;
57         Clipboard cpb;
58         public SelectionLayer selectionLayer;
59     private TechTypeTableSortModel sortModel;
60
61         public TechTypeTable(Composite parent, int style) {
62                 super(parent, style);
63                 defaultInitializeUI();
64         }
65
66         private void defaultInitializeUI() {
67                 GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
68                 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this);
69                 
70                 Composite filterComposite = new Composite(this, SWT.NONE);
71                 GridDataFactory.fillDefaults().grab(true, false).applyTo(filterComposite);
72         GridLayoutFactory.fillDefaults().numColumns(1).applyTo(filterComposite);
73         
74
75         createFilterBar(filterComposite);
76         
77                 Composite tableComposite = new Composite(this, SWT.NONE);
78                 GridDataFactory.fillDefaults().grab(true, true).applyTo(tableComposite);
79         GridLayoutFactory.fillDefaults().numColumns(1).applyTo(tableComposite);
80                 createTable(tableComposite);
81         }
82
83         private void createFilterBar(Composite filterComposite) {
84             
85         Text filterText = new Text(filterComposite, SWT.BORDER);
86         GridDataFactory.fillDefaults().grab(true, true).applyTo(filterText);
87         filterText.addModifyListener(new ModifyListener() {
88             
89             @Override
90             public void modifyText(ModifyEvent e) {
91                 System.out.println("text modified");
92                 bodyDataProvider.setFilter(filterText.getText());
93                 table.refresh(true);
94             }
95         });
96         
97     }
98
99     private void createTable(Composite parent) {
100
101                 // build the body layer stack 
102                 // Usually you would create a new layer stack by extending AbstractIndexLayerTransform and
103                 // setting the ViewportLayer as underlying layer. But in this case using the ViewportLayer
104                 // directly as body layer is also working.
105                 bodyDataProvider = new TechTypeTableDataProvider();
106                 bodyDataLayer = new DataLayer(bodyDataProvider);
107
108                 RowReorderLayer rowReorderLayer =
109                                 new RowReorderLayer(columnHideShowLayer = new ColumnHideShowLayer(bodyDataLayer));
110
111                 HoverLayer hoverLayer = new HoverLayer(rowReorderLayer, false);
112                 // we need to ensure that the hover styling is removed when the mouse
113                 // cursor moves out of the cell area
114                 hoverLayer.addConfiguration(new BodyHoverStylingBindings(hoverLayer));
115
116                 selectionLayer = new SelectionLayer(hoverLayer);
117                 
118                 viewportLayer = new ViewportLayer(selectionLayer);
119                 viewportLayer.setRegionName(GridRegion.BODY);
120                 freezeLayer = new FreezeLayer(selectionLayer);
121                 compositeFreezeLayer = new CompositeFreezeLayer(freezeLayer, viewportLayer, selectionLayer);
122
123                 // build the column header layer
124                 columnHeaderDataProvider = new TechTypeColumnHeaderTableDataProvider(bodyDataProvider);
125                 DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
126                 columnHeaderDataLayer.setRowsResizableByDefault(false);
127                 columnHeaderDataLayer.setColumnsResizableByDefault(true);
128                 ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, compositeFreezeLayer, selectionLayer);
129                 ColumnGroupHeaderLayer columnGroupHeaderLayer = new ColumnGroupHeaderLayer(columnHeaderLayer, selectionLayer, columnGroupModel);
130                 columnGroupHeaderLayer.setCalculateHeight(true);
131                 SortHeaderLayer<String> columnSortHeaderLayer = new SortHeaderLayer<>(columnGroupHeaderLayer, sortModel = new TechTypeTableSortModel(bodyDataProvider));
132
133                 // build the row header layer
134                 IDataProvider rowHeaderDataProvider = new TechTypeRowHeaderTableDataProvider(bodyDataProvider);
135                 DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
136                 rowHeaderDataLayer.setRowsResizableByDefault(false);
137                 rowHeaderDataLayer.setColumnsResizableByDefault(false);
138                 RowHeaderLayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, compositeFreezeLayer, selectionLayer);
139
140                 // build the corner layer
141                 IDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider);
142                 DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
143                 ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnSortHeaderLayer);
144
145                 // build the grid layer
146                 GridLayer gridLayer = new GridLayer(compositeFreezeLayer, columnSortHeaderLayer, rowHeaderLayer, cornerLayer);
147                 
148                 table = new NatTable(parent, NatTable.DEFAULT_STYLE_OPTIONS | SWT.BORDER, gridLayer, false);
149                 GridDataFactory.fillDefaults().grab(true, true).applyTo(table);
150                 
151                 // Register a CopyDataCommandHandler that also copies the headers and
152                 // uses the configured IDisplayConverters
153                 CopyDataCommandHandler copyHandler = new CopyDataCommandHandler(
154                                 selectionLayer,
155                                 columnHeaderDataLayer,
156                                 rowHeaderDataLayer);
157                 copyHandler.setCopyFormattedText(true);
158                 gridLayer.registerCommandHandler(copyHandler);
159                 
160                 // initialize paste handler with SWT clipboard
161                 cpb = new Clipboard(getDisplay());
162                 //PasteDataCommandHandler pasteHandler = new PasteDataCommandHandler(bodyDataProvider, bodyDataLayer, selectionLayer, cpb);
163                 //bodyDataLayer.registerCommandHandler(pasteHandler);
164                 
165                 table.addConfiguration(new DefaultNatTableStyleConfiguration());
166                 //table.addConfiguration(new EditingSupportConfiguration(bodyDataProvider));
167                 table.configure();
168         }
169         
170         @Override
171         public void dispose() {
172                 cpb.dispose();
173                 super.dispose();
174         }
175
176     public void setTechTypePath(String path) {
177         bodyDataProvider.setPath(path);
178         table.refresh(true);
179     }
180
181 }