Project

General

Profile

Download (27.2 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.TermType;
85
import eu.etaxonomy.cdm.model.common.TermVocabulary;
86
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
87
import eu.etaxonomy.cdm.model.description.Distribution;
88
import eu.etaxonomy.cdm.model.description.TaxonDescription;
89
import eu.etaxonomy.cdm.model.location.NamedArea;
90
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
91
import eu.etaxonomy.cdm.model.reference.Reference;
92
import eu.etaxonomy.taxeditor.editor.EditorUtil;
93
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
94
import eu.etaxonomy.taxeditor.model.AbstractUtility;
95
import eu.etaxonomy.taxeditor.preference.EditorPreferencePredicate;
96
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
97
import eu.etaxonomy.taxeditor.preference.Resources;
98
import eu.etaxonomy.taxeditor.preference.wizard.AvailableDistributionWizard;
99
import eu.etaxonomy.taxeditor.store.CdmStore;
100
import eu.etaxonomy.taxeditor.store.StoreUtil;
101
import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
102

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

    
110

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

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

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

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

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

    
134
    private boolean isShowRank = false;
135
    private Integer countNodes;
136
    private Text searchText;
137

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

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

    
143

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

    
151
    private SelectionLayer selectionLayer;
152

    
153

    
154

    
155
 //  private FixedSummaryRowLayer summaryRowLayer;
156

    
157

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

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

    
171

    
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
        Composite bottomComposite = new Composite(parent, SWT.NONE);
180
        GridLayoutFactory.fillDefaults().applyTo(bottomComposite);
181
        createStatusBar(bottomComposite);
182
    }
183

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

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

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

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

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

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

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

    
212

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

    
217

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

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

    
226

    
227

    
228
    private void createStatusBar(Composite composite) {
229
        GridData gridData = new GridData();
230
        gridData.horizontalSpan = 2;
231
        gridData.grabExcessHorizontalSpace = true;
232
        gridData.horizontalAlignment = GridData.FILL;
233
        GridLayout gridLayout = new GridLayout();
234
        gridLayout.numColumns = 2;
235
        composite.setLayoutData(gridData);
236
        composite.setLayout(gridLayout);
237

    
238

    
239
        gridData = new GridData();
240
        gridData.horizontalSpan = 1;
241
        gridData.grabExcessHorizontalSpace = true;
242
        gridData.horizontalAlignment = GridData.FILL;
243

    
244
        statusLabel = new Label(composite, SWT.LEFT);
245
        statusLabel.setText(ELEMENT_COUNT + (taxonList != null ? taxonList.size() : UNKNOWN));
246
        statusLabel.setLayoutData(gridData);
247

    
248
        statusLabelSourceReference = new Label(composite, SWT.RIGHT);
249

    
250
        statusLabelSourceReference.setLayoutData(gridData);
251

    
252
        if (defaultSource != null){
253
            statusLabelSourceReference.setText("Default Source Reference: " + defaultSource.getAbbrevTitle() != null? defaultSource.getAbbrevTitle() : defaultSource.getAbbrevTitleCache());
254

    
255
        }
256
    }
257

    
258
    private void applyStyles(){
259
        ModernNatTableThemeConfiguration configuration = new ModernNatTableThemeConfiguration();
260
        configuration.summaryRowHAlign = HorizontalAlignmentEnum.CENTER;
261
        // NOTE: Getting the colors and fonts from the GUIHelper ensures that
262
        // they are disposed properly (required by SWT)
263
        configuration.cHeaderBgColor = GUIHelper.getColor(211, 211, 211);
264
        configuration.rHeaderBgColor = GUIHelper.getColor(211, 211, 211);
265
        natTable.addConfiguration(configuration);
266

    
267
    }
268

    
269
    private void configureNatTable(ConfigRegistry configRegistry,
270
            AbstractLayer topMostLayer) {
271
        /**
272
         * CONFIGURATION
273
         */
274
        natTable.setConfigRegistry(configRegistry);
275

    
276
        applyStyles();
277

    
278
        //add default configuration because autoconfigure is set to false in constructor
279
        natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
280

    
281

    
282

    
283
        // override the default sort configuration and change the mouse bindings
284
        // to sort on a single click
285

    
286
        natTable.addConfiguration(new SingleClickSortConfiguration());
287
        natTable.addConfiguration(new FilterRowConfiguration(this));
288

    
289
        // add the header menu configuration for adding the column header menu
290
        // with hide/show actions
291
        natTable.addConfiguration(new DistributionEditorHeaderMenuConfiguration(natTable));
292

    
293
        // add custom configuration for data conversion and add column labels to viewport layer
294
        topMostLayer.addConfiguration(new DistributionCellEditorDataConversionConfiguration(this));
295

    
296
        natTable.configure();
297
    }
