Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / NamedAreaSelectionDialog.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.ui.dialog.selection;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.List;
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.TermType;
31 import eu.etaxonomy.cdm.model.common.TermVocabulary;
32 import eu.etaxonomy.cdm.model.location.NamedArea;
33 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
34 import eu.etaxonomy.taxeditor.model.MessagingUtils;
35 import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
36 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
37 import eu.etaxonomy.taxeditor.store.CdmStore;
38
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 IncludeNamedAreaVocabulary extends Action {
48 private final TermVocabulary<NamedArea> vocabulary;
49
50 /**
51 * Creates a new instance of the class.
52 */
53 public IncludeNamedAreaVocabulary(TermVocabulary<NamedArea> vocabulary) {
54 super(vocabulary.getTitleCache(), IAction.AS_CHECK_BOX);
55 this.vocabulary = vocabulary;
56 }
57
58 @Override
59 public void run(){
60 if(isChecked()){
61 selectedVocabularies.add(vocabulary);
62 }else{
63 selectedVocabularies.remove(vocabulary);
64 }
65 PreferencesUtil.getPreferenceStore().setValue(getPrefKey(vocabulary), !isChecked());
66 initModel();
67 }
68 }
69
70 protected Collection<TermVocabulary> selectedVocabularies;
71 protected ArrayList<TermVocabulary> preselectedVocabularies;
72 private Object preferenceID;
73
74
75 /**
76 * Creates a filtered selection dialog to select a named area.
77 *
78 * @param shell
79 * The shell for displaying this widget
80 * @param namedArea
81 * A namedArea that should be selected when the dialog opens
82 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
83 * @param preferenceId a class which is used for generating the preference key so that every
84 * dialogs can be grouped to have their own preferences depending on this id
85 * @param preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies
86 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
87 */
88 public static NamedArea select(Shell shell, ConversationHolder conversation, NamedArea namedArea, String preferenceId, UUID... preselectedVocabularyUuids) {
89 NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell, conversation,
90 "Choose an area", false, namedArea, preferenceId, preselectedVocabularyUuids);
91 return getSelectionFromDialog(dialog);
92 }
93
94 protected NamedAreaSelectionDialog(Shell shell, ConversationHolder conversation, String title, boolean multi, NamedArea namedArea, Object preferenceId, UUID... preselectedVocabularyUuids) {
95 super(shell, conversation, title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
96 this.preferenceID = preferenceId;
97 preselectedVocabularies = new ArrayList<TermVocabulary>();
98 for(int i=0;i<preselectedVocabularyUuids.length;i++){
99 TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
100 preselectedVocabularies.add(preselectedVocabulary);
101 }
102 Collection<TermVocabulary> tempSelectedVocabularies = new HashSet<>();
103 for(TermVocabulary vocabulary:selectedVocabularies){
104 if(preselectedVocabularies.contains(vocabulary)
105 || !PreferencesUtil.getPreferenceStore().getBoolean(getPrefKey(vocabulary))){
106 tempSelectedVocabularies.add(vocabulary);
107 }
108 }
109 selectedVocabularies = tempSelectedVocabularies;
110 initModel();//re-init to consider pre-selected vocabularies
111 }
112
113 /** {@inheritDoc} */
114 @Override
115 protected void fillViewMenu(IMenuManager menuManager) {
116
117 super.fillViewMenu(menuManager);
118
119 for(TermVocabulary<NamedArea> vocabulary : getAvailableVocabularies()){
120 IncludeNamedAreaVocabulary action = new IncludeNamedAreaVocabulary(vocabulary);
121 menuManager.add(action);
122 if(preselectedVocabularies.contains(vocabulary)) {
123 action.setChecked(true);
124 }
125 else{
126 action.setChecked(!PreferencesUtil.getPreferenceStore().getBoolean(getPrefKey(vocabulary)));
127 }
128 }
129 }
130
131 private String getPrefKey(TermVocabulary vocabulary){
132 return "hide_"+NamedAreaSelectionDialog.class.getCanonicalName()+vocabulary.getUuid()+preferenceID;
133 }
134
135 /** {@inheritDoc} */
136 @Override
137 protected NamedArea getPersistentObject(UUID uuid) {
138 for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
139 for(Object object : vocabulary.getTerms()){
140 CdmBase cdmBaseObject = (CdmBase) object;
141 if(uuid.equals(cdmBaseObject.getUuid())){
142 return CdmBase.deproxy(cdmBaseObject, NamedArea.class);
143 }
144 }
145 }
146 return null;
147 }
148
149 /** {@inheritDoc} */
150 @Override
151 protected void init() {
152 selectedVocabularies = getAvailableVocabularies();
153 }
154
155 private List<TermVocabulary> getAvailableVocabularies(){
156 List<TermVocabulary> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
157 return vocabularies;
158 }
159
160 /** {@inheritDoc} */
161 @Override
162 protected void initModel() {
163
164 Set<NamedArea> terms = new HashSet<NamedArea>();
165 for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
166 terms.addAll(vocabulary.getTermsOrderedByLabels(CdmStore.getDefaultLanguage()));
167 }
168
169 if(model == null){
170 model = new ArrayList<UuidAndTitleCache<NamedArea>>();
171 }
172 model.clear();
173 for(Object areaObject : terms){
174 NamedArea area = (NamedArea) HibernateProxyHelper.deproxy(areaObject);
175 UuidAndTitleCache<NamedArea> element = new UuidAndTitleCache<NamedArea>(NamedArea.class, area.getUuid(), area.getId(), getTitle(area));
176 model.add(element);
177 }
178 }
179
180 /** {@inheritDoc} */
181 @Override
182 protected Control createExtendedContentArea(Composite parent) {
183 return null;
184 }
185
186 /** {@inheritDoc} */
187 @Override
188 protected String getTitle(NamedArea namedArea) {
189 try {
190 String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
191 return result;
192 } catch (Exception e) {
193 MessagingUtils.error(NamedAreaSelectionDialog.class, "Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid(), e);
194 return namedArea.getTitleCache();
195 }
196 }
197
198 /** {@inheritDoc} */
199 @Override
200 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
201 return null;
202 }
203
204 /** {@inheritDoc} */
205 @Override
206 protected String getNewWizardLinkText() {
207 return null;
208 }
209 }