1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
|
10
|
|
11
|
import java.io.File;
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Collection;
|
14
|
import java.util.Collections;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.HashSet;
|
17
|
import java.util.LinkedList;
|
18
|
import java.util.List;
|
19
|
import java.util.Map;
|
20
|
import java.util.Map.Entry;
|
21
|
import java.util.Properties;
|
22
|
import java.util.Set;
|
23
|
import java.util.TreeSet;
|
24
|
import java.util.UUID;
|
25
|
import java.util.stream.Collectors;
|
26
|
|
27
|
import javax.inject.Inject;
|
28
|
|
29
|
import org.apache.commons.collections4.map.LinkedMap;
|
30
|
import org.eclipse.core.runtime.ICoreRunnable;
|
31
|
import org.eclipse.core.runtime.IProgressMonitor;
|
32
|
import org.eclipse.core.runtime.SubMonitor;
|
33
|
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
|
34
|
import org.eclipse.core.runtime.jobs.Job;
|
35
|
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
|
36
|
import org.eclipse.e4.core.di.annotations.Optional;
|
37
|
import org.eclipse.e4.ui.di.UIEventTopic;
|
38
|
import org.eclipse.e4.ui.di.UISynchronize;
|
39
|
import org.eclipse.e4.ui.services.EMenuService;
|
40
|
import org.eclipse.jface.layout.GridDataFactory;
|
41
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
42
|
import org.eclipse.jface.viewers.StructuredSelection;
|
43
|
import org.eclipse.nebula.widgets.nattable.NatTable;
|
44
|
import org.eclipse.nebula.widgets.nattable.config.AbstractUiBindingConfiguration;
|
45
|
import org.eclipse.nebula.widgets.nattable.config.ConfigRegistry;
|
46
|
import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration;
|
47
|
import org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate;
|
48
|
import org.eclipse.nebula.widgets.nattable.coordinate.Range;
|
49
|
import org.eclipse.nebula.widgets.nattable.copy.command.InternalCopyDataCommandHandler;
|
50
|
import org.eclipse.nebula.widgets.nattable.copy.command.InternalPasteDataCommandHandler;
|
51
|
import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
|
52
|
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
|
53
|
import org.eclipse.nebula.widgets.nattable.export.command.ExportCommandHandler;
|
54
|
import org.eclipse.nebula.widgets.nattable.extension.e4.selection.E4SelectionListener;
|
55
|
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.GlazedListsEventLayer;
|
56
|
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.GlazedListsSortModel;
|
57
|
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.tree.GlazedListTreeData;
|
58
|
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.tree.GlazedListTreeRowModel;
|
59
|
import org.eclipse.nebula.widgets.nattable.freeze.CompositeFreezeLayer;
|
60
|
import org.eclipse.nebula.widgets.nattable.freeze.FreezeHelper;
|
61
|
import org.eclipse.nebula.widgets.nattable.freeze.FreezeLayer;
|
62
|
import org.eclipse.nebula.widgets.nattable.grid.command.ClientAreaResizeCommand;
|
63
|
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider;
|
64
|
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider;
|
65
|
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider;
|
66
|
import org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer;
|
67
|
import org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer;
|
68
|
import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer;
|
69
|
import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer;
|
70
|
import org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer;
|
71
|
import org.eclipse.nebula.widgets.nattable.group.ColumnGroupGroupHeaderLayer;
|
72
|
import org.eclipse.nebula.widgets.nattable.group.ColumnGroupHeaderLayer;
|
73
|
import org.eclipse.nebula.widgets.nattable.group.ColumnGroupModel;
|
74
|
import org.eclipse.nebula.widgets.nattable.layer.AbstractLayer;
|
75
|
import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
|
76
|
import org.eclipse.nebula.widgets.nattable.layer.ILayer;
|
77
|
import org.eclipse.nebula.widgets.nattable.persistence.gui.PersistenceDialog;
|
78
|
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
|
79
|
import org.eclipse.nebula.widgets.nattable.sort.SortHeaderLayer;
|
80
|
import org.eclipse.nebula.widgets.nattable.sort.config.SingleClickSortConfiguration;
|
81
|
import org.eclipse.nebula.widgets.nattable.style.theme.ModernNatTableThemeConfiguration;
|
82
|
import org.eclipse.nebula.widgets.nattable.tree.ITreeRowModel;
|
83
|
import org.eclipse.nebula.widgets.nattable.tree.TreeLayer;
|
84
|
import org.eclipse.nebula.widgets.nattable.tree.command.TreeExpandToLevelCommand;
|
85
|
import org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry;
|
86
|
import org.eclipse.nebula.widgets.nattable.ui.matcher.MouseEventMatcher;
|
87
|
import org.eclipse.nebula.widgets.nattable.ui.menu.PopupMenuAction;
|
88
|
import org.eclipse.nebula.widgets.nattable.ui.menu.PopupMenuBuilder;
|
89
|
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
|
90
|
import org.eclipse.swt.SWT;
|
91
|
import org.eclipse.swt.layout.GridData;
|
92
|
import org.eclipse.swt.layout.GridLayout;
|
93
|
import org.eclipse.swt.widgets.Button;
|
94
|
import org.eclipse.swt.widgets.Composite;
|
95
|
import org.eclipse.swt.widgets.Menu;
|
96
|
|
97
|
import ca.odell.glazedlists.BasicEventList;
|
98
|
import ca.odell.glazedlists.EventList;
|
99
|
import ca.odell.glazedlists.SortedList;
|
100
|
import ca.odell.glazedlists.TreeList;
|
101
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
102
|
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
|
103
|
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
|
104
|
import eu.etaxonomy.cdm.api.service.dto.SpecimenRowWrapperDTO;
|
105
|
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
|
106
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
107
|
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
|
108
|
import eu.etaxonomy.cdm.model.description.Feature;
|
109
|
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
|
110
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
111
|
import eu.etaxonomy.cdm.model.term.TermTree;
|
112
|
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
|
113
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
114
|
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
|
115
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
116
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
117
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
118
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
119
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
120
|
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
|
121
|
|
122
|
/**
|
123
|
* Character matrix editor for editing specimen/taxon descriptions in a table
|
124
|
* @author pplitzner
|
125
|
* @since Nov 26, 2017
|
126
|
*
|
127
|
*/
|
128
|
public class CharacterMatrix extends Composite {
|
129
|
|
130
|
private static final String CHARACTER_MATRIX_STATE_PROPERTIES = "characterMatrixState.properties"; //$NON-NLS-1$
|
131
|
|
132
|
static final int LEADING_COLUMN_COUNT = 4;
|
133
|
static final String TAXON_COLUMN = "taxon_column"; //$NON-NLS-1$
|
134
|
static final String COLLECTOR_COLUMN = "collector_column"; //$NON-NLS-1$
|
135
|
static final String IDENTIFIER_COLUMN = "identifier_column"; //$NON-NLS-1$
|
136
|
static final String COUNTRY_COLUMN = "country_column"; //$NON-NLS-1$
|
137
|
|
138
|
static final String LABEL_TAXON_ROW = "TAXON_ROW"; //$NON-NLS-1$
|
139
|
static final String LABEL_TAXON_AGGREGATED_DESCRIPTION = "TAXON_AGGREGATED_DESCRIPTION"; //$NON-NLS-1$
|
140
|
static final String LABEL_TAXON_AGGREGATED_DESCRIPTION_ICON = "TAXON_AGGREGATED_DESCRIPTION_ICON"; //$NON-NLS-1$
|
141
|
static final String LABEL_TAXON_DEFAULT_DESCRIPTION = "TAXON_DEFAULT_DESCRIPTION"; //$NON-NLS-1$
|
142
|
static final String LABEL_TAXON_DEFAULT_DESCRIPTION_ICON = "TAXON_DEFAULT_DESCRIPTION_ICON"; //$NON-NLS-1$
|
143
|
static final String LABEL_TAXON_LITERATURE_DESCRIPTION = "TAXON_LITERATURE_DESCRIPTION"; //$NON-NLS-1$
|
144
|
static final String LABEL_TAXON_LITERATURE_DESCRIPTION_ICON = "TAXON_LITERATURE_DESCRIPTION_ICON"; //$NON-NLS-1$
|
145
|
static final String LABEL_TAXON_DESCRIPTION = "LABEL_TAXON_DESCRIPTION"; //$NON-NLS-1$
|
146
|
static final String LABEL_DESCRIPTION_HAS_SUPPLEMENTAL_DATA = "LABEL_DESCRIPTION_HAS_SUPPLEMENTAL_DATA"; //$NON-NLS-1$
|
147
|
|
148
|
@Inject
|
149
|
private UISynchronize sync;
|
150
|
|
151
|
@Inject
|
152
|
private EMenuService menuService;
|
153
|
|
154
|
private NatTable natTable;
|
155
|
|
156
|
private Collection<RowWrapperDTO<?>> rowsToSave = new HashSet<>();
|
157
|
|
158
|
private Map<Integer, Feature> indexToFeatureMap = new HashMap<>();
|
159
|
|
160
|
private Map<Feature, List<TermDto>> categoricalFeatureToStateMap = new HashMap<>();
|
161
|
|
162
|
private LinkedMap<String, String> propertyToLabelMap = new LinkedMap<>();
|
163
|
|
164
|
private EventList<Object> descriptions;
|
165
|
|
166
|
private Collection<SpecimenNodeWrapper> specimenCache = null;
|
167
|
|
168
|
private Map<Feature, CategoricalDataHistogram> featureToHistogramMap = new HashMap<>();
|
169
|
|
170
|
private Map<Feature, QuantitativeDataStatistics> featureToQuantDataStatisticsMap = new HashMap<>();
|
171
|
|
172
|
private ListDataProvider<Object> bodyDataProvider;
|
173
|
|
174
|
private FreezeLayer freezeLayer;
|
175
|
|
176
|
private List<Feature> features;
|
177
|
|
178
|
private CharacterMatrixPart part;
|
179
|
|
180
|
private AbstractLayer topMostLayer;
|
181
|
|
182
|
private ConfigRegistry configRegistry;
|
183
|
|
184
|
private MatrixBodyLayerStack bodyLayer;
|
185
|
|
186
|
private boolean isTreeView = true;
|
187
|
|
188
|
private CharacterMatrixToolbar toolbar;
|
189
|
|
190
|
private DescriptionTreeFormat treeFormat;
|
191
|
|
192
|
public CharacterMatrix(Composite parent, CharacterMatrixPart part) {
|
193
|
super(parent, SWT.NONE);
|
194
|
this.part = part;
|
195
|
this.setLayout(new GridLayout());
|
196
|
|
197
|
createToolBar();
|
198
|
|
199
|
natTable = new NatTable(this, false);
|
200
|
|
201
|
createBottomToolbar();
|
202
|
|
203
|
}
|
204
|
|
205
|
private void createToolBar(){
|
206
|
toolbar = new CharacterMatrixToolbar(this, SWT.NONE);
|
207
|
}
|
208
|
|
209
|
@SuppressWarnings("unused")
|
210
|
private void createBottomToolbar() {
|
211
|
new CharacterMatrixBottomToolbar(this, SWT.NONE);
|
212
|
}
|
213
|
|
214
|
|
215
|
private void applyStyles(){
|
216
|
ModernNatTableThemeConfiguration configuration = new ModernNatTableThemeConfiguration();
|
217
|
// NOTE: Getting the colors and fonts from the GUIHelper ensures that
|
218
|
// they are disposed properly (required by SWT)
|
219
|
configuration.cHeaderBgColor = GUIHelper.getColor(211, 211, 211);
|
220
|
configuration.rHeaderBgColor = GUIHelper.getColor(211, 211, 211);
|
221
|
natTable.addConfiguration(configuration);
|
222
|
|
223
|
}
|
224
|
|
225
|
void toggleTreeFlat(boolean isTree, Button btnToggleFlat, Button btnToggleTree, Button btnCollapseAll, Button btnExpandAll, Button btnFreezeSuppInfo) {
|
226
|
isTreeView = isTree;
|
227
|
createTable(isTree, freezeLayer.isFrozen(), true);
|
228
|
btnToggleFlat.setEnabled(isTree);
|
229
|
btnToggleTree.setEnabled(!isTree);
|
230
|
btnCollapseAll.setEnabled(isTree);
|
231
|
btnExpandAll.setEnabled(isTree);
|
232
|
}
|
233
|
|
234
|
public boolean isTreeView() {
|
235
|
return isTreeView;
|
236
|
}
|
237
|
|
238
|
public void createTable(boolean treeView, boolean freezeSupplementalColumns, boolean isInitialExpandToDeepestTaxonLevel){
|
239
|
/**
|
240
|
* layers
|
241
|
*/
|
242
|
createLayers(treeView);
|
243
|
|
244
|
/**
|
245
|
* configuration
|
246
|
*/
|
247
|
configureNatTable(treeView, configRegistry, topMostLayer);
|
248
|
|
249
|
/**
|
250
|
* handlers and listeners
|
251
|
*/
|
252
|
registerHandlersAndListeners();
|
253
|
|
254
|
//grab all space
|
255
|
GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
|
256
|
|
257
|
//update label to current data set
|
258
|
toolbar.getWsLabel().setText(getDescriptiveDataSet().getLabel());
|
259
|
toolbar.getWsLabel().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
|
260
|
toolbar.getWsLabel().getParent().layout();
|
261
|
|
262
|
//initial freeze of supplemental columns
|
263
|
freezeSupplementalColumns(freezeSupplementalColumns);
|
264
|
|
265
|
|
266
|
//add tooltip to table
|
267
|
// new CategoricalChartTooltip(this);
|
268
|
// new QuantitativeChartTooltip(this);
|
269
|
|
270
|
|
271
|
this.layout();
|
272
|
natTable.doCommand(new ClientAreaResizeCommand(natTable));
|
273
|
|
274
|
// expand all taxa
|
275
|
if(isInitialExpandToDeepestTaxonLevel){
|
276
|
Integer deepestTaxonLevel = treeFormat.getDeepestTaxonLevel();
|
277
|
if(deepestTaxonLevel!=null){
|
278
|
natTable.doCommand(new TreeExpandToLevelCommand(deepestTaxonLevel-2));
|
279
|
}
|
280
|
}
|
281
|
|
282
|
// clean up table state
|
283
|
getNatTableState().remove(NatTable.INITIAL_PAINT_COMPLETE_FLAG);
|
284
|
getNatTableState().remove(PersistenceDialog.ACTIVE_VIEW_CONFIGURATION_KEY);
|
285
|
}
|
286
|
|
287
|
private List<Feature> initFeatureList(TermNode<Feature> node){
|
288
|
List<Feature> features = new ArrayList<>();
|
289
|
List<TermNode<Feature>> childNodes = node.getChildNodes();
|
290
|
for (TermNode<Feature> childNode : childNodes) {
|
291
|
features.add(childNode.getTerm());
|
292
|
features.addAll(initFeatureList(childNode));
|
293
|
}
|
294
|
return features;
|
295
|
}
|
296
|
|
297
|
public void initDescriptiveDataSet(){
|
298
|
//get features/columns stored in descriptive data set
|
299
|
TermTree<Feature> tree = getDescriptiveDataSet().getDescriptiveSystem();
|
300
|
features = initFeatureList(tree.getRoot());
|
301
|
|
302
|
Set<Feature> duplicateFeatures = features.stream().filter(i -> Collections.frequency(features, i) >1)
|
303
|
.collect(Collectors.toSet());
|
304
|
|
305
|
if (!duplicateFeatures.isEmpty()) {
|
306
|
throw new IllegalArgumentException("Duplicate features found: "
|
307
|
+ duplicateFeatures.stream().map(feature -> feature.getLabel()).collect(Collectors.joining(",")));
|
308
|
}
|
309
|
|
310
|
//init state data for categorical features
|
311
|
features.forEach(feature->fetchSupportedStates(feature));
|
312
|
|
313
|
descriptions = new BasicEventList<>();
|
314
|
|
315
|
}
|
316
|
|
317
|
private void fetchSupportedStates(Feature feature) {
|
318
|
List<TermDto> supportedStates = CdmStore.getService(IDescriptiveDataSetService.class).getSupportedStatesForFeature(feature.getUuid());
|
319
|
categoricalFeatureToStateMap.put(feature, supportedStates);
|
320
|
}
|
321
|
|
322
|
private void createLayers(boolean treeView) {
|
323
|
SortedList<Object> sortedList = new SortedList<>(descriptions, new MatrixRowComparator());
|
324
|
// wrap the SortedList with the TreeList
|
325
|
treeFormat = new DescriptionTreeFormat(getDescriptiveDataSet());
|
326
|
TreeList<Object> treeList = new TreeList(sortedList, treeFormat, TreeList.NODES_START_COLLAPSED);
|
327
|
/**
|
328
|
* data provider
|
329
|
*/
|
330
|
SpecimenColumnPropertyAccessor columnPropertyAccessor = new SpecimenColumnPropertyAccessor(this);
|
331
|
bodyDataProvider = treeView?new ListDataProvider<>(treeList, columnPropertyAccessor):new ListDataProvider<>(sortedList, columnPropertyAccessor);
|
332
|
|
333
|
configRegistry = new ConfigRegistry();
|
334
|
|
335
|
|
336
|
/**
|
337
|
* BODY layer
|
338
|
*
|
339
|
*
|
340
|
|
341
|
ViewportLayer
|
342
|
|
343
|
^
|
344
|
TreeLayer (default visible)
|
345
|
|
346
|
^
|
347
|
CompositeFreezeLayer
|
348
|
- viewportLayer
|
349
|
- selectionLayer
|
350
|
- freezeLayer
|
351
|
|
352
|
^
|
353
|
FreezeLayer
|
354
|
|
355
|
^
|
356
|
SelectionLayer
|
357
|
|
358
|
^
|
359
|
ColumnHideShowLayer
|
360
|
|
361
|
^
|
362
|
DataLayer
|
363
|
|
364
|
*
|
365
|
|
366
|
*/
|
367
|
DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
|
368
|
bodyDataLayer.registerCommandHandler(new CopyPasteUpdateDataCommandHandler(bodyDataLayer));
|
369
|
|
370
|
//register labels
|
371
|
CharacterMatrixConfigLabelAccumulator labelAccumulator = new CharacterMatrixConfigLabelAccumulator(this);
|
372
|
bodyDataLayer.setConfigLabelAccumulator(labelAccumulator);
|
373
|
|
374
|
|
375
|
propertyToLabelMap.put(TAXON_COLUMN, Messages.CharacterMatrix_TAXON);
|
376
|
propertyToLabelMap.put(COLLECTOR_COLUMN, Messages.CharacterMatrix_COLLECTOR_NO);
|
377
|
propertyToLabelMap.put(IDENTIFIER_COLUMN, Messages.CharacterMatrix_IDENTIFIER);
|
378
|
propertyToLabelMap.put(COUNTRY_COLUMN, Messages.CharacterMatrix_COUNTRY);
|
379
|
for(int i=0;i<features.size();i++){
|
380
|
Feature feature = features.get(i);
|
381
|
initLabels(i, feature);
|
382
|
}
|
383
|
|
384
|
// layer for event handling of GlazedLists and PropertyChanges
|
385
|
GlazedListsEventLayer eventLayer = new GlazedListsEventLayer<>(bodyDataLayer, (EventList)bodyDataProvider.getList());
|
386
|
|
387
|
GlazedListTreeData treeData = new GlazedListTreeData<>(treeList);
|
388
|
ITreeRowModel treeRowModel = new GlazedListTreeRowModel<>(treeData);
|
389
|
|
390
|
// assemble the column groups
|
391
|
LinkedList<ColumnGroupWrapper> columnGroups = new LinkedList<>();
|
392
|
List<TermNode<Feature>> rootChildren = getDescriptiveDataSet().getDescriptiveSystem().getRootChildren();
|
393
|
buildHeader(rootChildren, columnGroups);
|
394
|
|
395
|
bodyLayer = new MatrixBodyLayerStack(eventLayer, columnGroups);
|
396
|
final SelectionLayer selectionLayer = bodyLayer.getSelectionLayer();
|
397
|
freezeLayer = new FreezeLayer(selectionLayer);
|
398
|
final CompositeFreezeLayer compositeFreezeLayer = new CompositeFreezeLayer(
|
399
|
freezeLayer, bodyLayer.getViewportLayer(), selectionLayer);
|
400
|
TreeLayer treeLayer = new TreeLayer(compositeFreezeLayer, treeRowModel);
|
401
|
|
402
|
topMostLayer = treeView?treeLayer:compositeFreezeLayer;
|
403
|
|
404
|
|
405
|
/**
|
406
|
* column header layer
|
407
|
*/
|
408
|
IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(
|
409
|
propertyToLabelMap.values().toArray(new String[] {}), propertyToLabelMap);
|
410
|
DataLayer columnHeaderDataLayer = new DataLayer(columnHeaderDataProvider);
|
411
|
ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, topMostLayer, selectionLayer);
|
412
|
|
413
|
ILayer topHeaderLayer = columnHeaderLayer;
|
414
|
|
415
|
if(!columnGroups.isEmpty()){
|
416
|
// first group level
|
417
|
ColumnGroupWrapper groupLevel1 = columnGroups.get(0);
|
418
|
ColumnGroupHeaderLayer groupLayerLevel1 = null;
|
419
|
ColumnGroupGroupHeaderLayer groupLayerLevel2 = null;
|
420
|
groupLayerLevel1 = new ColumnGroupHeaderLayer(columnHeaderLayer, selectionLayer, groupLevel1.getModel());
|
421
|
for (Entry<TermNode<Feature>, TreeSet<Integer>> entry: groupLevel1.getColumnGroupToIndexMap().entrySet()) {
|
422
|
TermNode<Feature> group = entry.getKey();
|
423
|
TreeSet<Integer> indexList = entry.getValue();
|
424
|
int[] intArray = indexList.stream().mapToInt(Integer::intValue).toArray();
|
425
|
groupLayerLevel1.addColumnsIndexesToGroup(group.getTerm().getTitleCache(), intArray);
|
426
|
groupLayerLevel1.setGroupUnbreakable(indexList.iterator().next());
|
427
|
}
|
428
|
topHeaderLayer = groupLayerLevel1;
|
429
|
|
430
|
// second group level
|
431
|
if(columnGroups.size()>1){
|
432
|
ColumnGroupWrapper groupLevel2 = columnGroups.get(1);
|
433
|
groupLayerLevel2 = new ColumnGroupGroupHeaderLayer(groupLayerLevel1, selectionLayer, groupLevel2.getModel());
|
434
|
for (Entry<TermNode<Feature>, TreeSet<Integer>> entry: groupLevel2.getColumnGroupToIndexMap().entrySet()) {
|
435
|
TermNode<Feature> group = entry.getKey();
|
436
|
TreeSet<Integer> indexList = entry.getValue();
|
437
|
int[] intArray = indexList.stream().mapToInt(Integer::intValue).toArray();
|
438
|
groupLayerLevel2.addColumnsIndexesToGroup(group.getTerm().getTitleCache(), intArray);
|
439
|
groupLayerLevel2.setGroupUnbreakable(indexList.iterator().next());
|
440
|
}
|
441
|
topHeaderLayer = groupLayerLevel2;
|
442
|
}
|
443
|
}
|
444
|
|
445
|
// add the SortHeaderLayer to the column header layer stack
|
446
|
// as we use GlazedLists, we use the GlazedListsSortModel which
|
447
|
// delegates the sorting to the SortedList
|
448
|
final SortHeaderLayer<DescriptionBase> sortHeaderLayer = new SortHeaderLayer<>(
|
449
|
topHeaderLayer,
|
450
|
new GlazedListsSortModel<>(
|
451
|
sortedList,
|
452
|
columnPropertyAccessor,
|
453
|
configRegistry,
|
454
|
columnHeaderDataLayer));
|
455
|
|
456
|
|
457
|
|
458
|
/**
|
459
|
* row header layer
|
460
|
*/
|
461
|
IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
|
462
|
DefaultRowHeaderDataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
|
463
|
RowHeaderLayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer,
|
464
|
topMostLayer, selectionLayer);
|
465
|
|
466
|
|
467
|
/**
|
468
|
* corner layer
|
469
|
*/
|
470
|
ILayer cornerLayer = new CornerLayer(
|
471
|
new DataLayer(new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider)),
|
472
|
rowHeaderLayer, sortHeaderLayer);
|
473
|
|
474
|
|
475
|
/**
|
476
|
* GRID layer (composition of all other layers)
|
477
|
*/
|
478
|
GridLayer gridLayer = new GridLayer(topMostLayer, sortHeaderLayer, rowHeaderLayer, cornerLayer);
|
479
|
|
480
|
natTable.setLayer(gridLayer);
|
481
|
|
482
|
}
|
483
|
|
484
|
private TreeSet<Integer> recurseChildIndexes(TermNode<Feature> node){
|
485
|
TreeSet<Integer> childIndexes = new TreeSet<>();
|
486
|
if(node.getChildCount()>0){
|
487
|
List<TermNode<Feature>> childNodes = node.getChildNodes();
|
488
|
for (TermNode<Feature> childNode: childNodes) {
|
489
|
childIndexes.addAll(recurseChildIndexes(childNode));
|
490
|
}
|
491
|
}
|
492
|
childIndexes.add(features.indexOf(node.getTerm())+LEADING_COLUMN_COUNT);
|
493
|
return childIndexes;
|
494
|
}
|
495
|
|
496
|
private void buildHeader(List<TermNode<Feature>> nodes, LinkedList<ColumnGroupWrapper> columnGroups){
|
497
|
Map<TermNode<Feature>, TreeSet<Integer>> columnGroupToIndexMap = new HashMap<>();
|
498
|
List<TermNode<Feature>> childNodes = new ArrayList<>();
|
499
|
for (TermNode<Feature> node : nodes) {
|
500
|
TreeSet<Integer> childIndexes = recurseChildIndexes(node);
|
501
|
if(childIndexes.size()>1){
|
502
|
// filter out groups that only have one member
|
503
|
columnGroupToIndexMap.put(node, childIndexes);
|
504
|
}
|
505
|
childNodes.addAll(node.getChildNodes());
|
506
|
}
|
507
|
if(!columnGroupToIndexMap.isEmpty()){
|
508
|
columnGroups.addFirst(new ColumnGroupWrapper(new ColumnGroupModel(), columnGroupToIndexMap));
|
509
|
}
|
510
|
if(!childNodes.isEmpty()){
|
511
|
buildHeader(childNodes, columnGroups);
|
512
|
}
|
513
|
}
|
514
|
|
515
|
private void registerHandlersAndListeners() {
|
516
|
natTable.registerCommandHandler(new ExportCommandHandler(natTable));
|
517
|
|
518
|
//selection listener
|
519
|
E4SelectionListener selectionListener = new CellSelectionListener(part.getSelectionService(),
|
520
|
bodyLayer.getSelectionLayer(), bodyDataProvider, part);
|
521
|
bodyLayer.getSelectionLayer().addLayerListener(selectionListener);
|
522
|
selectionListener.setFullySelectedRowsOnly(false);
|
523
|
|
524
|
//register handler for view configuration menu
|
525
|
natTable.registerCommandHandler(toolbar.getDisplayPersistenceDialogCommandHandler());
|
526
|
|
527
|
//register handlers for copy&paste
|
528
|
natTable.registerCommandHandler(
|
529
|
new InternalPasteDataCommandHandler(bodyLayer.getSelectionLayer(), natTable.getInternalCellClipboard()));
|
530
|
natTable.registerCommandHandler(
|
531
|
new InternalCopyDataCommandHandler(bodyLayer.getSelectionLayer(), natTable.getInternalCellClipboard()));
|
532
|
}
|
533
|
|
534
|
private void configureNatTable(boolean treeView,
|
535
|
ConfigRegistry configRegistry,
|
536
|
AbstractLayer topMostLayer) {
|
537
|
/**
|
538
|
* CONFIGURATION
|
539
|
*/
|
540
|
natTable.setConfigRegistry(configRegistry);
|
541
|
|
542
|
applyStyles();
|
543
|
|
544
|
//add default configuration because autoconfigure is set to false in constructor
|
545
|
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
|
546
|
|
547
|
// this is for DEBUG ONLY
|
548
|
// natTable.addConfiguration(new DebugMenuConfiguration(natTable));
|
549
|
|
550
|
// override the default sort configuration and change the mouse bindings
|
551
|
// to sort on a single click
|
552
|
if(!treeView){
|
553
|
natTable.addConfiguration(new SingleClickSortConfiguration());
|
554
|
}
|
555
|
|
556
|
natTable.addConfiguration(new CharacterMatrixLabelStyleConfiguration());
|
557
|
|
558
|
// add the header menu configuration for adding the column header menu
|
559
|
// with hide/show actions
|
560
|
natTable.addConfiguration(new CharacterMatrixHeaderMenuConfiguration(natTable));
|
561
|
|
562
|
// add custom configuration for data conversion and add column labels to viewport layer
|
563
|
topMostLayer.addConfiguration(new CellEditorDataConversionConfiguration(this));
|
564
|
|
565
|
//copy&paste configuration
|
566
|
natTable.addConfiguration(new CopyPasteEditBindings(bodyLayer.getSelectionLayer(), natTable.getInternalCellClipboard()));
|
567
|
|
568
|
//+++CONTEXT MENU+++
|
569
|
menuService.registerContextMenu(natTable, "eu.etaxonomy.taxeditor.editor.popupmenu.charactermatrix"); //$NON-NLS-1$
|
570
|
// get the menu registered by EMenuService
|
571
|
final Menu e4Menu = natTable.getMenu();
|
572
|
// remove the menu reference from NatTable instance
|
573
|
natTable.setMenu(null);
|
574
|
natTable.addConfiguration(
|
575
|
new AbstractUiBindingConfiguration() {
|
576
|
@Override
|
577
|
public void configureUiBindings(
|
578
|
UiBindingRegistry uiBindingRegistry) {
|
579
|
// add e4 menu to NatTable
|
580
|
new PopupMenuBuilder(natTable, e4Menu)
|
581
|
.build();
|
582
|
|
583
|
// register the UI binding for header, corner and body region
|
584
|
uiBindingRegistry.registerMouseDownBinding(
|
585
|
new MouseEventMatcher(
|
586
|
SWT.NONE,
|
587
|
null,
|
588
|
MouseEventMatcher.RIGHT_BUTTON),
|
589
|
new PopupMenuAction(e4Menu));
|
590
|
}
|
591
|
});
|
592
|
|
593
|
natTable.configure();
|
594
|
}
|
595
|
|
596
|
void freezeSupplementalColumns(boolean freeze){
|
597
|
int rightMostFreezeColumIndex = LEADING_COLUMN_COUNT-1;
|
598
|
Collection<Integer> hiddenColumnIndexes = bodyLayer.getColumnHideShowLayer().getHiddenColumnIndexes();
|
599
|
for (Integer integer : hiddenColumnIndexes) {
|
600
|
if(integer<LEADING_COLUMN_COUNT){
|
601
|
rightMostFreezeColumIndex--;
|
602
|
}
|
603
|
}
|
604
|
if(freeze){
|
605
|
FreezeHelper.freeze(freezeLayer, bodyLayer.getViewportLayer(),
|
606
|
new PositionCoordinate(bodyLayer.getViewportLayer(), 0, 0),
|
607
|
new PositionCoordinate(bodyLayer.getViewportLayer(), rightMostFreezeColumIndex, -1));
|
608
|
}
|
609
|
else{
|
610
|
FreezeHelper.unfreeze(freezeLayer, bodyLayer.getViewportLayer());
|
611
|
}
|
612
|
}
|
613
|
|
614
|
private void initLabels(int index, Feature feature) {
|
615
|
indexToFeatureMap.put(index+LEADING_COLUMN_COUNT, feature);
|
616
|
|
617
|
String label = feature.getLabel();
|
618
|
String property = feature.getUuid().toString();
|
619
|
//show unit for quantitative data
|
620
|
if(feature.isSupportsQuantitativeData()){
|
621
|
Set<MeasurementUnit> recommendedMeasurementUnits = feature.getRecommendedMeasurementUnits();
|
622
|
// if(recommendedMeasurementUnits.size()>1){
|
623
|
// MessagingUtils.warningDialog(Messages.CharacterMatrix_INIT_PROBLEM, CharacterMatrix.class,
|
624
|
// String.format(Messages.CharacterMatrix_INIT_PROBLEM_MESSAGE, feature.getLabel()));
|
625
|
// }
|
626
|
if(recommendedMeasurementUnits.size()==1){
|
627
|
MeasurementUnit unit = recommendedMeasurementUnits.iterator().next();
|
628
|
label += " ["+unit.getIdInVocabulary()+"]"; //$NON-NLS-1$ //$NON-NLS-2$
|
629
|
}
|
630
|
}
|
631
|
propertyToLabelMap.put(property, label);
|
632
|
}
|
633
|
|
634
|
public void loadDescriptions(UUID descriptiveDataSetUuid, boolean isInitialExpandToDeepestTaxonLevel) {
|
635
|
UUID monitorUuid = CdmApplicationState.getLongRunningTasksService().monitGetRowWrapper(descriptiveDataSetUuid);
|
636
|
|
637
|
final Collection<RowWrapperDTO> wrappers = new ArrayList<>();
|
638
|
String jobLabel = Messages.CharacterMatrix_LOAD_CHARACTER_DATA;
|
639
|
Job job = Job.create(jobLabel, (ICoreRunnable) monitor -> {
|
640
|
SubMonitor subMonitor = SubMonitor.convert(monitor);
|
641
|
subMonitor.beginTask(jobLabel, IProgressMonitor.UNKNOWN);
|
642
|
IRemotingProgressMonitor remotingMonitor;
|
643
|
try {
|
644
|
remotingMonitor = CdmStore.getProgressMonitorClientManager()
|
645
|
.pollMonitor(jobLabel,
|
646
|
monitorUuid,
|
647
|
50,
|
648
|
null,
|
649
|
(List)null,
|
650
|
subMonitor);
|
651
|
} catch (InterruptedException e) {
|
652
|
MessagingUtils.informationDialog(Messages.CharacterMatrix_LOADING_FAILED_TITLE,
|
653
|
Messages.CharacterMatrix_LOADING_FAILED_MESSAGE);
|
654
|
return;
|
655
|
}
|
656
|
Object result = remotingMonitor.getResult();
|
657
|
if(result instanceof Collection){
|
658
|
wrappers.addAll((Collection<RowWrapperDTO>) result);
|
659
|
}
|
660
|
if(result instanceof Exception){
|
661
|
MessagingUtils.errorDialog("Exception during description loading", this.getClass(), "An exception occured during loading", TaxeditorEditorPlugin.PLUGIN_ID, (Throwable) result, true);
|
662
|
}
|
663
|
else if(wrappers.isEmpty()){
|
664
|
MessagingUtils.informationDialog(Messages.CharacterMatrix_NO_DESCRIPTION_TITLE,
|
665
|
Messages.CharacterMatrix_NO_DESCRIPTION_MESSAGE);
|
666
|
}
|
667
|
monitor.done();
|
668
|
});
|
669
|
job.addJobChangeListener(new JobChangeAdapter(){
|
670
|
@Override
|
671
|
public void done(IJobChangeEvent event) {
|
672
|
sync.syncExec(()->{
|
673
|
List<RowWrapperDTO> rowsWithoutTaxonNode = wrappers.stream().filter(row->row.getTaxonNode()==null).collect(Collectors.toList());
|
674
|
if(!rowsWithoutTaxonNode.isEmpty()){
|
675
|
String collect = rowsWithoutTaxonNode.stream().
|
676
|
map(row->row.getDescription().toString())
|
677
|
.collect(Collectors.joining("\n\n - ")); //$NON-NLS-1$
|
678
|
MessagingUtils.warningDialog(
|
679
|
Messages.CharacterMatrix_NO_NODE_FOUND_TITLE,
|
680
|
this.getClass(),
|
681
|
String.format(Messages.CharacterMatrix_NO_NODE_FOUND_MESSAGE, collect)
|
682
|
);
|
683
|
}
|
684
|
descriptions.clear();
|
685
|
wrappers.stream().filter(row->row.getTaxonNode()!=null).forEach(wrapper->CharacterMatrix.this.descriptions.add(wrapper));
|
686
|
loadingDone(isInitialExpandToDeepestTaxonLevel);
|
687
|
});
|
688
|
}
|
689
|
});
|
690
|
job.schedule();
|
691
|
}
|
692
|
|
693
|
public IStructuredSelection getSelection(){
|
694
|
Set<Range> selectedRowPositions = bodyLayer.getSelectionLayer().getSelectedRowPositions();
|
695
|
List<Object> selectedObjects = new ArrayList<>();
|
696
|
for (Range range : selectedRowPositions) {
|
697
|
for(int i=range.start;i<range.end;i++){
|
698
|
selectedObjects.add(bodyDataProvider.getRowObject(i));
|
699
|
}
|
700
|
}
|
701
|
return new StructuredSelection(selectedObjects);
|
702
|
}
|
703
|
|
704
|
private void loadingDone(boolean isInitialExpandToDeepestTaxonLevel) {
|
705
|
this.part.loadingDone();
|
706
|
createTable(isTreeView, freezeLayer.isFrozen(), isInitialExpandToDeepestTaxonLevel);
|
707
|
}
|
708
|
|
709
|
public List<TermDto> getSupportedStatesForCategoricalFeature(Feature feature){
|
710
|
return categoricalFeatureToStateMap.get(feature);
|
711
|
}
|
712
|
|
713
|
public Map<Integer, Feature> getIndexToFeatureMap() {
|
714
|
return indexToFeatureMap;
|
715
|
}
|
716
|
|
717
|
public LinkedMap<String, String> getPropertyToLabelMap() {
|
718
|
return propertyToLabelMap;
|
719
|
}
|
720
|
|
721
|
public void setDirty() {
|
722
|
part.setDirty();
|
723
|
}
|
724
|
|
725
|
public CharacterMatrixPart getPart() {
|
726
|
return part;
|
727
|
}
|
728
|
|
729
|
public NatTable getNatTable() {
|
730
|
return natTable;
|
731
|
}
|
732
|
|
733
|
public EventList<Object> getDescriptions() {
|
734
|
return descriptions;
|
735
|
}
|
736
|
|
737
|
public DescriptiveDataSet getDescriptiveDataSet() {
|
738
|
return part.getDescriptiveDataSet();
|
739
|
}
|
740
|
|
741
|
public void setDescriptiveDataSet(DescriptiveDataSet dataSet) {
|
742
|
part.setDescriptiveDataSet(dataSet);
|
743
|
}
|
744
|
|
745
|
public Collection<SpecimenNodeWrapper> getSpecimenCache() {
|
746
|
return specimenCache;
|
747
|
}
|
748
|
|
749
|
public void setSpecimenCache(Collection<SpecimenNodeWrapper> specimenCache) {
|
750
|
this.specimenCache = specimenCache.stream()
|
751
|
.filter(wrapper ->
|
752
|
//map descriptions on a list of uuids of the described specimen
|
753
|
!this.descriptions.stream()
|
754
|
.filter(rowWrapper->rowWrapper instanceof SpecimenRowWrapperDTO)
|
755
|
.map(specimenRowWrapper->((SpecimenRowWrapperDTO) specimenRowWrapper).getSpecimen().getUuid())
|
756
|
.collect(Collectors.toList())
|
757
|
//and check if the specimen to add is already contained
|
758
|
.contains(wrapper.getUuidAndTitleCache().getUuid())
|
759
|
)
|
760
|
.collect(Collectors.toList());
|
761
|
}
|
762
|
|
763
|
public void addRowToSave(RowWrapperDTO<?> row){
|
764
|
rowsToSave.add(row);
|
765
|
}
|
766
|
|
767
|
public Collection<RowWrapperDTO<?>> getRowsToSave() {
|
768
|
return rowsToSave;
|
769
|
}
|
770
|
|
771
|
public Properties getNatTableState() {
|
772
|
return toolbar.getNatTableState();
|
773
|
}
|
774
|
|
775
|
public ListDataProvider<Object> getBodyDataProvider() {
|
776
|
return bodyDataProvider;
|
777
|
}
|
778
|
|
779
|
MatrixBodyLayerStack getBodyLayer() {
|
780
|
return bodyLayer;
|
781
|
}
|
782
|
|
783
|
File getStatePropertiesFile() {
|
784
|
return new File(WorkbenchUtility.getBaseLocation(), CHARACTER_MATRIX_STATE_PROPERTIES);
|
785
|
}
|
786
|
|
787
|
public List<Feature> getFeatures() {
|
788
|
return features;
|
789
|
}
|
790
|
|
791
|
public Map<Feature, CategoricalDataHistogram> getFeatureToHistogramMap() {
|
792
|
return featureToHistogramMap;
|
793
|
}
|
794
|
|
795
|
public Map<Feature, QuantitativeDataStatistics> getFeatureToQuantDataStatisticsMap() {
|
796
|
return featureToQuantDataStatisticsMap;
|
797
|
}
|
798
|
|
799
|
public ICdmEntitySession getCdmEntitiySession(){
|
800
|
return part.getCdmEntitySession();
|
801
|
}
|
802
|
|
803
|
@Inject
|
804
|
@Optional
|
805
|
private void updateSpecimenList(@UIEventTopic(WorkbenchEventConstants.REFRESH_DESCRIPTIVE_DATASET)UUID uuid){
|
806
|
if(uuid!= null && uuid.equals(part.getDescriptiveDataSet().getUuid())){
|
807
|
this.specimenCache = null;
|
808
|
|
809
|
}
|
810
|
}
|
811
|
|
812
|
}
|