Project

General

Profile

Download (4.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.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.lang.StringUtils;
16
import org.eclipse.swt.widgets.Shell;
17

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

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

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

    
47

    
48
    }
49

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

    
69

    
70
    protected List<TermVocabulary> getAvailableVocabularies(){
71
        List<TermVocabulary> vocabularies = new ArrayList();
72

    
73
        UUID[] preselectedVocabularyUuids = createVocabularyUuidList();
74
        CdmPreferenceCache cache = CdmPreferenceCache.instance();
75
        CdmPreference pref = cache.get(PreferencePredicate.CommonNameAreaVocabularies.getKey());
76
        if ((pref != null && !pref.isAllowOverride()) && preselectedVocabularyUuids!=null) {
77
                for(int i=0;i<preselectedVocabularyUuids.length;i++){
78
                    TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
79
                    vocabularies.add(preselectedVocabulary);
80
                }
81
        }else{
82
            vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
83
        }
84

    
85
        return vocabularies;
86
    }
87

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