Project

General

Profile

Download (26.2 KB) Statistics
| Branch: | Tag: | Revision:
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.HashMap;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Properties;
18
import java.util.Set;
19
import java.util.UUID;
20
import java.util.stream.Collectors;
21

    
22
import javax.inject.Inject;
23

    
24
import org.apache.commons.collections4.map.LinkedMap;
25
import org.eclipse.core.runtime.ICoreRunnable;
26
import org.eclipse.core.runtime.IProgressMonitor;
27
import org.eclipse.core.runtime.SubMonitor;
28
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
29
import org.eclipse.core.runtime.jobs.Job;
30
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
31
import org.eclipse.e4.ui.di.UISynchronize;
32
import org.eclipse.jface.layout.GridDataFactory;
33
import org.eclipse.jface.viewers.ComboViewer;
34
import org.eclipse.nebula.widgets.nattable.NatTable;
35
import org.eclipse.nebula.widgets.nattable.config.ConfigRegistry;
36
import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration;
37
import org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate;
38
import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
39
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
40
import org.eclipse.nebula.widgets.nattable.export.command.ExportCommandHandler;
41
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.GlazedListsEventLayer;
42
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.GlazedListsSortModel;
43
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.tree.GlazedListTreeData;
44
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.tree.GlazedListTreeRowModel;
45
import org.eclipse.nebula.widgets.nattable.freeze.CompositeFreezeLayer;
46
import org.eclipse.nebula.widgets.nattable.freeze.FreezeHelper;
47
import org.eclipse.nebula.widgets.nattable.freeze.FreezeLayer;
48
import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
49
import org.eclipse.nebula.widgets.nattable.grid.command.ClientAreaResizeCommand;
50
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider;
51
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider;
52
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider;
53
import org.eclipse.nebula.widgets.nattable.grid.data.FixedSummaryRowHeaderLayer;
54
import org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer;
55
import org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer;
56
import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer;
57
import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer;
58
import org.eclipse.nebula.widgets.nattable.layer.AbstractLayer;
59
import org.eclipse.nebula.widgets.nattable.layer.CompositeLayer;
60
import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
61
import org.eclipse.nebula.widgets.nattable.layer.ILayer;
62
import org.eclipse.nebula.widgets.nattable.layer.cell.ColumnOverrideLabelAccumulator;
63
import org.eclipse.nebula.widgets.nattable.layer.stack.DefaultBodyLayerStack;
64
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
65
import org.eclipse.nebula.widgets.nattable.sort.SortHeaderLayer;
66
import org.eclipse.nebula.widgets.nattable.sort.config.SingleClickSortConfiguration;
67
import org.eclipse.nebula.widgets.nattable.style.HorizontalAlignmentEnum;
68
import org.eclipse.nebula.widgets.nattable.style.theme.ModernNatTableThemeConfiguration;
69
import org.eclipse.nebula.widgets.nattable.summaryrow.FixedSummaryRowLayer;
70
import org.eclipse.nebula.widgets.nattable.summaryrow.SummaryRowLayer;
71
import org.eclipse.nebula.widgets.nattable.tree.ITreeRowModel;
72
import org.eclipse.nebula.widgets.nattable.tree.TreeLayer;
73
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
74
import org.eclipse.swt.SWT;
75
import org.eclipse.swt.layout.GridData;
76
import org.eclipse.swt.layout.GridLayout;
77
import org.eclipse.swt.widgets.Button;
78
import org.eclipse.swt.widgets.Composite;
79

    
80
import ca.odell.glazedlists.BasicEventList;
81
import ca.odell.glazedlists.EventList;
82
import ca.odell.glazedlists.SortedList;
83
import ca.odell.glazedlists.TreeList;
84
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
85
import eu.etaxonomy.cdm.api.service.UpdateResult;
86
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
87
import eu.etaxonomy.cdm.api.service.dto.SpecimenRowWrapperDTO;
88
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
89
import eu.etaxonomy.cdm.model.description.DescriptionBase;
90
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
91
import eu.etaxonomy.cdm.model.description.Feature;
92
import eu.etaxonomy.cdm.model.description.FeatureNode;
93
import eu.etaxonomy.cdm.model.description.FeatureTree;
94
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
95
import eu.etaxonomy.cdm.model.description.State;
96
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
97
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
98
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
99
import eu.etaxonomy.taxeditor.model.MessagingUtils;
100
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
101
import eu.etaxonomy.taxeditor.store.CdmStore;
102
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
103

    
104
/**
105
 * Character matrix editor for editing specimen/taxon descriptions in a table
106
 * @author pplitzner
107
 * @since Nov 26, 2017
108
 *
109
 */
