Fix named area filter for selection dialog (#2353)
[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 * @author n.hoffmann
40 * @created May 11, 2010
41 * @version 1.0
42 */
43 public class NamedAreaSelectionDialog extends
44 AbstractFilteredCdmResourceSelectionDialog<NamedArea> {
45
46 private class IncludeNamedAreaVocabulary extends Action {
47 private final TermVocabulary<NamedArea> vocabulary;
48
49 /**
50 * Creates a new instance of the class.
51 */
52 public IncludeNamedAreaVocabulary(TermVocabulary<NamedArea> vocabulary) {
53 super(vocabulary.getTitleCache(), IAction.AS_CHECK_BOX);
54 this.vocabulary = vocabulary;
55 }
56
57 @Override
58 public void run(){
59 if(isChecked()){
60 selectedVocabularies.add(vocabulary);
61 }else{
62 selectedVocabularies.remove(vocabulary);
63 }
64 initModel();
65 }
66 }
67
68 private Collection<TermVocabulary<NamedArea>> selectedVocabularies;
69 private ArrayList<TermVocabulary> preselectedVocabularies;
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 preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies
81 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
82 */
83 public static NamedArea select(Shell shell, ConversationHolder conversation, NamedArea namedArea, UUID... preselectedVocabularyUuids) {
84 NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell, conversation,
85 "Choose an area", false, namedArea, preselectedVocabularyUuids);
86 return getSelectionFromDialog(dialog);
87 }
88
89 protected NamedAreaSelectionDialog(Shell shell, ConversationHolder conversation, String title, boolean multi, NamedArea namedArea, UUID... preselectedVocabularyUuids) {
90 super(shell, conversation, title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
91 if(preselectedVocabularyUuids.length>0){
92 preselectedVocabularies = new ArrayList<TermVocabulary>();
93 for(int i=0;i<preselectedVocabularyUuids.length;i++){
94 TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
95 preselectedVocabularies.add(preselectedVocabulary);
96 }
97 selectedVocabularies.retainAll(preselectedVocabularies);
98 initModel();//re-init to consider pre-selected vocabularies
99 }
100 }
101
102 /** {@inheritDoc} */
103 @Override
104 protected void fillViewMenu(IMenuManager menuManager) {
105
106 super.fillViewMenu(menuManager);
107
108 for(TermVocabulary<NamedArea> vocabulary : getAvailableVocabularies()){
109 IncludeNamedAreaVocabulary action = new IncludeNamedAreaVocabulary(vocabulary);
110 menuManager.add(action);
111 if(preselectedVocabularies==null || preselectedVocabularies.isEmpty() || preselectedVocabularies.contains(vocabulary)) {
112 action.setChecked(true);
113 }
114 }
115 }
116
117 /** {@inheritDoc} */
118 @Override
119 protected NamedArea getPersistentObject(UUID uuid) {
120 for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
121 for(Object object : vocabulary.getTerms()){
122 CdmBase cdmBaseObject = (CdmBase) object;
123 if(uuid.equals(cdmBaseObject.getUuid())){
124 return CdmBase.deproxy(cdmBaseObject, NamedArea.class);
125 }
126 }
127 }
128 return null;
129 }
130
131 /** {@inheritDoc} */
132 @Override
133 protected void init() {
134 selectedVocabularies = getAvailableVocabularies();
135 }
136
137 private List<TermVocabulary<NamedArea>> getAvailableVocabularies(){
138 List<TermVocabulary<NamedArea>> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermClass(NamedArea.class, null, null, null, null);
139 vocabularies.add(CdmStore.getService(IVocabularyService.class).find(Country.uuidCountryVocabulary));
140 return vocabularies;
141 }
142
143 /** {@inheritDoc} */
144 @Override
145 protected void initModel() {
146
147 Set<NamedArea> terms = new HashSet<NamedArea>();
148 for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
149 terms.addAll(vocabulary.getTermsOrderedByLabels(CdmStore.getDefaultLanguage()));
150 }
151
152 if(model == null){
153 model = new ArrayList<UuidAndTitleCache<NamedArea>>();
154 }
155 model.clear();
156 for(Object areaObject : terms){
157 NamedArea area = (NamedArea) HibernateProxyHelper.deproxy(areaObject);
158 UuidAndTitleCache<NamedArea> element = new UuidAndTitleCache<NamedArea>(NamedArea.class, area.getUuid(), area.getId(), getTitle(area));
159 model.add(element);
160 }
161 }
162
163 /** {@inheritDoc} */
164 @Override
165 protected Control createExtendedContentArea(Composite parent) {
166 return null;
167 }
168
169 /** {@inheritDoc} */
170 @Override
171 protected String getTitle(NamedArea namedArea) {
172 try {
173 String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
174 return result;
175 } catch (Exception e) {
176 //TODO still need to learn how errors are handled in the Tax Editor
177 System.out.println("Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid());
178 return namedArea.getTitleCache();
179 }
180 }
181
182 /** {@inheritDoc} */
183 @Override
184 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
185 return null;
186 }
187
188 /** {@inheritDoc} */
189 @Override
190 protected String getNewWizardLinkText() {
191 return null;
192 }
193 }