Project

General

Profile

Download (33.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 org.apache.commons.collections4.map.LinkedMap;
23
import org.eclipse.core.runtime.ICoreRunnable;
24
import org.eclipse.core.runtime.IProgressMonitor;
25
import org.eclipse.core.runtime.jobs.Job;
26
import org.eclipse.jface.layout.GridDataFactory;
27
import org.eclipse.jface.viewers.ComboViewer;
28
import org.eclipse.jface.viewers.StructuredSelection;
29
import org.eclipse.nebula.widgets.nattable.NatTable;
30
import org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration;
31
import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
32
import org.eclipse.nebula.widgets.nattable.config.ConfigRegistry;
33
import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration;
34
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
35
import org.eclipse.nebula.widgets.nattable.config.IEditableRule;
36
import org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate;
37
import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
38
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
39
import org.eclipse.nebula.widgets.nattable.edit.EditConfigAttributes;
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.data.DefaultColumnHeaderDataProvider;
50
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider;
51
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider;
52
import org.eclipse.nebula.widgets.nattable.grid.data.FixedSummaryRowHeaderLayer;
53
import org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer;
54
import org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer;
55
import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer;
56
import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer;
57
import org.eclipse.nebula.widgets.nattable.layer.AbstractLayer;
58
import org.eclipse.nebula.widgets.nattable.layer.CompositeLayer;
59
import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
60
import org.eclipse.nebula.widgets.nattable.layer.ILayer;
61
import org.eclipse.nebula.widgets.nattable.layer.ILayerListener;
62
import org.eclipse.nebula.widgets.nattable.layer.cell.ColumnOverrideLabelAccumulator;
63
import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
64
import org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent;
65
import org.eclipse.nebula.widgets.nattable.layer.stack.DefaultBodyLayerStack;
66
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
67
import org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent;
68
import org.eclipse.nebula.widgets.nattable.sort.SortHeaderLayer;
69
import org.eclipse.nebula.widgets.nattable.sort.config.SingleClickSortConfiguration;
70
import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes;
71
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
72
import org.eclipse.nebula.widgets.nattable.style.HorizontalAlignmentEnum;
73
import org.eclipse.nebula.widgets.nattable.style.Style;
74
import org.eclipse.nebula.widgets.nattable.style.theme.ModernNatTableThemeConfiguration;
75
import org.eclipse.nebula.widgets.nattable.summaryrow.DefaultSummaryRowConfiguration;
76
import org.eclipse.nebula.widgets.nattable.summaryrow.FixedSummaryRowLayer;
77
import org.eclipse.nebula.widgets.nattable.summaryrow.ISummaryProvider;
78
import org.eclipse.nebula.widgets.nattable.summaryrow.SummaryRowConfigAttributes;
79
import org.eclipse.nebula.widgets.nattable.summaryrow.SummaryRowLayer;
80
import org.eclipse.nebula.widgets.nattable.tree.ITreeRowModel;
81
import org.eclipse.nebula.widgets.nattable.tree.TreeLayer;
82
import org.eclipse.nebula.widgets.nattable.ui.menu.AbstractHeaderMenuConfiguration;
83
import org.eclipse.nebula.widgets.nattable.ui.menu.PopupMenuBuilder;
84
import org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer;
85
import org.eclipse.swt.SWT;
86
import org.eclipse.swt.layout.GridData;
87
import org.eclipse.swt.layout.GridLayout;
88
import org.eclipse.swt.widgets.Button;
89
import org.eclipse.swt.widgets.Composite;
90

    
91
import ca.odell.glazedlists.BasicEventList;
92
import ca.odell.glazedlists.EventList;
93
import ca.odell.glazedlists.SortedList;
94
import ca.odell.glazedlists.TreeList;
95
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
96
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
97
import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
98
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
99
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
100
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
101
import eu.etaxonomy.cdm.model.description.Feature;
102
import eu.etaxonomy.cdm.model.description.FeatureNode;
103
import eu.etaxonomy.cdm.model.description.FeatureTree;
104
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
105
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
106
import eu.etaxonomy.cdm.model.description.State;
107
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
108
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.categorical.CategoricalDataCellEditor;
109
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.categorical.CategoricalDataDisplayConverter;
110
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.quantitative.QuantitativeDataCellEditor;
111
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.quantitative.QuantitativeDataDisplayConverter;
112
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.supplementalInfo.SupplementalInfoDisplayConverter;
113
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
114
import eu.etaxonomy.taxeditor.model.MessagingUtils;
115
import eu.etaxonomy.taxeditor.store.CdmStore;
116
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
117

    
118
/**
119
 * Character matrix editor for editing specimen/taxon descriptions in a table
120
 * @author pplitzner
121
 * @since Nov 26, 2017
122
 *
123
 */
124
public class CharacterMatrix extends Composite {
125

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

    
128
    private static final int LEADING_COLUMN_COUNT = 4;
129
    private static final String TAXON_COLUMN = "taxon_column"; //$NON-NLS-1$
130
    private static final String COLLECTOR_COLUMN = "collector_column"; //$NON-NLS-1$
131
    private static final String IDENTIFIER_COLUMN = "identifier_column"; //$NON-NLS-1$
132
    private static final String COUNTRY_COLUMN = "country_column"; //$NON-NLS-1$
133

    
134
    private DescriptiveDataSet descriptiveDataSet;
135

    
136
    private NatTable natTable;
137

    
138
    private Map<Integer, Feature> indexToFeatureMap = new HashMap<>();
139

    
140
    private Map<Feature, List<State>> categoricalFeatureToStateMap = new HashMap<>();
141

    
142
    private LinkedMap<String, String> propertyToLabelMap = new LinkedMap<>();
143

    
144
    private EventList<Object> descriptions;
145

    
146
    private Collection<SpecimenNodeWrapper> specimenCache = null;
147

    
148
    private ListDataProvider<Object> bodyDataProvider;
149

    
150
    private FreezeLayer freezeLayer;
151

    
152
    private ViewportLayer viewportLayer;
153

    
154
    private List<Feature> features;
155

    
156
    private CharacterMatrixPart part;
157

    
158
    private AbstractLayer topMostLayer;
159

    
160
    private FixedSummaryRowLayer summaryRowLayer;
161

    
162
    private ConfigRegistry configRegistry;
163

    
164
    private DefaultBodyLayerStack bodyLayer;
165

    
166
    private boolean isTreeView = true;
167

    
168
    private CharacterMatrixToolbar toolbar;
169

    
170

    
171
    public CharacterMatrix(Composite parent, CharacterMatrixPart part) {
172
        super(parent, SWT.NONE);
173
        this.part = part;
174
        this.setLayout(new GridLayout());
175

    
176
        createToolBar();
177

    
178
        natTable = new NatTable(this, false);
179

    
180
        createBottomToolbar();
181

    
182
    }
183

    
184
    private void createToolBar(){
185
        toolbar = new CharacterMatrixToolbar(this, SWT.NONE);
186
    }
187

    
188
    @SuppressWarnings("unused")
189
    private void createBottomToolbar() {
190
        new CharacterMatrixBottomToolbar(this, SWT.NONE);
191
    }
192

    
193
    private void applyStyles(){
194
        natTable.addConfiguration(new ModernNatTableThemeConfiguration());
195

    
196
//        // NOTE: Getting the colors and fonts from the GUIHelper ensures that
197
//        // they are disposed properly (required by SWT)
198
//        DefaultNatTableStyleConfiguration natTableConfiguration = new DefaultNatTableStyleConfiguration();
199
//        natTableConfiguration.bgColor = GUIHelper.getColor(249, 172, 7);
200
//        natTableConfiguration.fgColor = GUIHelper.getColor(30, 76, 19);
201
//        natTableConfiguration.hAlign = HorizontalAlignmentEnum.LEFT;
202
//        natTableConfiguration.vAlign = VerticalAlignmentEnum.TOP;
203
////        natTableConfiguration.borderStyle = new BorderStyle(1, GUIHelper.getColor(249, 172, 7), LineStyleEnum.SOLID);
204
//
205
//        // Setup even odd row colors - row colors override the NatTable default
206
//        // colors
207
//        DefaultRowStyleConfiguration rowStyleConfiguration = new DefaultRowStyleConfiguration();
208
//        rowStyleConfiguration.oddRowBgColor = ColorResources.getColor(Resources.COLOR_LIST_ODD);
209
//        rowStyleConfiguration.evenRowBgColor = ColorResources.getColor(Resources.COLOR_LIST_EVEN);
210
//
211
//        // Setup selection styling
212
//        DefaultSelectionStyleConfiguration selectionStyle = new DefaultSelectionStyleConfiguration();
213
////        selectionStyle.selectionFont = GUIHelper.getFont(new FontData("Verdana", 8, SWT.NORMAL));
214
////        selectionStyle.selectionBgColor = GUIHelper.getColor(217, 232, 251);
215
////        selectionStyle.selectionFgColor = GUIHelper.COLOR_BLACK;
216
////        selectionStyle.anchorBorderStyle = new BorderStyle(1, GUIHelper.COLOR_DARK_GRAY, LineStyleEnum.SOLID);
217
////        selectionStyle.anchorBgColor = GUIHelper.getColor(65, 113, 43);
218
//        selectionStyle.selectedHeaderBgColor = GUIHelper.getColor(156, 209, 103);
219
//
220
//        // Add all style configurations to NatTable
221
//        natTable.addConfiguration(natTableConfiguration);
222
//        natTable.addConfiguration(rowStyleConfiguration);
223
//        natTable.addConfiguration(selectionStyle);
224
//
225
//        // Column/Row header style and custom painters
226
//        DefaultRowHeaderStyleConfiguration rowHeaderConfig = new DefaultRowHeaderStyleConfiguration();
227
//        Color rowColumnColor = GUIHelper.getColor(230, 255, 255);
228
//        rowHeaderConfig.bgColor = rowColumnColor;
229
//        natTable.addConfiguration(rowHeaderConfig);
230
//        DefaultColumnHeaderStyleConfiguration columnHeaderStyle = new DefaultColumnHeaderStyleConfiguration();
231
//        columnHeaderStyle.bgColor = rowColumnColor;
232
//        columnHeaderStyle.font = GUIHelper.getFont(new FontData("Verdana", 9, SWT.BOLD)); //$NON-NLS-1$
233
//        natTable.addConfiguration(columnHeaderStyle);
234

    
235
    }
236

    
237
    void toggleTreeFlat(boolean isTree, Button btnToggleFlat, Button btnToggleTree, Button btnCollapseAll, Button btnExpandAll, Button btnFreezeSuppInfo) {
238
        isTreeView = isTree;
239
        createTable(isTree);
240
        btnToggleFlat.setEnabled(isTree);
241
        btnToggleTree.setEnabled(!isTree);
242
        btnCollapseAll.setEnabled(isTree);
243
        btnExpandAll.setEnabled(isTree);
244
    }
245

    
246
    public boolean isTreeView() {
247
        return isTreeView;
248
    }
249

    
250
    public void createTable(boolean treeView){
251
        /**
252
         * layers
253
         */
254
        createLayers(treeView);
255

    
256
        /**
257
         * configuration
258
         */
259
        configureNatTable(treeView, configRegistry, topMostLayer, summaryRowLayer);
260

    
261

    
262
        /**
263
         * style table
264
         */
265
        applyStyles();
266

    
267
        /**
268
         * handlers and listeners
269
         */
270
        registerHandlersAndListeners(topMostLayer);
271

    
272
        GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
273

    
274
        toolbar.getWsLabel().setText(descriptiveDataSet.getLabel());
275
        toolbar.getWsLabel().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
276
        toolbar.getWsLabel().getParent().layout();
277

    
278
        freezeSupplementalColumns(true);
279

    
280
        this.layout();
281
    }
282

    
283
    private List<Feature> initFeatureList(FeatureNode node){
284
        List<Feature> features = new ArrayList<>();
285
        node.getChildNodes().forEach(childNode->
286
                {
287
                    features.add(childNode.getFeature());
288
                    features.addAll(initFeatureList(childNode));
289
                });
290
        return features;
291
    }
292

    
293
    public void initDescriptiveDataSet(DescriptiveDataSet descriptiveDataSet){
294
        this.descriptiveDataSet = descriptiveDataSet;
295
        //get features/columns stored in descriptive data set
296
        FeatureTree tree = descriptiveDataSet.getDescriptiveSystem();
297
        features = initFeatureList(tree.getRoot());
298

    
299
        //init state data for categorical features
300
        features.forEach(feature->
301
        {
302
            if(feature.isSupportsCategoricalData()){
303
                List<State> supportedStates = new ArrayList<>();
304
                feature.getSupportedCategoricalEnumerations().forEach(voc->supportedStates.addAll(voc.getTerms()));
305
                categoricalFeatureToStateMap.put(feature, supportedStates);
306
            }
307
        });
308
        descriptions = new BasicEventList<>();
309

    
310
    }
311

    
312
    private void createLayers(boolean treeView) {
313
        // use the SortedList constructor with 'null' for the Comparator
314
        // because the Comparator will be set by configuration
315
        SortedList<Object> sortedList = new SortedList<>(descriptions, new MatrixRowComparator());
316
        // wrap the SortedList with the TreeList
317
        TreeList<Object> treeList = new TreeList(sortedList, new DescriptionTreeFormat(descriptiveDataSet), TreeList.NODES_START_EXPANDED);
318
        /**
319
         * data provider
320
         */
321
        SpecimenColumnPropertyAccessor columnPropertyAccessor = new SpecimenColumnPropertyAccessor(this);
322
        bodyDataProvider = treeView?new ListDataProvider<>(treeList, columnPropertyAccessor):new ListDataProvider<>(sortedList, columnPropertyAccessor);
323

    
324
        configRegistry = new ConfigRegistry();
325

    
326

    
327
        /**
328
         * BODY layer
329
         *
330
         *
331

    
332
        CompositeLayer
333
         - (top) SummaryRowLayer
334
         - (bottom) ViewportLayer
335

    
336
             ^
337
        ViewportLayer
338

    
339
             ^
340
        TreeLayer (default visible)
341

    
342
             ^
343
        CompositeFreezeLayer
344
         - viewportLayer
345
         - selectionLayer
346
         - freezeLayer
347

    
348
             ^
349
        FreezeLayer
350

    
351
             ^
352
        SelectionLayer
353

    
354
             ^
355
        ColumnHideShowLayer
356

    
357
             ^
358
        ColumnReorderLayer
359

    
360
             ^
361
        DataLayer
362

    
363
         *
364

    
365
         */
366
        DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
367

    
368
        //register labels for columns
369
        ColumnOverrideLabelAccumulator bodyColumnLabelAccumulator =new ColumnOverrideLabelAccumulator(bodyDataLayer);
370
        bodyDataLayer.setConfigLabelAccumulator(bodyColumnLabelAccumulator);
371
        propertyToLabelMap.put(TAXON_COLUMN, Messages.CharacterMatrix_TAXON);
372
        bodyColumnLabelAccumulator.registerColumnOverrides(0, TAXON_COLUMN);
373
        propertyToLabelMap.put(COLLECTOR_COLUMN, Messages.CharacterMatrix_COLLECTOR_NO);
374
        bodyColumnLabelAccumulator.registerColumnOverrides(1, COLLECTOR_COLUMN);
375
        propertyToLabelMap.put(IDENTIFIER_COLUMN, Messages.CharacterMatrix_IDENTIFIER);
376
        bodyColumnLabelAccumulator.registerColumnOverrides(2, IDENTIFIER_COLUMN);
377
        propertyToLabelMap.put(COUNTRY_COLUMN, Messages.CharacterMatrix_COUNTRY);
378
        bodyColumnLabelAccumulator.registerColumnOverrides(3, COUNTRY_COLUMN);
379
        for(int i=0;i<features.size();i++){
380
            Feature feature = features.get(i);
381
            initLabels(bodyColumnLabelAccumulator, i, feature);
382
        }
383

    
384
        // layer for event handling of GlazedLists and PropertyChanges
385
        GlazedListsEventLayer eventLayer = new GlazedListsEventLayer<>(bodyDataLayer, treeList);
386
        GlazedListTreeData treeData = new GlazedListTreeData<>(treeList);
387
        ITreeRowModel treeRowModel = new GlazedListTreeRowModel<>(treeData);
388

    
389
        bodyLayer = new DefaultBodyLayerStack(
390
                eventLayer);
391
        viewportLayer = bodyLayer.getViewportLayer();
392
        final SelectionLayer selectionLayer = bodyLayer.getSelectionLayer();
393
        freezeLayer = new FreezeLayer(selectionLayer);
394
        final CompositeFreezeLayer compositeFreezeLayer = new CompositeFreezeLayer(
395
                freezeLayer, bodyLayer.getViewportLayer(), selectionLayer);
396
        TreeLayer treeLayer = new TreeLayer(compositeFreezeLayer, treeRowModel);
397

    
398
        topMostLayer = treeView?treeLayer:compositeFreezeLayer;
399

    
400
        summaryRowLayer = new FixedSummaryRowLayer(bodyDataLayer, topMostLayer, configRegistry, false);
401
        //regoster labels with summary prefix for summary layer
402
        ColumnOverrideLabelAccumulator summaryColumnLabelAccumulator =new ColumnOverrideLabelAccumulator(bodyDataLayer);
403
        summaryRowLayer.setConfigLabelAccumulator(summaryColumnLabelAccumulator);
404
        for(int i=0;i<features.size();i++){
405
            Feature feature = features.get(i);
406
            summaryColumnLabelAccumulator.registerColumnOverrides(
407
                    i+LEADING_COLUMN_COUNT,
408
                    SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX+MatrixUtility.getProperty(feature));
409
        }
410
        // because the horizontal dependency is the ViewportLayer
411
        // we need to set the composite dependency to false
412
        summaryRowLayer.setHorizontalCompositeDependency(false);
413

    
414
        CompositeLayer composite = new CompositeLayer(1, 2);
415
        composite.setChildLayer("SUMMARY", summaryRowLayer, 0, 0); //$NON-NLS-1$
416
        composite.setChildLayer(GridRegion.BODY, topMostLayer, 0, 1);
417

    
418

    
419
        /**
420
         * column header layer
421
         */
422
        IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(
423
                propertyToLabelMap.values().toArray(new String[] {}), propertyToLabelMap);
424
        DataLayer columnHeaderDataLayer = new DataLayer(columnHeaderDataProvider);
425
        ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, topMostLayer, selectionLayer);
426

    
427
        // add the SortHeaderLayer to the column header layer stack
428
        // as we use GlazedLists, we use the GlazedListsSortModel which
429
        // delegates the sorting to the SortedList
430
        final SortHeaderLayer<SpecimenDescription> sortHeaderLayer = new SortHeaderLayer<>(
431
                columnHeaderLayer,
432
                new GlazedListsSortModel<>(
433
                        sortedList,
434
                        columnPropertyAccessor,
435
                        configRegistry,
436
                        columnHeaderDataLayer));
437

    
438

    
439
        /**
440
         * row header layer
441
         */
442
        IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
443
        DefaultRowHeaderDataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
444
        FixedSummaryRowHeaderLayer fixedSummaryRowHeaderLayer = new FixedSummaryRowHeaderLayer(rowHeaderDataLayer,
445
                composite, selectionLayer);
446
        fixedSummaryRowHeaderLayer.setSummaryRowLabel("\u2211"); //$NON-NLS-1$
447

    
448

    
449
        /**
450
         * corner layer
451
         */
452
        ILayer cornerLayer = new CornerLayer(
453
                new DataLayer(new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider)),
454
                fixedSummaryRowHeaderLayer, sortHeaderLayer);
455

    
456

    
457
        /**
458
         * GRID layer (composition of all other layers)
459
         */
460
        GridLayer gridLayer = new GridLayer(composite, sortHeaderLayer, fixedSummaryRowHeaderLayer, cornerLayer);
461

    
462
        natTable.setLayer(gridLayer);
463

    
464
    }
