Project

General

Profile

Download (7.38 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.dialogs.filteredSelection;
12

    
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.HashSet;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.eclipse.jface.action.Action;
20
import org.eclipse.jface.action.IAction;
21
import org.eclipse.jface.action.IMenuManager;
22
import org.eclipse.swt.widgets.Composite;
23
import org.eclipse.swt.widgets.Control;
24
import org.eclipse.swt.widgets.Shell;
25

    
26
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
27
import eu.etaxonomy.cdm.common.CdmUtils;
28
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
29
import eu.etaxonomy.cdm.model.common.CdmBase;
30
import eu.etaxonomy.cdm.model.common.Representation;
31
import eu.etaxonomy.cdm.model.common.TermVocabulary;
32
import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
33
import eu.etaxonomy.cdm.model.common.VocabularyEnum;
34
import eu.etaxonomy.cdm.model.location.NamedArea;
35
import eu.etaxonomy.taxeditor.editor.newWizard.AbstractNewEntityWizard;
36
import eu.etaxonomy.taxeditor.store.CdmStore;
37

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

    
48
	private class IncludeTdwgAreaAction extends Action {
49
		/**
50
		 * Creates a new instance of the class.
51
		 */
52
		public IncludeTdwgAreaAction() {
53
			super("TDWG Areas", IAction.AS_CHECK_BOX);
54
		}
55

    
56
		public void run() {
57
			if(isChecked()){
58
				selectedVocabularies.add(VocabularyEnum.TdwgArea);
59
			}else{
60
				selectedVocabularies.remove(VocabularyEnum.TdwgArea);
61
			}
62
			
63
			initModel();
64
		}
65
	}
66
	
67
	private class IncludeWaterbodyOrCountryAction extends Action {
68
		/**
69
		 * Creates a new instance of the class.
70
		 */
71
		public IncludeWaterbodyOrCountryAction() {
72
			super("Waterbody Or Country", IAction.AS_CHECK_BOX);
73
		}
74

    
75
		public void run() {
76
			if(isChecked()){
77
				selectedVocabularies.add(VocabularyEnum.WaterbodyOrCountry);
78
			}else{
79
				selectedVocabularies.remove(VocabularyEnum.WaterbodyOrCountry);
80
			}
81
			
82
			initModel();
83
		}
84
	}
85
	
86
	private class IncludeContinentAction extends Action {
87
		/**
88
		 * Creates a new instance of the class.
89
		 */
90
		public IncludeContinentAction() {
91
			super("Continent", IAction.AS_CHECK_BOX);
92
		}
93

    
94
		public void run() {
95
			if(isChecked()){
96
				selectedVocabularies.add(VocabularyEnum.Continent);
97
			}else{
98
				selectedVocabularies.remove(VocabularyEnum.Continent);
99
			}
100
			
101
			initModel();
102
		}
103
	}
104
	
105
	private Collection<VocabularyEnum> selectedVocabularies;
106
	
107
	private IncludeTdwgAreaAction includeTdwgAreaAction;
108
	private IncludeWaterbodyOrCountryAction includeWaterbodyOrCountryAction;
109
	private IncludeContinentAction includeContinentAction;
110
	
111
	/**
112
	 * Creates a filtered selection dialog to select a named area.
113
	 *
114
	 * @param shell
115
	 * 				The shell for displaying this widget
116
	 * @param namedArea
117
	 * 				A namedArea that should be selected when the dialog opens
118
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
119
	 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
120
	 */
121
	public static NamedArea select(Shell shell, ConversationHolder conversation, NamedArea namedArea) {
122
		FilteredNamedAreaSelectionDialog dialog = new FilteredNamedAreaSelectionDialog(shell, conversation,
123
				"Choose an area", false, namedArea);
124
		return getSelectionFromDialog(dialog);
125
	}
126
	
127
	/**
128
	 * <p>Constructor for FilteredNamedAreaSelectionDialog.</p>
129
	 *
130
	 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
131
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
132
	 * @param title a {@link java.lang.String} object.
133
	 * @param multi a boolean.
134
	 * @param namedArea a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
135
	 */
136
	protected FilteredNamedAreaSelectionDialog(Shell shell, ConversationHolder conversation, String title, boolean multi, NamedArea namedArea) {
137
		super(shell, conversation, title, multi, FilteredNamedAreaSelectionDialog.class.getCanonicalName(), namedArea);		
138
	}
139
	
140
	/** {@inheritDoc} */
141
	@Override
142
	protected void fillViewMenu(IMenuManager menuManager) {
143
				
144
		super.fillViewMenu(menuManager);
145
		
146
		includeTdwgAreaAction = new IncludeTdwgAreaAction();
147
		menuManager.add(includeTdwgAreaAction);
148
		includeTdwgAreaAction.setChecked(true);
149
		
150
		includeWaterbodyOrCountryAction = new IncludeWaterbodyOrCountryAction();
151
		menuManager.add(includeWaterbodyOrCountryAction);
152
		includeWaterbodyOrCountryAction.setChecked(true);
153
		
154
		includeContinentAction = new IncludeContinentAction();
155
		menuManager.add(includeContinentAction);
156
		includeContinentAction.setChecked(true);
157
	}
158
	
159
	
160
	/** {@inheritDoc} */
161
	@Override
162
	protected NamedArea getPersistentObject(UUID uuid) {
163
		for(VocabularyEnum selectedVocabulary : selectedVocabularies){
164
			TermVocabulary vocabulary = CdmStore.getVocabularyService().getVocabulary(selectedVocabulary);
165
			for(Object object : vocabulary.getTerms()){
166
				CdmBase cdmBaseObject = (CdmBase) object;
167
				if(uuid.equals(cdmBaseObject.getUuid())){
168
					return (NamedArea) cdmBaseObject;
169
				}
170
			}
171
		}
172
		return null;
173
	}
174
	
175
	/** {@inheritDoc} */
176
	@Override
177
	protected void init() {
178
		// testing 
179
		selectedVocabularies = new HashSet<VocabularyEnum>();
180
		selectedVocabularies.add(VocabularyEnum.TdwgArea);
181
		selectedVocabularies.add(VocabularyEnum.WaterbodyOrCountry);
182
		selectedVocabularies.add(VocabularyEnum.Continent);
183
	}
184

    
185
	/** {@inheritDoc} */
186
	@Override
187
	protected void initModel() {
188
		
189
		Set<NamedArea> terms = new HashSet<NamedArea>();
190
		for(VocabularyEnum vocabularyEnum : selectedVocabularies){
191
			TermVocabulary vocabulary = (TermVocabulary) HibernateProxyHelper.deproxy(CdmStore.getVocabularyService().getVocabulary(vocabularyEnum));
192
			terms.addAll(vocabulary.getTermsOrderedByLabels(CdmStore.getDefaultLanguage()));
193
		}
194
		
195
		if(model == null){
196
			model = new ArrayList<UuidAndTitleCache<NamedArea>>();
197
		}
198
		model.clear();
199
		for(Object areaObject : terms){
200
			NamedArea area = (NamedArea) HibernateProxyHelper.deproxy(areaObject);
201
			UuidAndTitleCache<NamedArea> element = new UuidAndTitleCache<NamedArea>(NamedArea.class, area.getUuid(), getTitle(area)); 
202
			model.add(element);
203
		}
204
	}
205

    
206
	/** {@inheritDoc} */
207
	@Override
208
	protected Control createExtendedContentArea(Composite parent) {
209
		return null;
210
	}
211
	
212
	/** {@inheritDoc} */
213
	@Override
214
	protected String getTitle(NamedArea namedArea) {
215
		NamedArea area = (NamedArea) HibernateProxyHelper.deproxy(namedArea);
216
		
217
		StringBuilder title = new StringBuilder();
218
		Representation representation = area.getRepresentation(CdmStore.getDefaultLanguage());
219
		
220
		title.append(representation.getDescription());
221
		title.append(" - ");
222
		title.append(area.getClass().getSimpleName());
223
		if(area.getLevel() != null){
224
			title.append(" - ");
225
			title.append(area.getLevel().getLabel(CdmStore.getDefaultLanguage()));
226
		}
227
		if(! CdmUtils.isEmpty(representation.getAbbreviatedLabel())){
228
			title.append(" - ");
229
			title.append(representation.getAbbreviatedLabel());
230
		}
231
		
232
		return title.toString();
233
	}
234
	
235
	/** {@inheritDoc} */
236
	@Override
237
	protected AbstractNewEntityWizard getNewEntityWizard() {
238
		return null;
239
	}
240

    
241
	/** {@inheritDoc} */
242
	@Override
243
	protected String getNewWizardLinkText() {
244
		return null;
245
	}
246
}
(9-9/15)