Project

General

Profile

Download (20.5 KB) Statistics
| Branch: | Tag: | Revision:
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.model.AbstractUtility;
54
import eu.etaxonomy.taxeditor.store.CdmStore;
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
    private Object[] checkedElements;
75

    
76
    public DistributionAggregationWizardPage(DistributionAggregationConfiguration configurator) {
77
        super(Messages.DistributionAggregationWizardPage_TITLE);
78
        this.configurator = configurator;
79

    
80
        this.setDescription(Messages.DistributionAggregationWizardPage_DESCRIPTION);
81
    }
82

    
83
    @Override
84
    public void createControl(Composite parent) {
85
        final Composite composite = new Composite(parent, SWT.NULL);
86

    
87
        String taxonStr = ""; //$NON-NLS-1$
88
        int count = configurator.getTaxonNodeFilter().getSubtreeFilter().size();
89

    
90
        for (LogicFilter<TaxonNode> filter: configurator.getTaxonNodeFilter().getSubtreeFilter()){
91
            subTreeNode = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(filter.getUuid());
92
            count--;
93
            if (subTreeNode.hasTaxon()){
94
                taxonStr += subTreeNode.getTaxon().getName().getTitleCache();
95
                if (count>0){
96
                    taxonStr += ", "; //$NON-NLS-1$
97
                }
98
            }
99
        }
100
        if (StringUtils.isNotBlank(taxonStr)){
101
            checkUseSelectedSubtree= new Button(composite,  SWT.RADIO);
102
            checkUseSelectedSubtree.setText(Messages.AggregationWizardPage_SUBTREE + " "+ taxonStr);
103
            checkUseSelectedSubtree.addListener(SWT.Selection, new Listener() {
104
                @Override
105
                public void handleEvent(Event e) {
106
                    Button b = (Button) e.widget;
107
                    GridData data = (GridData)  comboClassificationSelection.getLayoutData();
108
                    data.exclude = b.getSelection();
109
                    comboClassificationSelection.setEnabled(!data.exclude);
110

    
111
                    updateHigherRankCombo();
112
               }
113
            });
114

    
115
            checkUseSelectedTaxonNode= new Button(composite,  SWT.RADIO);
116
            checkUseSelectedTaxonNode.setText(Messages.AggregationWizardPage_SINGLE_TAXON + " " + taxonStr + " "+Messages.AggregationWizardPage_WITHOUT_CHILDREN);
117
            checkUseSelectedTaxonNode.addListener(SWT.Selection, new Listener() {
118
                @Override
119
                public void handleEvent(Event e) {
120
                    Button b = (Button) e.widget;
121
                    GridData data = (GridData)  comboClassificationSelection.getLayoutData();
122
                    data.exclude = b.getSelection();
123
                    comboClassificationSelection.setEnabled(!data.exclude);
124
                    updateHigherRankCombo();
125
                }
126
            });
127
        }
128
        checkUseHigherLevel= new Button(composite,  SWT.RADIO);
129
        checkUseHigherLevel.setText(Messages.DistributionAggregationWizardPage_CLASSIFICATION);
130
        checkUseHigherLevel.addListener(SWT.Selection, new Listener() {
131
           @Override
132
           public void handleEvent(Event e) {
133
               Button b = (Button) e.widget;
134
               GridData data = (GridData)  comboClassificationSelection.getLayoutData();
135
               data.exclude = b.getSelection();
136
               comboClassificationSelection.setEnabled(data.exclude);
137
               updateHigherRankCombo();
138
           }
139
        });
140

    
141
        GridLayoutFactory.fillDefaults();
142

    
143
        if (subTreeNode!= null){
144
            selectedClassification = subTreeNode.getClassification();
145
        }
146
        GridData gridDataClassificationCombo = new GridData();
147
        gridDataClassificationCombo = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
148
        gridDataClassificationCombo.horizontalIndent = 5;
149

    
150
        comboClassificationSelection = new Combo(composite, SWT.BORDER| SWT.READ_ONLY);
151
        comboClassificationSelection.setLayoutData(gridDataClassificationCombo);
152
        classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
153
        Collections.sort(classifications, new Comparator<Classification>() {
154

    
155
            @Override
156
            public int compare(Classification o1, Classification o2) {
157
                if (o1.equals(o2)){
158
                    return 0;
159
                }
160
                int result = o1.getTitleCache().compareTo(o2.getTitleCache());
161
                if (result == 0){
162
                    return o1.getUuid().compareTo(o2.getUuid());
163
                }
164
                return result;
165
            }
166
        });
167
        for(Classification tree : classifications){
168
            comboClassificationSelection.add(tree.getName().getText(), classifications.indexOf(tree));
169
        }
170

    
171
        comboClassificationSelection.select(classifications.indexOf(selectedClassification));
172
        comboClassificationSelection.addSelectionListener(this);
173

    
174
        if (StringUtils.isNotBlank(taxonStr)){
175
            checkUseSelectedSubtree.setSelection(true);
176
            comboClassificationSelection.setEnabled(false);
177
        }else{
178
            checkUseHigherLevel.setSelection(true);
179
        }
180
        Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
181
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
182
        gridData.horizontalSpan = 2;
183
        gridData.verticalIndent = 5;
184
        separator.setLayoutData(gridData);
185

    
186
        GridLayout gridLayout = new GridLayout();
187
        gridLayout.numColumns = 1;
188

    
189
        composite.setLayout(gridLayout);
190
        final Composite control = new Composite(composite, SWT.NULL);
191
        GridLayout gridLayoutControl = new GridLayout();
192
        gridLayoutControl.numColumns = 2;
193
        control.setLayout(gridLayoutControl);
194
        control.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, true, true));