110
public class CharacterMatrix extends Composite {
111

    
112
    private static final String CHARACTER_MATRIX_STATE_PROPERTIES = "characterMatrixState.properties"; //$NON-NLS-1$
113

    
114
    static final int LEADING_COLUMN_COUNT = 4;
115
    static final String TAXON_COLUMN = "taxon_column"; //$NON-NLS-1$
116
    static final String COLLECTOR_COLUMN = "collector_column"; //$NON-NLS-1$
117
    static final String IDENTIFIER_COLUMN = "identifier_column"; //$NON-NLS-1$
118
    static final String COUNTRY_COLUMN = "country_column"; //$NON-NLS-1$
119

    
120
    static final String LABEL_TAXON_ROW = "TAXON_ROW"; //$NON-NLS-1$
121
    static final String LABEL_TAXON_AGGREGATED_DESCRIPTION = "TAXON_AGGREGATED_DESCRIPTION"; //$NON-NLS-1$
122
    static final String LABEL_TAXON_DESCRIPTION = "LABEL_TAXON_DESCRIPTION"; //$NON-NLS-1$
123

    
124
    @Inject
125
    private UISynchronize sync;
126

    
127
    private DescriptiveDataSet descriptiveDataSet;
128

    
129
    private NatTable natTable;
130

    
131
    private Map<Integer, Feature> indexToFeatureMap = new HashMap<>();
132

    
133
    private Map<Feature, List<State>> categoricalFeatureToStateMap = new HashMap<>();
134

    
135
    private LinkedMap<String, String> propertyToLabelMap = new LinkedMap<>();
136

    
137
    private EventList<Object> descriptions;
138

    
139
    private Collection<SpecimenNodeWrapper> specimenCache = null;
140

    
141
    private Map<Feature, CategoricalDataHistogram> featureToHistogramMap = new HashMap<>();
142

    
143
    private Map<Feature, QuantitativeDataStatistics> featureToQuantDataStatisticsMap = new HashMap<>();
144

    
145
    private ListDataProvider<Object> bodyDataProvider;
146

    
147
    private FreezeLayer freezeLayer;
148

    
149
    private List<Feature> features;
150

    
151
    private CharacterMatrixPart part;
152

    
153
    private AbstractLayer topMostLayer;
154

    
155
    private FixedSummaryRowLayer summaryRowLayer;
156

    
157
    private ConfigRegistry configRegistry;
158

    
159
    private DefaultBodyLayerStack bodyLayer;
160

    
161
    private boolean isTreeView = true;
162

    
163
    private CharacterMatrixToolbar toolbar;
164

    
165
    private boolean isShowTooltips = true;
166

    
167
    public CharacterMatrix(Composite parent, CharacterMatrixPart part) {
168
        super(parent, SWT.NONE);
169
        this.part = part;
170
        this.setLayout(new GridLayout());
171

    
172
        createToolBar();
173

    
174
        natTable = new NatTable(this, false);
175

    
176
        createBottomToolbar();
177

    
178
    }
179

    
180
    private void createToolBar(){
181
        toolbar = new CharacterMatrixToolbar(this, SWT.NONE);
182
    }
183

    
184
    @SuppressWarnings("unused")
185
    private void createBottomToolbar() {
186
        new CharacterMatrixBottomToolbar(this, SWT.NONE);
187
    }
188

    
189

    
190
    private void applyStyles(){
191
        ModernNatTableThemeConfiguration configuration = new ModernNatTableThemeConfiguration();
192
        configuration.summaryRowHAlign = HorizontalAlignmentEnum.CENTER;
193
        // NOTE: Getting the colors and fonts from the GUIHelper ensures that
194
        // they are disposed properly (required by SWT)
195
        configuration.cHeaderBgColor = GUIHelper.getColor(211, 211, 211);
196
        configuration.rHeaderBgColor = GUIHelper.getColor(211, 211, 211);
197
        natTable.addConfiguration(configuration);
198

    
199
    }
200

    
201
    void toggleTreeFlat(boolean isTree, Button btnToggleFlat, Button btnToggleTree, Button btnCollapseAll, Button btnExpandAll, Button btnFreezeSuppInfo) {
202
        isTreeView = isTree;
203
        createTable(isTree, freezeLayer.isFrozen());
204
        btnToggleFlat.setEnabled(isTree);
205
        btnToggleTree.setEnabled(!isTree);
206
        btnCollapseAll.setEnabled(isTree);
207
        btnExpandAll.setEnabled(isTree);
208
    }
209

    
210
    public boolean isTreeView() {
211
        return isTreeView;
212
    }
213

    
214
    public void createTable(boolean treeView, boolean freezeSupplementalColumns){
215
        /**
216
         * layers
217
         */
218
        createLayers(treeView);
219

    
220
        /**
221
         * configuration
222
         */
223
        configureNatTable(treeView, configRegistry, topMostLayer, summaryRowLayer);
224

    
225
        /**
226
         * handlers and listeners
227
         */
228
        registerHandlersAndListeners(topMostLayer);
229

    
230
        //grab all space
231
        GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
232

    
233
        //update label to current data set
234
        toolbar.getWsLabel().setText(descriptiveDataSet.getLabel());
235
        toolbar.getWsLabel().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
236
        toolbar.getWsLabel().getParent().layout();
237

    
238
        //initial freeze of supplemental columns
239
        freezeSupplementalColumns(freezeSupplementalColumns);
240

    
241

    
242
        //add tooltip to table
243
        new CategoricalChartTooltip(this);
244
        new QuantitativeChartTooltip(this);
245

    
246
        this.layout();
247
        natTable.doCommand(new ClientAreaResizeCommand(natTable));
248
    }
249

    
250
    private List<Feature> initFeatureList(FeatureNode node){
251
        List<Feature> features = new ArrayList<>();
252
        node.getChildNodes().forEach(childNode->
253
                {
254
                    features.add(childNode.getFeature());
255
                    features.addAll(initFeatureList(childNode));
256
                });
257
        return features;
258
    }
259

    
260
    public void initDescriptiveDataSet(DescriptiveDataSet descriptiveDataSet){
261
        this.descriptiveDataSet = descriptiveDataSet;
262
        //get features/columns stored in descriptive data set
263
        FeatureTree tree = descriptiveDataSet.getDescriptiveSystem();
264
        features = initFeatureList(tree.getRoot());
265

    
266
        //init state data for categorical features
267
        features.forEach(feature->
268
        {
269
            if(feature.isSupportsCategoricalData()){
270
                List<State> supportedStates = new ArrayList<>();
271
                feature.getSupportedCategoricalEnumerations().forEach(voc->supportedStates.addAll(voc.getTerms()));
272
                categoricalFeatureToStateMap.put(feature, supportedStates);
273
            }
274
        });
275
        descriptions = new BasicEventList<>();
276

    
277
    }
278

    
279
    private void createLayers(boolean treeView) {
280
        // use the SortedList constructor with 'null' for the Comparator
281
        // because the Comparator will be set by configuration
282
        SortedList<Object> sortedList = new SortedList<>(descriptions, new MatrixRowComparator());
283
        // wrap the SortedList with the TreeList
284
        TreeList<Object> treeList = new TreeList(sortedList, new DescriptionTreeFormat(descriptiveDataSet), TreeList.NODES_START_EXPANDED);
285
        /**
286
         * data provider
287
         */
288
        SpecimenColumnPropertyAccessor columnPropertyAccessor = new SpecimenColumnPropertyAccessor(this);
289
        bodyDataProvider = treeView?new ListDataProvider<>(treeList, columnPropertyAccessor):new ListDataProvider<>(sortedList, columnPropertyAccessor);
290

    
291
        configRegistry = new ConfigRegistry();
292

    
293

    
294
        /**
295
         * BODY layer
296
         *
297
         *
298

    
299
        CompositeLayer
300
         - (top) SummaryRowLayer
301
         - (bottom) ViewportLayer
302

    
303
             ^
304
        ViewportLayer
305

    
306
             ^
307
        TreeLayer (default visible)
308

    
309
             ^
310
        CompositeFreezeLayer
311
         - viewportLayer
312
         - selectionLayer
313
         - freezeLayer
314

    
315
             ^
316
        FreezeLayer
317

    
318
             ^
319
        SelectionLayer
320

    
321
             ^
322
        ColumnHideShowLayer
323

    
324
             ^
325
        ColumnReorderLayer
326

    
327
             ^
328
        DataLayer
329

    
330
         *
331

    
332
         */
333
        DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
334

    
335
        //register labels
336
        CharacterMatrixConfigLabelAccumulator labelAccumulator = new CharacterMatrixConfigLabelAccumulator(this);
337
        bodyDataLayer.setConfigLabelAccumulator(labelAccumulator);
338

    
339

    
340
        propertyToLabelMap.put(TAXON_COLUMN, Messages.CharacterMatrix_TAXON);
341
        propertyToLabelMap.put(COLLECTOR_COLUMN, Messages.CharacterMatrix_COLLECTOR_NO);
342
        propertyToLabelMap.put(IDENTIFIER_COLUMN, Messages.CharacterMatrix_IDENTIFIER);
343
        propertyToLabelMap.put(COUNTRY_COLUMN, Messages.CharacterMatrix_COUNTRY);
344
        for(int i=0;i<features.size();i++){
345
            Feature feature = features.get(i);
346
            initLabels(i, feature);
347
        }
348

    
349
        // layer for event handling of GlazedLists and PropertyChanges
350
        GlazedListsEventLayer eventLayer = new GlazedListsEventLayer<>(bodyDataLayer, treeList);
351
        GlazedListTreeData treeData = new GlazedListTreeData<>(treeList);
352
        ITreeRowModel treeRowModel = new GlazedListTreeRowModel<>(treeData);
353

    
354
        bodyLayer = new DefaultBodyLayerStack(eventLayer);
355
        final SelectionLayer selectionLayer = bodyLayer.getSelectionLayer();
356
        freezeLayer = new FreezeLayer(selectionLayer);
357
        final CompositeFreezeLayer compositeFreezeLayer = new CompositeFreezeLayer(
358
                freezeLayer, bodyLayer.getViewportLayer(), selectionLayer);
359
        TreeLayer treeLayer = new TreeLayer(compositeFreezeLayer, treeRowModel);
360

    
361
        topMostLayer = treeView?treeLayer:compositeFreezeLayer;
362

    
363
        summaryRowLayer = new FixedSummaryRowLayer(bodyDataLayer, topMostLayer, configRegistry, false);
364
        //regoster labels with summary prefix for summary layer
365
        ColumnOverrideLabelAccumulator summaryColumnLabelAccumulator =new ColumnOverrideLabelAccumulator(bodyDataLayer);
366
        summaryRowLayer.setConfigLabelAccumulator(summaryColumnLabelAccumulator);
367
        for(int i=0;i<features.size();i++){
368
            Feature feature = features.get(i);
369
            summaryColumnLabelAccumulator.registerColumnOverrides(
370
                    i+LEADING_COLUMN_COUNT,
371
                    SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX+MatrixUtility.getProperty(feature));
372
        }
373
        // because the horizontal dependency is the ViewportLayer
374
        // we need to set the composite dependency to false
375
        summaryRowLayer.setHorizontalCompositeDependency(false);
376

    
377
        CompositeLayer composite = new CompositeLayer(1, 2);
378
        composite.setChildLayer("SUMMARY", summaryRowLayer, 0, 0); //$NON-NLS-1$
379
        composite.setChildLayer(GridRegion.BODY, topMostLayer, 0, 1);
380

    
381

    
382
        /**
383
         * column header layer
384
         */
385
        IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(
386
                propertyToLabelMap.values().toArray(new String[] {}), propertyToLabelMap);
387
        DataLayer columnHeaderDataLayer = new DataLayer(columnHeaderDataProvider);
388
        ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, topMostLayer, selectionLayer);
389

    
390
        // add the SortHeaderLayer to the column header layer stack
391
        // as we use GlazedLists, we use the GlazedListsSortModel which
392
        // delegates the sorting to the SortedList
393
        final SortHeaderLayer<DescriptionBase> sortHeaderLayer = new SortHeaderLayer<>(
394
                columnHeaderLayer,
395
                new GlazedListsSortModel<>(
396
                        sortedList,
397
                        columnPropertyAccessor,
398
                        configRegistry,
399
                        columnHeaderDataLayer));
400

    
401

    
402
        /**
403
         * row header layer
404
         */
405
        IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
406
        DefaultRowHeaderDataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
407
        FixedSummaryRowHeaderLayer fixedSummaryRowHeaderLayer = new FixedSummaryRowHeaderLayer(rowHeaderDataLayer,
408
                composite, selectionLayer);
409
        fixedSummaryRowHeaderLayer.setSummaryRowLabel("\u2211"); //$NON-NLS-1$
410

    
411

    
412
        /**
413
         * corner layer
414
         */
415
        ILayer cornerLayer = new CornerLayer(
416
                new DataLayer(new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider)),
417
                fixedSummaryRowHeaderLayer, sortHeaderLayer);