298

    
299
    public void createTable(){
300
        /**
301
         * layers
302
         */
303

    
304
        createLayers();
305

    
306
        /**
307
         * configuration
308
         */
309
        configureNatTable( configRegistry, topMostLayer);
310

    
311
        /**
312
         * handlers and listeners
313
         */
314
        registerHandlersAndListeners(topMostLayer);
315
        FreezeHelper.freeze(freezeLayer, bodyLayerStack.getViewPortLayer(),
316
                new PositionCoordinate(bodyLayerStack.getViewPortLayer(), 0, 0),
317
                new PositionCoordinate(bodyLayerStack.getViewPortLayer(), 0, -1));
318
        //grab all space
319
        GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
320

    
321
        this.layout();
322
      //  boolean ok = natTable.doCommand(new ClientAreaResizeCommand(natTable));
323
      //  System.out.println(ok);
324
    }
325

    
326

    
327
    private void createLayers() {
328

    
329
        DistributionColumnAccessor columnPropertyAccessor = new DistributionColumnAccessor(this);
330

    
331
        DistributionEditorConfigLabelAccumulator labelAccumulator = new DistributionEditorConfigLabelAccumulator(this);
332
        bodyLayerStack = new BodyLayerStack<>(taxonList, columnPropertyAccessor, new TaxonDistributionDtoComparator(), labelAccumulator);
333

    
334
        bodyDataProvider = bodyLayerStack.getBodyDataProvider();
335

    
336

    
337

    
338
        propertyToLabelMap.put(TAXON_COLUMN, Messages.ChecklistEditor_TAXON);
339
        if (isShowRank){
340
            propertyToLabelMap.put(RANK_COLUMN, Messages.ChecklistEditor_RANK);
341
        }
342
        configRegistry = new ConfigRegistry();
343

    
344
        initLabels();
345
        this.selectionLayer = bodyLayerStack.getSelectionLayer();
346

    
347

    
348

    
349
        freezeLayer = new FreezeLayer(selectionLayer);
350
        final CompositeFreezeLayer compositeFreezeLayer = new CompositeFreezeLayer(
351
                freezeLayer,  bodyLayerStack.getViewPortLayer(), selectionLayer);
352

    
353
        // as the selection mouse bindings are registered for the region label
354
        // GridRegion.BODY
355
        // we need to set that region label to the viewport so the selection via mouse
356
        // is working correctly
357
        compositeFreezeLayer.setRegionName(GridRegion.BODY);
358

    
359

    
360
        IDataProvider columnHeaderDataProvider =
361
                new DefaultColumnHeaderDataProvider(propertyToLabelMap.values().toArray(new String[] {}), propertyToLabelMap);
362
        DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
363
        ILayer columnHeaderLayer = new ColumnHeaderLayer(
364
            columnHeaderDataLayer,
365
            compositeFreezeLayer,
366
            selectionLayer);
367

    
368
        FilterRowHeaderComposite<TaxonDistributionDTO> filterRowHeaderLayer =
369
                new FilterRowHeaderComposite<>(
370
                        new DefaultGlazedListsFilterStrategy<>(
371
                                bodyLayerStack.getFilterList(),
372
                                columnPropertyAccessor,
373
                                configRegistry),
374
                        columnHeaderLayer,
375
                        columnHeaderDataLayer.getDataProvider(),
376
                        configRegistry);
377

    
378

    
379

    
380
     // build the row header layer stack
381
        IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
382
        DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
383
        ILayer rowHeaderLayer = new RowHeaderLayer(
384
            rowHeaderDataLayer,
385
            compositeFreezeLayer,
386
            selectionLayer);
387

    
388

    
389

    
390
        IDataProvider cornerDataProvider =
391
                new DefaultCornerDataProvider(
392
                    columnHeaderDataProvider,
393
                    rowHeaderDataProvider);
394
        DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
395
        ILayer cornerLayer = new CornerLayer(
396
            cornerDataLayer,
397
            rowHeaderLayer,
398
            filterRowHeaderLayer);
399

    
400
        GridLayer gridLayer = new GridLayer(
401
                compositeFreezeLayer,
402
                filterRowHeaderLayer,
403
                rowHeaderLayer,
404
                cornerLayer);
405

    
406
        natTable.setLayer(gridLayer);
407

    
408
        topMostLayer = compositeFreezeLayer;
409

    
410

    
411
    }
