Project

General

Profile

Download (5.14 KB) Statistics
| Branch: | Tag: | Revision:
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 java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import org.apache.commons.lang.StringUtils;
17
import org.eclipse.jface.layout.GridLayoutFactory;
18
import org.eclipse.jface.viewers.CheckboxTreeViewer;
19
import org.eclipse.jface.wizard.WizardPage;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.layout.GridLayout;
22
import org.eclipse.swt.widgets.Composite;
23

    
24
import eu.etaxonomy.cdm.api.service.ITermService;
25
import eu.etaxonomy.cdm.api.service.IVocabularyService;
26
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
27
import eu.etaxonomy.cdm.model.common.TermType;
28
import eu.etaxonomy.cdm.model.common.TermVocabulary;
29
import eu.etaxonomy.cdm.persistence.dto.AbstractTermDto;
30
import eu.etaxonomy.cdm.persistence.dto.TermDto;
31
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
32
import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermSorter;
33
import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoContentProvider;
34
import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoLabelProvider;
35
import eu.etaxonomy.taxeditor.store.CdmStore;
36

    
37
/**
38
 * @author k.luther
39
 * @since 04.06.2018
40
 *
41
 */
42
public abstract class AbstractTermSelectionWizardPage extends WizardPage {
43

    
44
    protected CheckBoxTreeComposite treeComposite;
45
    private List<TermVocabularyDto> vocabularies = new ArrayList<>();
46
    boolean localPref;
47

    
48
    protected TermType type;
49

    
50
    protected AbstractTermSelectionWizardPage(String pageName, TermType type) {
51
        super(pageName);
52
        // TODO check if configuration exists
53
        CdmStore.getCurrentSessionManager().bindNullSession();
54
        this.type = type;
55
    }
56

    
57
    @Override
58
    public void createControl(Composite parent){
59
        parent.setLayout(new GridLayout());
60
        treeComposite = new CheckBoxTreeComposite(parent, new TermDtoContentProvider(), new TermDtoLabelProvider(), SWT.NONE);
61
        treeComposite.getViewer().setComparator(new DefinedTermSorter());
62
        rememberCheckedValues(getCheckedValuesFromPreferences());
63
        GridLayoutFactory.fillDefaults().applyTo(treeComposite);
64
        setControl(treeComposite);
65
    }
66

    
67
    protected abstract String getCheckedValuesFromPreferences();
68

    
69
    protected CheckboxTreeViewer getViewer(){
70
        return treeComposite.getViewer();
71
    }
72

    
73
    public List<TermVocabularyDto> getVocabularies() {
74
        return vocabularies;
75
    }
76

    
77
    public void addVocabularies(TermVocabularyDto vocabulary) {
78
        this.vocabularies.add(vocabulary);
79
    }
80

    
81
    protected void setVocabularies(List<TermVocabularyDto> vocs) {
82
        vocabularies = vocs;
83
    }
84

    
85
    protected void rememberCheckedValues(String checkedValues) {
86
        initialiseVocabularies();
87
        treeComposite.getViewer().setInput(getVocabularies());
88

    
89
        if (checkedValues != null && checkedValues != "") { //$NON-NLS-1$
90
            String[] listChecked = checkedValues.split(";"); //$NON-NLS-1$
91
            String[] listCheckedComma = checkedValues.split(","); //$NON-NLS-1$
92
            List<String> checked = new ArrayList<>();
93
            if (listChecked != null ){
94
                checked = Arrays.asList(listChecked);
95
            }
96
            if (listCheckedComma != null && checkedValues.contains(",")){ //$NON-NLS-1$
97
                checked = Arrays.asList(listCheckedComma);
98
            }
99
            List<AbstractTermDto> termsFromStringValues = getTermsFromStringValues(checked);
100
            treeComposite.setCheckedElements(termsFromStringValues.toArray());
101

    
102
        }
103
    }
104

    
105
    private List<AbstractTermDto> getTermsFromStringValues(List<String> listValue) {
106
        List<AbstractTermDto> termlist = new ArrayList<>();
107
        for (String s : listValue) {
108
            if (!StringUtils.isBlank(s)){
109
                UUID uuid = UUID.fromString(s);
110
                ITermService termService = CdmStore.getService(ITermService.class);
111
                DefinedTermBase definedTermBase = termService.load(uuid);
112
                if(definedTermBase != null){
113
                    termlist.add(TermDto.fromTerm(definedTermBase, true));
114
                }else{
115
                    IVocabularyService vocabularyService = CdmStore.getService(IVocabularyService.class);
116
                    TermVocabulary termVocabulary = vocabularyService.load(uuid);
117
                    termlist.add(new TermVocabularyDto(uuid, termVocabulary.getRepresentations()));
118
                }
119
            }
120
        }
121
        return termlist;
122
    }
123

    
124
    protected void initialiseVocabularies() {
125
        if (getVocabularies() != null) {
126
            getVocabularies().clear();
127
        }
128
        List<TermVocabularyDto> vocs = new ArrayList<>();
129
        if (localPref){
130
            vocs = getVocabulariesFromPreference();
131

    
132
        }else{
133
            vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
134
        }
135
        setVocabularies(vocs);
136
    }
137

    
138
    protected abstract List<TermVocabularyDto> getVocabulariesFromPreference();
139

    
140

    
141
}
(1-1/13)