465

    
466
    private void registerHandlersAndListeners(AbstractLayer topMostLayer) {
467
        // add the ExportCommandHandler to the ViewportLayer in order to make
468
        // exporting work
469
        topMostLayer.registerCommandHandler(new ExportCommandHandler(topMostLayer));
470

    
471
        //propagate single cell selection
472
        natTable.addLayerListener(new ILayerListener() {
473
            @Override
474
            public void handleLayerEvent(ILayerEvent event) {
475
                if(event instanceof CellSelectionEvent){
476
                    CellSelectionEvent cellSelectionEvent = (CellSelectionEvent)event;
477
                    int columnPosition = cellSelectionEvent.getColumnPosition();
478
                    if(columnPosition>LEADING_COLUMN_COUNT){
479
                        Collection<ILayerCell> selectedCells = cellSelectionEvent.getSelectionLayer().getSelectedCells();
480
                        StructuredSelection selection = new StructuredSelection();
481
                        if(selectedCells.size()==1){
482
                            ILayerCell cell = selectedCells.iterator().next();
483
                            Object dataValue = cell.getDataValue();
484
                            if(dataValue!=null){
485
                                selection = new StructuredSelection(dataValue);
486
                            }
487
                        }
488
                        part.getSelectionService().setSelection(selection);
489
                    }
490
                }
491
            }
492
        });
493

    
494
        //register handler for view configuration menu
495
        natTable.registerCommandHandler(toolbar.getDisplayPersistenceDialogCommandHandler());
496
    }