412

    
413
    /**
414
     * @param parent
415
     * @return
416
     */
417
    private Text createSearchBar(Composite parent) {
418

    
419
        Button button1 = new Button(parent, SWT.PUSH );
420
        GridData gridData2 = new GridData();
421
        gridData2.horizontalSpan = 1;
422
        gridData2.horizontalAlignment = SWT.RIGHT;
423

    
424
        button1.setLayoutData(gridData2);
425

    
426

    
427
        button1.setText(Messages.ChecklistEditor_DIST_STATUS);
428
        button1.setToolTipText(Messages.ChecklistEditor_DIST_STATUS_TOOLTIP);
429
        button1.addSelectionListener(new SelectionAdapter() {
430
            @Override
431
            public void widgetSelected(SelectionEvent event) {
432
                AvailableDistributionWizard availableDistributionWizard = new AvailableDistributionWizard();
433
                WizardDialog dialog = new WizardDialog(StoreUtil.getShell(),
434
                        availableDistributionWizard);
435

    
436
                int open = dialog.open();
437
                if(open == 0){
438
                    reload();
439
                }
440
            }
441
        });
442

    
443
        Button button2 = new Button(parent, SWT.PUSH );
444
        GridData gridData3 = new GridData();
445
        gridData2.horizontalSpan = 1;
446
        button2.setLayoutData(gridData3);
447

    
448

    
449
        button2.setText(Messages.ChecklistEditor_DEFAULT_SOURCE);
450
        button2.setToolTipText(Messages.ChecklistEditor_DEFAULT_SOURCE_TOOLTIP);
451
        button2.addSelectionListener(new SelectionAdapter() {
452

    
453
            @Override
454
            public void widgetSelected(SelectionEvent event) {
455
                defaultSource = ReferenceSelectionDialog.select(AbstractUtility.getShell(), null);
456

    
457
                String defaultSourceStr = (defaultSource == null) ? "" : "Default Source Reference: " + defaultSource.getTitleCache();
458
                if (defaultSourceStr.length()> 100){
459
                    defaultSourceStr = defaultSourceStr.substring(0, 98) + "...";
460
                }
461
                statusLabelSourceReference.setText(defaultSourceStr);
462

    
463
                button2.setBackground(EditorUtil.getColor(Resources.COLOR_CONTROL_SELECTED));
464
            }
465

    
466
        });
467

    
468
        parent.pack();
469
        return searchText;
470
    }
471
    /**
472
    * This method should only be called for adding new Distribution columns and reloading the table.<br>
473
    * It will hide the old distribution column and load the newly added columns.<br>
474
    * <p>
475
    * <b>Notice:</b> for data update please use <b>refresh()</b>
476
    *
477
    */
478
   @SuppressWarnings({ "unchecked", "rawtypes" })
479
   public void reload(){
480
       this.areaToColumnIndexMap.clear();
481
       this.areas.clear();
482
       this.propertyToLabelMap.clear();
483
       createTable();
484
       natTable.redraw();
485
    }
486

    
487
    /**
488
     * @param parent
489
     */
490
    private void createTopComposite(Composite parent) {
491
        GridLayout gridLayout = new GridLayout(3, false);
492
        gridLayout.marginWidth = 0;
493
        gridLayout.marginHeight = 0;
494
        GridData gridData2 = new GridData();
495
        gridData2.horizontalSpan = 1;
496
        gridData2.horizontalAlignment = SWT.RIGHT;
497
        gridData2.horizontalIndent = 3;
498
        parent.setLayoutData(gridData2);
499
        parent.setLayout(gridLayout);
500
        searchText = createSearchBar(parent);
501

    
502

    
503
    }
504
    public void loadDistributions(List<TaxonDistributionDTO> taxonList) {
505
        if (this.taxonList == null){
506
            this.taxonList = new BasicEventList<>();
507
        }
508
       taxonList.stream().forEach(wrapper->DistributionEditor.this.taxonList.add(wrapper));
509
       statusLabel.setText(ELEMENT_COUNT + (taxonList != null ? taxonList.size() : UNKNOWN));
510
       createTaxonDistributionMap();
511
    }
