Project

General

Profile

Download (25.5 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.common.monitor.IRemotingProgressMonitor;
88
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
89
import eu.etaxonomy.cdm.model.description.Feature;
90
import eu.etaxonomy.cdm.model.description.FeatureNode;
91
import eu.etaxonomy.cdm.model.description.FeatureTree;
92
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
93
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
94
import eu.etaxonomy.cdm.model.description.State;
95
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
96
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
97
import eu.etaxonomy.taxeditor.model.MessagingUtils;
98
import eu.etaxonomy.taxeditor.store.CdmStore;
99
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
100

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

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

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

    
117
    static final String LABEL_TAXON_ROW = "TAXON_ROW"; //$NON-NLS-1$
118

    
119
    @Inject
120
    private UISynchronize sync;
121

    
122
    private DescriptiveDataSet descriptiveDataSet;
123

    
124
    private NatTable natTable;
125

    
126
    private Map<Integer, Feature> indexToFeatureMap = new HashMap<>();
127

    
128
    private Map<Feature, List<State>> categoricalFeatureToStateMap = new HashMap<>();
129

    
130
    private LinkedMap<String, String> propertyToLabelMap = new LinkedMap<>();
131

    
132
    //FIXME use more concrete generic
133
    private EventList<Object> descriptions;
134

    
135
    private Collection<SpecimenNodeWrapper> specimenCache = null;
136

    
137
    private Map<Feature, CategoricalDataHistogram> featureToHistogramMap = new HashMap<>();
138

    
139
    private Map<Feature, QuantitativeDataStatistics> featureToQuantDataStatisticsMap = new HashMap<>();
140

    
141
    private ListDataProvider<Object> bodyDataProvider;
142

    
143
    private FreezeLayer freezeLayer;
144

    
145
    private List<Feature> features;
146

    
147
    private CharacterMatrixPart part;
148

    
149
    private AbstractLayer topMostLayer;
150

    
151
    private FixedSummaryRowLayer summaryRowLayer;
152

    
153
    private ConfigRegistry configRegistry;
154

    
155
    private DefaultBodyLayerStack bodyLayer;
156

    
157
    private boolean isTreeView = true;
158

    
159
    private CharacterMatrixToolbar toolbar;
160

    
161
    private boolean isShowTooltips = true;
162

    
163
    public CharacterMatrix(Composite parent, CharacterMatrixPart part) {
164
        super(parent, SWT.NONE);
165
        this.part = part;
166
        this.setLayout(new GridLayout());
167

    
168
        createToolBar();
169

    
170
        natTable = new NatTable(this, false);
171

    
172
        createBottomToolbar();
173

    
174
    }
175

    
176
    private void createToolBar(){
177
        toolbar = new CharacterMatrixToolbar(this, SWT.NONE);
178
    }
179

    
180
    @SuppressWarnings("unused")
181
    private void createBottomToolbar() {
182
        new CharacterMatrixBottomToolbar(this, SWT.NONE);
183
    }
184

    
185

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

    
196
    }
197

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

    
207
    public boolean isTreeView() {
208
        return isTreeView;
209
    }
210

    
211
    public void createTable(boolean treeView, boolean freezeSupplementalColumns){
212
        /**
213
         * layers
214
         */
215
        createLayers(treeView);
216

    
217
        /**
218
         * configuration
219
         */
220
        configureNatTable(treeView, configRegistry, topMostLayer, summaryRowLayer);
221

    
222
        /**
223
         * handlers and listeners
224
         */
225
        registerHandlersAndListeners(topMostLayer);
226

    
227
        //grab all space
228
        GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
229

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

    
235
        //initial freeze of supplemental columns
236
        freezeSupplementalColumns(freezeSupplementalColumns);
237

    
238

    
239
        //add tooltip to table
240
        new CategoricalChartTooltip(this);
241
        new QuantitativeChartTooltip(this);
242

    
243
        this.layout();
244
        natTable.doCommand(new ClientAreaResizeCommand(natTable));
245
    }
246

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

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

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

    
274
    }
275

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

    
288
        configRegistry = new ConfigRegistry();
289

    
290

    
291
        /**
292
         * BODY layer
293
         *
294
         *
295

    
296
        CompositeLayer
297
         - (top) SummaryRowLayer
298
         - (bottom) ViewportLayer
299

    
300
             ^
301
        ViewportLayer
302

    
303
             ^
304
        TreeLayer (default visible)
305

    
306
             ^
307
        CompositeFreezeLayer
308
         - viewportLayer
309
         - selectionLayer
310
         - freezeLayer
311

    
312
             ^
313
        FreezeLayer
314

    
315
             ^
316
        SelectionLayer
317

    
318
             ^
319
        ColumnHideShowLayer
320

    
321
             ^
322
        ColumnReorderLayer
323

    
324
             ^
325
        DataLayer
326

    
327
         *
328

    
329
         */
330
        DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
331

    
332
        //register labels
333
        CharacterMatrixConfigLabelAccumulator labelAccumulator = new CharacterMatrixConfigLabelAccumulator(this);
334
        bodyDataLayer.setConfigLabelAccumulator(labelAccumulator);
335

    
336

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

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

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

    
358
        topMostLayer = treeView?treeLayer:compositeFreezeLayer;
