Project

General

Profile

Download (26.3 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.view.checklist.e4;
10

    
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.HashMap;
14
import java.util.HashSet;
15
import java.util.Iterator;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19
import java.util.SortedSet;
20
import java.util.TreeSet;
21
import java.util.UUID;
22

    
23
import javax.inject.Inject;
24

    
25
import org.apache.commons.collections4.map.LinkedMap;
26
import org.apache.log4j.Logger;
27
import org.eclipse.e4.ui.services.EMenuService;
28
import org.eclipse.jface.layout.GridDataFactory;
29
import org.eclipse.jface.layout.GridLayoutFactory;
30
import org.eclipse.jface.wizard.WizardDialog;
31
import org.eclipse.nebula.widgets.nattable.NatTable;
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.coordinate.PositionCoordinate;
35
import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
36
import org.eclipse.nebula.widgets.nattable.data.IRowDataProvider;
37
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
38
import org.eclipse.nebula.widgets.nattable.extension.e4.selection.E4SelectionListener;
39
import org.eclipse.nebula.widgets.nattable.extension.glazedlists.filterrow.DefaultGlazedListsFilterStrategy;
40
import org.eclipse.nebula.widgets.nattable.filterrow.FilterRowHeaderComposite;
41
import org.eclipse.nebula.widgets.nattable.freeze.CompositeFreezeLayer;
42
import org.eclipse.nebula.widgets.nattable.freeze.FreezeHelper;
43
import org.eclipse.nebula.widgets.nattable.freeze.FreezeLayer;
44
import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
45
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider;
46
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider;
47
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider;
48
import org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer;
49
import org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer;
50
import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer;
51
import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer;
52
import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer;
53
import org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer;
54
import org.eclipse.nebula.widgets.nattable.layer.AbstractLayer;
55
import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
56
import org.eclipse.nebula.widgets.nattable.layer.ILayer;
57
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
58
import org.eclipse.nebula.widgets.nattable.sort.config.SingleClickSortConfiguration;
59
import org.eclipse.nebula.widgets.nattable.style.HorizontalAlignmentEnum;
60
import org.eclipse.nebula.widgets.nattable.style.theme.ModernNatTableThemeConfiguration;
61
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
62
import org.eclipse.swt.SWT;
63
import org.eclipse.swt.events.SelectionAdapter;
64
import org.eclipse.swt.events.SelectionEvent;
65
import org.eclipse.swt.layout.GridData;
66
import org.eclipse.swt.layout.GridLayout;
67
import org.eclipse.swt.widgets.Button;
68
import org.eclipse.swt.widgets.Composite;
69
import org.eclipse.swt.widgets.Label;
70
import org.eclipse.swt.widgets.Text;
71

    
72
import ca.odell.glazedlists.BasicEventList;
73
import ca.odell.glazedlists.EventList;
74
import eu.etaxonomy.cdm.api.service.IVocabularyService;
75
import eu.etaxonomy.cdm.api.service.dto.TaxonDescriptionDTO;
76
import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
77
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
78
import eu.etaxonomy.cdm.model.common.Language;
79
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
80
import eu.etaxonomy.cdm.model.common.OrderedTermComparator;
81
import eu.etaxonomy.cdm.model.common.Representation;
82
import eu.etaxonomy.cdm.model.common.TermIdInVocabularyComparator;
83
import eu.etaxonomy.cdm.model.common.TermLanguageComparator;
84
import eu.etaxonomy.cdm.model.common.TermVocabulary;
85
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
86
import eu.etaxonomy.cdm.model.description.Distribution;
87
import eu.etaxonomy.cdm.model.description.TaxonDescription;
88
import eu.etaxonomy.cdm.model.location.NamedArea;
89
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
90
import eu.etaxonomy.cdm.model.reference.Reference;
91
import eu.etaxonomy.taxeditor.editor.EditorUtil;
92
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
93
import eu.etaxonomy.taxeditor.model.AbstractUtility;
94
import eu.etaxonomy.taxeditor.preference.EditorPreferencePredicate;
95
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
96
import eu.etaxonomy.taxeditor.preference.Resources;
97
import eu.etaxonomy.taxeditor.preference.wizard.AvailableDistributionWizard;
98
import eu.etaxonomy.taxeditor.store.CdmStore;
99
import eu.etaxonomy.taxeditor.store.StoreUtil;
100
import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
101

    
102
/**
103
 * @author k.luther
104
 * @since 27.11.2018
105
 *
106
 */
107
public class DistributionEditor extends Composite {
108

    
109

    
110
    private static final String DISTRIBUTION_EDITOR = "Distribution Editor";
111
    private static final String LOADING_TAXA = Messages.ChecklistEditor_LOAD_TAXA;
112
    private static final String UNKNOWN = Messages.ChecklistEditor_UNKNOWN;
113
    private static final String ELEMENT_COUNT = Messages.ChecklistEditor_ELEMENT_COUNT;
114
    public static final String TYPE_FILTER_TEXT = "type filter text"; //$NON-NLS-1$
115

    
116
    static final String TAXON_COLUMN = "taxon_column"; //$NON-NLS-1$
117
    static final String RANK_COLUMN = "collector_column"; //$NON-NLS-1$
118

    
119
    private static final Logger logger = Logger.getLogger(DistributionEditor.class);
120
    @Inject
121
    private EMenuService menuService;
122
    private NatTable natTable;
123
    private Label statusLabel;
124
    private Label statusLabelSourceReference;
125
    private Reference defaultSource;
126

    
127
    private Map<Integer, NamedArea> areaToColumnIndexMap= new HashMap();
128
    private int firstDataColumnIndex;
129

    
130
    private ListDataProvider<TaxonDistributionDTO> bodyDataProvider;
131
    private LinkedMap<String, String> propertyToLabelMap = new LinkedMap<>();
132

    
133
    private boolean isShowRank;
134
    private Integer countNodes;
135
    private Text searchText;
136

    
137
    EventList<TaxonDistributionDTO> taxonList;
138
    Map<UUID,Map<NamedArea,Set<DescriptionElementBase>>> taxonDistributionMap = new HashMap<>();
139

    
140
    List<TaxonDistributionDTO> descriptionsToSave = new ArrayList<>();
141

    
142

    
143
    SortedSet<DefinedTermBase> areas;
144
    DistributionEditorPart part;
145
    BodyLayerStack<TaxonDistributionDTO> bodyLayerStack;
146
    private FreezeLayer freezeLayer;
147
    private ConfigRegistry configRegistry;
148
    private AbstractLayer topMostLayer;
149

    
150
    private SelectionLayer selectionLayer;
151

    
152

    
153

    
154
 //  private FixedSummaryRowLayer summaryRowLayer;
155

    
156

    
157
    /**
158
     * @param parent
159
     * @param style
160
     */
161
    public DistributionEditor(Composite parent, DistributionEditorPart part) {
162
        super(parent, SWT.NULL);
163
        this.part = part;
164
        this.setLayout(new GridLayout());
165
        GridLayoutFactory.fillDefaults().applyTo(parent);
166

    
167
        Composite topComposite = new Composite(parent, SWT.NONE);
168
        createTopComposite(topComposite);
169

    
170
        Composite bottomComposite = new Composite(parent, SWT.NONE);
171
        GridLayoutFactory.fillDefaults().applyTo(bottomComposite);
172

    
173
        natTable = new NatTable(parent, false);
174
        parent.pack();
175

    
176

    
177
//       // natTable = new NatTable(parent, viewportLayer);
178
//        natTable = new NatTable(parent, false);
179

    
180
        createStatusBar(parent);
181
    }
182

    
183
    public boolean isShowRank() {
184
        return isShowRank;
185
    }
186

    
187
    public void setShowRank(boolean isShowRank) {
188
        this.isShowRank = isShowRank;
189
    }
190

    
191
    public int getFirstDataColumnIndex() {
192
        return firstDataColumnIndex;
193
    }
194

    
195
    public void setFirstDataColumnIndex(int firstDataColumnIndex) {
196
        this.firstDataColumnIndex = firstDataColumnIndex;
197
    }
198

    
199
    public LinkedMap<String, String> getPropertyToLabelMap() {
200
        return propertyToLabelMap;
201
    }
202

    
203
    public void setPropertyToLabelMap(LinkedMap<String, String> propertyToLabelMap) {
204
        this.propertyToLabelMap = propertyToLabelMap;
205
    }
206

    
207
    public Map<Integer, NamedArea> getAreaToColumnIndexMap() {
208
        return areaToColumnIndexMap;
209
    }
210

    
211

    
212
    public void setAreaToColumnIndexMap(Map<Integer, NamedArea> areaToColumnIndexMap) {
213
        this.areaToColumnIndexMap = areaToColumnIndexMap;
214
    }
215

    
216

    
217
    public List<TaxonDistributionDTO> getDescriptionsToSave() {
218
        return descriptionsToSave;
219
    }
220

    
221
    public void setDescriptionsToSave(List<TaxonDistributionDTO> descriptionsToSave) {
222
        this.descriptionsToSave = descriptionsToSave;
223
    }
224

    
225

    
226

    
227
    private void createStatusBar(Composite composite) {
228
        GridData gridData = new GridData();
229
        gridData.horizontalSpan = 1;
230
        gridData.grabExcessHorizontalSpace = true;
231
        gridData.horizontalAlignment = GridData.FILL;
232

    
233
        statusLabel = new Label(composite, SWT.LEFT);
234
        statusLabel.setText(ELEMENT_COUNT + (taxonList != null ? taxonList.size() : UNKNOWN));
235
        statusLabel.setLayoutData(gridData);
236

    
237
        statusLabelSourceReference = new Label(composite, SWT.RIGHT);
238

    
239
        statusLabelSourceReference.setLayoutData(gridData);
240

    
241
        if (defaultSource != null){
242
            statusLabelSourceReference.setText("Default Source Reference: " + defaultSource.getAbbrevTitle() != null? defaultSource.getAbbrevTitle() : defaultSource.getAbbrevTitleCache());
243

    
244
        }
245
    }
246

    
247
    private void applyStyles(){
248
        ModernNatTableThemeConfiguration configuration = new ModernNatTableThemeConfiguration();
249
        configuration.summaryRowHAlign = HorizontalAlignmentEnum.CENTER;
250
        // NOTE: Getting the colors and fonts from the GUIHelper ensures that
251
        // they are disposed properly (required by SWT)
252
        configuration.cHeaderBgColor = GUIHelper.getColor(211, 211, 211);
253
        configuration.rHeaderBgColor = GUIHelper.getColor(211, 211, 211);
254
        natTable.addConfiguration(configuration);
255

    
256
    }
257

    
258
    private void configureNatTable(ConfigRegistry configRegistry,
259
            AbstractLayer topMostLayer) {
260
        /**
261
         * CONFIGURATION
262
         */
263
        natTable.setConfigRegistry(configRegistry);
264

    
265
        applyStyles();
266

    
267
        //add default configuration because autoconfigure is set to false in constructor
268
        natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
269

    
270

    
271

    
272
        // override the default sort configuration and change the mouse bindings
273
        // to sort on a single click
274

    
275
        natTable.addConfiguration(new SingleClickSortConfiguration());
276
        natTable.addConfiguration(new FilterRowConfiguration(this));
277

    
278
        // add the header menu configuration for adding the column header menu
279
        // with hide/show actions
280
        natTable.addConfiguration(new DistributionEditorHeaderMenuConfiguration(natTable));
281

    
282
        // add custom configuration for data conversion and add column labels to viewport layer
283
        topMostLayer.addConfiguration(new DistributionCellEditorDataConversionConfiguration(this));
284

    
285
        natTable.configure();
286
    }
287

    
288
    public void createTable(){
289
        /**
290
         * layers
291
         */
292

    
293
        createLayers();
294

    
295
        /**
296
         * configuration
297
         */
298
        configureNatTable( configRegistry, topMostLayer);
299

    
300
        /**
301
         * handlers and listeners
302
         */
303
        registerHandlersAndListeners(topMostLayer);
304
        FreezeHelper.freeze(freezeLayer, bodyLayerStack.getViewPortLayer(),
305
                new PositionCoordinate(bodyLayerStack.getViewPortLayer(), 0, 0),
306
                new PositionCoordinate(bodyLayerStack.getViewPortLayer(), 0, -1));
307
        //grab all space
308
        GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
309

    
310
        this.layout();
311
      //  boolean ok = natTable.doCommand(new ClientAreaResizeCommand(natTable));
312
      //  System.out.println(ok);
313
    }
314

    
315

    
316
    private void createLayers() {
317

    
318
        DistributionColumnAccessor columnPropertyAccessor = new DistributionColumnAccessor(this);
319

    
320
        DistributionEditorConfigLabelAccumulator labelAccumulator = new DistributionEditorConfigLabelAccumulator(this);
321
        bodyLayerStack = new BodyLayerStack<>(taxonList, columnPropertyAccessor, new TaxonDistributionDtoComparator(), labelAccumulator);
322

    
323
        bodyDataProvider = bodyLayerStack.getBodyDataProvider();
324

    
325

    
326

    
327
        propertyToLabelMap.put(TAXON_COLUMN, Messages.ChecklistEditor_TAXON);
328
        if (isShowRank){
329
            propertyToLabelMap.put(RANK_COLUMN, Messages.ChecklistEditor_RANK);
330
        }
331
        configRegistry = new ConfigRegistry();
332

    
333
        initLabels();
334
        this.selectionLayer = bodyLayerStack.getSelectionLayer();
335

    
336

    
337

    
338
        freezeLayer = new FreezeLayer(selectionLayer);
339
        final CompositeFreezeLayer compositeFreezeLayer = new CompositeFreezeLayer(
340
                freezeLayer,  bodyLayerStack.getViewPortLayer(), selectionLayer);
341

    
342
        // as the selection mouse bindings are registered for the region label
343
        // GridRegion.BODY
344
        // we need to set that region label to the viewport so the selection via mouse
345
        // is working correctly
346
        compositeFreezeLayer.setRegionName(GridRegion.BODY);
347

    
348

    
349
        IDataProvider columnHeaderDataProvider =
350
                new DefaultColumnHeaderDataProvider(propertyToLabelMap.values().toArray(new String[] {}), propertyToLabelMap);
351
        DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
352
        ILayer columnHeaderLayer = new ColumnHeaderLayer(
353
            columnHeaderDataLayer,
354
            compositeFreezeLayer,
355
            selectionLayer);
356

    
357
        FilterRowHeaderComposite<TaxonDistributionDTO> filterRowHeaderLayer =
358
                new FilterRowHeaderComposite<>(
359
                        new DefaultGlazedListsFilterStrategy<>(
360
                                bodyLayerStack.getFilterList(),
361
                                columnPropertyAccessor,
362
                                configRegistry),
363
                        columnHeaderLayer,
364
                        columnHeaderDataLayer.getDataProvider(),
365
                        configRegistry);
366

    
367

    
368

    
369
     // build the row header layer stack
370
        IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
371
        DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
372
        ILayer rowHeaderLayer = new RowHeaderLayer(
373
            rowHeaderDataLayer,
374
            compositeFreezeLayer,
375
            selectionLayer);
376

    
377

    
378

    
379
        IDataProvider cornerDataProvider =
380
                new DefaultCornerDataProvider(
381
                    columnHeaderDataProvider,
382
                    rowHeaderDataProvider);
383
        DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
384
        ILayer cornerLayer = new CornerLayer(
385
            cornerDataLayer,
386
            rowHeaderLayer,
387
            filterRowHeaderLayer);
388

    
389
        GridLayer gridLayer = new GridLayer(
390
                compositeFreezeLayer,
391
                filterRowHeaderLayer,
392
                rowHeaderLayer,
393
                cornerLayer);
394

    
395
        natTable.setLayer(gridLayer);
396

    
397
        topMostLayer = compositeFreezeLayer;
398

    
399

    
400
    }
401

    
402
    /**
403
     * @param parent
404
     * @return
405
     */
406
    private Text createSearchBar(Composite parent) {
407

    
408
        Button button1 = new Button(parent, SWT.PUSH );
409
        GridData gridData2 = new GridData();
410
        gridData2.horizontalSpan = 1;
411
        gridData2.horizontalAlignment = SWT.RIGHT;
412
        button1.setLayoutData(gridData2);
413

    
414

    
415
        button1.setText(Messages.ChecklistEditor_DIST_STATUS);
416
        button1.setToolTipText(Messages.ChecklistEditor_DIST_STATUS_TOOLTIP);
417
        button1.addSelectionListener(new SelectionAdapter() {
418
            @Override
419
            public void widgetSelected(SelectionEvent event) {
420
                AvailableDistributionWizard availableDistributionWizard = new AvailableDistributionWizard();
421
                WizardDialog dialog = new WizardDialog(StoreUtil.getShell(),
422
                        availableDistributionWizard);
423

    
424
                int open = dialog.open();
425
                if(open == 0){
426
                    reload();
427
                }
428
            }
429
        });
430

    
431
        Button button2 = new Button(parent, SWT.PUSH );
432
        GridData gridData3 = new GridData();
433
        gridData2.horizontalSpan = 1;
434
        button2.setLayoutData(gridData3);
435

    
436

    
437
        button2.setText(Messages.ChecklistEditor_DEFAULT_SOURCE);
438
        button2.setToolTipText(Messages.ChecklistEditor_DEFAULT_SOURCE_TOOLTIP);
439
        button2.addSelectionListener(new SelectionAdapter() {
440

    
441
            @Override
442
            public void widgetSelected(SelectionEvent event) {
443
                defaultSource = ReferenceSelectionDialog.select(AbstractUtility.getShell(), null);
444

    
445
                String defaultSourceStr = (defaultSource == null) ? "" : "Default Source Reference: " + defaultSource.getTitleCache();
446
                if (defaultSourceStr.length()> 100){
447
                    defaultSourceStr = defaultSourceStr.substring(0, 98) + "...";
448
                }
449
                statusLabelSourceReference.setText(defaultSourceStr);
450

    
451
                button2.setBackground(EditorUtil.getColor(Resources.COLOR_CONTROL_SELECTED));
452
            }
453

    
454
        });
455

    
456
        parent.pack();
457
        return searchText;
458
    }
459
    /**
460
    * This method should only be called for adding new Distribution columns and reloading the table.<br>
461
    * It will hide the old distribution column and load the newly added columns.<br>
462
    * <p>
463
    * <b>Notice:</b> for data update please use <b>refresh()</b>
464
    *
465
    */
466
   @SuppressWarnings({ "unchecked", "rawtypes" })
467
   public void reload(){
468
       this.areaToColumnIndexMap.clear();
469
       this.areas.clear();
470
       this.propertyToLabelMap.clear();
471
       createTable();
472
       natTable.redraw();
473
    }
474

    
475
    /**
476
     * @param parent
477
     */
478
    private void createTopComposite(Composite parent) {
479
        GridLayout gridLayout = new GridLayout(3, false);
480
        gridLayout.marginWidth = 0;
481
        gridLayout.marginHeight = 0;
482
        parent.setLayout(gridLayout);
483
        searchText = createSearchBar(parent);
484

    
485

    
486
    }
487
    public void loadDistributions(List<TaxonDistributionDTO> taxonList) {
488
        if (this.taxonList == null){
489
            this.taxonList = new BasicEventList<>();
490
        }
491
       taxonList.stream().forEach(wrapper->DistributionEditor.this.taxonList.add(wrapper));
492
       statusLabel.setText(ELEMENT_COUNT + (taxonList != null ? taxonList.size() : UNKNOWN));
493
       createTaxonDistributionMap();
494
    }
495

    
496
    protected void createTaxonDistributionMap() {
497
        Iterator<TaxonDistributionDTO> iterator = this.taxonList.iterator();
498
           while (iterator.hasNext()){
499
               TaxonDistributionDTO dto = iterator.next();
500
               TaxonDescriptionDTO descriptionDto = dto.getDescriptionsWrapper();
501
               for (TaxonDescription desc: descriptionDto.getDescriptions()){
502
                   if(this.part.getCdmEntitySession()!=null){
503
                       this.part.getCdmEntitySession().load(desc, true);
504
                   }
505
                   for (DescriptionElementBase descElement: desc.getElements()){
506
                       if (descElement instanceof Distribution){
507
                           Map<NamedArea, Set<DescriptionElementBase>> distributionsMap = taxonDistributionMap.get(dto.getTaxonUuid());
508

    
509
                           if (distributionsMap == null){
510
                               distributionsMap = new HashMap();
511
                               taxonDistributionMap.put(dto.getTaxonUuid(), distributionsMap);
512
                           }
513
                           Set<DescriptionElementBase> distributions = distributionsMap.get(((Distribution) descElement).getArea());
514
                           if (distributions == null){
515
                               distributions = new HashSet();
516
                               distributionsMap.put(((Distribution)descElement).getArea(), distributions);
517
                           }
518
                           distributions.add(descElement);
519

    
520
                       }
521
                   }
522
               }
523
           }
524
    }
525

    
526

    
527

    
528

    
529

    
530
    private void initLabels() {
531

    
532
        int index = 1;
533
        if (isShowRank){
534
            index++;
535
        }
536

    
537
        loadNamedAreas();
538
        for (DefinedTermBase area: areas) {
539
            this.areaToColumnIndexMap.put(index++, (NamedArea)area);
540
            String areaLabel;
541
            //TODO: adapt to preference
542
            Representation rep = area.getPreferredRepresentation(CdmStore.getDefaultLanguage());
543
            String label = rep.getLabel();
544
            if (label == null){
545
                label = area.getTitleCache();
546
            }
547
            if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
548
                if (area.getIdInVocabulary() != null){
549
                    areaLabel = area.getIdInVocabulary();
550
                } else{
551
                    areaLabel = label;
552
                }
553
            }else if (PreferencesUtil.isShowSymbol1InChecklistEditor()){
554
                if (area.getSymbol() != null){
555
                    areaLabel = area.getSymbol();
556
                } else{
557
                    areaLabel = label;
558
                }
559
            }else if (PreferencesUtil.isShowSymbol2InChecklistEditor()){
560
                if (area.getSymbol2() != null){
561
                    areaLabel = area.getSymbol2();
562
                } else{
563
                    areaLabel = label;
564
                }
565
            }else{
566
                areaLabel = label;
567
            }
568

    
569

    
570
            //String areaLabel = area.getLabel();
571
            String property = areaLabel;
572
            propertyToLabelMap.put(property, areaLabel);
573
        }
574
    }
575

    
576
    private SortedSet<DefinedTermBase> loadNamedAreas() {
577
        //IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
578

    
579
        String valuesAreas = PreferencesUtil.getStringValue(EditorPreferencePredicate.AvailableDistributionAreaTerms.getKey());
580
        String values = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
581
        if (values != null && values != "") { //$NON-NLS-1$
582
            String[] split = values.split(";"); //$NON-NLS-1$
583
            List<String> listValue = Arrays.asList(split);
584
            List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
585
            Set<UUID> uuidList = new HashSet<UUID>();
586
            UUID uuid;
587
            for(String s : listValue){
588
                uuid = UUID.fromString(s);
589
                uuidList.add(uuid);
590

    
591
            }
592
            IVocabularyService service =  CdmStore.getService(IVocabularyService.class);
593
            List<TermVocabulary> vocs = service.find(uuidList);
594
            split = valuesAreas.split(";");
595
            listValue = Arrays.asList(split);
596
            for (TermVocabulary voc: vocs){
597
                termlist.addAll(service.getTerms(voc, null, null, null, null).getRecords());
598
            }
599
            List<DefinedTermBase> filteredList = new ArrayList();
600
            for (DefinedTermBase area: termlist){
601
                if (listValue.contains(area.getUuid().toString())) {
602
                    filteredList.add(area);
603
                }
604

    
605
            }
606

    
607
            if (PreferencesUtil.isSortNamedAreaByOrderInVocabulary()){
608
                areas =  getTermsOrderedByVocabularyOrder(filteredList);
609
            } else if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
610
                areas = getTermsOrderedByIdInVocabulary(filteredList);
611
            }else{
612
                areas =  getTermsOrderedByLabels(filteredList, CdmStore.getDefaultLanguage());
613
            }
614
        }
615
        return null;
616
    }