418

    
419

    
420
        /**
421
         * GRID layer (composition of all other layers)
422
         */
423
        GridLayer gridLayer = new GridLayer(composite, sortHeaderLayer, fixedSummaryRowHeaderLayer, cornerLayer);
424

    
425
        natTable.setLayer(gridLayer);
426

    
427
    }
428

    
429
    private void registerHandlersAndListeners(AbstractLayer topMostLayer) {
430
        // add the ExportCommandHandler to the ViewportLayer in order to make
431
        // exporting work
432
        topMostLayer.registerCommandHandler(new ExportCommandHandler(topMostLayer));
433

    
434
        //propagate single cell selection
435
        natTable.addLayerListener(new CellSelectionListener(part));
436

    
437
        //register handler for view configuration menu
438
        natTable.registerCommandHandler(toolbar.getDisplayPersistenceDialogCommandHandler());
439
    }
440

    
441
    private void configureNatTable(boolean treeView,
442
            ConfigRegistry configRegistry,
443
            AbstractLayer topMostLayer,
444
            FixedSummaryRowLayer summaryRowLayer) {
445
        /**
446
         * CONFIGURATION
447
         */
448
        natTable.setConfigRegistry(configRegistry);
449

    
450
        applyStyles();
451

    
452
        //add default configuration because autoconfigure is set to false in constructor
453
        natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
454

    
455
        //FIXME: this is for DEBUG ONLY
456
        //        natTable.addConfiguration(new DebugMenuConfiguration(natTable));
457

    
458
        // override the default sort configuration and change the mouse bindings
459
        // to sort on a single click
460
        if(!treeView){
461
            natTable.addConfiguration(new SingleClickSortConfiguration());
462
        }
463

    
464
        natTable.addConfiguration(new CharacterMatrixLabelStyleConfiguration());
465

    
466
        // add the header menu configuration for adding the column header menu
467
        // with hide/show actions
468
        natTable.addConfiguration(new CharacterMatrixHeaderMenuConfiguration(natTable));
469

    
470
        // add custom configuration for data conversion and add column labels to viewport layer
471
        topMostLayer.addConfiguration(new CellEditorDataConversionConfiguration(this));
472

    
473
        //register aggregation configuration
474
        summaryRowLayer.addConfiguration(new AggregationConfiguration(this));
475

    
476
        natTable.configure();
477
    }
