minor
[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.eclipse.jface.action.Action;
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.SelectionListener;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Shell;
25
26 import eu.etaxonomy.cdm.api.service.ITermService;
27 import eu.etaxonomy.cdm.api.service.IVocabularyService;
28 import eu.etaxonomy.cdm.model.location.NamedArea;
29 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
30 import eu.etaxonomy.cdm.model.term.TermType;
31 import eu.etaxonomy.cdm.model.term.TermVocabulary;
32 import eu.etaxonomy.taxeditor.model.ImageResources;
33 import eu.etaxonomy.taxeditor.model.MessagingUtils;
34 import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
35 import eu.etaxonomy.taxeditor.preference.CdmPreferenceCache;
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.setBooleanValue(getPrefKey(vocabulary), !isChecked());
66 search();
67 }
68 }
69
70 protected List<TermVocabulary> selectedVocabularies;
71 protected List<TermVocabulary> vocabularies;
72
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,
89 NamedArea namedArea, String preferenceId, UUID... preselectedVocabularyUuids) {
90 NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell, //conversation,
91 "Choose an area", false, namedArea, preferenceId, preselectedVocabularyUuids);
92 return getSelectionFromDialog(dialog);
93 }
94
95 protected NamedAreaSelectionDialog(Shell shell, //ConversationHolder conversation,
96 String title, boolean multi, NamedArea namedArea, Object preferenceId, UUID... preselectedVocabularyUuids) {
97 super(shell, //conversation,
98 title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
99
100 selectedVocabularies = new ArrayList<TermVocabulary>();
101 this.preferenceID = preferenceId;
102
103 if (preselectedVocabularyUuids != null && preselectedVocabularyUuids.length > 0){
104 for(int i=0;i<preselectedVocabularyUuids.length;i++){
105 TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
106 selectedVocabularies.add(preselectedVocabulary);
107 }
108 }else{
109 selectedVocabularies = createSelectedVocabularies();
110 }
111
112 }
113
114 protected List<TermVocabulary> createSelectedVocabularies() {
115 List<TermVocabulary> tempSelectedVocabularies = new ArrayList<TermVocabulary>();
116 for(TermVocabulary vocabulary:vocabularies){
117 if((selectedVocabularies.contains(vocabulary) && !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary)))
118 || !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary))){
119 tempSelectedVocabularies.add(vocabulary);
120 }
121 }
122 return tempSelectedVocabularies;
123 }
124
125 // private static UUID[] createVocabularyUuidList() {
126 // String preselectedVocString = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
127 // if (StringUtils.isBlank(preselectedVocString)){
128 // return null;
129 // }
130 // String[] preselectedVocArray = preselectedVocString.split(";");
131 // UUID[] uuidList = new UUID[preselectedVocArray.length];
132 // int i = 0;
133 // for (String uuidString: preselectedVocArray){
134 // uuidList[i]= UUID.fromString(uuidString);
135 // i++;
136 // }
137 // return uuidList;
138 // }
139
140
141 private String getPrefKey(TermVocabulary vocabulary){
142 return "hide_"+NamedAreaSelectionDialog.class.getCanonicalName()+vocabulary.getUuid()+preferenceID;
143 }
144
145 /** {@inheritDoc} */
146 @Override
147 protected NamedArea getPersistentObject(UUID uuid) {
148
149 DefinedTermBase area = CdmStore.getService(ITermService.class).find(uuid);
150 if (area instanceof NamedArea){
151 return (NamedArea) area;
152 }
153
154 return null;
155 }
156
157 /** {@inheritDoc} */
158 @Override
159 protected void init() {
160 vocabularies = getAvailableVocabularies();
161 }
162
163 private List<TermVocabulary> getAvailableVocabularies(){
164 vocabularies = new ArrayList();
165 CdmPreferenceCache cache = CdmPreferenceCache.instance();
166 // CdmPreference pref = cache.get(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
167 // if (pref != null && !pref.isAllowOverride()){
168 // UUID[] preselectedVocabularyUuids = createVocabularyUuidList();
169 //
170 // for(int i=0;i<preselectedVocabularyUuids.length;i++){
171 // TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
172 // vocabularies.add(preselectedVocabulary);
173 // }
174 // }else{
175 vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
176 // }
177 //List<TermVocabulary> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
178 return vocabularies;
179 }
180
181 // /** {@inheritDoc} */
182 // @Override
183 // protected void search() {
184 // Control control =getSearchField();
185 // String pattern = null;
186 // if (control != null){
187 // pattern = ((Text)control).getText();
188 // }
189 //
190 // if (pattern == null || pattern.equals("?")){
191 // model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, null, PreferencesUtil.getGlobalLanguage());
192 // }else{
193 // model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
194 // }
195 // }
196
197 // /** {@inheritDoc} */
198 // @Override
199 // protected Control createExtendedContentArea(Composite parent) {
200 // return null;
201 // }
202
203 /** {@inheritDoc} */
204 @Override
205 protected String getTitle(NamedArea namedArea) {
206 try {
207 String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
208 return result;
209 } catch (Exception e) {
210 MessagingUtils.error(NamedAreaSelectionDialog.class, "Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid(), e);
211 return namedArea.getTitleCache();
212 }
213 }
214
215 /** {@inheritDoc} */
216 @Override
217 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
218 return null;
219 }
220
221 /** {@inheritDoc} */
222 @Override
223 protected String[] getNewWizardText() {
224 return null;
225 }
226
227 @Override
228 void createFilterButton(Composite searchAndFilter)
229 {
230 filterButton = new Button(searchAndFilter, SWT.NONE);
231 // filterButton.setText("Filter");
232 filterButton.setImage(ImageResources.getImage(ImageResources.FUNNEL_ICON));
233 // SelectionListener filterSelectionListener = new FilterSelectionListener(preferenceID, this);
234 filterButton.addSelectionListener(new SelectionListener(){
235 @Override
236 public void widgetSelected(SelectionEvent e) {
237
238 Object source = e.getSource();
239 String text = null;
240 if (source instanceof Button){
241 Shell shell = ((Button)source).getShell();
242 Dialog dialog = new FilterDialog(getShell(), preferenceID, selectedVocabularies, vocabularies);
243 if(dialog!=null){
244 dialog.open();
245 }
246 createSelectedVocabularies();
247 search();
248 }
249
250
251
252
253 }
254
255 @Override
256 public void widgetDefaultSelected(SelectionEvent e) {
257 // TODO Auto-generated method stub
258
259 }
260
261
262 });
263
264 }
265
266
267
268 /* (non-Javadoc)
269 * @see eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog#callService(java.lang.String)
270 */
271 @Override
272 void callService(String pattern) {
273 if (selectedVocabularies == null || selectedVocabularies.size() == 0){
274 model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(vocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
275 }else{
276 model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
277 }
278 }
279
280 }