195

    
196
        Label higherRankLabel = new Label(control, SWT.NULL);
197
        higherRankLabel.setText(Messages.DistributionAggregationWizardPage_HIGHEST_RANK);
198

    
199
        comboHigherRank = new Combo(control, SWT.BORDER | SWT.READ_ONLY);
200
        updateHigherRankCombo();
201
        comboHigherRank.addListener(SWT.Selection, this);
202

    
203
        Label lowerRankLabel = new Label(control, SWT.NULL);
204
        lowerRankLabel.setText(Messages.DistributionAggregationWizardPage_LOWEST_RANK);
205
        comboLowerRank = new Combo(control, SWT.BORDER | SWT.READ_ONLY);
206
        updateLowerRankCombo();
207
        comboLowerRank.addListener(SWT.Selection, this);
208

    
209
        separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
210
        gridData = new GridData(GridData.FILL_HORIZONTAL);
211
        gridData.horizontalSpan = 2;
212
        gridData.verticalIndent = 5;
213
        separator.setLayoutData(gridData);
214

    
215
        Label aggregationModeLabel = new Label(control, SWT.NULL);
216
        aggregationModeLabel.setText(Messages.DistributionAggregationWizardPage_AGGREGATION_MODE);
217
        aggregationModeLabel.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_AGGR_MODE);
218
        aggregationModeViewer =  CheckboxTableViewer.newCheckList(control, SWT.BORDER | SWT.SINGLE);
219
        aggregationModeViewer.setContentProvider(new ArrayContentProvider());
220
        aggregationModeViewer.setLabelProvider(new LabelProvider(){
221
            @Override
222
            public String getText(Object element){
223
                if (element instanceof AggregationMode){
224
                    if (((AggregationMode)element).equals(AggregationMode.ToParent)){
225
                        return Messages.DistributionAggregationWizardPage_CHILD_PARENT;
226
                    }
227
                    if (((AggregationMode)element).equals(AggregationMode.WithinTaxon)){
228
                        return Messages.DistributionAggregationWizardPage_AREA;
229
                    }
230
                }
231
                return null;
232
            }
233

    
234
        });