478

    
479
    void freezeSupplementalColumns(boolean freeze){
480
        if(freeze){
481
            FreezeHelper.freeze(freezeLayer, bodyLayer.getViewportLayer(),
482
                    new PositionCoordinate(bodyLayer.getViewportLayer(), 0, 0),
483
                    new PositionCoordinate(bodyLayer.getViewportLayer(), LEADING_COLUMN_COUNT-1, -1));
484
        }
485
        else{
486
            FreezeHelper.unfreeze(freezeLayer, bodyLayer.getViewportLayer());
487
        }
488
    }
489

    
490
    void selectStateItem(ComboViewer comboStates, String stateName){
491
        String[] items = comboStates.getCombo().getItems();
492
        for(int i=0;i<items.length;i++){
493
            if(items[i].equals(stateName)){
494
                comboStates.getCombo().select(i);
495
                break;
496
            }
497
        }
498
    }
499

    
500
    private void initLabels(int index, Feature feature) {
501
        indexToFeatureMap.put(index+LEADING_COLUMN_COUNT, feature);
502

    
503
        String featureLabel = feature.getLabel();
504
        String property = featureLabel;
505
        //show unit for quantitative data
506
        if(feature.isSupportsQuantitativeData()){
507
            Set<MeasurementUnit> recommendedMeasurementUnits = feature.getRecommendedMeasurementUnits();
508
            if(recommendedMeasurementUnits.size()>1){
509
                MessagingUtils.warningDialog(Messages.CharacterMatrix_INIT_PROBLEM, CharacterMatrix.class,
510
                        String.format(Messages.CharacterMatrix_INIT_PROBLEM_MESSAGE, feature.getLabel()));
511
            }
512
            if(recommendedMeasurementUnits.size()==1){
513
                MeasurementUnit unit = recommendedMeasurementUnits.iterator().next();
514
                featureLabel += " ["+unit.getIdInVocabulary()+"]"; //$NON-NLS-1$ //$NON-NLS-2$
515
            }
516
        }
517
        propertyToLabelMap.put(property, featureLabel);
518
    }
