Project

General

Profile

Download (9.29 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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.jface.dialogs.Dialog;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.events.SelectionEvent;
19
import org.eclipse.swt.events.SelectionListener;
20
import org.eclipse.swt.widgets.Button;
21
import org.eclipse.swt.widgets.Composite;
22
import org.eclipse.swt.widgets.Shell;
23

    
24
import eu.etaxonomy.cdm.api.service.ITermService;
25
import eu.etaxonomy.cdm.api.service.IVocabularyService;
26
import eu.etaxonomy.cdm.model.location.NamedArea;
27
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
28
import eu.etaxonomy.cdm.model.metadata.TermSearchField;
29
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
30
import eu.etaxonomy.cdm.model.term.TermType;
31
import eu.etaxonomy.cdm.model.term.TermVocabulary;
32
import eu.etaxonomy.taxeditor.model.ImageResources;
33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
34
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
35
import eu.etaxonomy.taxeditor.preference.CdmPreferenceCache;
36
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
37
import eu.etaxonomy.taxeditor.store.CdmStore;
38

    
39
/**
40
 * @author n.hoffmann
41
 * @created May 11, 2010
42
 */
43
public class NamedAreaSelectionDialog extends
44
		AbstractFilteredCdmResourceSelectionDialog<NamedArea> {
45

    
46
//	private class IncludeNamedAreaVocabulary extends Action {
47
//		private final TermVocabulary<NamedArea> vocabulary;
48
//
49
//		/**
50
//		 * Creates a new instance of the class.
51
//		 */
52
//		public IncludeNamedAreaVocabulary(TermVocabulary<NamedArea> vocabulary) {
53
//			super(vocabulary.getTitleCache(), IAction.AS_CHECK_BOX);
54
//			this.vocabulary = vocabulary;
55
//		}
56
//
57
//		@Override
58
//        public void run(){
59
//			if(isChecked()){
60
//				selectedVocabularies.add(vocabulary);
61
//			}else{
62
//				selectedVocabularies.remove(vocabulary);
63
//			}
64
//			PreferencesUtil.setBooleanValue(getPrefKey(vocabulary), !isChecked());
65
//			search();
66
//		}
67
//	}
68

    
69
	protected List<NamedArea> selectedAreas;
70
	protected List<TermVocabulary> selectedVocabularies;
71
    protected List<TermVocabulary> vocabularies;
72

    
73
	/**
74
	 * Creates a filtered selection dialog to select a named area.
75
	 *
76
	 * @param shell
77
	 * 				The shell for displaying this widget
78
	 * @param namedArea
79
	 * 				A namedArea that should be selected when the dialog opens
80
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
81
	 * @param preferenceId a class which is used for generating the preference key so that every
82
	 * dialogs can be grouped to have their own preferences depending on this id
83
	 * @param preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies
84
	 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
85
	 */
86
	public static NamedArea select(Shell shell,
87
	        NamedArea namedArea, String preferenceId, UUID... preselectedVocabularyUuids) {
88
		NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell,
89
				"Choose an area", false, namedArea, preferenceId, preselectedVocabularyUuids);
90
		return getSelectionFromDialog(dialog);
91
	}
92

    
93
	protected NamedAreaSelectionDialog(Shell shell,
94
	        String title, boolean multi, NamedArea namedArea, Object preferenceId, UUID... preselectedVocabularyUuids) {
95
		super(shell,
96
		        title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
97

    
98
		selectedVocabularies = new ArrayList<TermVocabulary>();
99
		this.preferenceID = preferenceId;
100

    
101
		if (preselectedVocabularyUuids != null && preselectedVocabularyUuids.length > 0){
102
    		for(int i=0;i<preselectedVocabularyUuids.length;i++){
103
    			TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
104
    			selectedVocabularies.add(preselectedVocabulary);
105
    		}
106
		}else{
107
		    selectedVocabularies = createSelectedVocabularies();
108
		}
109
	}
110

    
111
    protected List<TermVocabulary> createSelectedVocabularies() {
112
        List<TermVocabulary> tempSelectedVocabularies = new ArrayList<TermVocabulary>();
113
        for(TermVocabulary vocabulary:vocabularies){
114
			if((selectedVocabularies.contains(vocabulary) && !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary)))
115
					|| !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary))){
116
				tempSelectedVocabularies.add(vocabulary);
117
			}
