Project

General

Profile

Download (8.87 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.eclipse.jface.action.Action;
17
import org.eclipse.jface.action.IAction;
18
import org.eclipse.jface.dialogs.Dialog;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.events.SelectionEvent;
21
import org.eclipse.swt.events.SelectionListener;
22
import org.eclipse.swt.widgets.Button;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Shell;
25

    
26
import eu.etaxonomy.cdm.api.service.ITermService;
27
import eu.etaxonomy.cdm.api.service.IVocabularyService;
28
import eu.etaxonomy.cdm.model.location.NamedArea;
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
 * @version 1.0
43
 */
44
public class NamedAreaSelectionDialog extends
45
		AbstractFilteredCdmResourceSelectionDialog<NamedArea> {
46

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

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

    
58
		@Override
59
        public void run(){
60
			if(isChecked()){
61
				selectedVocabularies.add(vocabulary);
62
			}else{
63
				selectedVocabularies.remove(vocabulary);
64
			}
65
			PreferencesUtil.setBooleanValue(getPrefKey(vocabulary), !isChecked());
66
			search();
67
		}
68
	}
69
	protected List<NamedArea> selectedAreas;
70
	protected List<TermVocabulary> selectedVocabularies;
71
    protected List<TermVocabulary> vocabularies;
72

    
73

    
74

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

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

    
100
		selectedVocabularies = new ArrayList<TermVocabulary>();
101
		this.preferenceID = preferenceId;
102

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

    
112
	}
113

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

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

    
140

    
141
	private String getPrefKey(TermVocabulary vocabulary){
142
		return "hide_"+NamedAreaSelectionDialog.class.getCanonicalName()+vocabulary.getUuid()+preferenceID;
143
	}
144

    
145
	/** {@inheritDoc} */
146
	@Override
147
	protected NamedArea getPersistentObject(UUID uuid) {
148

    
149
	    DefinedTermBase area =  CdmStore.getService(ITermService.class).find(uuid);
150
	    if (area instanceof NamedArea){
151
	        return (NamedArea) area;
152
	    }
153

    
154
		return null;
155
	}
156

    
157
	/** {@inheritDoc} */
158
	@Override
159
	protected void init() {
160
		vocabularies = getAvailableVocabularies();
161
	}
162

    
163
	private List<TermVocabulary> getAvailableVocabularies(){
164
	    vocabularies = new ArrayList();
165
	    CdmPreferenceCache cache = CdmPreferenceCache.instance();
166
	    vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
167
	    return vocabularies;
168
	}
169

    
170

    
171

    
172
	/** {@inheritDoc} */
173
	@Override
174
	protected String getTitle(NamedArea namedArea) {
175
		try {
176
			String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
177
			return result;
178
		} catch (Exception e) {
179
			MessagingUtils.error(NamedAreaSelectionDialog.class, "Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid(), e);
180
			return namedArea.getTitleCache();
181
		}
182
	}
183

    
184
	/** {@inheritDoc} */
185
	@Override
186
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
187
		return null;
188
	}
189

    
190
	/** {@inheritDoc} */
191
	@Override
192
	protected String[] getNewWizardText() {
193
		return null;
194
	}
195

    
196
	@Override
197
    void createFilterButton(Composite searchAndFilter)
198
	    {
199
            filterButton = new Button(searchAndFilter, SWT.NONE);
200
//            filterButton.setText("Filter");
201
            filterButton.setImage(ImageResources.getImage(ImageResources.FUNNEL_ICON));
202
//            SelectionListener filterSelectionListener = new FilterSelectionListener(preferenceID, this);
203
            filterButton.addSelectionListener(new SelectionListener(){
204
                @Override
205
                public void widgetSelected(SelectionEvent e) {
206

    
207
                        Object source = e.getSource();
208
                        String text = null;
209
                        if (source instanceof Button){
210
                            Shell shell = ((Button)source).getShell();
211
                            Dialog dialog = new FilterDialog(getShell(), preferenceID, selectedVocabularies, vocabularies);
212
                            if(dialog!=null){
213
                                dialog.open();
214
                            }
215
                            createSelectedVocabularies();
216
                            search();
217
                        }
218

    
219

    
220

    
221

    
222
                }
223

    
224
                @Override
225
                public void widgetDefaultSelected(SelectionEvent e) {
226
                    // TODO Auto-generated method stub
227

    
228
                }
229

    
230

    
231
            });
232

    
233
        }
234

    
235

    
236

    
237
    /* (non-Javadoc)
238
     * @see eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog#callService(java.lang.String)
239
     */
240
    @Override
241
    void callService(String pattern) {
242
        if (selectedVocabularies == null || selectedVocabularies.size() == 0){
243
            model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(vocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
244
        }else{
245
            model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
246
        }
247
    }
248

    
249
}
(23-23/45)