Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / e4 / DistributionEditor.java
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 org.apache.commons.collections4.map.LinkedMap;
24 import org.apache.log4j.Logger;
25 import org.eclipse.jface.layout.GridDataFactory;
26 import org.eclipse.jface.layout.GridLayoutFactory;
27 import org.eclipse.jface.wizard.WizardDialog;
28 import org.eclipse.nebula.widgets.nattable.NatTable;
29 import org.eclipse.nebula.widgets.nattable.config.ConfigRegistry;
30 import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration;
31 import org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate;
32 import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
33 import org.eclipse.nebula.widgets.nattable.data.IRowDataProvider;
34 import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
35 import org.eclipse.nebula.widgets.nattable.extension.e4.selection.E4SelectionListener;
36 import org.eclipse.nebula.widgets.nattable.extension.glazedlists.filterrow.DefaultGlazedListsFilterStrategy;
37 import org.eclipse.nebula.widgets.nattable.filterrow.FilterRowHeaderComposite;
38 import org.eclipse.nebula.widgets.nattable.freeze.CompositeFreezeLayer;
39 import org.eclipse.nebula.widgets.nattable.freeze.FreezeHelper;
40 import org.eclipse.nebula.widgets.nattable.freeze.FreezeLayer;
41 import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
42 import org.eclipse.nebula.widgets.nattable.grid.command.ClientAreaResizeCommand;
43 import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider;
44 import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider;
45 import org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider;
46 import org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer;
47 import org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer;
48 import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer;
49 import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer;
50 import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer;
51 import org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer;
52 import org.eclipse.nebula.widgets.nattable.layer.AbstractLayer;
53 import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
54 import org.eclipse.nebula.widgets.nattable.layer.ILayer;
55 import org.eclipse.nebula.widgets.nattable.layer.config.DefaultColumnHeaderStyleConfiguration;
56 import org.eclipse.nebula.widgets.nattable.painter.cell.TextPainter;
57 import org.eclipse.nebula.widgets.nattable.painter.cell.decorator.BeveledBorderDecorator;
58 import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
59 import org.eclipse.nebula.widgets.nattable.sort.config.SingleClickSortConfiguration;
60 import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
61 import org.eclipse.swt.SWT;
62 import org.eclipse.swt.events.SelectionAdapter;
63 import org.eclipse.swt.events.SelectionEvent;
64 import org.eclipse.swt.layout.GridData;
65 import org.eclipse.swt.layout.GridLayout;
66 import org.eclipse.swt.widgets.Button;
67 import org.eclipse.swt.widgets.Composite;
68 import org.eclipse.swt.widgets.Label;
69 import org.eclipse.swt.widgets.Text;
70
71 import ca.odell.glazedlists.BasicEventList;
72 import ca.odell.glazedlists.EventList;
73 import eu.etaxonomy.cdm.api.service.IVocabularyService;
74 import eu.etaxonomy.cdm.api.service.dto.TaxonDescriptionDTO;
75 import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
76 import eu.etaxonomy.cdm.compare.term.OrderedTermComparator;
77 import eu.etaxonomy.cdm.compare.term.TermIdInVocabularyComparator;
78 import eu.etaxonomy.cdm.compare.term.TermLanguageComparator;
79 import eu.etaxonomy.cdm.model.common.Language;
80 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
81 import eu.etaxonomy.cdm.model.description.Distribution;
82 import eu.etaxonomy.cdm.model.description.TaxonDescription;
83 import eu.etaxonomy.cdm.model.location.NamedArea;
84 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
85 import eu.etaxonomy.cdm.model.metadata.TermOrder;
86 import eu.etaxonomy.cdm.model.reference.Reference;
87 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
88 import eu.etaxonomy.cdm.model.term.OrderedTermBase;
89 import eu.etaxonomy.cdm.model.term.Representation;
90 import eu.etaxonomy.cdm.model.term.TermType;
91 import eu.etaxonomy.cdm.model.term.TermVocabulary;
92 import eu.etaxonomy.taxeditor.editor.IDistributionEditor;
93 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
94 import eu.etaxonomy.taxeditor.model.AbstractUtility;
95 import eu.etaxonomy.taxeditor.model.ImageResources;
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.ui.dialog.selection.ReferenceSelectionDialog;
101
102 /**
103 * @author k.luther
104 * @since 27.11.2018
105 */
106 public class DistributionEditor extends Composite implements IDistributionEditor{
107
108 @SuppressWarnings("unused")
109 private static final Logger logger = Logger.getLogger(DistributionEditor.class);
110
111 private static final String DISTRIBUTION_EDITOR = "Distribution Editor"; //$NON-NLS-1$
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 static final String SYNONYM_COLUMN = "synonym_column";//$NON-NLS-1$
120
121
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 = false;
134 private Text searchText;
135
136 private Button button3;
137 private Button button2;
138
139 protected EventList<TaxonDistributionDTO> taxonList;
140 private String actualNameCache;
141 protected Map<UUID,Map<NamedArea,Set<DescriptionElementBase>>> taxonDistributionMap = new HashMap<>();
142
143 protected List<TaxonDistributionDTO> descriptionsToSave = new ArrayList<>();
144
145 protected SortedSet<DefinedTermBase> areas;
146 protected DistributionEditorPart part;
147 private BodyLayerStack<TaxonDistributionDTO> bodyLayerStack;
148 private FreezeLayer freezeLayer;
149 private ConfigRegistry configRegistry;
150 private AbstractLayer topMostLayer;
151
152 private SelectionLayer selectionLayer;
153
154 public DistributionEditor(Composite parent, DistributionEditorPart part) {
155 super(parent, SWT.NULL);
156 isShowRank = PreferencesUtil.getBooleanValue(PreferencePredicate.ShowRankInDistributionEditor.getKey());
157 this.part = part;
158 this.setLayout(new GridLayout());
159 GridLayoutFactory.fillDefaults().applyTo(parent);
160
161 Composite topComposite = new Composite(parent, SWT.NONE);
162 createTopComposite(topComposite);
163
164 natTable = new NatTable(parent, false);
165 parent.pack();
166
167 Composite bottomComposite = new Composite(parent, SWT.NONE);
168 GridLayoutFactory.fillDefaults().applyTo(bottomComposite);
169 createStatusBar(bottomComposite);
170 }
171
172 public boolean isShowRank() {
173 return isShowRank;
174 }
175
176 public void setShowRank(boolean isShowRank) {
177 this.isShowRank = isShowRank;
178 }
179
180 public int getFirstDataColumnIndex() {
181 return firstDataColumnIndex;
182 }
183
184 public void setFirstDataColumnIndex(int firstDataColumnIndex) {
185 this.firstDataColumnIndex = firstDataColumnIndex;
186 }
187
188 @Override
189 public String getActualNameCache() {
190 return actualNameCache;
191 }
192
193 public void setActualNameCache(String actualNameCache) {
194 this.actualNameCache = actualNameCache;
195 }
196
197 public LinkedMap<String, String> getPropertyToLabelMap() {
198 return propertyToLabelMap;
199 }
200
201 public void setPropertyToLabelMap(LinkedMap<String, String> propertyToLabelMap) {
202 this.propertyToLabelMap = propertyToLabelMap;
203 }
204
205 public Map<Integer, NamedArea> getAreaToColumnIndexMap() {
206 return areaToColumnIndexMap;
207 }
208
209 public void setAreaToColumnIndexMap(Map<Integer, NamedArea> areaToColumnIndexMap) {
210 this.areaToColumnIndexMap = areaToColumnIndexMap;
211 }
212
213 public List<TaxonDistributionDTO> getDescriptionsToSave() {
214 return descriptionsToSave;
215 }
216
217 public void setDescriptionsToSave(List<TaxonDistributionDTO> descriptionsToSave) {
218 this.descriptionsToSave = descriptionsToSave;
219 }
220
221 private void createStatusBar(Composite composite) {
222 GridData gridData = new GridData();
223 gridData.horizontalSpan = 2;
224 gridData.grabExcessHorizontalSpace = true;
225 gridData.horizontalAlignment = GridData.FILL;
226 GridLayout gridLayout = new GridLayout();
227 gridLayout.numColumns = 3;
228 composite.setLayoutData(gridData);
229 composite.setLayout(gridLayout);
230
231
232 gridData = new GridData();
233 gridData.horizontalSpan = 1;
234 gridData.grabExcessHorizontalSpace = true;
235 gridData.horizontalAlignment = GridData.FILL;
236
237 statusLabel = new Label(composite, SWT.LEFT);
238 statusLabel.setText(ELEMENT_COUNT + (taxonList != null ? taxonList.size() : UNKNOWN));
239 statusLabel.setLayoutData(gridData);
240
241 statusLabelSourceReference = new Label(composite, SWT.RIGHT);
242
243 statusLabelSourceReference.setLayoutData(gridData);
244
245 if (defaultSource != null){
246 statusLabelSourceReference.setText(Messages.DistributionEditor_defaultSource + (defaultSource.getAbbrevTitle() != null? defaultSource.getAbbrevTitle() : defaultSource.getAbbrevTitleCache()));
247 }
248
249 button3 = new Button(composite, SWT.PUSH );
250 button3.setEnabled(false);
251 GridData gridData3 = new GridData();
252 gridData3.horizontalAlignment = SWT.RIGHT;
253 button3.setLayoutData(gridData3);
254 //button3.setText(Messages.ChecklistEditor_REMOVE_DEFAULT_SOURCE);
255 button3.setImage(ImageResources.getImage(ImageResources.TRASH_ICON));
256 button3.setToolTipText(Messages.ChecklistEditor_REMOVE_DEFAULT_SOURCE_TOOLTIP);
257 button3.addSelectionListener(new SelectionAdapter() {
258
259 @Override
260 public void widgetSelected(SelectionEvent event) {
261 defaultSource = null;
262
263 String defaultSourceStr = (defaultSource == null) ? "" : Messages.DistributionEditor_defaultSource + defaultSource.getTitleCache(); //$NON-NLS-1$
264 if (defaultSourceStr.length()> 100){
265 defaultSourceStr = defaultSourceStr.substring(0, 98) + "..."; //$NON-NLS-1$
266 }
267 statusLabelSourceReference.setText(defaultSourceStr);
268
269 button3.setBackground(AbstractUtility.getColor(Resources.COLOR_CONTROL_SELECTED));
270 button3.setEnabled(false);
271 button2.setText(Messages.ChecklistEditor_DEFAULT_SOURCE);
272 }
273
274 });
275 }
276
277 private void applyStyles(){
278 CdmModernNatTableThemeConfiguration configuration = new CdmModernNatTableThemeConfiguration();
279
280 // configuration.summaryRowHAlign = HorizontalAlignmentEnum.CENTER;
281 // NOTE: Getting the colors and fonts from the GUIHelper ensures that
282 // they are disposed properly (required by SWT)
283 configuration.cHeaderBgColor = GUIHelper.getColor(211, 211, 211);
284 configuration.rHeaderBgColor = GUIHelper.getColor(211, 211, 211);
285 natTable.addConfiguration(configuration);
286 }
287
288 private void configureNatTable(ConfigRegistry configRegistry,
289 AbstractLayer topMostLayer) {
290 /**
291 * CONFIGURATION
292 */
293 natTable.setConfigRegistry(configRegistry);
294
295 //add default configuration because autoconfigure is set to false in constructor
296 natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
297
298
299 natTable.addConfiguration(new DefaultColumnHeaderStyleConfiguration() {
300 {
301 TextPainter painter = new TextPainter();
302 painter.setCalculateByTextLength(true);
303 painter.setWrapText(false);
304
305 cellPainter = new BeveledBorderDecorator(painter);
306
307 }
308 });
309 // applyStyles();
310 // override the default sort configuration and change the mouse bindings
311 // to sort on a single click
312
313 natTable.addConfiguration(new SingleClickSortConfiguration());
314 natTable.addConfiguration(new FilterRowConfiguration(this));
315
316 // add the header menu configuration for adding the column header menu
317 // with hide/show actions
318 natTable.addConfiguration(new DistributionEditorHeaderMenuConfiguration(natTable));
319
320 // add custom configuration for data conversion and add column labels to viewport layer
321 topMostLayer.addConfiguration(new DistributionCellEditorDataConversionConfiguration(this));
322
323 applyStyles();
324 natTable.configure();
325 }
326
327 public void createTable(){
328 /**
329 * layers
330 */
331 createLayers();
332
333 /**
334 * configuration
335 */
336 configureNatTable( configRegistry, topMostLayer);
337
338 /**
339 * handlers and listeners
340 */
341 registerHandlersAndListeners(topMostLayer);
342 FreezeHelper.freeze(freezeLayer, bodyLayerStack.getViewPortLayer(),
343 new PositionCoordinate(bodyLayerStack.getViewPortLayer(), 0, 0),
344 new PositionCoordinate(bodyLayerStack.getViewPortLayer(), 0, -1));
345 //grab all space
346 GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
347
348 this.layout();
349 natTable.doCommand(new ClientAreaResizeCommand(natTable));
350 // boolean ok = natTable.doCommand(new ClientAreaResizeCommand(natTable));
351 }
352
353 private void createLayers() {
354
355 DistributionColumnAccessor columnPropertyAccessor = new DistributionColumnAccessor(this);
356 DistributionEditorConfigLabelAccumulator labelAccumulator = new DistributionEditorConfigLabelAccumulator(this);
357
358 // bodyLayerStack = new BodyLayerStack<>(taxonList, columnPropertyAccessor, new TaxonDistributionDtoComparator(), labelAccumulator);
359 bodyLayerStack = new BodyLayerStack<>(taxonList, columnPropertyAccessor, null, labelAccumulator);
360 bodyDataProvider = bodyLayerStack.getBodyDataProvider();
361
362 propertyToLabelMap.put(TAXON_COLUMN, Messages.ChecklistEditor_TAXON);
363 if (isShowRank){
364 propertyToLabelMap.put(RANK_COLUMN, Messages.ChecklistEditor_RANK);
365 propertyToLabelMap.put(SYNONYM_COLUMN, Messages.ChecklistEditor_SYNONYMS);
366 }else{
367 propertyToLabelMap.put(SYNONYM_COLUMN, Messages.ChecklistEditor_SYNONYMS);
368 }
369 configRegistry = new ConfigRegistry();
370
371 initLabels();
372 this.selectionLayer = bodyLayerStack.getSelectionLayer();
373
374 freezeLayer = new FreezeLayer(selectionLayer);
375 final CompositeFreezeLayer compositeFreezeLayer = new CompositeFreezeLayer(
376 freezeLayer, bodyLayerStack.getViewPortLayer(), selectionLayer);
377
378 // as the selection mouse bindings are registered for the region label
379 // GridRegion.BODY
380 // we need to set that region label to the viewport so the selection via mouse
381 // is working correctly
382 compositeFreezeLayer.setRegionName(GridRegion.BODY);
383
384 IDataProvider columnHeaderDataProvider =
385 new DefaultColumnHeaderDataProvider(propertyToLabelMap.values().toArray(new String[] {}), propertyToLabelMap);
386 DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
387 ILayer columnHeaderLayer = new ColumnHeaderLayer(
388 columnHeaderDataLayer,
389 compositeFreezeLayer,
390 selectionLayer);
391
392 FilterRowHeaderComposite<TaxonDistributionDTO> filterRowHeaderLayer =
393 new FilterRowHeaderComposite<>(
394 new DefaultGlazedListsFilterStrategy<>(
395 bodyLayerStack.getFilterList(),
396 columnPropertyAccessor,
397 configRegistry),
398 columnHeaderLayer,
399 columnHeaderDataLayer.getDataProvider(),
400 configRegistry);
401
402 // build the row header layer stack
403 IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
404 DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
405 ILayer rowHeaderLayer = new RowHeaderLayer(
406 rowHeaderDataLayer,
407 compositeFreezeLayer,
408 selectionLayer);
409
410 IDataProvider cornerDataProvider =
411 new DefaultCornerDataProvider(
412 columnHeaderDataProvider,
413 rowHeaderDataProvider);
414 DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
415 ILayer cornerLayer = new CornerLayer(
416 cornerDataLayer,
417 rowHeaderLayer,
418 filterRowHeaderLayer);
419
420 GridLayer gridLayer = new GridLayer(
421 compositeFreezeLayer,
422 filterRowHeaderLayer,
423 rowHeaderLayer,
424 cornerLayer);
425
426 natTable.setLayer(gridLayer);
427 topMostLayer = compositeFreezeLayer;
428 }
429
430 private Text createSearchBar(Composite parent) {
431
432 Button button1 = new Button(parent, SWT.PUSH );
433 GridData gridData2 = new GridData();
434 gridData2.horizontalSpan = 1;
435 gridData2.horizontalAlignment = SWT.RIGHT;
436
437 button1.setLayoutData(gridData2);
438
439 button1.setText(Messages.ChecklistEditor_DIST_STATUS);
440 button1.setToolTipText(Messages.ChecklistEditor_DIST_STATUS_TOOLTIP);
441 button1.addSelectionListener(new SelectionAdapter() {
442 @Override
443 public void widgetSelected(SelectionEvent event) {
444 AvailableDistributionWizard availableDistributionWizard = new AvailableDistributionWizard();
445 WizardDialog dialog = new WizardDialog(AbstractUtility.getShell(),
446 availableDistributionWizard);
447
448 int open = dialog.open();
449 if(open == 0){
450 reload();
451 }
452 }
453 });
454
455 button2 = new Button(parent, SWT.PUSH );
456 GridData gridData3 = new GridData();
457 gridData3.horizontalAlignment = SWT.RIGHT;
458
459 button2.setLayoutData(gridData3);
460
461 button2.setText(Messages.ChecklistEditor_DEFAULT_SOURCE);
462 button2.setToolTipText(Messages.ChecklistEditor_DEFAULT_SOURCE_TOOLTIP);
463 button2.addSelectionListener(new SelectionAdapter() {
464
465 @Override
466 public void widgetSelected(SelectionEvent event) {
467 defaultSource = ReferenceSelectionDialog.select(AbstractUtility.getShell(), null);
468
469 String defaultSourceStr = (defaultSource == null) ? "" : Messages.DistributionEditor_defaultSource + defaultSource.getTitleCache(); //$NON-NLS-1$
470 if (defaultSourceStr.length()> 100){
471 defaultSourceStr = defaultSourceStr.substring(0, 98) + "..."; //$NON-NLS-1$
472 }
473 statusLabelSourceReference.setText(defaultSourceStr);
474
475 button2.setBackground(AbstractUtility.getColor(Resources.COLOR_CONTROL_SELECTED));
476 // EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS, true);
477 button3.setEnabled(true);
478 button2.setText(Messages.ChecklistEditor_SWITCH_DEFAULT_SOURCE);
479 }
480 });
481
482 parent.pack();
483 return searchText;
484 }
485 /**
486 * This method should only be called for adding new Distribution columns and reloading the table.<br>
487 * It will hide the old distribution column and load the newly added columns.<br>
488 * <p>
489 * <b>Notice:</b> for data update please use <b>refresh()</b>
490 *
491 */
492 public void reload(){
493 this.areaToColumnIndexMap.clear();
494 this.areas.clear();
495 this.propertyToLabelMap.clear();
496 createTable();
497 natTable.redraw();
498 }
499
500 private void createTopComposite(Composite parent) {
501 GridLayout gridLayout = new GridLayout(3, false);
502 gridLayout.marginWidth = 0;
503 gridLayout.marginHeight = 0;
504 GridData gridData2 = new GridData();
505 gridData2.horizontalSpan = 1;
506 gridData2.horizontalAlignment = SWT.RIGHT;
507 gridData2.horizontalIndent = 3;
508 parent.setLayoutData(gridData2);
509 parent.setLayout(gridLayout);
510 searchText = createSearchBar(parent);
511
512
513 }
514 public void loadDistributions(List<TaxonDistributionDTO> taxonList) {
515 if (this.taxonList == null){
516 this.taxonList = new BasicEventList<>();
517 }
518 taxonList.stream().forEach(wrapper->DistributionEditor.this.taxonList.add(wrapper));
519 statusLabel.setText(ELEMENT_COUNT + taxonList.size());
520 createTaxonDistributionMap();
521 }
522
523 protected void createTaxonDistributionMap() {
524 Iterator<TaxonDistributionDTO> iterator = this.taxonList.iterator();
525 while (iterator.hasNext()){
526 TaxonDistributionDTO dto = iterator.next();
527 TaxonDescriptionDTO descriptionDto = dto.getDescriptionsWrapper();
528 for (TaxonDescription desc: descriptionDto.getDescriptions()){
529 if(this.part.getCdmEntitySession()!=null){
530 this.part.getCdmEntitySession().load(desc, true);
531 }
532 for (DescriptionElementBase descElement: desc.getElements()){
533 if (descElement instanceof Distribution){
534 Map<NamedArea, Set<DescriptionElementBase>> distributionsMap = taxonDistributionMap.get(dto.getTaxonUuid());
535
536 if (distributionsMap == null){
537 distributionsMap = new HashMap<>();
538 taxonDistributionMap.put(dto.getTaxonUuid(), distributionsMap);
539 }
540 Set<DescriptionElementBase> distributions = distributionsMap.get(((Distribution) descElement).getArea());
541 if (distributions == null){
542 distributions = new HashSet<>();
543 distributionsMap.put(((Distribution)descElement).getArea(), distributions);
544 }
545 distributions.add(descElement);
546 }
547 }
548 }
549 }
550 }
551
552 private void initLabels() {
553
554 int index = 2;
555 if (isShowRank){
556 index++;
557 }
558
559 loadNamedAreas();
560 if (areas == null){
561 areas = new TreeSet<>();
562 }
563 for (DefinedTermBase<?> area: areas) {
564 this.areaToColumnIndexMap.put(index++, (NamedArea)area);
565 String areaLabel;
566 //TODO: adapt to preference
567 Representation rep = area.getPreferredRepresentation(CdmStore.getDefaultLanguage());
568 String label = rep.getLabel();
569 if (label == null){
570 label = area.getTitleCache();
571 }
572 if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
573 if (area.getIdInVocabulary() != null){
574 areaLabel = area.getIdInVocabulary();
575 } else{
576 areaLabel = label;
577 }
578 }else if (PreferencesUtil.isShowSymbol1InChecklistEditor()){
579 if (area.getSymbol() != null){
580 areaLabel = area.getSymbol();
581 } else{
582 areaLabel = label;
583 }
584 }else if (PreferencesUtil.isShowSymbol2InChecklistEditor()){
585 if (area.getSymbol2() != null){
586 areaLabel = area.getSymbol2();
587 } else{
588 areaLabel = label;
589 }
590 }else{
591 areaLabel = label;
592 }
593
594 //String areaLabel = area.getLabel();
595 String property = area.getUuid().toString();
596 propertyToLabelMap.put(property, areaLabel);
597 }
598 }
599
600 private SortedSet<DefinedTermBase> loadNamedAreas() {
601 //IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
602
603 String valuesAreas = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaTerms.getKey(), true);
604 String values = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
605 Set<UUID> uuidList = new HashSet<UUID>();
606 String[] split;
607 List<String> listValue;
608 List<DefinedTermBase> termlist = new ArrayList<>();
609 if (values != null && values != "") { //$NON-NLS-1$
610 split = values.split(";"); //$NON-NLS-1$
611 listValue = Arrays.asList(split);
612
613 UUID uuid;
614 for(String s : listValue){
615 uuid = UUID.fromString(s);
616 uuidList.add(uuid);
617 }
618 }
619
620 List<TermVocabulary> vocs = new ArrayList<>();
621 IVocabularyService service = CdmStore.getService(IVocabularyService.class);
622 if (uuidList.isEmpty()){
623 List<TermVocabulary<DefinedTermBase>> vocList = CdmStore.getService(IVocabularyService.class).findByTermType(TermType.NamedArea, null);
624 vocs.addAll(vocList);
625 }else{
626 vocs= service.find(uuidList);
627 }
628 for (TermVocabulary<?> voc: vocs){
629 termlist.addAll(service.getTerms(voc, null, null, null, null).getRecords());
630 }
631 List<DefinedTermBase> filteredList = new ArrayList<>();
632 if (valuesAreas != null && valuesAreas != "") {
633 split = valuesAreas.split(";"); //$NON-NLS-1$
634 listValue = Arrays.asList(split);
635
636 for (DefinedTermBase<?> area: termlist){
637 if (listValue.contains(area.getUuid().toString())) {
638 filteredList.add(area);
639 }
640 }
641 }else{
642 filteredList.addAll(termlist);
643 }
644
645 TermOrder sortOrder = PreferencesUtil.getSortNamedAreasInDistributionEditor();
646 if (sortOrder.equals(TermOrder.Natural)){
647 areas = getTermsOrderedByVocabularyOrder(filteredList);
648 } else if (sortOrder.equals(TermOrder.IdInVoc)){
649 areas = getTermsOrderedByIdInVocabulary(filteredList);
650 }else{
651 areas = getTermsOrderedByLabels(filteredList, CdmStore.getDefaultLanguage());
652 }
653
654 return null;
655 }
656
657 public SortedSet<DefinedTermBase> getTermsOrderedByLabels(List<DefinedTermBase> listTerm,Language language){
658 TermLanguageComparator<DefinedTermBase> comp = new TermLanguageComparator<>(Language.DEFAULT(), language);
659 SortedSet<DefinedTermBase> result = new TreeSet<>(comp);
660 if(listTerm != null){
661 result.addAll(listTerm);
662 }
663
664 return result;
665 }
666
667 public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
668 TermIdInVocabularyComparator<DefinedTermBase> comp = new TermIdInVocabularyComparator<>();
669
670 SortedSet<DefinedTermBase> result = new TreeSet<>(comp);
671 if(namedAreas != null){
672 result.addAll(namedAreas);
673 }
674 return result;
675 }
676
677 public SortedSet<DefinedTermBase> getTermsOrderedByVocabularyOrder(List<DefinedTermBase> listAreas){
678 OrderedTermComparator<DefinedTermBase> comp = new OrderedTermComparator<>();
679 boolean allOrderedTerms = true;
680 List<TermVocabulary> alreadyOrderIndexNull = new ArrayList<>();
681 for (DefinedTermBase<?> term: listAreas){
682 if (!(term instanceof OrderedTermBase)){
683 allOrderedTerms = false;
684 break;
685 }else if (((OrderedTermBase<?>)term).getOrderIndex() == 0){
686 if(alreadyOrderIndexNull.contains(term.getVocabulary())) {
687 allOrderedTerms = false;
688 break;
689 }else{
690 alreadyOrderIndexNull.add(term.getVocabulary());
691 }
692
693
694 }
695 }
696 if (allOrderedTerms){
697 SortedSet<DefinedTermBase> result = new TreeSet<>(comp.reversed());
698 result.addAll(listAreas);
699 return result;
700 }else{
701 return getTermsOrderedByLabels(listAreas, PreferencesUtil.getGlobalLanguage());
702 }
703 }
704
705 private void registerHandlersAndListeners(AbstractLayer topMostLayer) {
706 //selection listener
707 E4SelectionListener<?> selectionListener = new DistributionCellSelectionListener(part.getSelectionService(),
708 selectionLayer, bodyDataProvider, part);
709 selectionLayer.addLayerListener(selectionListener);
710 selectionListener.setFullySelectedRowsOnly(false);
711
712 //register handler for view configuration menu
713 // natTable.registerCommandHandler(toolbar.getDisplayPersistenceDialogCommandHandler());
714 }
715
716 public IRowDataProvider<TaxonDistributionDTO> getBodyDataProvider() {
717 return bodyDataProvider;
718 }
719
720 public void setDirty() {
721 part.setDirty();
722 }
723
724 @Override
725 public Reference getDefaultSource(){
726 return defaultSource;
727 }
728
729 // /**
730 // * @param result
731 // */
732 // public void reloadDistributions() {
733 // loadDistributions(taxonList);
734 //
735 // }
736 }