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