519

    
520
    public void loadDescriptions(DescriptiveDataSet descriptiveDataSet) {
521
        UUID monitorUuid = CdmStore.getService(IDescriptiveDataSetService.class).monitGetRowWrapper(descriptiveDataSet);
522

    
523
        final Collection<RowWrapperDTO> wrappers = new ArrayList<>();
524
        String jobLabel = Messages.CharacterMatrix_LOAD_CHARACTER_DATA;
525
        Job job = Job.create(jobLabel, (ICoreRunnable) monitor -> {
526
            SubMonitor subMonitor = SubMonitor.convert(monitor);
527
            subMonitor.beginTask(jobLabel, IProgressMonitor.UNKNOWN);
528
            IRemotingProgressMonitor remotingMonitor;
529
            try {
530
                 remotingMonitor = CdmStore.getProgressMonitorClientManager()
531
                .pollMonitor(jobLabel,
532
                        monitorUuid,
533
                        50,
534
                        null,
535
                        (List)null,
536
                        subMonitor);
537
            } catch (InterruptedException e) {
538
                MessagingUtils.informationDialog(Messages.CharacterMatrix_LOADING_FAILED_TITLE,
539
                        Messages.CharacterMatrix_LOADING_FAILED_MESSAGE);
540
                return;
541
            }
542
            Object result = remotingMonitor.getResult();
543
            if(result instanceof Collection){
544
                wrappers.addAll((Collection<RowWrapperDTO>) result);
545
            }
546
            if(result instanceof Exception){
547
                MessagingUtils.errorDialog("Exception during description loading", this.getClass(), "An exception occured during loading", TaxeditorEditorPlugin.PLUGIN_ID, (Throwable) result, true);
548
            }
549
            else if(wrappers.isEmpty()){
550
                MessagingUtils.informationDialog(Messages.CharacterMatrix_NO_DESCRIPTION_TITLE,
551
                        Messages.CharacterMatrix_NO_DESCRIPTION_MESSAGE);
552
            }
553
            monitor.done();
554
        });
555
        job.addJobChangeListener(new JobChangeAdapter(){
556
            @Override
557
            public void done(IJobChangeEvent event) {
558
                sync.syncExec(()->{
559
                    List<RowWrapperDTO> rowsWithoutTaxonNode = wrappers.stream().filter(row->row.getTaxonNode()==null).collect(Collectors.toList());
560
                    if(!rowsWithoutTaxonNode.isEmpty()){
561
                        String collect = rowsWithoutTaxonNode.stream().
562
                        map(row->row.getDescription().toString())
563
                        .collect(Collectors.joining("\n\n - ")); //$NON-NLS-1$
564
                        MessagingUtils.warningDialog(
565
                                Messages.CharacterMatrix_NO_NODE_FOUND_TITLE,
566
                                this.getClass(),
567
                                String.format(Messages.CharacterMatrix_NO_NODE_FOUND_MESSAGE, collect)
568
                                );
569
                    }
570
                    wrappers.stream().filter(row->row.getTaxonNode()!=null).forEach(wrapper->CharacterMatrix.this.descriptions.add(wrapper));
571
                    loadingDone();
572
                });
573
            }
574
        });
575
        job.schedule();
576
    }
