Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / configurator / DistributionAggregationWizardPage.java
1 /**
2 * Copyright (C) 2019 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.ui.dialog.configurator;
10
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.Comparator;
15 import java.util.List;
16
17 import org.apache.commons.lang3.StringUtils;
18 import org.eclipse.jface.layout.GridLayoutFactory;
19 import org.eclipse.jface.viewers.ArrayContentProvider;
20 import org.eclipse.jface.viewers.CheckStateChangedEvent;
21 import org.eclipse.jface.viewers.CheckboxTableViewer;
22 import org.eclipse.jface.viewers.ICheckStateListener;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.jface.wizard.WizardDialog;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.events.SelectionListener;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Combo;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Event;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Listener;
37
38 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
39 import eu.etaxonomy.cdm.api.service.IClassificationService;
40 import eu.etaxonomy.cdm.api.service.ITermTreeService;
41 import eu.etaxonomy.cdm.api.service.description.AggregationMode;
42 import eu.etaxonomy.cdm.api.service.description.AggregationSourceMode;
43 import eu.etaxonomy.cdm.api.service.description.DistributionAggregation;
44 import eu.etaxonomy.cdm.api.service.description.DistributionAggregationConfiguration;
45 import eu.etaxonomy.cdm.filter.LogicFilter;
46 import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
47 import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
48 import eu.etaxonomy.cdm.model.taxon.Classification;
49 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
50 import eu.etaxonomy.cdm.model.term.TermTree;
51 import eu.etaxonomy.cdm.model.term.TermType;
52 import eu.etaxonomy.taxeditor.l10n.Messages;
53 import eu.etaxonomy.taxeditor.store.CdmStore;
54 import eu.etaxonomy.taxeditor.store.StoreUtil;
55 import eu.etaxonomy.taxeditor.ui.combo.OriginalSourceTypeComparator;
56 import eu.etaxonomy.taxeditor.ui.dialog.SuperAreaSelectionWizard;
57
58 /**
59 * @author k.luther
60 * @since 15.11.2019
61 */
62 public class DistributionAggregationWizardPage extends AggregationConfigurationWizardPage<DistributionAggregation> implements Listener, SelectionListener{
63
64 private Button buttonSuperArea;
65 private Combo comboAreaLevel;
66 private Combo comboStatusOrder;
67
68 private Combo comboClassificationSelection;
69 private List<Classification> classifications;
70 private Classification selectedClassification;
71
72 private Button checkExportUnpublished;
73
74
75
76 Object[] checkedElements;
77
78
79
80
81 /**
82 * @param pageName
83 */
84 public DistributionAggregationWizardPage(DistributionAggregationConfiguration configurator) {
85 super(Messages.DistributionAggregationWizardPage_TITLE);
86 this.configurator = configurator;
87
88 this.setDescription(Messages.DistributionAggregationWizardPage_DESCRIPTION);
89
90 }
91
92
93 @Override
94 public void createControl(Composite parent) {
95 final Composite composite = new Composite(parent, SWT.NULL);
96
97 String taxonStr = ""; //$NON-NLS-1$
98 int count = configurator.getTaxonNodeFilter().getSubtreeFilter().size();
99
100 for (LogicFilter<TaxonNode> filter: configurator.getTaxonNodeFilter().getSubtreeFilter()){
101 subTreeNode = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(filter.getUuid());
102 count--;
103 if (subTreeNode.hasTaxon()){
104 taxonStr += subTreeNode.getTaxon().getName().getTitleCache();
105 if (count>0){
106 taxonStr += ", "; //$NON-NLS-1$
107 }
108 }
109 }
110 if (StringUtils.isNotBlank(taxonStr)){
111 checkUseSelectedSubtree= new Button(composite, SWT.RADIO);
112 checkUseSelectedSubtree.setText(Messages.AggregationWizardPage_SUBTREE + " "+ taxonStr);
113 checkUseSelectedSubtree.addListener(SWT.Selection, new Listener() {
114 @Override
115 public void handleEvent(Event e) {
116 Button b = (Button) e.widget;
117 GridData data = (GridData) comboClassificationSelection.getLayoutData();
118 data.exclude = b.getSelection();
119 comboClassificationSelection.setEnabled(!data.exclude);
120
121 updateHigherRankCombo();
122 }
123 });
124
125 checkUseSelectedTaxonNode= new Button(composite, SWT.RADIO);
126 checkUseSelectedTaxonNode.setText(Messages.AggregationWizardPage_SINGLE_TAXON + " " + taxonStr + " "+Messages.AggregationWizardPage_WITHOUT_CHILDREN);
127 checkUseSelectedTaxonNode.addListener(SWT.Selection, new Listener() {
128 @Override
129 public void handleEvent(Event e) {
130 Button b = (Button) e.widget;
131 GridData data = (GridData) comboClassificationSelection.getLayoutData();
132 data.exclude = b.getSelection();
133 comboClassificationSelection.setEnabled(!data.exclude);
134 updateHigherRankCombo();
135 }
136
137
138 });
139 }
140 checkUseHigherLevel= new Button(composite, SWT.RADIO);
141 checkUseHigherLevel.setText(Messages.DistributionAggregationWizardPage_CLASSIFICATION);
142 checkUseHigherLevel.addListener(SWT.Selection, new Listener() {
143 @Override
144 public void handleEvent(Event e) {
145 Button b = (Button) e.widget;
146 GridData data = (GridData) comboClassificationSelection.getLayoutData();
147 data.exclude = b.getSelection();
148 comboClassificationSelection.setEnabled(data.exclude);
149 updateHigherRankCombo();
150 }
151 });
152
153 GridLayoutFactory.fillDefaults();
154
155 if (subTreeNode!= null){
156 selectedClassification = subTreeNode.getClassification();
157 }
158 GridData gridDataClassificationCombo = new GridData();
159 gridDataClassificationCombo = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
160 gridDataClassificationCombo.horizontalIndent = 5;
161
162 comboClassificationSelection = new Combo(composite, SWT.BORDER| SWT.READ_ONLY);
163 comboClassificationSelection.setLayoutData(gridDataClassificationCombo);
164 classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
165 Collections.sort(classifications, new Comparator<Classification>() {
166
167 @Override
168 public int compare(Classification o1, Classification o2) {
169 if (o1.equals(o2)){
170 return 0;
171 }
172 int result = o1.getTitleCache().compareTo(o2.getTitleCache());
173 if (result == 0){
174 return o1.getUuid().compareTo(o2.getUuid());
175 }
176 return result;
177 }
178 });
179 for(Classification tree : classifications){
180 comboClassificationSelection.add(tree.getName().getText(), classifications.indexOf(tree));
181
182 }
183
184 comboClassificationSelection.select(classifications.indexOf(selectedClassification));
185 comboClassificationSelection.addSelectionListener(this);
186
187 if (StringUtils.isNotBlank(taxonStr)){
188 checkUseSelectedSubtree.setSelection(true);
189 comboClassificationSelection.setEnabled(false);
190 }else{
191 checkUseHigherLevel.setSelection(true);
192 }
193 Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
194 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
195 gridData.horizontalSpan = 2;
196 gridData.verticalIndent = 5;
197 separator.setLayoutData(gridData);
198
199 GridLayout gridLayout = new GridLayout();
200 gridLayout.numColumns = 1;
201
202 composite.setLayout(gridLayout);
203 final Composite control = new Composite(composite, SWT.NULL);
204 GridLayout gridLayoutControl = new GridLayout();
205 gridLayoutControl.numColumns = 2;
206 control.setLayout(gridLayoutControl);
207 control.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, true, true));
208
209 Label higherRankLabel = new Label(control, SWT.NULL);
210 higherRankLabel.setText(Messages.DistributionAggregationWizardPage_HIGHEST_RANK);
211
212 comboHigherRank = new Combo(control, SWT.BORDER | SWT.READ_ONLY);
213 updateHigherRankCombo();
214 comboHigherRank.addListener(SWT.Selection, this);
215
216
217 Label lowerRankLabel = new Label(control, SWT.NULL);
218 lowerRankLabel.setText(Messages.DistributionAggregationWizardPage_LOWEST_RANK);
219 comboLowerRank = new Combo(control, SWT.BORDER | SWT.READ_ONLY);
220 updateLowerRankCombo();
221 comboLowerRank.addListener(SWT.Selection, this);
222
223 separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
224 gridData = new GridData(GridData.FILL_HORIZONTAL);
225 gridData.horizontalSpan = 2;
226 gridData.verticalIndent = 5;
227 separator.setLayoutData(gridData);
228
229 Label aggregationModeLabel = new Label(control, SWT.NULL);
230 aggregationModeLabel.setText(Messages.DistributionAggregationWizardPage_AGGREGATION_MODE);
231 aggregationModeLabel.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_AGGR_MODE);
232 aggregationModeViewer = CheckboxTableViewer.newCheckList(control, SWT.BORDER | SWT.SINGLE);
233 aggregationModeViewer.setContentProvider(new ArrayContentProvider());
234 aggregationModeViewer.setLabelProvider(new LabelProvider(){
235 @Override
236 public String getText(Object element){
237 if (element instanceof AggregationMode){
238 if (((AggregationMode)element).equals(AggregationMode.ToParent)){
239 return Messages.DistributionAggregationWizardPage_CHILD_PARENT;
240 }
241 if (((AggregationMode)element).equals(AggregationMode.WithinTaxon)){
242 return Messages.DistributionAggregationWizardPage_AREA;
243 }
244 }
245 return null;
246 }
247
248 });
249 List<AggregationMode> aggregationModeList = new ArrayList<>();
250 for (AggregationMode mode: AggregationMode.values()){
251 aggregationModeList.add(mode);
252 }
253
254 aggregationModeViewer.setInput(aggregationModeList);
255
256 aggregationModeViewer.addCheckStateListener(new ICheckStateListener(){
257 @Override
258 public void checkStateChanged( CheckStateChangedEvent event){
259 Object[] checked =aggregationModeViewer.getCheckedElements();
260 boolean areachecked = false;
261 boolean toParentChecked = false;
262 for (int i = 0; i<checked.length;i++){
263 if (checked[i] instanceof AggregationMode){
264 if (((AggregationMode)checked[i]).equals(AggregationMode.WithinTaxon)){
265 areachecked = true;
266 }
267 if (((AggregationMode)checked[i]).equals(AggregationMode.ToParent)){
268 toParentChecked = true;
269 }
270 }
271 }
272 comboAreaLevel.setEnabled(areachecked);
273 buttonSuperArea.setEnabled(areachecked);
274 comboSourceModeWithinTaxon.setEnabled(areachecked);
275 comboSourceModeChildParent.setEnabled(toParentChecked);
276 AggregationSourceMode areaMode = (AggregationSourceMode)comboSourceModeWithinTaxon.getData(comboSourceModeWithinTaxon.getText());
277 AggregationSourceMode taxonMode = (AggregationSourceMode)comboSourceModeChildParent.getData(comboSourceModeChildParent.getText());
278 sourceTypeViewer.getTable().setEnabled(((areachecked && (areaMode.equals(AggregationSourceMode.ALL) || areaMode.equals(AggregationSourceMode.ALL_SAMEVALUE) ) )|| toParentChecked && (taxonMode.equals(AggregationSourceMode.ALL) || taxonMode.equals(AggregationSourceMode.ALL_SAMEVALUE) )) );
279 getWizard().getContainer().updateButtons();
280 }
281 });
282 Label areaLevelLabel = new Label(control, SWT.NULL);
283 areaLevelLabel.setText(Messages.DistributionAggregationWizardPage_AREA_LEVEL);
284 areaLevelLabel.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_AREA_LEVEL);
285 comboAreaLevel = new Combo(control, SWT.BORDER | SWT.READ_ONLY);
286 List<NamedAreaLevel> namedAreaLevels = CdmStore.getTermManager().getAllTerms(TermType.NamedAreaLevel, null);
287 for (NamedAreaLevel level: namedAreaLevels) {
288 comboAreaLevel.add(level.getLabel());
289 comboAreaLevel.setData(level.getLabel(), level);
290 }
291 comboAreaLevel.addSelectionListener(this);
292 comboAreaLevel.setEnabled(false);
293 GridData gridData2 = new GridData();
294 gridData2.horizontalSpan = 2;
295 gridData2.horizontalAlignment = SWT.LEFT;
296
297 buttonSuperArea = new Button(control, SWT.PUSH );
298 buttonSuperArea.setLayoutData(gridData2);
299 buttonSuperArea.setEnabled(false);
300 buttonSuperArea.setText(Messages.DistributionAggregationWizardPage_SELECT_AREA);
301 buttonSuperArea.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_AREA_SELECTION);
302 buttonSuperArea.addSelectionListener(new SelectionAdapter() {
303 @Override
304 public void widgetSelected(SelectionEvent event) {
305 SuperAreaSelectionWizard availableDistributionWizard = new SuperAreaSelectionWizard((DistributionAggregationConfiguration) configurator, (NamedAreaLevel)comboAreaLevel.getData(comboAreaLevel.getText()));
306 WizardDialog dialog = new WizardDialog(StoreUtil.getShell(),
307 availableDistributionWizard);
308 int open = dialog.open();
309 }
310 });
311
312
313
314
315 Label sourceModeLabel = new Label(control, SWT.NULL);
316
317 sourceModeLabel.setText(Messages.DistributionAggregationWizardPage_SOURCEMODE_CHILD_PARENT);
318 sourceModeLabel.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_SOURCEMODE_CHILD_PARENT);
319 comboSourceModeChildParent = new Combo(control, SWT.BORDER| SWT.READ_ONLY);
320 comboSourceModeChildParent.setText(Messages.DistributionAggregationWizardPage_AGGREGATION_MODE);
321
322 for (AggregationSourceMode mode :AggregationSourceMode.values()){
323 comboSourceModeChildParent.add(mode.getLabel());
324 comboSourceModeChildParent.setData(mode.getLabel(), mode);
325
326 }
327
328 comboSourceModeChildParent.addSelectionListener(this);
329 comboSourceModeChildParent.setEnabled(false);
330 comboSourceModeChildParent.select(0);
331
332 Label sourceModeLabelSuperArea = new Label(control, SWT.NULL);
333 sourceModeLabelSuperArea.setText(Messages.DistributionAggregationWizardPage_SOURCE_MODE_AREA);
334 sourceModeLabelSuperArea.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_SOURCEMODE_AREA);
335 comboSourceModeWithinTaxon = new Combo(control, SWT.BORDER| SWT.READ_ONLY);
336 comboSourceModeWithinTaxon.setText(Messages.DistributionAggregationWizardPage_AGGREGATION_MODE);
337
338 for (AggregationSourceMode mode :AggregationSourceMode.values()){
339 comboSourceModeWithinTaxon.add(mode.getLabel());
340 comboSourceModeWithinTaxon.setData(mode.getLabel(), mode);
341 }
342
343 comboSourceModeWithinTaxon.addSelectionListener(this);
344 comboSourceModeWithinTaxon.setEnabled(false);
345 comboSourceModeWithinTaxon.select(0);
346
347 Label sourceTypeLabel = new Label(control, SWT.NULL);
348 sourceTypeLabel.setText(Messages.DistributionAggregationWizardPage_SOURCE_TYPE);
349 sourceTypeLabel.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_SOURCE_TYPE);
350 sourceTypeViewer = CheckboxTableViewer.newCheckList(control, SWT.BORDER | SWT.SINGLE);
351 sourceTypeViewer.setContentProvider(new ArrayContentProvider());
352
353
354 sourceTypeViewer.addCheckStateListener(new ICheckStateListener(){
355 @Override
356 public void checkStateChanged( CheckStateChangedEvent event){
357 checkedElements=sourceTypeViewer.getCheckedElements();
358 getWizard().getContainer().updateButtons();
359 }
360 });
361
362 List<String> typeStrings = new ArrayList<>();
363 OriginalSourceType[] typeArray = OriginalSourceType.values();
364 Arrays.sort(typeArray, new OriginalSourceTypeComparator(null));
365 Arrays.stream(typeArray).forEach(p ->typeMap.put(p.getLabel(), p));
366 Arrays.stream(typeArray).forEach(p ->typeStrings.add(p.getLabel()));
367 typeStrings.remove(OriginalSourceType.NomenclaturalReference.getLabel());
368 sourceTypeViewer.setInput(typeStrings);
369 sourceTypeViewer.setChecked(OriginalSourceType.PrimaryMediaSource.getLabel(), true);
370 sourceTypeViewer.setChecked(OriginalSourceType.PrimaryTaxonomicSource.getLabel(), true);
371 sourceTypeViewer.getTable().setEnabled(false);
372
373 //aggregation mode
374
375
376
377 Label selectStatusLabel = new Label(control, SWT.NULL);
378 selectStatusLabel.setText(Messages.DistributionAggregationWizardPage_STATUS_ORDER);
379 comboStatusOrder = new Combo(control, SWT.PUSH );
380 comboStatusOrder.add(Messages.DistributionAggregationWizardPage_DEFAULT);
381 comboStatusOrder.setData(Messages.DistributionAggregationWizardPage_DEFAULT, null);
382 for (TermTree tree: CdmStore.getService(ITermTreeService.class).list(TermType.PresenceAbsenceTerm, null, 0, null,null)){
383 comboStatusOrder.add(tree.getTitleCache());
384 comboStatusOrder.setData(tree.getTitleCache(), tree);
385 }
386
387 comboStatusOrder.addSelectionListener(this);
388 comboStatusOrder.select(0);
389
390 checkExportUnpublished = new Button(composite, SWT.CHECK);
391 checkExportUnpublished.setText(Messages.DistributionAggregationWizardPage_EXPORT_UNPUBLISHED);
392
393 GridLayoutFactory.fillDefaults();
394
395
396
397 setControl(composite);
398
399 }
400
401
402
403
404
405
406 @Override
407 public void widgetSelected(SelectionEvent e) {
408
409 if (e.getSource().equals(comboSourceModeChildParent) || e.getSource().equals(comboSourceModeWithinTaxon)){
410 AggregationSourceMode modeChild = (AggregationSourceMode)comboSourceModeChildParent.getData(comboSourceModeChildParent.getText());
411 AggregationSourceMode modeArea = (AggregationSourceMode)comboSourceModeWithinTaxon.getData(comboSourceModeWithinTaxon.getText());
412 if ((comboSourceModeChildParent.isEnabled() && (modeChild != null && modeChild.equals(AggregationSourceMode.ALL) || modeChild.equals(AggregationSourceMode.ALL_SAMEVALUE))) ||(comboSourceModeWithinTaxon.isEnabled() && (modeArea != null && modeArea.equals(AggregationSourceMode.ALL) || modeArea.equals(AggregationSourceMode.ALL_SAMEVALUE)))){
413 sourceTypeViewer.getTable().setEnabled(true);
414 }else{
415 sourceTypeViewer.getTable().setEnabled(false);
416 }
417
418 }
419
420 if (e.getSource().equals(comboAreaLevel)){
421 buttonSuperArea.setEnabled(StringUtils.isNotBlank(comboAreaLevel.getText()));
422 }
423
424
425 }
426
427
428 @Override
429 public void widgetDefaultSelected(SelectionEvent e) {
430 // TODO Auto-generated method stub
431
432 }
433
434
435 @Override
436 public void handleEvent(Event event) {
437 if (event.widget.equals(comboHigherRank)){
438 updateLowerRankCombo();
439 }
440 if (event.widget.equals(comboClassificationSelection)){
441 updateHigherRankCombo();
442 updateLowerRankCombo();
443 }
444 if (event.widget.equals(this.checkUseHigherLevel)){
445 updateHigherRankCombo();
446 updateLowerRankCombo();
447 }
448 if (event.widget.equals(this.checkUseSelectedSubtree)){
449 updateHigherRankCombo();
450 updateLowerRankCombo();
451 }
452 if (event.widget.equals(this.checkUseSelectedTaxonNode)){
453 updateHigherRankCombo();
454 updateLowerRankCombo();
455 }
456
457 }
458
459
460
461
462
463 public TermTree getStatusOrder(){
464 if (StringUtils.isNotBlank(comboStatusOrder.getText())){
465 return (TermTree)comboStatusOrder.getData(comboStatusOrder.getText());
466 }
467 return null;
468 }
469
470
471
472 public boolean useUnpublishedData(){
473 return checkExportUnpublished.getSelection();
474 }
475
476
477
478
479
480 public Classification getSelectedClassification() {
481 return selectedClassification;
482 }
483
484
485 public TaxonNode getSubTreeNode() {
486 return subTreeNode;
487 }
488 }