235
        List<AggregationMode> aggregationModeList = new ArrayList<>();
236
        for (AggregationMode mode: AggregationMode.values()){
237
            aggregationModeList.add(mode);
238
        }
239

    
240
        aggregationModeViewer.setInput(aggregationModeList);
241

    
242
        aggregationModeViewer.addCheckStateListener(new ICheckStateListener(){
243
            @Override
244
            public void checkStateChanged(    CheckStateChangedEvent event){
245
                Object[] checked =aggregationModeViewer.getCheckedElements();
246
                boolean areachecked = false;
247
                boolean toParentChecked = false;
248
                for (int i = 0; i<checked.length;i++){
249
                    if (checked[i] instanceof AggregationMode){
250
                        if (((AggregationMode)checked[i]).equals(AggregationMode.WithinTaxon)){
251
                            areachecked = true;
252
                        }
253
                        if (((AggregationMode)checked[i]).equals(AggregationMode.ToParent)){
254
                            toParentChecked = true;
255
                        }
256
                    }
257
                }
258
                comboAreaLevel.setEnabled(areachecked);
259
                buttonSuperArea.setEnabled(areachecked);
260
                comboSourceModeWithinTaxon.setEnabled(areachecked);
261
                comboSourceModeChildParent.setEnabled(toParentChecked);
262
                AggregationSourceMode areaMode = (AggregationSourceMode)comboSourceModeWithinTaxon.getData(comboSourceModeWithinTaxon.getText());
263
                AggregationSourceMode taxonMode = (AggregationSourceMode)comboSourceModeChildParent.getData(comboSourceModeChildParent.getText());
264
                sourceTypeViewer.getTable().setEnabled(((areachecked && (areaMode.equals(AggregationSourceMode.ALL) || areaMode.equals(AggregationSourceMode.ALL_SAMEVALUE) ) )|| toParentChecked && (taxonMode.equals(AggregationSourceMode.ALL) || taxonMode.equals(AggregationSourceMode.ALL_SAMEVALUE) )) );
265
                getWizard().getContainer().updateButtons();
266
              }
267
        });
268
        Label areaLevelLabel = new Label(control, SWT.NULL);
269
        areaLevelLabel.setText(Messages.DistributionAggregationWizardPage_AREA_LEVEL);
270
        areaLevelLabel.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_AREA_LEVEL);
271
        comboAreaLevel = new Combo(control,  SWT.BORDER | SWT.READ_ONLY);
272
        List<NamedAreaLevel> namedAreaLevels = CdmStore.getTermManager().getAllTerms(TermType.NamedAreaLevel, null);
273
        for (NamedAreaLevel level: namedAreaLevels) {
274
            comboAreaLevel.add(level.getLabel());
275
            comboAreaLevel.setData(level.getLabel(), level);
276
        }
277
        comboAreaLevel.addSelectionListener(this);
278
        comboAreaLevel.setEnabled(false);
279
        GridData gridData2 = new GridData();
280
        gridData2.horizontalSpan = 2;
281
        gridData2.horizontalAlignment = SWT.LEFT;
282

    
283
        buttonSuperArea = new Button(control, SWT.PUSH );
284
        buttonSuperArea.setLayoutData(gridData2);
285
        buttonSuperArea.setEnabled(false);
286
        buttonSuperArea.setText(Messages.DistributionAggregationWizardPage_SELECT_AREA);
287
        buttonSuperArea.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_AREA_SELECTION);
288
        buttonSuperArea.addSelectionListener(new SelectionAdapter() {
289
            @Override
290
            public void widgetSelected(SelectionEvent event) {
291
                SuperAreaSelectionWizard availableDistributionWizard = new SuperAreaSelectionWizard((DistributionAggregationConfiguration) configurator, (NamedAreaLevel)comboAreaLevel.getData(comboAreaLevel.getText()));
292
                WizardDialog dialog = new WizardDialog(AbstractUtility.getShell(),
293
                        availableDistributionWizard);
294
                int open = dialog.open();
295
            }
296
        });