497

    
498
    private void configureNatTable(boolean treeView, ConfigRegistry configRegistry, AbstractLayer topMostLayer,
499
            FixedSummaryRowLayer summaryRowLayer) {
500
        /**
501
         * CONFIGURATION
502
         */
503
        natTable.setConfigRegistry(configRegistry);
504

    
505
        //add default configuration because autoconfigure is set to false in constructor
506
        natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
507

    
508
        //FIXME: this is for DEBUG ONLY
509
        //        natTable.addConfiguration(new DebugMenuConfiguration(natTable));
510

    
511
        // override the default sort configuration and change the mouse bindings
512
        // to sort on a single click
513
        if(!treeView){
514
            natTable.addConfiguration(new SingleClickSortConfiguration());
515
        }
516

    
517
        // add the header menu configuration for adding the column header menu
518
        // with hide/show actions
519
        natTable.addConfiguration(new AbstractHeaderMenuConfiguration(natTable) {
520

    
521
            @Override
522
            protected PopupMenuBuilder createColumnHeaderMenu(NatTable natTable) {
523
                return super.createColumnHeaderMenu(natTable)
524
                        .withHideColumnMenuItem()
525
                        .withShowAllColumnsMenuItem();
526
            }
527

    
528
        });
529

    
530
        Style cellStyle = new Style();
531
        cellStyle.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.LEFT);