577

    
578
    private void loadingDone() {
579
        this.part.loadingDone();
580
    }
581

    
582
    public List<State> getSupportedStatesForCategoricalFeature(Feature feature){
583
        return categoricalFeatureToStateMap.get(feature);
584
    }
585

    
586
    public Map<Integer, Feature> getIndexToFeatureMap() {
587
        return indexToFeatureMap;
588
    }
589

    
590
    public LinkedMap<String, String> getPropertyToLabelMap() {
591
        return propertyToLabelMap;
592
    }
593

    
594
    public void setDirty() {
595
        part.setDirty();
596
    }
597

    
598
    public NatTable getNatTable() {
599
        return natTable;
600
    }
601

    
602
    public EventList<Object> getDescriptions() {
603
        return descriptions;
604
    }
605

    
606
    public DescriptiveDataSet getDescriptiveDataSet() {
607
        return descriptiveDataSet;
608
    }
609

    
610
    public Collection<SpecimenNodeWrapper> getSpecimenCache() {
611
        return specimenCache;
612
    }
613

    
614
    public void setSpecimenCache(Collection<SpecimenNodeWrapper> specimenCache) {
615
        this.specimenCache = specimenCache.stream()
616
                .filter(wrapper ->
617
        //map descriptions on a list of uuids of the described specimen
618
        !this.descriptions.stream()
619
        .filter(rowWrapper->rowWrapper instanceof SpecimenRowWrapperDTO)
620
        .map(specimenRowWrapper->((SpecimenRowWrapperDTO) specimenRowWrapper).getSpecimen().getUuid())
621
        .collect(Collectors.toList())
622
        //an check if the specimen to add is already contained
623
        .contains(wrapper.getUuidAndTitleCache().getUuid())
624
        )
625
        .collect(Collectors.toList());
626
    }