359

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

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

    
378

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

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

    
398

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

    
408

    
409
        /**
410
         * corner layer
411
         */
412
        ILayer cornerLayer = new CornerLayer(
413
                new DataLayer(new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider)),
414
                fixedSummaryRowHeaderLayer, sortHeaderLayer);
415

    
416

    
417
        /**
418
         * GRID layer (composition of all other layers)
419
         */
420
        GridLayer gridLayer = new GridLayer(composite, sortHeaderLayer, fixedSummaryRowHeaderLayer, cornerLayer);
421

    
422
        natTable.setLayer(gridLayer);
423

    
424
    }
425

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

    
431
        //propagate single cell selection
432
        natTable.addLayerListener(new CellSelectionListener(part));
433

    
434
        //register handler for view configuration menu
435
        natTable.registerCommandHandler(toolbar.getDisplayPersistenceDialogCommandHandler());
436
    }
437

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

    
447
        applyStyles();
448

    
449
        //add default configuration because autoconfigure is set to false in constructor
450
        natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
451

    
452
        //FIXME: this is for DEBUG ONLY
453
        //        natTable.addConfiguration(new DebugMenuConfiguration(natTable));
454

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

    
461
        natTable.addConfiguration(new CharacterMatrixLabelStyleConfiguration());
462

    
463
        // add the header menu configuration for adding the column header menu
464
        // with hide/show actions
465
        natTable.addConfiguration(new CharacterMatrixHeaderMenuConfiguration(natTable));
466

    
467
        // add custom configuration for data conversion and add column labels to viewport layer
468
        topMostLayer.addConfiguration(new CellEditorDataConversionConfiguration(this));
469

    
470
        //register aggregation configuration
471
        summaryRowLayer.addConfiguration(new AggregationConfiguration(this));
472

    
473
        natTable.configure();
474
    }
475

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

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

    
497
    private void initLabels(int index, Feature feature) {
498
        indexToFeatureMap.put(index+LEADING_COLUMN_COUNT, feature);
499

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

    
517
    public void loadDescriptions(DescriptiveDataSet descriptiveDataSet) {
518
        UUID monitorUuid = CdmStore.getService(IDescriptiveDataSetService.class).monitGetRowWrapper(descriptiveDataSet);
519

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

    
572
    private void loadingDone() {
573
        this.part.loadingDone();
574
    }
575

    
576
    public List<State> getSupportedStatesForCategoricalFeature(Feature feature){
577
        return categoricalFeatureToStateMap.get(feature);
578
    }
579

    
580
    public Map<Integer, Feature> getIndexToFeatureMap() {
581
        return indexToFeatureMap;
582
    }
583

    
584
    public LinkedMap<String, String> getPropertyToLabelMap() {
585
        return propertyToLabelMap;
586
    }
587

    
588
    public void setDirty() {
589
        part.setDirty();
590
    }
591

    
592
    public NatTable getNatTable() {
593
        return natTable;
594
    }
595

    
596
    public EventList<Object> getDescriptions() {
597
        return descriptions;
598
    }
599

    
600
    public DescriptiveDataSet getDescriptiveDataSet() {
601
        return descriptiveDataSet;
602
    }
603

    
604
    public Collection<SpecimenNodeWrapper> getSpecimenCache() {
605
        return specimenCache;
606
    }
607

    
608
    public void setSpecimenCache(Collection<SpecimenNodeWrapper> specimenCache) {
609
        this.specimenCache = specimenCache.stream()
610
                .filter(wrapper ->
611
        //map descriptions on a list of uuids of the described specimen
612
        !this.descriptions.stream()
613
        .map(o->((RowWrapperDTO)o).getSpecimen().getUuid())
614
        .collect(Collectors.toList())
615
        //an check if the specimen to add is already contained
616
        .contains(wrapper.getUuidAndTitleCache().getUuid())
617
        )
618
        .collect(Collectors.toList());
619
    }
620

    
621
    public Properties getNatTableState() {
622
        return toolbar.getNatTableState();
623
    }
624

    
625
    public ListDataProvider<Object> getBodyDataProvider() {
626
        return bodyDataProvider;
627
    }
628

    
629
    DefaultBodyLayerStack getBodyLayer() {
630
        return bodyLayer;
631
    }
632

    
633
    File getStatePropertiesFile() {
634
        return new File(WorkbenchUtility.getBaseLocation(), CHARACTER_MATRIX_STATE_PROPERTIES);
635
    }
636

    
637
    public List<Feature> getFeatures() {
638
        return features;
639
    }
640

    
641
    public void addUpdateResult(UpdateResult result){
642
        part.addUpdateResult(result);
643
    }
644

    
645
    public Map<Feature, CategoricalDataHistogram> getFeatureToHistogramMap() {
646
        return featureToHistogramMap;
647
    }
648

    
649
    public Map<Feature, QuantitativeDataStatistics> getFeatureToQuantDataStatisticsMap() {
650
        return featureToQuantDataStatisticsMap;
651
    }
652

    
653
    public void toogleIsShowTooltips() {
654
        this.isShowTooltips = !this.isShowTooltips;
655
    }
656

    
657
    public boolean isShowTooltips() {
658
        return isShowTooltips;
659
    }
660

    
661
}
(6-6/20)