Project

General

Profile

Download (4.18 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.IPreferenceKeys;
26
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
27
import eu.etaxonomy.taxeditor.store.CdmStore;
28

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

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

    
48

    
49
    }
50

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

    
70

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

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

    
86
        return vocabularies;
87
    }
88

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