297

    
298
        Label sourceModeLabel = new Label(control, SWT.NULL);
299

    
300
        sourceModeLabel.setText(Messages.DistributionAggregationWizardPage_SOURCEMODE_CHILD_PARENT);
301
        sourceModeLabel.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_SOURCEMODE_CHILD_PARENT);
302
        comboSourceModeChildParent = new Combo(control,  SWT.BORDER| SWT.READ_ONLY);
303
        comboSourceModeChildParent.setText(Messages.DistributionAggregationWizardPage_AGGREGATION_MODE);
304

    
305
        for (AggregationSourceMode mode :AggregationSourceMode.values()){
306
            comboSourceModeChildParent.add(mode.getLabel());
307
            comboSourceModeChildParent.setData(mode.getLabel(), mode);
308
        }
309

    
310
        comboSourceModeChildParent.addSelectionListener(this);
311
        comboSourceModeChildParent.setEnabled(false);
312
        comboSourceModeChildParent.select(0);
313

    
314
        Label sourceModeLabelSuperArea = new Label(control, SWT.NULL);
315
        sourceModeLabelSuperArea.setText(Messages.DistributionAggregationWizardPage_SOURCE_MODE_AREA);
316
        sourceModeLabelSuperArea.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_SOURCEMODE_AREA);
317
        comboSourceModeWithinTaxon = new Combo(control,  SWT.BORDER| SWT.READ_ONLY);
318
        comboSourceModeWithinTaxon.setText(Messages.DistributionAggregationWizardPage_AGGREGATION_MODE);
319

    
320
        for (AggregationSourceMode mode :AggregationSourceMode.values()){
321
            comboSourceModeWithinTaxon.add(mode.getLabel());
322
            comboSourceModeWithinTaxon.setData(mode.getLabel(), mode);
323
        }
324

    
325
        comboSourceModeWithinTaxon.addSelectionListener(this);
326
        comboSourceModeWithinTaxon.setEnabled(false);
327
        comboSourceModeWithinTaxon.select(0);
328

    
329
        Label sourceTypeLabel = new Label(control, SWT.NULL);
330
        sourceTypeLabel.setText(Messages.DistributionAggregationWizardPage_SOURCE_TYPE);
331
        sourceTypeLabel.setToolTipText(Messages.DistributionAggregationWizardPage_TOOLTIP_SOURCE_TYPE);
332
        sourceTypeViewer = CheckboxTableViewer.newCheckList(control, SWT.BORDER | SWT.SINGLE);
333
        sourceTypeViewer.setContentProvider(new ArrayContentProvider());
334

    
335
        sourceTypeViewer.addCheckStateListener(new ICheckStateListener(){
336
            @Override
337
            public void checkStateChanged(    CheckStateChangedEvent event){
338
                checkedElements=sourceTypeViewer.getCheckedElements();
339
                getWizard().getContainer().updateButtons();
340
              }
341
        });
342

    
343
        List<String> typeStrings = new ArrayList<>();
344
        OriginalSourceType[] typeArray = OriginalSourceType.values();
345
        Arrays.sort(typeArray, new OriginalSourceTypeComparator(null));
346
        Arrays.stream(typeArray).forEach(p ->typeMap.put(p.getLabel(), p));
347
        Arrays.stream(typeArray).forEach(p ->typeStrings.add(p.getLabel()));
348
        sourceTypeViewer.setInput(typeStrings);
349
        sourceTypeViewer.setChecked(OriginalSourceType.PrimaryMediaSource.getLabel(), true);
350
        sourceTypeViewer.setChecked(OriginalSourceType.PrimaryTaxonomicSource.getLabel(), true);
