Project

General

Profile

Download (4.44 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.ui.dialog.selection;
10

    
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.UUID;
14

    
15
import org.apache.commons.lang3.StringUtils;
16
import org.eclipse.swt.widgets.Shell;
17

    
18
import eu.etaxonomy.cdm.api.service.IVocabularyService;
19
import eu.etaxonomy.cdm.model.location.NamedArea;
20
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
21
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
22
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
23
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
24
import eu.etaxonomy.cdm.model.term.TermType;
25
import eu.etaxonomy.cdm.model.term.TermVocabulary;
26
import eu.etaxonomy.taxeditor.preference.CdmPreferenceCache;
27
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
28
import eu.etaxonomy.taxeditor.store.CdmStore;
29

    
30
/**
31
 * @author k.luther
32
 * @since 09.10.2018
33
 *
34
 */
35
public class CommonNameNamedAreaSelectionDialog extends NamedAreaSelectionDialog {
36

    
37
    /**
38
     * @param shell
39
     * @param title
40
     * @param multi
41
     * @param namedArea
42
     * @param preferenceId
43
     * @param preselectedVocabularyUuids
44
     */
45
    protected CommonNameNamedAreaSelectionDialog(Shell shell, String title, boolean multi, NamedArea namedArea,
46
            Object preferenceId) {
47
        super(shell, title, multi, namedArea, preferenceId, createVocabularyUuidList());
48

    
49

    
50
    }
51

    
52
    private static UUID[] createVocabularyUuidList() {
53
        String preselectedVocString = PreferencesUtil.getStringValue(PreferencePredicate.CommonNameAreaVocabularies.getKey(), false);
54
        if (StringUtils.isBlank(preselectedVocString)){
55
            return null;
56
        }
57
        String[] preselectedVocArray = preselectedVocString.split(";");
58
        UUID[] uuidList = new UUID[preselectedVocArray.length];
59
        int i = 0;
60
        for (String uuidString: preselectedVocArray){
61
            uuidList[i]= UUID.fromString(uuidString);
62
            i++;
63
        }
64
        return uuidList;
65
    }
66
    @Override
67
    protected void init() {
68
        vocabularies = getAvailableVocabularies();
69
    }
70

    
71

    
72
    protected List<TermVocabulary> getAvailableVocabularies(){
73
        List<TermVocabulary> vocabularies = new ArrayList();
74

    
75
        UUID[] preselectedVocabularyUuids = createVocabularyUuidList();
76
        CdmPreferenceCache cache = CdmPreferenceCache.instance();
77
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.CommonNameAreaVocabularies);
78
        CdmPreference pref = cache.findBestMatching(key);
79
        if (preselectedVocabularyUuids != null){
80
            for(int i=0;i<preselectedVocabularyUuids.length;i++){
81
                TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
82
                vocabularies.add(preselectedVocabulary);
83
            }
84
        }
85
        if ((pref != null && !pref.isAllowOverride()) && preselectedVocabularyUuids!=null) {
86
             return vocabularies;
87
        }else{
88
            selectedVocabularies = vocabularies;
89

    
90
            return CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
91
        }
92

    
93

    
94
    }
95

    
96
    /**
97
     * Creates a filtered selection dialog to select a named area.
98
     *
99
     * @param shell
100
     *              The shell for displaying this widget
101
     * @param namedArea
102
     *              A namedArea that should be selected when the dialog opens
103
     * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
104
     * @param preferenceId a class which is used for generating the preference key so that every
105
     * dialogs can be grouped to have their own preferences depending on this id
106
     * @param preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies
107
     * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
108
     */
109
    public static NamedArea select(Shell shell, //ConversationHolder conversation,
110
            NamedArea namedArea, String preferenceId) {
111
        CommonNameNamedAreaSelectionDialog dialog = new CommonNameNamedAreaSelectionDialog(shell, //conversation,
112
                "Choose an area", false, namedArea, preferenceId);
113
        return getSelectionFromDialog(dialog);
114
    }
115

    
116

    
117
}
(10-10/46)