Project

General

Profile

Download (10.1 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.PreferencePredicate;
34
import eu.etaxonomy.taxeditor.model.ImageResources;
35
import eu.etaxonomy.taxeditor.model.MessagingUtils;
36
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
37
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
38
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
39
import eu.etaxonomy.taxeditor.store.CdmStore;
40

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

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

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

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

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

    
75

    
76

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

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

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

    
114

    
115
		selectedVocabularies = createSelectedVocabularies();
116

    
117
	}
118

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

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

    
145

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

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

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

    
159
		return null;
160
	}
161

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

    
168
	private List<TermVocabulary> getAvailableVocabularies(){
169
	    vocabularies = new ArrayList();
170
	    if (!PreferencesUtil.getBooleanValue(IPreferenceKeys.DISTRIBUTION_VOCABULARIES_ALLOW_OVERRIDE)){
171
            UUID[] preselectedVocabularyUuids = createVocabularyUuidList();
172

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

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

    
200
//	/** {@inheritDoc} */
201
//	@Override
202
//	protected Control createExtendedContentArea(Composite parent) {
203
//		return null;
204
//	}
205

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

    
218
	/** {@inheritDoc} */
219
	@Override
220
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
221
		return null;
222
	}
223

    
224
	/** {@inheritDoc} */
225
	@Override
226
	protected String[] getNewWizardText() {
227
		return null;
228
	}
229

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

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

    
253

    
254

    
255

    
256
                }
257

    
258
                @Override
259
                public void widgetDefaultSelected(SelectionEvent e) {
260
                    // TODO Auto-generated method stub
261

    
262
                }
263

    
264

    
265
            });
266

    
267
        }
268

    
269

    
270

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

    
283
}
(23-23/44)