Project

General

Profile

Revision 4e514541

ID4e514541a392a5ef4b338956d1f2174c692e0e82
Parent 08bcc80a
Child 790e1587

Added by Patrick Plitzner over 1 year ago

ref #7875 first implementation of multiple area select (experimental)

View differences:

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/DescriptiveDataSetComposite.java
8 8
 */
9 9
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet;
10 10

  
11
import java.util.HashSet;
12
import java.util.Set;
13

  
14
import org.apache.commons.lang.StringUtils;
11 15
import org.eclipse.jface.viewers.TreeViewer;
12 16
import org.eclipse.swt.SWT;
13 17
import org.eclipse.swt.layout.GridData;
......
40 44
    private TermUuidComboViewer comboRankMax;
41 45
    private FeatureTreeEditorComposite featureTreeEditorComposite;
42 46
    private TreeViewer taxonNodeTree;
43
    private NamedArea area;
47
    private Set<NamedArea> areas = new HashSet<>();
44 48
    private Text textAreaText;
45 49
    private Button btnChooseArea;
46 50
    private Button btnRemoveArea;
......
173 177
    public TreeViewer getTaxonNodeTree() {
174 178
        return taxonNodeTree;
175 179
    }
176

  
177
    public NamedArea getArea(){
178
        return area;
180
    public Set<NamedArea> getAreas() {
181
        return areas;
179 182
    }
180
    public void setArea(NamedArea area) {
181
        this.area = area;
182
        textAreaText.setText(area.getLabel());
183
    public void setAreas(Set<NamedArea> areas) {
184
        this.areas = areas;
185
        textAreaText.setText(StringUtils.join(areas, ", "));
183 186
    }
184 187
    public void removeArea() {
185
        this.area = null;
188
        this.areas = null;
186 189
        textAreaText.setText(""); //$NON-NLS-1$
187 190
    }
188 191
    public Button getBtnChooseArea() {
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/DescriptiveDataSetEditor.java
9 9
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet;
10 10

  
11 11
import java.util.ArrayList;
12
import java.util.Arrays;
12 13
import java.util.Collection;
13 14
import java.util.Collections;
14 15
import java.util.HashSet;
......
32 33
import org.eclipse.e4.ui.services.EMenuService;
33 34
import org.eclipse.e4.ui.services.IServiceConstants;
34 35
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
36
import org.eclipse.jface.dialogs.Dialog;
35 37
import org.eclipse.jface.util.LocalSelectionTransfer;
36 38
import org.eclipse.jface.viewers.IStructuredSelection;
37 39
import org.eclipse.jface.viewers.StructuredSelection;
40
import org.eclipse.jface.viewers.StyledString.Styler;
38 41
import org.eclipse.jface.viewers.Viewer;
39 42
import org.eclipse.swt.SWT;
40 43
import org.eclipse.swt.dnd.DND;
......
46 49
import org.eclipse.swt.events.ModifyEvent;
47 50
import org.eclipse.swt.events.ModifyListener;
48 51
import org.eclipse.swt.events.SelectionAdapter;
52
import org.eclipse.swt.graphics.TextStyle;
49 53
import org.eclipse.swt.widgets.Composite;
54
import org.eclipse.swt.widgets.Control;
50 55
import org.eclipse.swt.widgets.Display;
51 56
import org.eclipse.swt.widgets.Shell;
52 57

  
......
55 60
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
56 61
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
57 62
import eu.etaxonomy.cdm.api.service.ITermService;
63
import eu.etaxonomy.cdm.api.service.IVocabularyService;
58 64
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
65
import eu.etaxonomy.cdm.model.common.TermType;
66
import eu.etaxonomy.cdm.model.common.TermVocabulary;
59 67
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
60 68
import eu.etaxonomy.cdm.model.description.FeatureTree;
61 69
import eu.etaxonomy.cdm.model.location.NamedArea;
......
69 77
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
70 78
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
71 79
import eu.etaxonomy.taxeditor.model.MessagingUtils;
80
import eu.etaxonomy.taxeditor.preference.wizard.AreaLabelProvider;
81
import eu.etaxonomy.taxeditor.preference.wizard.CheckBoxTreeComposite;
82
import eu.etaxonomy.taxeditor.preference.wizard.DistributionContentProvider;
72 83
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
73 84
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
74 85
import eu.etaxonomy.taxeditor.store.CdmStore;
75
import eu.etaxonomy.taxeditor.ui.dialog.selection.NamedAreaSelectionDialog;
76 86
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
77 87
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
78 88

  
......
152 162

  
153 163
            @Override
154 164
            public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
155
                NamedArea area = NamedAreaSelectionDialog.select(shell, null, null);
156
                if(area!=null){
157
                    composite.setArea(area);
165
                AreasSelectionDialog areasSelectionDialog = new AreasSelectionDialog(composite.getShell(), composite.getAreas());
166
                areasSelectionDialog.open();
167
                Set<NamedArea> selectedAreas = areasSelectionDialog.getSelectedAreas();
168
                if(selectedAreas!=null){
169
                    composite.setAreas(selectedAreas);
170
                    dirty.setDirty(true);
158 171
                }
159
                dirty.setDirty(true);
160 172
            }
161 173
        });
162 174
        composite.getBtnRemoveArea().addSelectionListener(new SelectionAdapter() {
......
207 219
        }
208 220
        Set<NamedArea> geoFilter = descriptiveDataSet.getGeoFilter();
209 221
        if(geoFilter!=null && !geoFilter.isEmpty()){
210
            composite.setArea(geoFilter.iterator().next());
222
            composite.setAreas(geoFilter);
211 223
        }
212 224
        Set<TaxonNode> taxonSubtreeFilter = descriptiveDataSet.getTaxonSubtreeFilter();
213 225
        if(taxonSubtreeFilter!=null){
......
271 283
    @Persist
272 284
    @Override
273 285
    public void save(IProgressMonitor monitor) {
274
        NamedArea area = composite.getArea();
275
        Set<NamedArea> areas = new HashSet<>();
276
        if(area!=null){
277
            areas.add(area);
278
        }
286
        Set<NamedArea> areas = composite.getAreas();
279 287
        Object input = composite.getTaxonNodeTree().getInput();
280 288
        if(input!=null){
281 289
            descriptiveDataSet.setTaxonSubtreeFilter(new HashSet<>());//clear existing filter
......
384 392
        dirty.setDirty(true);
385 393
    }
386 394

  
395
    private class AreasSelectionDialog extends Dialog{
396

  
397
        private CheckBoxTreeComposite treeComposite;
398

  
399
        private Set<NamedArea> selectedAreas = new HashSet<>();
400

  
401
        private Collection<TermVocabulary<NamedArea>> namedAreas;
402

  
403

  
404
        protected AreasSelectionDialog(Shell parentShell, Set<NamedArea> selectedAreas) {
405
            super(parentShell);
406
            this.selectedAreas = selectedAreas;
407
            this.namedAreas = CdmStore.getService(IVocabularyService.class).findByTermType(TermType.NamedArea, null);
408
        }
409

  
410
        @Override
411
        protected Control createDialogArea(Composite parent) {
412
            Styler styler = new Styler() {
413
                @Override
414
                public void applyStyles(TextStyle textStyle) {
415
                    textStyle.foreground = Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
416
                }
417
            };
418
            treeComposite = new CheckBoxTreeComposite(parent, new DistributionContentProvider(), new AreaLabelProvider(styler), SWT.NONE);
419
            treeComposite.getViewer().setInput(namedAreas);
420
            treeComposite.getViewer().setCheckedElements(selectedAreas.toArray());
421
            return treeComposite;
422
        }
423

  
424
        @Override
425
        protected void configureShell(Shell newShell) {
426
            super.configureShell(newShell);
427
            newShell.setText("Choose areas");
428
        }
429

  
430
        @Override
431
        protected void okPressed() {
432
            selectedAreas.clear();
433
            selectedAreas = new HashSet(Arrays.asList(treeComposite.getViewer().getCheckedElements()));
434
            super.okPressed();
435
        }
436

  
437
        @Override
438
        protected boolean isResizable() {
439
            return true;
440
        }
441

  
442
        public Set<NamedArea> getSelectedAreas(){
443
            return selectedAreas;
444
        }
445
    }
387 446
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/wizard/CheckBoxTreeComposite.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.preference.wizard;
10

  
11
import org.eclipse.jface.viewers.CheckboxTreeViewer;
12
import org.eclipse.jface.viewers.IBaseLabelProvider;
13
import org.eclipse.jface.viewers.IContentProvider;
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.layout.GridData;
16
import org.eclipse.swt.layout.GridLayout;
17
import org.eclipse.swt.widgets.Composite;
18
import org.eclipse.swt.widgets.Tree;
19
import org.eclipse.swt.widgets.TreeItem;
20

  
21
/**
22
 * @author pplitzner
23
 * @since Oct 26, 2018
24
 *
25
 */
26
public class CheckBoxTreeComposite extends Composite {
27

  
28
    private CheckboxTreeViewer viewer;
29

  
30
    public CheckBoxTreeComposite(Composite parent, IContentProvider contentProvider, IBaseLabelProvider labelProvider, int style) {
31
        super(parent, style);
32
        Composite composite = new Composite(parent, SWT.NULL);
33
        composite.setLayout(new GridLayout());
34
        Tree tree = new Tree(parent, SWT.BORDER | SWT.CHECK);
35
        tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
36
        tree.addListener(SWT.Selection, event -> {
37
            if (event.detail == SWT.CHECK) {
38
                TreeItem item = (TreeItem) event.item;
39
                boolean checked = item.getChecked();
40
                checkItems(item, checked);
41
                checkPath(item.getParentItem(), checked, false);
42
            }
43
        });
44
        viewer = new CheckboxTreeViewer(tree);
45
        viewer.setContentProvider(contentProvider);
46
        viewer.setLabelProvider(labelProvider);
47

  
48
    }
49

  
50
    private void checkPath(TreeItem item, boolean checked, boolean grayed) {
51
        if (item == null) {
52
            return;
53
        }
54
        if (grayed) {
55
            checked = true;
56
        } else {
57
            int index = 0;
58
            TreeItem[] items = item.getItems();
59
            while (index < items.length) {
60
                TreeItem child = items[index];
61
                if (child.getGrayed() || checked != child.getChecked()) {
62
                    checked = grayed = true;
63
                    break;
64
                }
65
                index++;
66
            }
67
        }
68
        item.setChecked(checked);
69
        item.setGrayed(grayed);
70
        checkPath(item.getParentItem(), checked, grayed);
71
    }
72

  
73
    private void checkItems(TreeItem item, boolean checked) {
74
        item.setGrayed(false);
75
        item.setChecked(checked);
76
        TreeItem[] items = item.getItems();
77
        for (int i = 0; i < items.length; i++) {
78
            checkItems(items[i], checked);
79
        }
80
    }
81

  
82
    public CheckboxTreeViewer getViewer() {
83
        return viewer;
84
    }
85

  
86
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)