532

    
533
        // add custom configuration for data conversion and add column labels to viewport layer
534
        topMostLayer.addConfiguration(new AbstractRegistryConfiguration() {
535
            @Override
536
            public void configureRegistry(IConfigRegistry configRegistry) {
537
                //add display converter for string representation
538
                configRegistry.registerConfigAttribute(
539
                        CellConfigAttributes.DISPLAY_CONVERTER,
540
                        new SupplementalInfoDisplayConverter(),
541
                        DisplayMode.NORMAL,
542
                        TAXON_COLUMN);
543
                configRegistry.registerConfigAttribute(
544
                        CellConfigAttributes.CELL_STYLE,
545
                        cellStyle,
546
                        DisplayMode.NORMAL,
547
                        TAXON_COLUMN);
548
                configRegistry.registerConfigAttribute(
549
                        CellConfigAttributes.DISPLAY_CONVERTER,
550
                        new SupplementalInfoDisplayConverter(),
551
                        DisplayMode.NORMAL,
552
                        COLLECTOR_COLUMN);
553
                configRegistry.registerConfigAttribute(
554
                        CellConfigAttributes.CELL_STYLE,
555
                        cellStyle,
556
                        DisplayMode.NORMAL,
557
                        COLLECTOR_COLUMN);
558
                configRegistry.registerConfigAttribute(
559
                        CellConfigAttributes.DISPLAY_CONVERTER,
560
                        new SupplementalInfoDisplayConverter(),
561
                        DisplayMode.NORMAL,
562
                        IDENTIFIER_COLUMN);
563
                configRegistry.registerConfigAttribute(
564
                        CellConfigAttributes.CELL_STYLE,
565
                        cellStyle,
566
                        DisplayMode.NORMAL,
567
                        IDENTIFIER_COLUMN);
568
                configRegistry.registerConfigAttribute(
569
                        CellConfigAttributes.DISPLAY_CONVERTER,
570
                        new SupplementalInfoDisplayConverter(),
571
                        DisplayMode.NORMAL,
572
                        COUNTRY_COLUMN);
573
                configRegistry.registerConfigAttribute(
574
                        CellConfigAttributes.CELL_STYLE,
575
                        cellStyle,
576
                        DisplayMode.NORMAL,
577
                        COUNTRY_COLUMN);
578
                features.forEach(feature->registerColumnConfiguration(feature, configRegistry));
579
            }
580

    
581
        });
