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

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

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

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

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

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

    
74

    
75

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

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

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

    
113

    
114
		selectedVocabularies = createSelectedVocabularies();
115

    
116
	}
117

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

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

    
144

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

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

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

    
158
		return null;
159
	}
160

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

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

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

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

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

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

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

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

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

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

    
252

    
253

    
254

    
255
                }
256

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

    
261
                }
262

    
263

    
264
            });
265

    
266
        }
267

    
268

    
269

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

    
282
}
(23-23/44)