2 * Copyright (C) 2019 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.ui
.dialog
.configurator
;
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
;
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
;
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
;
62 public class DistributionAggregationWizardPage
extends AggregationConfigurationWizardPage
<DistributionAggregation
> implements Listener
, SelectionListener
{
64 private Button buttonSuperArea
;
65 private Combo comboAreaLevel
;
66 private Combo comboStatusOrder
;
68 private Combo comboClassificationSelection
;
69 private List
<Classification
> classifications
;
70 private Classification selectedClassification
;
72 private Button checkExportUnpublished
;
76 Object
[] checkedElements
;
84 public DistributionAggregationWizardPage(DistributionAggregationConfiguration configurator
) {
85 super(Messages
.DistributionAggregationWizardPage_TITLE
);
86 this.configurator
= configurator
;
88 this.setDescription(Messages
.DistributionAggregationWizardPage_DESCRIPTION
);
94 public void createControl(Composite parent
) {
95 final Composite composite
= new Composite(parent
, SWT
.NULL
);
97 String taxonStr
= ""; //$NON-NLS-1$
98 int count
= configurator
.getTaxonNodeFilter().getSubtreeFilter().size();
100 for (LogicFilter
<TaxonNode
> filter
: configurator
.getTaxonNodeFilter().getSubtreeFilter()){
101 subTreeNode
= CdmApplicationState
.getCurrentAppConfig().getTaxonNodeService().load(filter
.getUuid());
103 if (subTreeNode
.hasTaxon()){
104 taxonStr
+= subTreeNode
.getTaxon().getName().getTitleCache();
106 taxonStr
+= ", "; //$NON-NLS-1$
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() {
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
);
121 updateHigherRankCombo();
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() {
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();
140 checkUseHigherLevel
= new Button(composite
, SWT
.RADIO
);
141 checkUseHigherLevel
.setText(Messages
.DistributionAggregationWizardPage_CLASSIFICATION
);
142 checkUseHigherLevel
.addListener(SWT
.Selection
, new Listener() {
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();
153 GridLayoutFactory
.fillDefaults();
155 if (subTreeNode
!= null){
156 selectedClassification
= subTreeNode
.getClassification();
158 GridData gridDataClassificationCombo
= new GridData();
159 gridDataClassificationCombo
= new GridData(GridData
.BEGINNING
, GridData
.CENTER
, true, false);
160 gridDataClassificationCombo
.horizontalIndent
= 5;
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
>() {
168 public int compare(Classification o1
, Classification o2
) {
172 int result
= o1
.getTitleCache().compareTo(o2
.getTitleCache());
174 return o1
.getUuid().compareTo(o2
.getUuid());
179 for(Classification tree
: classifications
){
180 comboClassificationSelection
.add(tree
.getName().getText(), classifications
.indexOf(tree
));
184 comboClassificationSelection
.select(classifications
.indexOf(selectedClassification
));
185 comboClassificationSelection
.addSelectionListener(this);
187 if (StringUtils
.isNotBlank(taxonStr
)){
188 checkUseSelectedSubtree
.setSelection(true);
189 comboClassificationSelection
.setEnabled(false);
191 checkUseHigherLevel
.setSelection(true);
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
);
199 GridLayout gridLayout
= new GridLayout();
200 gridLayout
.numColumns
= 1;
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));
209 Label higherRankLabel
= new Label(control
, SWT
.NULL
);
210 higherRankLabel
.setText(Messages
.DistributionAggregationWizardPage_HIGHEST_RANK
);
212 comboHigherRank
= new Combo(control
, SWT
.BORDER
| SWT
.READ_ONLY
);
213 updateHigherRankCombo();
214 comboHigherRank
.addListener(SWT
.Selection
, this);
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);
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
);
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(){
236 public String
getText(Object element
){
237 if (element
instanceof AggregationMode
){
238 if (((AggregationMode
)element
).equals(AggregationMode
.ToParent
)){
239 return Messages
.DistributionAggregationWizardPage_CHILD_PARENT
;
241 if (((AggregationMode
)element
).equals(AggregationMode
.WithinTaxon
)){
242 return Messages
.DistributionAggregationWizardPage_AREA
;
249 List
<AggregationMode
> aggregationModeList
= new ArrayList
<>();
250 for (AggregationMode mode
: AggregationMode
.values()){
251 aggregationModeList
.add(mode
);
254 aggregationModeViewer
.setInput(aggregationModeList
);
256 aggregationModeViewer
.addCheckStateListener(new ICheckStateListener(){
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
)){
267 if (((AggregationMode
)checked
[i
]).equals(AggregationMode
.ToParent
)){
268 toParentChecked
= true;
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();
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
);
291 comboAreaLevel
.addSelectionListener(this);
292 comboAreaLevel
.setEnabled(false);
293 GridData gridData2
= new GridData();
294 gridData2
.horizontalSpan
= 2;
295 gridData2
.horizontalAlignment
= SWT
.LEFT
;
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() {
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();
315 Label sourceModeLabel
= new Label(control
, SWT
.NULL
);
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
);
322 for (AggregationSourceMode mode
:AggregationSourceMode
.values()){
323 comboSourceModeChildParent
.add(mode
.getLabel());
324 comboSourceModeChildParent
.setData(mode
.getLabel(), mode
);
328 comboSourceModeChildParent
.addSelectionListener(this);
329 comboSourceModeChildParent
.setEnabled(false);
330 comboSourceModeChildParent
.select(0);
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
);
338 for (AggregationSourceMode mode
:AggregationSourceMode
.values()){
339 comboSourceModeWithinTaxon
.add(mode
.getLabel());
340 comboSourceModeWithinTaxon
.setData(mode
.getLabel(), mode
);
343 comboSourceModeWithinTaxon
.addSelectionListener(this);
344 comboSourceModeWithinTaxon
.setEnabled(false);
345 comboSourceModeWithinTaxon
.select(0);
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());
354 sourceTypeViewer
.addCheckStateListener(new ICheckStateListener(){
356 public void checkStateChanged( CheckStateChangedEvent event
){
357 checkedElements
=sourceTypeViewer
.getCheckedElements();
358 getWizard().getContainer().updateButtons();
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);
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
);
387 comboStatusOrder
.addSelectionListener(this);
388 comboStatusOrder
.select(0);
390 checkExportUnpublished
= new Button(composite
, SWT
.CHECK
);
391 checkExportUnpublished
.setText(Messages
.DistributionAggregationWizardPage_EXPORT_UNPUBLISHED
);
393 GridLayoutFactory
.fillDefaults();
397 setControl(composite
);
407 public void widgetSelected(SelectionEvent e
) {
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);
415 sourceTypeViewer
.getTable().setEnabled(false);
420 if (e
.getSource().equals(comboAreaLevel
)){
421 buttonSuperArea
.setEnabled(StringUtils
.isNotBlank(comboAreaLevel
.getText()));
429 public void widgetDefaultSelected(SelectionEvent e
) {
430 // TODO Auto-generated method stub
436 public void handleEvent(Event event
) {
437 if (event
.widget
.equals(comboHigherRank
)){
438 updateLowerRankCombo();
440 if (event
.widget
.equals(comboClassificationSelection
)){
441 updateHigherRankCombo();
442 updateLowerRankCombo();
444 if (event
.widget
.equals(this.checkUseHigherLevel
)){
445 updateHigherRankCombo();
446 updateLowerRankCombo();
448 if (event
.widget
.equals(this.checkUseSelectedSubtree
)){
449 updateHigherRankCombo();
450 updateLowerRankCombo();
452 if (event
.widget
.equals(this.checkUseSelectedTaxonNode
)){
453 updateHigherRankCombo();
454 updateLowerRankCombo();
463 public TermTree
getStatusOrder(){
464 if (StringUtils
.isNotBlank(comboStatusOrder
.getText())){
465 return (TermTree
)comboStatusOrder
.getData(comboStatusOrder
.getText());
472 public boolean useUnpublishedData(){
473 return checkExportUnpublished
.getSelection();
480 public Classification
getSelectedClassification() {
481 return selectedClassification
;
485 public TaxonNode
getSubTreeNode() {