617

    
618
    public SortedSet<DefinedTermBase> getTermsOrderedByLabels(List<DefinedTermBase> listTerm,Language language){
619
        TermLanguageComparator<?> comp = new TermLanguageComparator<>();
620
        comp.setCompareLanguage(language);
621
        SortedSet<DefinedTermBase> result = new TreeSet(comp);
622
        if(listTerm != null){
623
            result.addAll(listTerm);
624
        }
625
        return result;
626
    }
627

    
628
    public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
629
        TermIdInVocabularyComparator<?> comp = new TermIdInVocabularyComparator<>();
630

    
631
        SortedSet<DefinedTermBase> result = new TreeSet(comp);
632
        if(namedAreas != null){
633
            result.addAll(namedAreas);
634
        }
635
        return result;
636
    }
637

    
638
    public SortedSet<DefinedTermBase> getTermsOrderedByVocabularyOrder(List<DefinedTermBase> listAreas){
639
        HashMap<TermVocabulary<DefinedTermBase>, List<DefinedTermBase>> vocs = new HashMap<>();
640
        OrderedTermComparator<?> comp = new OrderedTermComparator<>();
641
        boolean allOrderedTerms = true;
642
        List<TermVocabulary> alreadyOrderIndexNull = new ArrayList<>();
643
        for (DefinedTermBase term: listAreas){
644
            if (!(term instanceof OrderedTermBase)){
645
                allOrderedTerms = false;
646
                break;
647
            }else if (((OrderedTermBase)term).getOrderIndex() == 0){
648
                if(alreadyOrderIndexNull.contains(term.getVocabulary())) {
649
                    allOrderedTerms = false;
650
                    break;
651
                }else{
652
                    alreadyOrderIndexNull.add(term.getVocabulary());
653
                }
654

    
655

    
656
            }
657
        }
658
        if (allOrderedTerms){
659
            SortedSet<DefinedTermBase> result = new TreeSet(comp.reversed());
660
            result.addAll(listAreas);
661
            return result;
662
        }else{
663
            return getTermsOrderedByLabels(listAreas, PreferencesUtil.getGlobalLanguage());
664
        }
665

    
666

    
667
    }
668

    
669
    private void registerHandlersAndListeners(AbstractLayer topMostLayer) {
670
        //selection listener
671
        E4SelectionListener selectionListener = new DistributionCellSelectionListener(part.getSelectionService(),
672
                selectionLayer, bodyDataProvider, part);
673
        selectionLayer.addLayerListener(selectionListener);
674
        selectionListener.setFullySelectedRowsOnly(false);
675

    
676
        //register handler for view configuration menu
677
      //  natTable.registerCommandHandler(toolbar.getDisplayPersistenceDialogCommandHandler());
678
    }
679

    
680
    /**
681
     * @return
682
     */
683
    public IRowDataProvider<TaxonDistributionDTO> getBodyDataProvider() {
684
        return bodyDataProvider;
685
    }
686

    
687
    public void setDirty() {
688
        part.setDirty();
689
    }
690

    
691
    public Reference getDefaultSource(){
692
        return defaultSource;
693
    }
694

    
695
//    /**
696
//     * @param result
697
//     */
698
//    public void reloadDistributions() {
699
//       loadDistributions(taxonList);
700
//
701
//    }
702

    
703

    
704

    
705
}
(8-8/20)