Project

General

Profile

Download (10.3 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

    
10
package eu.etaxonomy.taxeditor.ui.dialog.selection;
11

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

    
16
import org.apache.commons.lang.StringUtils;
17
import org.eclipse.jface.action.Action;
18
import org.eclipse.jface.action.IAction;
19
import org.eclipse.jface.dialogs.Dialog;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.events.SelectionEvent;
22
import org.eclipse.swt.events.SelectionListener;
23
import org.eclipse.swt.widgets.Button;
24
import org.eclipse.swt.widgets.Composite;
25
import org.eclipse.swt.widgets.Shell;
26

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

    
42
/**
43
 * @author n.hoffmann
44
 * @created May 11, 2010
45
 * @version 1.0
46
 */
47
public class NamedAreaSelectionDialog extends
48
		AbstractFilteredCdmResourceSelectionDialog<NamedArea> {
49

    
50
	private class IncludeNamedAreaVocabulary extends Action {
51
		private final TermVocabulary<NamedArea> vocabulary;
52

    
53
		/**
54
		 * Creates a new instance of the class.
55
		 */
56
		public IncludeNamedAreaVocabulary(TermVocabulary<NamedArea> vocabulary) {
57
			super(vocabulary.getTitleCache(), IAction.AS_CHECK_BOX);
58
			this.vocabulary = vocabulary;
59
		}
60

    
61
		@Override
62
        public void run(){
63
			if(isChecked()){
64
				selectedVocabularies.add(vocabulary);
65
			}else{
66
				selectedVocabularies.remove(vocabulary);
67
			}
68
			PreferencesUtil.setBooleanValue(getPrefKey(vocabulary), !isChecked());
69
			search();
70
		}
71
	}
72

    
73
	protected List<TermVocabulary> selectedVocabularies;
74
    protected List<TermVocabulary> vocabularies;
75

    
76

    
77

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

    
98
	protected NamedAreaSelectionDialog(Shell shell, //ConversationHolder conversation,
99
	        String title, boolean multi, NamedArea namedArea, Object preferenceId, UUID... preselectedVocabularyUuids) {
100
		super(shell, //conversation,
101
		        title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
102

    
103
		selectedVocabularies = new ArrayList<TermVocabulary>();
104
		this.preferenceID = preferenceId;
105
		if (preselectedVocabularyUuids == null || preselectedVocabularyUuids.length == 0){
106
		    preselectedVocabularyUuids = createVocabularyUuidList();
107
		}
108
		if (preselectedVocabularyUuids != null){
109
    		for(int i=0;i<preselectedVocabularyUuids.length;i++){
110
    			TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
111
    			selectedVocabularies.add(preselectedVocabulary);
112
    		}
113
		}
114

    
115

    
116
		selectedVocabularies = createSelectedVocabularies();
117

    
118
	}
119

    
120
    protected List<TermVocabulary> createSelectedVocabularies() {
121
        List<TermVocabulary> tempSelectedVocabularies = new ArrayList<TermVocabulary>();
122
        for(TermVocabulary vocabulary:vocabularies){
123
			if((selectedVocabularies.contains(vocabulary) && !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary)))
124
					|| !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary))){
125
				tempSelectedVocabularies.add(vocabulary);
126
			}
127
		}
128
        return tempSelectedVocabularies;
129
    }
130

    
131
    private static UUID[] createVocabularyUuidList() {
132
        String preselectedVocString = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
133
        if (StringUtils.isBlank(preselectedVocString)){
134
            return null;
135
        }
136
        String[] preselectedVocArray = preselectedVocString.split(";");
137
        UUID[] uuidList = new UUID[preselectedVocArray.length];
138
        int i = 0;
139
        for (String uuidString: preselectedVocArray){
140
            uuidList[i]= UUID.fromString(uuidString);
141
            i++;
142
        }
143
        return uuidList;
144
    }
145

    
146

    
147
	private String getPrefKey(TermVocabulary vocabulary){
148
		return "hide_"+NamedAreaSelectionDialog.class.getCanonicalName()+vocabulary.getUuid()+preferenceID;
149
	}
150

    
151
	/** {@inheritDoc} */