582

    
583
        //no summary for the supplemental columns
584
        for(int i=0;i<LEADING_COLUMN_COUNT;i++){
585
            int index = i;
586
            summaryRowLayer.addConfiguration(new DefaultSummaryRowConfiguration() {
587
                @Override
588
                public void addSummaryProviderConfig(IConfigRegistry configRegistry) {
589
                    configRegistry.registerConfigAttribute(
590
                            SummaryRowConfigAttributes.SUMMARY_PROVIDER,
591
                            new ISummaryProvider() {
592

    
593
                                @Override
594
                                public Object summarize(int columnIndex) {
595
                                    return "";
596
                                }
597
                            },
598
                            DisplayMode.NORMAL,
599
                            SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX+index);
600
                }
601
            });
602
        }
603
        //register aggregation configuration for each feature
604
        features.forEach(feature->summaryRowLayer.addConfiguration(new AggregationConfiguration(bodyDataProvider, feature)));
605

    
606
        natTable.configure();
607
    }
608

    
609
    void freezeSupplementalColumns(boolean freeze){
610
        if(freeze){
611
            FreezeHelper.freeze(freezeLayer, viewportLayer,
612
                    new PositionCoordinate(viewportLayer, 0, 0),
613
                    new PositionCoordinate(viewportLayer, LEADING_COLUMN_COUNT-1, -1));
614
        }
615
        else{
616
            FreezeHelper.unfreeze(freezeLayer, viewportLayer);
617
        }
618
    }