118
		}
119
        return tempSelectedVocabularies;
120
    }
121

    
122
//    private static UUID[] createVocabularyUuidList() {
123
//        String preselectedVocString = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
124
//        if (StringUtils.isBlank(preselectedVocString)){
125
//            return null;
126
//        }
127
//        String[] preselectedVocArray = preselectedVocString.split(";");
128
//        UUID[] uuidList = new UUID[preselectedVocArray.length];
129
//        int i = 0;
130
//        for (String uuidString: preselectedVocArray){
131
//            uuidList[i]= UUID.fromString(uuidString);
132
//            i++;
133
//        }
134
//        return uuidList;
135
//    }
136

    
137
	private String getPrefKey(TermVocabulary vocabulary){
138
		return "hide_"+NamedAreaSelectionDialog.class.getCanonicalName()+vocabulary.getUuid()+preferenceID;
139
	}
140

    
141
	@Override
142
	protected NamedArea getPersistentObject(UUID uuid) {
143

    
144
	    DefinedTermBase area =  CdmStore.getService(ITermService.class).find(uuid);
145
	    if (area instanceof NamedArea){
146
	        return (NamedArea) area;
147
	    }
148

    
149
		return null;
150
	}
151

    
152
	@Override
153
	protected void init() {
154
		vocabularies = getAvailableVocabularies();
155
	}
156

    
157
	private List<TermVocabulary> getAvailableVocabularies(){
158
	    vocabularies = new ArrayList<>();
159
	    CdmPreferenceCache cache = CdmPreferenceCache.instance();
160
	    vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
161
	    return vocabularies;
162
	}
163

    
164
	@Override
165
	protected String getTitle(NamedArea namedArea) {
166
		try {
167
			String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
168
			return result;
169
		} catch (Exception e) {
170
			MessagingUtils.error(NamedAreaSelectionDialog.class, "Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid(), e);
171
			return namedArea.getTitleCache();
172
		}
173
	}
174

    
175
	@Override
176
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
177
		return null;
178
	}
179

    
180
	@Override
181
	protected String[] getNewWizardText() {
182
		return null;
183
	}
184

    
185
	@Override
186
    void createFilterButton(Composite searchAndFilter){
187
        filterButton = new Button(searchAndFilter, SWT.NONE);
188
//            filterButton.setText("Filter");
189
        filterButton.setImage(ImageResources.getImage(ImageResources.FUNNEL_ICON));
190
//            SelectionListener filterSelectionListener = new FilterSelectionListener(preferenceID, this);
191
        filterButton.addSelectionListener(new SelectionListener(){
192
            @Override
193
            public void widgetSelected(SelectionEvent e) {
194
                Object source = e.getSource();
195
                if (source instanceof Button){
196
                    Dialog dialog = new FilterDialog(getShell(), preferenceID, selectedVocabularies, vocabularies);
197
                    dialog.open();
198
                    createSelectedVocabularies();
199
                    search();
200
                }
201
            }
202

    
203
            @Override
204
            public void widgetDefaultSelected(SelectionEvent e) {
205
                // TODO Auto-generated method stub
206
            }
207
        });
208
    }
209

    
210
    @Override
211
    protected void callService(String pattern) {
212
        String abbrevTypeString = PreferencesUtil.getStringValue(PreferencePredicate.NamedAreaSearchForAbbrev.getKey());
213
        TermSearchField abbrevType;
214
        try{
215
            abbrevType = StringUtils.isNotBlank(abbrevTypeString)? TermSearchField.byKey(abbrevTypeString): null;
216
        }catch(IllegalArgumentException e){
217
            abbrevType = null;
218
        }
219
        if (selectedVocabularies == null || selectedVocabularies.size() == 0){
220
            if (abbrevType != null && !abbrevType.equals(TermSearchField.NoAbbrev)){
221
                model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedAreaByAbbrev(vocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage(), abbrevType);
222
            }else{
223
                model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(vocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
224
            }
225
        }else{
226
            if (abbrevType != null  && !abbrevType.equals(TermSearchField.NoAbbrev)){
227
                model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedAreaByAbbrev(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage(), abbrevType);
228
            }else{
229
                model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
230
            }
231
        }
232
    }
233
}
(24-24/46)