152
	@Override
153
	protected NamedArea getPersistentObject(UUID uuid) {
154

    
155
	    DefinedTermBase area =  CdmStore.getService(ITermService.class).find(uuid);
156
	    if (area instanceof NamedArea){
157
	        return (NamedArea) area;
158
	    }
159

    
160
		return null;
161
	}
162

    
163
	/** {@inheritDoc} */
164
	@Override
165
	protected void init() {
166
		vocabularies = getAvailableVocabularies();
167
	}
168

    
169
	private List<TermVocabulary> getAvailableVocabularies(){
170
	    vocabularies = new ArrayList();
171
	    CdmPreferenceCache cache = CdmPreferenceCache.instance();
172
        CdmPreference pref = cache.get(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
173
	    if (pref != null && !pref.isAllowOverride()){
174
            UUID[] preselectedVocabularyUuids = createVocabularyUuidList();
175

    
176
            for(int i=0;i<preselectedVocabularyUuids.length;i++){
177
                TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
178
                vocabularies.add(preselectedVocabulary);
179
            }
180
        }else{
181
            vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
182
        }
183
		//List<TermVocabulary> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
184
		return vocabularies;
185
	}
186

    
187
//	/** {@inheritDoc} */
188
//	@Override
189
//	protected void search() {
190
//	    Control control =getSearchField();
191
//        String pattern = null;
192
//        if (control != null){
193
//            pattern = ((Text)control).getText();
194
//        }
195
//
196
//        if (pattern == null || pattern.equals("?")){
197
//            model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, null, PreferencesUtil.getGlobalLanguage());
198
//        }else{
199
//            model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
200
//        }
201
//	}
202

    
203
//	/** {@inheritDoc} */
204
//	@Override
205
//	protected Control createExtendedContentArea(Composite parent) {
206
//		return null;
207
//	}
208

    
209
	/** {@inheritDoc} */
210
	@Override
211
	protected String getTitle(NamedArea namedArea) {
212
		try {
213
			String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
214
			return result;
215
		} catch (Exception e) {
216
			MessagingUtils.error(NamedAreaSelectionDialog.class, "Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid(), e);
217
			return namedArea.getTitleCache();
218
		}
219
	}
220

    
221
	/** {@inheritDoc} */
222
	@Override
223
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
224
		return null;
225
	}
226

    
227
	/** {@inheritDoc} */
228
	@Override
229
	protected String[] getNewWizardText() {
230
		return null;
231
	}
232

    
233
	@Override
234
    void createFilterButton(Composite searchAndFilter)
235
	    {
236
            filterButton = new Button(searchAndFilter, SWT.NONE);
237
//            filterButton.setText("Filter");
238
            filterButton.setImage(ImageResources.getImage(ImageResources.FUNNEL_ICON));
239
//            SelectionListener filterSelectionListener = new FilterSelectionListener(preferenceID, this);
240
            filterButton.addSelectionListener(new SelectionListener(){
241
                @Override
242
                public void widgetSelected(SelectionEvent e) {
243

    
244
                        Object source = e.getSource();
245
                        String text = null;
246
                        if (source instanceof Button){
247
                            Shell shell = ((Button)source).getShell();
248
                            Dialog dialog = new FilterDialog(getShell(), preferenceID, selectedVocabularies, vocabularies);
249
                            if(dialog!=null){
250
                                dialog.open();
251
                            }
252
                            createSelectedVocabularies();
253
                            search();
254
                        }
255

    
256

    
257

    
258

    
259
                }
260

    
261
                @Override
262
                public void widgetDefaultSelected(SelectionEvent e) {
263
                    // TODO Auto-generated method stub
264

    
265
                }
266

    
267

    
268
            });
269

    
270
        }
271

    
272

    
273

    
274
    /* (non-Javadoc)
275
     * @see eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog#callService(java.lang.String)
276
     */
277
    @Override
278
    void callService(String pattern) {
279
        if (selectedVocabularies == null || selectedVocabularies.size() == 0){
280
            model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(vocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
281
        }else{
282
            model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
283
        }
284
    }
285

    
286
}
(23-23/44)