619

    
620
    void selectStateItem(ComboViewer comboStates, String stateName){
621
        String[] items = comboStates.getCombo().getItems();
622
        for(int i=0;i<items.length;i++){
623
            if(items[i].equals(stateName)){
624
                comboStates.getCombo().select(i);
625
                break;
626
            }
627
        }
628
    }
629

    
630
    private void initLabels(final ColumnOverrideLabelAccumulator columnLabelAccumulator,
631
            int index, Feature feature) {
632

    
633
        columnLabelAccumulator.registerColumnOverrides(index+LEADING_COLUMN_COUNT, MatrixUtility.getProperty(feature));
634
        indexToFeatureMap.put(index+LEADING_COLUMN_COUNT, feature);
635

    
636
        String featureLabel = feature.getLabel();
637
        String property = featureLabel;
638
        //show unit for quantitative data
639
        if(feature.isSupportsQuantitativeData()){
640
            Set<MeasurementUnit> recommendedMeasurementUnits = feature.getRecommendedMeasurementUnits();
641
            if(recommendedMeasurementUnits.size()>1){
642
                MessagingUtils.warningDialog(Messages.CharacterMatrix_INIT_PROBLEM, CharacterMatrix.class,
643
                        String.format(Messages.CharacterMatrix_INIT_PROBLEM_MESSAGE, feature.getLabel()));
644
            }
645
            if(recommendedMeasurementUnits.size()==1){
646
                MeasurementUnit unit = recommendedMeasurementUnits.iterator().next();
647
                featureLabel += " ["+unit.getIdInVocabulary()+"]"; //$NON-NLS-1$ //$NON-NLS-2$
648
            }
649
        }
650
        propertyToLabelMap.put(property, featureLabel);
651
    }