512

    
513
    protected void createTaxonDistributionMap() {
514
        Iterator<TaxonDistributionDTO> iterator = this.taxonList.iterator();
515
           while (iterator.hasNext()){
516
               TaxonDistributionDTO dto = iterator.next();
517
               TaxonDescriptionDTO descriptionDto = dto.getDescriptionsWrapper();
518
               for (TaxonDescription desc: descriptionDto.getDescriptions()){
519
                   if(this.part.getCdmEntitySession()!=null){
520
                       this.part.getCdmEntitySession().load(desc, true);
521
                   }
522
                   for (DescriptionElementBase descElement: desc.getElements()){
523
                       if (descElement instanceof Distribution){
524
                           Map<NamedArea, Set<DescriptionElementBase>> distributionsMap = taxonDistributionMap.get(dto.getTaxonUuid());
525

    
526
                           if (distributionsMap == null){
527
                               distributionsMap = new HashMap();
528
                               taxonDistributionMap.put(dto.getTaxonUuid(), distributionsMap);
529
                           }
530
                           Set<DescriptionElementBase> distributions = distributionsMap.get(((Distribution) descElement).getArea());
531
                           if (distributions == null){
532
                               distributions = new HashSet();
533
                               distributionsMap.put(((Distribution)descElement).getArea(), distributions);
534
                           }
535
                           distributions.add(descElement);
536

    
537
                       }
538
                   }
539
               }
540
           }
541
    }
542

    
543

    
544

    
545

    
546

    
547
    private void initLabels() {
548

    
549
        int index = 1;
550
        if (isShowRank){
551
            index++;
552
        }
553

    
554
        loadNamedAreas();
555
        if (areas == null){
556
            areas = new TreeSet<>();
557
        }
558
        for (DefinedTermBase area: areas) {
559
            this.areaToColumnIndexMap.put(index++, (NamedArea)area);
560
            String areaLabel;
561
            //TODO: adapt to preference
562
            Representation rep = area.getPreferredRepresentation(CdmStore.getDefaultLanguage());
563
            String label = rep.getLabel();
564
            if (label == null){
565
                label = area.getTitleCache();
566
            }
567
            if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
568
                if (area.getIdInVocabulary() != null){
569
                    areaLabel = area.getIdInVocabulary();
570
                } else{
571
                    areaLabel = label;
572
                }
573
            }else if (PreferencesUtil.isShowSymbol1InChecklistEditor()){
574
                if (area.getSymbol() != null){
575
                    areaLabel = area.getSymbol();
576
                } else{
577
                    areaLabel = label;
578
                }
579
            }else if (PreferencesUtil.isShowSymbol2InChecklistEditor()){
580
                if (area.getSymbol2() != null){
581
                    areaLabel = area.getSymbol2();
582
                } else{
583
                    areaLabel = label;
584
                }
585
            }else{
586
                areaLabel = label;
587
            }
588

    
589

    
590
            //String areaLabel = area.getLabel();
591
            String property = areaLabel;
592
            propertyToLabelMap.put(property, areaLabel);
593
        }
594

    
595
    }
596

    
597
    private SortedSet<DefinedTermBase> loadNamedAreas() {
598
        //IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
599

    
600
        String valuesAreas = PreferencesUtil.getStringValue(EditorPreferencePredicate.AvailableDistributionAreaTerms.getKey());
601
        String values = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
602
        Set<UUID> uuidList = new HashSet<UUID>();
603
        String[] split;
604
        List<String> listValue;
605
        List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
606
        if (values != null && values != "") { //$NON-NLS-1$
607
            split = values.split(";"); //$NON-NLS-1$
608
            listValue = Arrays.asList(split);
609

    
610

    
611
            UUID uuid;
612
            for(String s : listValue){
613
                uuid = UUID.fromString(s);
614
                uuidList.add(uuid);
615

    
616
            }
617
        }
618

    
619
        List<TermVocabulary> vocs = new ArrayList<>();
620
        IVocabularyService service =  CdmStore.getService(IVocabularyService.class);
621
        if (uuidList.isEmpty()){
622
            List<TermVocabulary<DefinedTermBase>> vocList = CdmStore.getService(IVocabularyService.class).findByTermType(TermType.NamedArea, null);
623
            vocs.addAll(vocList);
624
        }else{
625
            vocs= service.find(uuidList);
626
        }
627
        split = valuesAreas.split(";");
628
        listValue = Arrays.asList(split);
629
        for (TermVocabulary voc: vocs){
630
            termlist.addAll(service.getTerms(voc, null, null, null, null).getRecords());
631
        }
632
        List<DefinedTermBase> filteredList = new ArrayList();
633
        for (DefinedTermBase area: termlist){
634
            if (listValue.contains(area.getUuid().toString())) {
635
                filteredList.add(area);
636
            }
637

    
638
        }
639

    
640
        if (PreferencesUtil.isSortNamedAreaByOrderInVocabulary()){
641
            areas =  getTermsOrderedByVocabularyOrder(filteredList);
642
        } else if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
643
            areas = getTermsOrderedByIdInVocabulary(filteredList);
644
        }else{
645
            areas =  getTermsOrderedByLabels(filteredList, CdmStore.getDefaultLanguage());
646
        }