351
        sourceTypeViewer.getTable().setEnabled(false);
352

    
353
        //aggregation mode
354

    
355
        Label selectStatusLabel = new Label(control, SWT.NULL);
356
        selectStatusLabel.setText(Messages.DistributionAggregationWizardPage_STATUS_ORDER);
357
        comboStatusOrder = new Combo(control, SWT.PUSH );
358
        comboStatusOrder.add(Messages.DistributionAggregationWizardPage_DEFAULT);
359
        comboStatusOrder.setData(Messages.DistributionAggregationWizardPage_DEFAULT, null);
360
        for (TermTree<?> tree: CdmStore.getService(ITermTreeService.class).list(TermType.PresenceAbsenceTerm, null, 0, null,null)){
361
            comboStatusOrder.add(tree.getTitleCache());
362
            comboStatusOrder.setData(tree.getTitleCache(), tree);
363
        }
364

    
365
        comboStatusOrder.addSelectionListener(this);
366
        comboStatusOrder.select(0);
367

    
368
        checkExportUnpublished = new Button(composite,  SWT.CHECK);
369
        checkExportUnpublished.setText(Messages.DistributionAggregationWizardPage_EXPORT_UNPUBLISHED);
370

    
371
        GridLayoutFactory.fillDefaults();
372

    
373
        setControl(composite);
374
    }
375

    
376
    @Override
377
    public void widgetSelected(SelectionEvent e) {
378

    
379
        if (e.getSource().equals(comboSourceModeChildParent) || e.getSource().equals(comboSourceModeWithinTaxon)){
380
            AggregationSourceMode modeChild = (AggregationSourceMode)comboSourceModeChildParent.getData(comboSourceModeChildParent.getText());
381
            AggregationSourceMode modeArea = (AggregationSourceMode)comboSourceModeWithinTaxon.getData(comboSourceModeWithinTaxon.getText());
382
            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)))){
383
                sourceTypeViewer.getTable().setEnabled(true);
384
            }else{
385
                sourceTypeViewer.getTable().setEnabled(false);
386
            }
387
        }
388

    
389
        if (e.getSource().equals(comboAreaLevel)){
390
            buttonSuperArea.setEnabled(StringUtils.isNotBlank(comboAreaLevel.getText()));
391
        }
392
    }
393

    
394
    @Override
395
    public void widgetDefaultSelected(SelectionEvent e) {
396
        // TODO Auto-generated method stub
397
    }
398

    
399
    @Override
400
    public void handleEvent(Event event) {
401
        if (event.widget.equals(comboHigherRank)){
402
            updateLowerRankCombo();
403
        }
404
        if (event.widget.equals(comboClassificationSelection)){
405
            updateHigherRankCombo();
406
            updateLowerRankCombo();
407
        }
408
        if (event.widget.equals(this.checkUseHigherLevel)){
409
            updateHigherRankCombo();
410
            updateLowerRankCombo();
411
        }
412
        if (event.widget.equals(this.checkUseSelectedSubtree)){
413
            updateHigherRankCombo();
414
            updateLowerRankCombo();
415
        }
416
        if (event.widget.equals(this.checkUseSelectedTaxonNode)){
417
            updateHigherRankCombo();
418
            updateLowerRankCombo();
419
        }
420
    }
421

    
422
   public TermTree getStatusOrder(){
423
       if (StringUtils.isNotBlank(comboStatusOrder.getText())){
424
           return (TermTree<?>)comboStatusOrder.getData(comboStatusOrder.getText());
425
       }
426
       return null;
427
   }
428

    
429
   public boolean useUnpublishedData(){
430
       return checkExportUnpublished.getSelection();
431
   }
432

    
433
    public Classification getSelectedClassification() {
434
        return selectedClassification;
435
    }
436

    
437
    public TaxonNode getSubTreeNode() {
438
        return subTreeNode;
439
    }
440
}
(4-4/10)