652

    
653
    private void registerColumnConfiguration(Feature feature, IConfigRegistry configRegistry) {
654
        //make cell editable
655
        configRegistry.registerConfigAttribute(
656
                EditConfigAttributes.CELL_EDITABLE_RULE,
657
                IEditableRule.ALWAYS_EDITABLE,
658
                DisplayMode.EDIT,
659
                MatrixUtility.getProperty(feature)
660
                );
661
        if(feature.isSupportsQuantitativeData()){
662
            //add display converter for string representation
663
            configRegistry.registerConfigAttribute(
664
                    CellConfigAttributes.DISPLAY_CONVERTER,
665
                    new QuantitativeDataDisplayConverter(),
666
                    DisplayMode.NORMAL,
667
                    MatrixUtility.getProperty(feature));
668
            //register quantitative editor
669
            configRegistry.registerConfigAttribute(
670
                    EditConfigAttributes.CELL_EDITOR,
671
                    new QuantitativeDataCellEditor(feature, this),
672
                    DisplayMode.EDIT,
673
                    MatrixUtility.getProperty(feature));
674
        }
675
        else if(feature.isSupportsCategoricalData()){
676
            //add display converter for string representation
677
            configRegistry.registerConfigAttribute(
678
                    CellConfigAttributes.DISPLAY_CONVERTER,
679
                    new CategoricalDataDisplayConverter(),
680
                    DisplayMode.NORMAL,
681
                    MatrixUtility.getProperty(feature));
682

    
683
            //add combo box cell editor
684
            //register editor
685
            configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR,
686
                    new CategoricalDataCellEditor(getSupportedStatesForCategoricalFeature(feature), this, feature),
687
                    DisplayMode.EDIT,
688
                    MatrixUtility.getProperty(feature));
689

    
690
        }
