a9279b4852de4677dee946a9204c8579e9591336
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / ui / dialogs / filteredSelection / NamedAreaSelectionDialog.java
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.ui.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.api.service.IVocabularyService;
28 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
29 import eu.etaxonomy.cdm.model.common.CdmBase;
30 import eu.etaxonomy.cdm.model.common.TermVocabulary;
31 import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
32 import eu.etaxonomy.cdm.model.common.VocabularyEnum;
33 import eu.etaxonomy.cdm.model.location.NamedArea;
34 import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
35 import eu.etaxonomy.taxeditor.store.CdmStore;
36
37 /**
38 * <p>FilteredNamedAreaSelectionDialog class.</p>
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 IncludeTdwgAreaAction extends Action {
48 /**
49 * Creates a new instance of the class.
50 */
51 public IncludeTdwgAreaAction() {
52 super("TDWG Areas", IAction.AS_CHECK_BOX);
53 }
54
55 public void run() {
56 if(isChecked()){
57 selectedVocabularies.add(VocabularyEnum.TdwgArea);
58 }else{
59 selectedVocabularies.remove(VocabularyEnum.TdwgArea);
60 }
61
62 initModel();
63 }
64 }
65
66 private class IncludeWaterbodyOrCountryAction extends Action {
67 /**
68 * Creates a new instance of the class.
69 */
70 public IncludeWaterbodyOrCountryAction() {
71 super("Waterbody Or Country", IAction.AS_CHECK_BOX);
72 }
73
74 public void run() {
75 if(isChecked()){
76 selectedVocabularies.add(VocabularyEnum.WaterbodyOrCountry);
77 }else{
78 selectedVocabularies.remove(VocabularyEnum.WaterbodyOrCountry);
79 }
80
81 initModel();
82 }
83 }
84
85 private class IncludeContinentAction extends Action {
86 /**
87 * Creates a new instance of the class.
88 */
89 public IncludeContinentAction() {
90 super("Continent", IAction.AS_CHECK_BOX);
91 }
92
93 public void run() {
94 if(isChecked()){
95 selectedVocabularies.add(VocabularyEnum.Continent);
96 }else{
97 selectedVocabularies.remove(VocabularyEnum.Continent);
98 }
99
100 initModel();
101 }
102 }
103
104 private Collection<VocabularyEnum> selectedVocabularies;
105
106 private IncludeTdwgAreaAction includeTdwgAreaAction;
107 private IncludeWaterbodyOrCountryAction includeWaterbodyOrCountryAction;
108 private IncludeContinentAction includeContinentAction;
109
110 /**
111 * Creates a filtered selection dialog to select a named area.
112 *
113 * @param shell
114 * The shell for displaying this widget
115 * @param namedArea
116 * A namedArea that should be selected when the dialog opens
117 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
118 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
119 */
120 public static NamedArea select(Shell shell, ConversationHolder conversation, NamedArea namedArea) {
121 NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell, conversation,
122 "Choose an area", false, namedArea);
123 return getSelectionFromDialog(dialog);
124 }
125
126 /**
127 * <p>Constructor for FilteredNamedAreaSelectionDialog.</p>
128 *
129 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
130 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
131 * @param title a {@link java.lang.String} object.
132 * @param multi a boolean.
133 * @param namedArea a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
134 */
135 protected NamedAreaSelectionDialog(Shell shell, ConversationHolder conversation, String title, boolean multi, NamedArea namedArea) {
136 super(shell, conversation, title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
137 }
138
139 /** {@inheritDoc} */
140 @Override
141 protected void fillViewMenu(IMenuManager menuManager) {
142
143 super.fillViewMenu(menuManager);
144
145 includeTdwgAreaAction = new IncludeTdwgAreaAction();
146 menuManager.add(includeTdwgAreaAction);
147 includeTdwgAreaAction.setChecked(true);
148
149 includeWaterbodyOrCountryAction = new IncludeWaterbodyOrCountryAction();
150 menuManager.add(includeWaterbodyOrCountryAction);
151 includeWaterbodyOrCountryAction.setChecked(true);
152
153 includeContinentAction = new IncludeContinentAction();
154 menuManager.add(includeContinentAction);
155 includeContinentAction.setChecked(true);
156 }
157
158
159 /** {@inheritDoc} */
160 @Override
161 protected NamedArea getPersistentObject(UUID uuid) {
162 for(VocabularyEnum selectedVocabulary : selectedVocabularies){
163 TermVocabulary vocabulary = CdmStore.getService(IVocabularyService.class).getVocabulary(selectedVocabulary);
164 for(Object object : vocabulary.getTerms()){
165 CdmBase cdmBaseObject = (CdmBase) object;
166 if(uuid.equals(cdmBaseObject.getUuid())){
167 return (NamedArea) cdmBaseObject;
168 }
169 }
170 }
171 return null;
172 }
173
174 /** {@inheritDoc} */
175 @Override
176 protected void init() {
177 // testing
178 selectedVocabularies = new HashSet<VocabularyEnum>();
179 selectedVocabularies.add(VocabularyEnum.TdwgArea);
180 selectedVocabularies.add(VocabularyEnum.WaterbodyOrCountry);
181 selectedVocabularies.add(VocabularyEnum.Continent);
182 }
183
184 /** {@inheritDoc} */
185 @Override
186 protected void initModel() {
187
188 Set<NamedArea> terms = new HashSet<NamedArea>();
189 for(VocabularyEnum vocabularyEnum : selectedVocabularies){
190 TermVocabulary vocabulary = (TermVocabulary) HibernateProxyHelper.deproxy(CdmStore.getService(IVocabularyService.class).getVocabulary(vocabularyEnum));
191 terms.addAll(vocabulary.getTermsOrderedByLabels(CdmStore.getDefaultLanguage()));
192 }
193
194 if(model == null){
195 model = new ArrayList<UuidAndTitleCache<NamedArea>>();
196 }
197 model.clear();
198 for(Object areaObject : terms){
199 NamedArea area = (NamedArea) HibernateProxyHelper.deproxy(areaObject);
200 UuidAndTitleCache<NamedArea> element = new UuidAndTitleCache<NamedArea>(NamedArea.class, area.getUuid(), getTitle(area));
201 model.add(element);
202 }
203 }
204
205 /** {@inheritDoc} */
206 @Override
207 protected Control createExtendedContentArea(Composite parent) {
208 return null;
209 }
210
211 /** {@inheritDoc} */
212 @Override
213 protected String getTitle(NamedArea namedArea) {
214 return NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
215 }
216
217 /** {@inheritDoc} */
218 @Override
219 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
220 return null;
221 }
222
223 /** {@inheritDoc} */
224 @Override
225 protected String getNewWizardLinkText() {
226 return null;
227 }
228 }