1deb9a06c6696eb147dae7a8696ce68c590fbf73
[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.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.common.UuidAndTitleCache;
33 import eu.etaxonomy.cdm.model.common.VocabularyEnum;
34 import eu.etaxonomy.cdm.model.location.NamedArea;
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 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 public void run(){
60 if(isChecked()){
61 selectedVocabularies.add(vocabulary);
62 }else{
63 selectedVocabularies.remove(vocabulary);
64 }
65
66 initModel();
67 }
68 }
69
70 private Collection<TermVocabulary<NamedArea>> selectedVocabularies = new ArrayList<TermVocabulary<NamedArea>>();
71
72
73 /**
74 * Creates a filtered selection dialog to select a named area.
75 *
76 * @param shell
77 * The shell for displaying this widget
78 * @param namedArea
79 * A namedArea that should be selected when the dialog opens
80 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
81 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
82 */
83 public static NamedArea select(Shell shell, ConversationHolder conversation, NamedArea namedArea) {
84 NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell, conversation,
85 "Choose an area", false, namedArea);
86 return getSelectionFromDialog(dialog);
87 }
88
89 /**
90 * <p>Constructor for FilteredNamedAreaSelectionDialog.</p>
91 *
92 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
93 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
94 * @param title a {@link java.lang.String} object.
95 * @param multi a boolean.
96 * @param namedArea a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
97 */
98 protected NamedAreaSelectionDialog(Shell shell, ConversationHolder conversation, String title, boolean multi, NamedArea namedArea) {
99 super(shell, conversation, title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
100 }
101
102 /** {@inheritDoc} */
103 @Override
104 protected void fillViewMenu(IMenuManager menuManager) {
105
106 super.fillViewMenu(menuManager);
107
108 List<TermVocabulary<NamedArea>> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermClass(NamedArea.class, null, null, null, null);
109 vocabularies.add(CdmStore.getService(IVocabularyService.class).getVocabulary(VocabularyEnum.TdwgArea));
110 vocabularies.add(CdmStore.getService(IVocabularyService.class).getVocabulary(VocabularyEnum.WaterbodyOrCountry));
111 vocabularies.add(CdmStore.getService(IVocabularyService.class).getVocabulary(VocabularyEnum.Continent));
112
113 for(TermVocabulary<NamedArea> vocabulary : vocabularies){
114 IncludeNamedAreaVocabulary action = new IncludeNamedAreaVocabulary(vocabulary);
115 menuManager.add(action);
116 action.setChecked(true);
117 }
118 }
119
120 /** {@inheritDoc} */
121 @Override
122 protected NamedArea getPersistentObject(UUID uuid) {
123 for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
124 for(Object object : vocabulary.getTerms()){
125 CdmBase cdmBaseObject = (CdmBase) object;
126 if(uuid.equals(cdmBaseObject.getUuid())){
127 return (NamedArea) cdmBaseObject;
128 }
129 }
130 }
131 return null;
132 }
133
134 /** {@inheritDoc} */
135 @Override
136 protected void init() {
137 selectedVocabularies = CdmStore.getService(IVocabularyService.class).listByTermClass(NamedArea.class, null, null, null, null);
138 selectedVocabularies.add(CdmStore.getService(IVocabularyService.class).getVocabulary(VocabularyEnum.TdwgArea));
139 selectedVocabularies.add(CdmStore.getService(IVocabularyService.class).getVocabulary(VocabularyEnum.WaterbodyOrCountry));
140 selectedVocabularies.add(CdmStore.getService(IVocabularyService.class).getVocabulary(VocabularyEnum.Continent));
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(), 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 return NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
173 }
174
175 /** {@inheritDoc} */
176 @Override
177 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
178 return null;
179 }
180
181 /** {@inheritDoc} */
182 @Override
183 protected String getNewWizardLinkText() {
184 return null;
185 }
186 }