Project

General

Profile

Download (6.04 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
    List<AbstractTermDto> listCheckedTerms = new ArrayList<>();
48
    List<AbstractTermDto> listGrayedTerms = new ArrayList<>();
49

    
50
    TermType type;
51

    
52
    /**
53
     * @param pageName
54
     */
55
    protected AbstractTermSelectionWizardPage(String pageName, TermType type) {
56
        super(pageName);
57
        // TODO check if configuration exists
58
        CdmStore.getCurrentSessionManager().bindNullSession();
59
        this.type = type;
60
    }
61

    
62
    /**
63
     * {@inheritDoc}
64
     */
65
    @Override
66
    public void createControl(Composite parent){
67
        parent.setLayout(new GridLayout());
68
        treeComposite = new CheckBoxTreeComposite(parent, new TermDtoContentProvider(), new TermDtoLabelProvider(), SWT.NONE);
69
        treeComposite.getViewer().setComparator(new DefinedTermSorter());
70
        rememberCheckedValues(getCheckedValuesFromPreferences(), getGrayedValuesFromPreferences());
71
        GridLayoutFactory.fillDefaults().applyTo(treeComposite);
72
        setControl(treeComposite);
73
    }
74

    
75
    protected abstract String getCheckedValuesFromPreferences();
76

    
77
    protected abstract String getGrayedValuesFromPreferences();
78

    
79
    protected CheckboxTreeViewer getViewer(){
80
        return treeComposite.getViewer();
81
    }
82

    
83
    public List<TermVocabularyDto> getVocabularies() {
84
        return vocabularies;
85
    }
86

    
87
    public void addVocabularies(TermVocabularyDto vocabulary) {
88
        this.vocabularies.add(vocabulary);
89
    }
90

    
91

    
92
    /**
93
     * @param vocs
94
     */
95
    protected void setVocabularies(List<TermVocabularyDto> vocs) {
96
        vocabularies = vocs;
97
    }
98

    
99
    public List<AbstractTermDto> getListCheckedTerms() {
100
        return listCheckedTerms;
101
    }
102

    
103
    public List<AbstractTermDto> getListGrayedTerms() {
104
        return listGrayedTerms;
105
    }
106

    
107
    protected void rememberCheckedValues(String checkedValues, String grayedValues) {
108
        initialiseVocabularies();
109
        treeComposite.getViewer().setInput(getVocabularies());
110

    
111
        if (grayedValues != null && grayedValues != "") { //$NON-NLS-1$
112
            String[] arrayGrayed = grayedValues.split(";"); //$NON-NLS-1$
113
            List<String> listGrayed = Arrays.asList(arrayGrayed);
114
            listGrayedTerms = getTermsFromStringValues(listGrayed);
115

    
116
        }
117
        if (checkedValues != null && checkedValues != "") { //$NON-NLS-1$
118
            String[] listChecked = checkedValues.split(";"); //$NON-NLS-1$
119
            String[] listCheckedComma = checkedValues.split(","); //$NON-NLS-1$
120
            List<String> checked = new ArrayList<>();
121
            if (listChecked != null ){
122
                checked = Arrays.asList(listChecked);
123
            }
124
            if (listCheckedComma != null && checkedValues.contains(",")){ //$NON-NLS-1$
125
                checked = Arrays.asList(listCheckedComma);
126
            }
127
            listCheckedTerms = getTermsFromStringValues(checked);
128

    
129
        }
130
//        treeComposite.getViewer().setParentsGrayed(element, state)
131
        treeComposite.setCheckedElements(listCheckedTerms.toArray());
132

    
133
    }
134

    
135
    /**
136
     * @param split
137
     * @param termlist
138
     */
139
    private List<AbstractTermDto> getTermsFromStringValues(List<String> listValue) {
140
        List<AbstractTermDto> termlist = new ArrayList<>();
141
        for (String s : listValue) {
142
            if (!StringUtils.isBlank(s)){
143
                UUID uuid = UUID.fromString(s);
144
                ITermService termService = CdmStore.getService(ITermService.class);
145
                DefinedTermBase definedTermBase = termService.load(uuid);
146
                if(definedTermBase != null){
147
                    termlist.add(TermDto.fromTerm(definedTermBase, true));
148
                }else{
149
                    IVocabularyService vocabularyService = CdmStore.getService(IVocabularyService.class);
150
                    TermVocabulary termVocabulary = vocabularyService.load(uuid);
151
                    termlist.add(new TermVocabularyDto(uuid, termVocabulary.getRepresentations()));
152
                }
153
            }
154
        }
155
        return termlist;
156
    }
157

    
158

    
159
    protected void initialiseVocabularies() {
160
        if (getVocabularies() != null) {
161
            getVocabularies().clear();
162
        }
163
        List<TermVocabularyDto> vocs = new ArrayList<>();
164
        if (localPref){
165
            vocs = getVocabulariesFromPreference();
166

    
167
        }else{
168
            vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
169
        }
170
        setVocabularies(vocs);
171
    }
172

    
173
    protected abstract List<TermVocabularyDto> getVocabulariesFromPreference();
174

    
175

    
176
}
(1-1/14)