Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / configurator / StructuredDescriptionAggregationConfigurationWizardPage.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.List;
14 import java.util.UUID;
15
16 import org.eclipse.jface.layout.GridLayoutFactory;
17 import org.eclipse.jface.viewers.ArrayContentProvider;
18 import org.eclipse.jface.viewers.CheckStateChangedEvent;
19 import org.eclipse.jface.viewers.CheckboxTableViewer;
20 import org.eclipse.jface.viewers.ICheckStateListener;
21 import org.eclipse.jface.viewers.LabelProvider;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Combo;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Event;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Listener;
31
32 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
33 import eu.etaxonomy.cdm.api.service.description.AggregationMode;
34 import eu.etaxonomy.cdm.api.service.description.AggregationSourceMode;
35 import eu.etaxonomy.cdm.api.service.description.AggregationType;
36 import eu.etaxonomy.cdm.api.service.description.StructuredDescriptionAggregation;
37 import eu.etaxonomy.cdm.api.service.description.StructuredDescriptionAggregationConfiguration;
38 import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
39 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
40 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
41 import eu.etaxonomy.taxeditor.l10n.Messages;
42 import eu.etaxonomy.taxeditor.ui.combo.OriginalSourceTypeComparator;
43
44 /**
45 * @author k.luther
46 * @since 11.12.2019
47 */
48 public class StructuredDescriptionAggregationConfigurationWizardPage extends AggregationConfigurationWizardPage<StructuredDescriptionAggregation> implements Listener {
49
50 protected CheckboxTableViewer subTreeSelectionViewer;
51 private List<TaxonNodeDto> selectedNodes;
52
53
54
55 /**
56 * @param pageName
57 */
58 protected StructuredDescriptionAggregationConfigurationWizardPage(StructuredDescriptionAggregationConfiguration configurator, List<TaxonNodeDto> nodes) {
59 super(Messages.DistributionAggregationWizardPage_TITLE);
60 this.configurator = configurator;
61 this.setDescription(Messages.DistributionAggregationWizardPage_DESCRIPTION);
62 selectedNodes = nodes;
63 }
64
65
66
67
68 @Override
69 public void createControl(Composite parent) {
70 final Composite control = new Composite(parent, SWT.NULL);
71 GridLayout gridLayoutControl = new GridLayout();
72 gridLayoutControl.numColumns = 2;
73 control.setLayout(gridLayoutControl);
74 control.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, true, true));
75
76 //select between taxonnode filter for the whole descriptive dataset or the selected taxonnode/subtree
77 GridData gridDataRadioComposite = new GridData();
78 gridDataRadioComposite.horizontalSpan = 2;
79 GridLayout gridLayoutRadioComposite = new GridLayout();
80 gridLayoutRadioComposite.numColumns = 1;
81 Composite radioComposite = new Composite(control, SWT.NULL);
82 radioComposite.setLayoutData(gridDataRadioComposite);
83 radioComposite.setLayout(gridLayoutRadioComposite);
84
85 checkUseSelectedSubtree= new Button(radioComposite, SWT.RADIO);
86 checkUseSelectedSubtree.setText(Messages.AggregationWizardPage_SUBTREE);
87 checkUseSelectedSubtree.addListener(SWT.Selection, new Listener() {
88 @Override
89 public void handleEvent(Event e) {
90 subTreeSelectionViewer.getTable().setEnabled(checkUseSelectedSubtree.getSelection());
91 if (checkUseSelectedSubtree.getSelection() && subTreeSelectionViewer.getCheckedElements().length == 0){
92 setPageComplete(false);
93 }else{
94 setPageComplete(true);
95 }
96 }
97 });
98
99 checkUseSelectedTaxonNode= new Button(radioComposite, SWT.RADIO);
100 checkUseSelectedTaxonNode.setText(Messages.StructuredDescriptionAggregationWizardPage_SELECT_SELECTED_TAXA_ONLY);
101 checkUseSelectedTaxonNode.addListener(SWT.Selection, new Listener() {
102 @Override
103 public void handleEvent(Event e) {
104 subTreeSelectionViewer.getTable().setEnabled(checkUseSelectedTaxonNode.getSelection());
105 if (checkUseSelectedTaxonNode.getSelection() && subTreeSelectionViewer.getCheckedElements().length == 0){
106 setPageComplete(false);
107 }else{
108 setPageComplete(true);
109 }
110 }
111
112
113 });
114
115 // configurator.getTaxonNodeFilter().getSubtreeFilter().stream().forEach(p ->uuidList.add(p.getUuid()));
116 // List<String> propertyPaths = new ArrayList<>();
117 // propertyPaths.add("taxon.name");
118 // List<TaxonNode> nodeList = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(uuidList, propertyPaths);
119
120 Label selectSubtreeLabel = new Label(control, SWT.NULL);
121 selectSubtreeLabel.setText(Messages.StructuredDescriptionAggregationWizardPage_SELECT_SUBTREE);
122 selectSubtreeLabel.setToolTipText(Messages.StructuredDescriptionAggregationWizardPage_TOOLTIP_SELECT_SUBTREE);
123 subTreeSelectionViewer = CheckboxTableViewer.newCheckList(control, SWT.BORDER | SWT.SINGLE);
124
125 subTreeSelectionViewer.setContentProvider(new ArrayContentProvider());
126
127
128 subTreeSelectionViewer.setLabelProvider(new LabelProvider(){
129 @Override
130 public String getText(Object element){
131 if (element instanceof TaxonNodeDto){
132 return ((TaxonNodeDto)element).getNameTitleCache();
133 }else if (element instanceof TaxonNode){
134 return ((TaxonNode)element).getTaxon().getName().getTitleCache();
135 }
136 return null;
137 }
138
139 });
140
141
142
143
144 if (selectedNodes == null || selectedNodes.isEmpty()){
145 List<TaxonNode> nodeList;
146 List<UUID> uuidList = new ArrayList<>();
147 configurator.getTaxonNodeFilter().getSubtreeFilter().stream().forEach(p ->uuidList.add(p.getUuid()));
148 List<String> propertyPaths = new ArrayList<>();
149 propertyPaths.add("taxon.name");
150 nodeList = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(uuidList, propertyPaths);
151
152 subTreeSelectionViewer.setInput(nodeList);
153
154 }else{
155 subTreeSelectionViewer.setInput(selectedNodes);
156 }
157 subTreeSelectionViewer.setAllChecked(true);
158 subTreeSelectionViewer.addCheckStateListener(new ICheckStateListener(){
159 @Override
160 public void checkStateChanged( CheckStateChangedEvent event){
161 Object[] checked =subTreeSelectionViewer.getCheckedElements();
162 if (checked.length == 0){
163 setPageComplete(false);
164 }else{
165 setPageComplete(true);
166 }
167 }
168 });
169
170
171 //use taxonnode filter of the descriptive data set
172 checkUseHigherLevel = new Button(radioComposite, SWT.RADIO);
173 checkUseHigherLevel.setText(Messages.StructuredDescriptionAggregationWizardPage_SELECT_ALL_SUBTREES);
174 checkUseHigherLevel.addListener(SWT.Selection, new Listener() {
175 @Override
176 public void handleEvent(Event e) {
177 subTreeSelectionViewer.getTable().setEnabled(!checkUseHigherLevel.getSelection());
178 updateHigherRankCombo();
179 if (checkUseHigherLevel.getSelection()){
180 setPageComplete(true);
181 }
182 }
183 });
184
185
186
187
188 GridLayoutFactory.fillDefaults();
189
190 Label higherRankLabel = new Label(control, SWT.NULL);
191 higherRankLabel.setText(Messages.DistributionAggregationWizardPage_HIGHEST_RANK);
192
193 comboHigherRank = new Combo(control, SWT.BORDER | SWT.READ_ONLY);
194 updateHigherRankCombo();
195 comboHigherRank.addListener(SWT.Selection, this);
196
197
198 Label lowerRankLabel = new Label(control, SWT.NULL);
199 lowerRankLabel.setText(Messages.DistributionAggregationWizardPage_LOWEST_RANK);
200 comboLowerRank = new Combo(control, SWT.BORDER | SWT.READ_ONLY);
201 updateLowerRankCombo();
202 comboLowerRank.addListener(SWT.Selection, this);
203
204 Label aggregationModeLabel = new Label(control, SWT.NULL);
205 aggregationModeLabel.setText(Messages.DistributionAggregationWizardPage_AGGREGATION_MODE);
206 aggregationModeLabel.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_AGGR_MODE);
207 aggregationModeViewer = CheckboxTableViewer.newCheckList(control, SWT.BORDER | SWT.SINGLE);
208 aggregationModeViewer.setContentProvider(new ArrayContentProvider());
209 aggregationModeViewer.setLabelProvider(new LabelProvider(){
210 @Override
211 public String getText(Object element){
212 if (element instanceof AggregationMode){
213 return ((AggregationMode)element).getLabel();
214 }
215 return null;
216 }
217
218 });
219 List<AggregationMode> aggregationModeList = new ArrayList<>();
220 for (AggregationMode mode: AggregationMode.values()){
221 aggregationModeList.add(mode);
222 }
223
224 aggregationModeViewer.setInput(aggregationModeList);
225
226 aggregationModeViewer.addCheckStateListener(new ICheckStateListener(){
227 @Override
228 public void checkStateChanged( CheckStateChangedEvent event){
229 Object[] checked =aggregationModeViewer.getCheckedElements();
230 boolean withinChecked = false;
231 boolean toParentChecked = false;
232 for (int i = 0; i<checked.length;i++){
233 if (checked[i] instanceof AggregationMode){
234 if (((AggregationMode)checked[i]).equals(AggregationMode.WithinTaxon)){
235 withinChecked = true;
236 }
237 if (((AggregationMode)checked[i]).equals(AggregationMode.ToParent)){
238 toParentChecked = true;
239 }
240 }
241 }
242
243 comboSourceModeChildParent.setEnabled(toParentChecked);
244 comboSourceModeWithinTaxon.setEnabled(withinChecked);
245 // AggregationSourceMode areaMode = (AggregationSourceMode)comboSourceModeSubAreaSuperArea.getData(comboSourceModeSubAreaSuperArea.getText());
246 AggregationSourceMode taxonMode = (AggregationSourceMode)comboSourceModeChildParent.getData(comboSourceModeChildParent.getText());
247 // getSourceTypeViewer().getTable().setEnabled(((areachecked && (areaMode.equals(AggregationSourceMode.ALL) || areaMode.equals(AggregationSourceMode.ALL_SAMEVALUE) ) )|| toParentChecked && (taxonMode.equals(AggregationSourceMode.ALL) || taxonMode.equals(AggregationSourceMode.ALL_SAMEVALUE) )) );
248 getWizard().getContainer().updateButtons();
249 }
250 });
251
252 Label sourceModeLabelWithin = new Label(control, SWT.NULL);
253
254 sourceModeLabelWithin.setText(Messages.DistributionAggregationWizardPage_SOURCEMODE_WITHIN_TAXON);
255 sourceModeLabelWithin.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_SOURCEMODE_WITHIN_TAXON);
256 comboSourceModeWithinTaxon = new Combo(control, SWT.BORDER| SWT.READ_ONLY);
257 comboSourceModeWithinTaxon.setText(Messages.DistributionAggregationWizardPage_AGGREGATION_MODE);
258
259 for (AggregationSourceMode mode :AggregationSourceMode.list(AggregationMode.WithinTaxon, AggregationType.StructuredDescription) ){
260 comboSourceModeWithinTaxon.add(mode.getLabel());
261 comboSourceModeWithinTaxon.setData(mode.getLabel(), mode);
262
263 }
264
265
266 comboSourceModeWithinTaxon.setEnabled(false);
267 comboSourceModeWithinTaxon.select(0);
268 comboSourceModeWithinTaxon.addListener(SWT.Selection, this);
269 Label sourceModeLabel = new Label(control, SWT.NULL);
270
271 sourceModeLabel.setText(Messages.DistributionAggregationWizardPage_SOURCEMODE_CHILD_PARENT);
272 sourceModeLabel.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_SOURCEMODE_CHILD_PARENT);
273 comboSourceModeChildParent = new Combo(control, SWT.BORDER| SWT.READ_ONLY);
274 comboSourceModeChildParent.setText(Messages.DistributionAggregationWizardPage_AGGREGATION_MODE);
275
276 for (AggregationSourceMode mode :AggregationSourceMode.list(AggregationMode.ToParent, AggregationType.StructuredDescription) ){
277 comboSourceModeChildParent.add(mode.getLabel());
278 comboSourceModeChildParent.setData(mode.getLabel(), mode);
279
280 }
281
282
283 comboSourceModeChildParent.setEnabled(false);
284 comboSourceModeChildParent.select(0);
285 comboSourceModeChildParent.addListener(SWT.Selection, this);
286 GridLayoutFactory.fillDefaults();
287 Label sourceTypeLabel = new Label(control, SWT.NULL);
288 sourceTypeLabel.setText(Messages.DistributionAggregationWizardPage_SOURCE_TYPE);
289 sourceTypeLabel.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_SOURCE_TYPE);
290 sourceTypeViewer = CheckboxTableViewer.newCheckList(control, SWT.BORDER | SWT.SINGLE);
291 sourceTypeViewer.setContentProvider(new ArrayContentProvider());
292
293
294 sourceTypeViewer.addCheckStateListener(new ICheckStateListener(){
295 @Override
296 public void checkStateChanged( CheckStateChangedEvent event){
297 // checkedElements=sourceTypeViewer.getCheckedElements();
298 getWizard().getContainer().updateButtons();
299 }
300 });
301
302 List<String> typeStrings = new ArrayList<>();
303 OriginalSourceType[] typeArray = OriginalSourceType.values();
304 Arrays.sort(typeArray, new OriginalSourceTypeComparator(null));
305 Arrays.stream(typeArray).forEach(p ->typeMap.put(p.getLabel(), p));
306 Arrays.stream(typeArray).forEach(p ->typeStrings.add(p.getLabel()));
307 typeStrings.remove(OriginalSourceType.NomenclaturalReference.getLabel());
308 sourceTypeViewer.setInput(typeStrings);
309 sourceTypeViewer.setChecked(OriginalSourceType.PrimaryMediaSource.getLabel(), true);
310 sourceTypeViewer.setChecked(OriginalSourceType.PrimaryTaxonomicSource.getLabel(), true);
311 sourceTypeViewer.getTable().setEnabled(false);
312 setControl(control);
313 }
314
315 @Override
316 public void handleEvent(Event event) {
317 if (event.widget.equals(comboHigherRank)){
318 updateLowerRankCombo();
319 }
320
321 if (event.widget.equals(this.checkUseHigherLevel)){
322 updateHigherRankCombo();
323 updateLowerRankCombo();
324 }
325 if (event.widget.equals(this.checkUseSelectedSubtree)){
326 updateHigherRankCombo();
327 updateLowerRankCombo();
328 }
329 if (event.widget.equals(this.checkUseSelectedTaxonNode)){
330 updateHigherRankCombo();
331 updateLowerRankCombo();
332 }
333
334 if (event.widget.equals(this.comboSourceModeChildParent) || event.widget.equals(this.comboSourceModeWithinTaxon)){
335 if (getSourceModeChildParent().equals(AggregationSourceMode.NONE) && getSourceModeWithinTaxon().equals(AggregationSourceMode.NONE)){
336 sourceTypeViewer.getTable().setEnabled(false);
337 }else {
338 sourceTypeViewer.getTable().setEnabled(true);
339 }
340 }
341
342 }
343
344 public List<TaxonNode> getSelectedTaxonNodes(){
345
346 List<TaxonNode> result = new ArrayList<>();
347 for (Object o: subTreeSelectionViewer.getCheckedElements()){
348 if (o instanceof TaxonNode){
349 result.add((TaxonNode)o);
350 }
351 }
352 return result;
353 }
354
355
356 }