691

    
692
    }
693

    
694

    
695
    public void loadDescriptions(DescriptiveDataSet descriptiveDataSet) {
696
        UUID monitorUuid = CdmStore.getService(IDescriptiveDataSetService.class).monitGetRowWrapper(descriptiveDataSet);
697
        IProgressMonitorService progressMonitorService = CdmApplicationState.getCurrentAppConfig().getProgressMonitorService();
698

    
699

    
700
        String jobLabel = "Load character data";
701
        Job job = Job.create(jobLabel, (ICoreRunnable) monitor -> {
702
            monitor.beginTask(jobLabel, IProgressMonitor.UNKNOWN);
703
            while(progressMonitorService.isMonitorThreadRunning(monitorUuid)){
704
                if(monitor.isCanceled()){
705
                    progressMonitorService.interrupt(monitorUuid);
706
                }
707
            }
708
            IRemotingProgressMonitor remotingMonitor = progressMonitorService.getRemotingMonitor(monitorUuid);
709
            Collection<RowWrapperDTO> wrappers = (Collection<RowWrapperDTO>) remotingMonitor.getResult();
710
            if(wrappers!=null){
711
                wrappers.forEach(wrapper->CharacterMatrix.this.descriptions.add(wrapper));
712
            }
713
            monitor.done();
714
        });
715
        job.schedule();
716
    }
717

    
718
    public List<State> getSupportedStatesForCategoricalFeature(Feature feature){
719
        return categoricalFeatureToStateMap.get(feature);
720
    }
721

    
722
    public Map<Integer, Feature> getIndexToFeatureMap() {
723
        return indexToFeatureMap;
724
    }
725

    
726
    public LinkedMap<String, String> getPropertyToLabelMap() {
727
        return propertyToLabelMap;
728
    }
729

    
730
    public void setDirty() {
731
        part.setDirty();
732
    }
733

    
734
    public NatTable getNatTable() {
735
        return natTable;
736
    }
737

    
738
    public EventList<Object> getDescriptions() {
739
        return descriptions;
740
    }
741

    
742
    public DescriptiveDataSet getDescriptiveDataSet() {
743
        return descriptiveDataSet;
744
    }
745

    
746
    public Collection<SpecimenNodeWrapper> getSpecimenCache() {
747
        return specimenCache;
748
    }
749

    
750
    public void setSpecimenCache(Collection<SpecimenNodeWrapper> specimenCache) {
751
        this.specimenCache = specimenCache.stream()
752
                .filter(wrapper ->
753
        //map descriptions on a list of uuids of the described specimen
754
        !this.descriptions.stream()
755
        .map(o->((RowWrapperDTO)o).getSpecimen().getUuid())
756
        .collect(Collectors.toList())
757
        //an check if the specimen to add is already contained
758
        .contains(wrapper.getUuidAndTitleCache().getUuid())
759
        )
760
        .collect(Collectors.toList());
761
    }
762

    
763
    public Properties getNatTableState() {
764
        return toolbar.getNatTableState();
765
    }
766

    
767
    public ListDataProvider<Object> getBodyDataProvider() {
768
        return bodyDataProvider;
769
    }
770

    
771
    DefaultBodyLayerStack getBodyLayer() {
772
        return bodyLayer;
773
    }
774

    
775
    File getStatePropertiesFile() {
776
        return new File(WorkbenchUtility.getBaseLocation(), CHARACTER_MATRIX_STATE_PROPERTIES);
777
    }
778

    
779
}
(2-2/11)