fix #8034: preselected references should have correct type
[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.List;
14 import java.util.UUID;
15
16 import org.apache.commons.lang.StringUtils;
17 import org.eclipse.jface.action.Action;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.events.SelectionListener;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Shell;
26
27 import eu.etaxonomy.cdm.api.service.ITermService;
28 import eu.etaxonomy.cdm.api.service.IVocabularyService;
29 import eu.etaxonomy.cdm.model.location.NamedArea;
30 import eu.etaxonomy.cdm.model.metadata.NamedAreaSearchField;
31 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
32 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
33 import eu.etaxonomy.cdm.model.term.TermType;
34 import eu.etaxonomy.cdm.model.term.TermVocabulary;
35 import eu.etaxonomy.taxeditor.model.ImageResources;
36 import eu.etaxonomy.taxeditor.model.MessagingUtils;
37 import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
38 import eu.etaxonomy.taxeditor.preference.CdmPreferenceCache;
39 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
40 import eu.etaxonomy.taxeditor.store.CdmStore;
41
42 /**
43 * @author n.hoffmann
44 * @created May 11, 2010
45 * @version 1.0
46 */
47 public class NamedAreaSelectionDialog extends
48 AbstractFilteredCdmResourceSelectionDialog<NamedArea> {
49
50 private class IncludeNamedAreaVocabulary extends Action {
51 private final TermVocabulary<NamedArea> vocabulary;
52
53 /**
54 * Creates a new instance of the class.
55 */
56 public IncludeNamedAreaVocabulary(TermVocabulary<NamedArea> vocabulary) {
57 super(vocabulary.getTitleCache(), IAction.AS_CHECK_BOX);
58 this.vocabulary = vocabulary;
59 }
60
61 @Override
62 public void run(){
63 if(isChecked()){
64 selectedVocabularies.add(vocabulary);
65 }else{
66 selectedVocabularies.remove(vocabulary);
67 }
68 PreferencesUtil.setBooleanValue(getPrefKey(vocabulary), !isChecked());
69 search();
70 }
71 }
72 protected List<NamedArea> selectedAreas;
73 protected List<TermVocabulary> selectedVocabularies;
74 protected List<TermVocabulary> vocabularies;
75
76
77
78 /**
79 * Creates a filtered selection dialog to select a named area.
80 *
81 * @param shell
82 * The shell for displaying this widget
83 * @param namedArea
84 * A namedArea that should be selected when the dialog opens
85 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
86 * @param preferenceId a class which is used for generating the preference key so that every
87 * dialogs can be grouped to have their own preferences depending on this id
88 * @param preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies
89 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
90 */
91 public static NamedArea select(Shell shell, //ConversationHolder conversation,
92 NamedArea namedArea, String preferenceId, UUID... preselectedVocabularyUuids) {
93 NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell, //conversation,
94 "Choose an area", false, namedArea, preferenceId, preselectedVocabularyUuids);
95 return getSelectionFromDialog(dialog);
96 }
97
98 protected NamedAreaSelectionDialog(Shell shell, //ConversationHolder conversation,
99 String title, boolean multi, NamedArea namedArea, Object preferenceId, UUID... preselectedVocabularyUuids) {
100 super(shell, //conversation,
101 title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
102
103 selectedVocabularies = new ArrayList<TermVocabulary>();
104 this.preferenceID = preferenceId;
105
106 if (preselectedVocabularyUuids != null && preselectedVocabularyUuids.length > 0){
107 for(int i=0;i<preselectedVocabularyUuids.length;i++){
108 TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
109 selectedVocabularies.add(preselectedVocabulary);
110 }
111 }else{
112 selectedVocabularies = createSelectedVocabularies();
113 }
114
115 }
116
117 protected List<TermVocabulary> createSelectedVocabularies() {
118 List<TermVocabulary> tempSelectedVocabularies = new ArrayList<TermVocabulary>();
119 for(TermVocabulary vocabulary:vocabularies){
120 if((selectedVocabularies.contains(vocabulary) && !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary)))
121 || !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary))){
122 tempSelectedVocabularies.add(vocabulary);
123 }
124 }
125 return tempSelectedVocabularies;
126 }
127
128 // private static UUID[] createVocabularyUuidList() {
129 // String preselectedVocString = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
130 // if (StringUtils.isBlank(preselectedVocString)){
131 // return null;
132 // }
133 // String[] preselectedVocArray = preselectedVocString.split(";");
134 // UUID[] uuidList = new UUID[preselectedVocArray.length];
135 // int i = 0;
136 // for (String uuidString: preselectedVocArray){
137 // uuidList[i]= UUID.fromString(uuidString);
138 // i++;
139 // }
140 // return uuidList;
141 // }
142
143
144 private String getPrefKey(TermVocabulary vocabulary){
145 return "hide_"+NamedAreaSelectionDialog.class.getCanonicalName()+vocabulary.getUuid()+preferenceID;
146 }
147
148 /** {@inheritDoc} */
149 @Override
150 protected NamedArea getPersistentObject(UUID uuid) {
151
152 DefinedTermBase area = CdmStore.getService(ITermService.class).find(uuid);
153 if (area instanceof NamedArea){
154 return (NamedArea) area;
155 }
156
157 return null;
158 }
159
160 /** {@inheritDoc} */
161 @Override
162 protected void init() {
163 vocabularies = getAvailableVocabularies();
164 }
165
166 private List<TermVocabulary> getAvailableVocabularies(){
167 vocabularies = new ArrayList();
168 CdmPreferenceCache cache = CdmPreferenceCache.instance();
169 vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
170 return vocabularies;
171 }
172
173
174
175 /** {@inheritDoc} */
176 @Override
177 protected String getTitle(NamedArea namedArea) {
178 try {
179 String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
180 return result;
181 } catch (Exception e) {
182 MessagingUtils.error(NamedAreaSelectionDialog.class, "Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid(), e);
183 return namedArea.getTitleCache();
184 }
185 }
186
187 /** {@inheritDoc} */
188 @Override
189 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
190 return null;
191 }
192
193 /** {@inheritDoc} */
194 @Override
195 protected String[] getNewWizardText() {
196 return null;
197 }
198
199 @Override
200 void createFilterButton(Composite searchAndFilter)
201 {
202 filterButton = new Button(searchAndFilter, SWT.NONE);
203 // filterButton.setText("Filter");
204 filterButton.setImage(ImageResources.getImage(ImageResources.FUNNEL_ICON));
205 // SelectionListener filterSelectionListener = new FilterSelectionListener(preferenceID, this);
206 filterButton.addSelectionListener(new SelectionListener(){
207 @Override
208 public void widgetSelected(SelectionEvent e) {
209
210 Object source = e.getSource();
211 String text = null;
212 if (source instanceof Button){
213 Shell shell = ((Button)source).getShell();
214 Dialog dialog = new FilterDialog(getShell(), preferenceID, selectedVocabularies, vocabularies);
215 if(dialog!=null){
216 dialog.open();
217 }
218 createSelectedVocabularies();
219 search();
220 }
221
222
223
224
225 }
226
227 @Override
228 public void widgetDefaultSelected(SelectionEvent e) {
229 // TODO Auto-generated method stub
230
231 }
232
233
234 });
235
236 }
237
238
239
240 /* (non-Javadoc)
241 * @see eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog#callService(java.lang.String)
242 */
243 @Override
244 void callService(String pattern) {
245 String abbrevTypeString = PreferencesUtil.getStringValue(PreferencePredicate.NamedAreaSearchForAbbrev.getKey());
246 NamedAreaSearchField abbrevType = StringUtils.isNotBlank(abbrevTypeString)? NamedAreaSearchField.byKey(abbrevTypeString): null;
247 if (selectedVocabularies == null || selectedVocabularies.size() == 0){
248 if (abbrevType != null && !abbrevType.equals(NamedAreaSearchField.NoAbbrev)){
249 model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedAreaByAbbrev(vocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage(), abbrevType);
250 }else{
251 model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(vocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
252 }
253 }else{
254 if (abbrevType != null && !abbrevType.equals(NamedAreaSearchField.NoAbbrev)){
255 model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedAreaByAbbrev(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage(), abbrevType);
256 }else{
257 model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
258 }
259
260 }
261 }
262
263 }