627

    
628
    public Properties getNatTableState() {
629
        return toolbar.getNatTableState();
630
    }
631

    
632
    public ListDataProvider<Object> getBodyDataProvider() {
633
        return bodyDataProvider;
634
    }
635

    
636
    DefaultBodyLayerStack getBodyLayer() {
637
        return bodyLayer;
638
    }
639

    
640
    File getStatePropertiesFile() {
641
        return new File(WorkbenchUtility.getBaseLocation(), CHARACTER_MATRIX_STATE_PROPERTIES);
642
    }
643

    
644
    public List<Feature> getFeatures() {
645
        return features;
646
    }
647

    
648
    public void addUpdateResult(UpdateResult result){
649
        part.addUpdateResult(result);
650
    }
651

    
652
    public Map<Feature, CategoricalDataHistogram> getFeatureToHistogramMap() {
653
        return featureToHistogramMap;
654
    }
655

    
656
    public Map<Feature, QuantitativeDataStatistics> getFeatureToQuantDataStatisticsMap() {
657
        return featureToQuantDataStatisticsMap;
658
    }
659

    
660
    public void toogleIsShowTooltips() {
661
        this.isShowTooltips = !this.isShowTooltips;
662
    }
663

    
664
    public boolean isShowTooltips() {
665
        return isShowTooltips;
666
    }
667

    
668
    public ICdmEntitySession getCdmEntitiySession(){
669
        return part.getCdmEntitySession();
670
    }
671

    
672
}
(6-6/19)