Project

General

Profile

Download (5.05 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.widgets.Composite;
22

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

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

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

    
47
    protected TermType type;
48

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

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

    
65
    protected abstract String getCheckedValuesFromPreferences();
66

    
67
    protected CheckboxTreeViewer getViewer(){
68
        return treeComposite.getViewer();
69
    }
70

    
71
    public List<TermVocabularyDto> getVocabularies() {
72
        return vocabularies;
73
    }
74

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

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

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

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

    
100
        }
101
    }
102

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

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

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

    
136
    protected abstract List<TermVocabularyDto> getVocabulariesFromPreference();
137

    
138

    
139
}
(1-1/13)