647

    
648
        return null;
649
    }
650

    
651
    public SortedSet<DefinedTermBase> getTermsOrderedByLabels(List<DefinedTermBase> listTerm,Language language){
652
        TermLanguageComparator<?> comp = new TermLanguageComparator<>();
653
        comp.setCompareLanguage(language);
654
        SortedSet<DefinedTermBase> result = new TreeSet(comp);
655
        if(listTerm != null){
656
            result.addAll(listTerm);
657
        }
658
        return result;
659
    }
660

    
661
    public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
662
        TermIdInVocabularyComparator<?> comp = new TermIdInVocabularyComparator<>();
663

    
664
        SortedSet<DefinedTermBase> result = new TreeSet(comp);
665
        if(namedAreas != null){
666
            result.addAll(namedAreas);
667
        }
668
        return result;
669
    }
670

    
671
    public SortedSet<DefinedTermBase> getTermsOrderedByVocabularyOrder(List<DefinedTermBase> listAreas){
672
        HashMap<TermVocabulary<DefinedTermBase>, List<DefinedTermBase>> vocs = new HashMap<>();
673
        OrderedTermComparator<?> comp = new OrderedTermComparator<>();
674
        boolean allOrderedTerms = true;
675
        List<TermVocabulary> alreadyOrderIndexNull = new ArrayList<>();
676
        for (DefinedTermBase term: listAreas){
677
            if (!(term instanceof OrderedTermBase)){
678
                allOrderedTerms = false;
679
                break;
680
            }else if (((OrderedTermBase)term).getOrderIndex() == 0){
681
                if(alreadyOrderIndexNull.contains(term.getVocabulary())) {
682
                    allOrderedTerms = false;
683
                    break;
684
                }else{
685
                    alreadyOrderIndexNull.add(term.getVocabulary());
686
                }
687

    
688

    
689
            }
690
        }
691
        if (allOrderedTerms){
692
            SortedSet<DefinedTermBase> result = new TreeSet(comp.reversed());
693
            result.addAll(listAreas);
694
            return result;
695
        }else{
696
            return getTermsOrderedByLabels(listAreas, PreferencesUtil.getGlobalLanguage());
697
        }
698

    
699

    
700
    }
701

    
702
    private void registerHandlersAndListeners(AbstractLayer topMostLayer) {
703
        //selection listener
704
        E4SelectionListener selectionListener = new DistributionCellSelectionListener(part.getSelectionService(),
705
                selectionLayer, bodyDataProvider, part);
706
        selectionLayer.addLayerListener(selectionListener);
707
        selectionListener.setFullySelectedRowsOnly(false);
708

    
709
        //register handler for view configuration menu
710
      //  natTable.registerCommandHandler(toolbar.getDisplayPersistenceDialogCommandHandler());
711
    }
712

    
713
    /**
714
     * @return
715
     */
716
    public IRowDataProvider<TaxonDistributionDTO> getBodyDataProvider() {
717
        return bodyDataProvider;
718
    }
719

    
720
    public void setDirty() {
721
        part.setDirty();
722
    }
723

    
724
    public Reference getDefaultSource(){
725
        return defaultSource;
726
    }
727

    
728
//    /**
729
//     * @param result
730
//     */
731
//    public void reloadDistributions() {
732
//       loadDistributions(taxonList);
733
//
734
//    }
735

    
736

    
737

    
738
}
(8-8/20)