Project

General

Profile

Download (7.54 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.action.IMenuManager;
19
import org.eclipse.swt.widgets.Composite;
20
import org.eclipse.swt.widgets.Control;
21
import org.eclipse.swt.widgets.Shell;
22
import org.eclipse.swt.widgets.Text;
23

    
24
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
25
import eu.etaxonomy.cdm.api.service.ITermService;
26
import eu.etaxonomy.cdm.api.service.IVocabularyService;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.TermType;
29
import eu.etaxonomy.cdm.model.common.TermVocabulary;
30
import eu.etaxonomy.cdm.model.location.NamedArea;
31
import eu.etaxonomy.taxeditor.model.MessagingUtils;
32
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
33
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
34
import eu.etaxonomy.taxeditor.store.CdmStore;
35

    
36
/**
37
 * @author n.hoffmann
38
 * @created May 11, 2010
39
 * @version 1.0
40
 */
41
public class NamedAreaSelectionDialog extends
42
		AbstractFilteredCdmResourceSelectionDialog<NamedArea> {
43

    
44
	private class IncludeNamedAreaVocabulary extends Action {
45
		private final TermVocabulary<NamedArea> vocabulary;
46

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

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

    
67
	protected List<TermVocabulary> selectedVocabularies;
68
    protected ArrayList<TermVocabulary> preselectedVocabularies;
69
    private Object preferenceID;
70

    
71

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

    
91
	protected NamedAreaSelectionDialog(Shell shell, ConversationHolder conversation, String title, boolean multi, NamedArea namedArea, Object preferenceId, UUID... preselectedVocabularyUuids) {
92
		super(shell, conversation, title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
93
		this.preferenceID = preferenceId;
94
		preselectedVocabularies = new ArrayList<TermVocabulary>();
95
		for(int i=0;i<preselectedVocabularyUuids.length;i++){
96
			TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
97
			preselectedVocabularies.add(preselectedVocabulary);
98
		}
99
		List<TermVocabulary> tempSelectedVocabularies = new ArrayList<>();
100
		for(TermVocabulary vocabulary:selectedVocabularies){
101
			if(preselectedVocabularies.contains(vocabulary)
102
					|| !PreferencesUtil.getPreferenceStore().getBoolean(getPrefKey(vocabulary))){
103
				tempSelectedVocabularies.add(vocabulary);
104
			}
105
		}
106
		selectedVocabularies = tempSelectedVocabularies;
107
//		search;//re-init to consider pre-selected vocabularies
108
	}
109

    
110
	/** {@inheritDoc} */
111
	@Override
112
	protected void fillViewMenu(IMenuManager menuManager) {
113

    
114
//		super.fillViewMenu(menuManager);
115

    
116
		for(TermVocabulary<NamedArea> vocabulary : getAvailableVocabularies()){
117
			IncludeNamedAreaVocabulary action = new IncludeNamedAreaVocabulary(vocabulary);
118
			menuManager.add(action);
119
			if(preselectedVocabularies.contains(vocabulary)) {
120
			    action.setChecked(true);
121
			}
122
			else{
123
				action.setChecked(!PreferencesUtil.getPreferenceStore().getBoolean(getPrefKey(vocabulary)));
124
			}
125
		}
126
	}
127

    
128
	private String getPrefKey(TermVocabulary vocabulary){
129
		return "hide_"+NamedAreaSelectionDialog.class.getCanonicalName()+vocabulary.getUuid()+preferenceID;
130
	}
131

    
132
	/** {@inheritDoc} */
133
	@Override
134
	protected NamedArea getPersistentObject(UUID uuid) {
135
		for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
136
			for(Object object : vocabulary.getTerms()){
137
				CdmBase cdmBaseObject = (CdmBase) object;
138
				if(uuid.equals(cdmBaseObject.getUuid())){
139
					return CdmBase.deproxy(cdmBaseObject, NamedArea.class);
140
				}
141
			}
142
		}
143
		return null;
144
	}
145

    
146
	/** {@inheritDoc} */
147
	@Override
148
	protected void init() {
149
		selectedVocabularies = getAvailableVocabularies();
150
	}
151

    
152
	private List<TermVocabulary> getAvailableVocabularies(){
153
		List<TermVocabulary> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
154
		return vocabularies;
155
	}
156

    
157
	/** {@inheritDoc} */
158
	@Override
159
	protected void search() {
160
	    Control control =getSearchField();
161
        String pattern = null;
162
        if (control != null){
163
            pattern = ((Text)control).getText();
164
        }
165

    
166
        if (pattern == null || pattern.equals("?")){
167
            model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, null, PreferencesUtil.getGlobalLanguage());
168
        }else{
169
            model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
170
        }
171

    
172
//		Set<NamedArea> terms = new HashSet<NamedArea>();
173
//		for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
174
//			terms.addAll(vocabulary.getTermsOrderedByLabels(CdmStore.getDefaultLanguage()));
175
//		}
176
//
177
//		if(model == null){
178
//			model = new ArrayList<UuidAndTitleCache<NamedArea>>();
179
//		}
180
//		model.clear();
181
//		for(Object areaObject : terms){
182
//			NamedArea area = (NamedArea) HibernateProxyHelper.deproxy(areaObject);
183
//			UuidAndTitleCache<NamedArea> element = new UuidAndTitleCache<NamedArea>(NamedArea.class, area.getUuid(), area.getId(), getTitle(area));
184
//			model.add(element);
185
//		}
186
	}
187

    
188
	/** {@inheritDoc} */
189
	@Override
190
	protected Control createExtendedContentArea(Composite parent) {
191
		return null;
192
	}
193

    
194
	/** {@inheritDoc} */
195
	@Override
196
	protected String getTitle(NamedArea namedArea) {
197
		try {
198
			String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
199
			return result;
200
		} catch (Exception e) {
201
			MessagingUtils.error(NamedAreaSelectionDialog.class, "Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid(), e);
202
			return namedArea.getTitleCache();
203
		}
204
	}
205

    
206
	/** {@inheritDoc} */
207
	@Override
208
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
209
		return null;
210
	}
211

    
212
	/** {@inheritDoc} */
213
	@Override
214
	protected String getNewWizardLinkText() {
215
		return null;
216
	}
217
}
(19-19/38)