Pre-select country as named area type for country selection for
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / 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.dialog.selection;
12
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Shell;
26
27 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
28 import eu.etaxonomy.cdm.api.service.IVocabularyService;
29 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.common.TermVocabulary;
32 import eu.etaxonomy.cdm.model.location.Country;
33 import eu.etaxonomy.cdm.model.location.NamedArea;
34 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
35 import eu.etaxonomy.taxeditor.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 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 initModel();
67 }
68 }
69
70 private Collection<TermVocabulary<NamedArea>> selectedVocabularies;
71 private final ArrayList<TermVocabulary> preselectedVocabularies;
72
73
74 /**
75 * Creates a filtered selection dialog to select a named area.
76 *
77 * @param shell
78 * The shell for displaying this widget
79 * @param namedArea
80 * A namedArea that should be selected when the dialog opens
81 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
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, UUID... preselectedVocabularyUuids) {
86 NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell, conversation,
87 "Choose an area", false, namedArea, preselectedVocabularyUuids);
88 return getSelectionFromDialog(dialog);
89 }
90
91 /**
92 * <p>Constructor for FilteredNamedAreaSelectionDialog.</p>
93 *
94 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
95 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
96 * @param title a {@link java.lang.String} object.
97 * @param multi a boolean.
98 * @param namedArea a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
99 * @param preselectedVocabularyUuids the {@link UUID}s of the pre-selected Vocabularies
100 */
101 protected NamedAreaSelectionDialog(Shell shell, ConversationHolder conversation, String title, boolean multi, NamedArea namedArea, UUID... preselectedVocabularyUuids) {
102 super(shell, conversation, title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
103 preselectedVocabularies = new ArrayList<TermVocabulary>();
104 for(int i=0;i<preselectedVocabularyUuids.length;i++){
105 TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
106 preselectedVocabularies.add(preselectedVocabulary);
107 }
108 selectedVocabularies.retainAll(preselectedVocabularies);
109 initModel();//re-init to consider pre-selected vocabularies
110 }
111
112 /** {@inheritDoc} */
113 @Override
114 protected void fillViewMenu(IMenuManager menuManager) {
115
116 super.fillViewMenu(menuManager);
117
118 for(TermVocabulary<NamedArea> vocabulary : getAvailableVocabularies()){
119 IncludeNamedAreaVocabulary action = new IncludeNamedAreaVocabulary(vocabulary);
120 menuManager.add(action);
121 if(preselectedVocabularies.isEmpty() || preselectedVocabularies.contains(vocabulary)) {
122 action.setChecked(true);
123 }
124 }
125 }
126
127 /** {@inheritDoc} */
128 @Override
129 protected NamedArea getPersistentObject(UUID uuid) {
130 for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
131 for(Object object : vocabulary.getTerms()){
132 CdmBase cdmBaseObject = (CdmBase) object;
133 if(uuid.equals(cdmBaseObject.getUuid())){
134 return CdmBase.deproxy(cdmBaseObject, NamedArea.class);
135 }
136 }
137 }
138 return null;
139 }
140
141 /** {@inheritDoc} */
142 @Override
143 protected void init() {
144 selectedVocabularies = getAvailableVocabularies();
145 }
146
147 private List<TermVocabulary<NamedArea>> getAvailableVocabularies(){
148 List<TermVocabulary<NamedArea>> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermClass(NamedArea.class, null, null, null, null);
149 vocabularies.add(CdmStore.getService(IVocabularyService.class).find(Country.uuidCountryVocabulary));
150 return vocabularies;
151 }
152
153 /** {@inheritDoc} */
154 @Override
155 protected void initModel() {
156
157 Set<NamedArea> terms = new HashSet<NamedArea>();
158 for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
159 terms.addAll(vocabulary.getTermsOrderedByLabels(CdmStore.getDefaultLanguage()));
160 }
161
162 if(model == null){
163 model = new ArrayList<UuidAndTitleCache<NamedArea>>();
164 }
165 model.clear();
166 for(Object areaObject : terms){
167 NamedArea area = (NamedArea) HibernateProxyHelper.deproxy(areaObject);
168 UuidAndTitleCache<NamedArea> element = new UuidAndTitleCache<NamedArea>(NamedArea.class, area.getUuid(), area.getId(), getTitle(area));
169 model.add(element);
170 }
171 }
172
173 /** {@inheritDoc} */
174 @Override
175 protected Control createExtendedContentArea(Composite parent) {
176 return null;
177 }
178
179 /** {@inheritDoc} */
180 @Override
181 protected String getTitle(NamedArea namedArea) {
182 try {
183 String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
184 return result;
185 } catch (Exception e) {
186 //TODO still need to learn how errors are handled in the Tax Editor
187 System.out.println("Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid());
188 return namedArea.getTitleCache();
189 }
190 }
191
192 /** {@inheritDoc} */
193 @Override
194 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
195 return null;
196 }
197
198 /** {@inheritDoc} */
199 @Override
200 protected String getNewWizardLinkText() {
201 return null;
202 }
203 }