ref #10284: adapt for no termtrees
[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 package eu.etaxonomy.taxeditor.ui.dialog.selection;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.UUID;
14
15 import org.apache.commons.lang3.StringUtils;
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Shell;
23
24 import eu.etaxonomy.cdm.api.service.ITermService;
25 import eu.etaxonomy.cdm.api.service.ITermTreeService;
26 import eu.etaxonomy.cdm.api.service.IVocabularyService;
27 import eu.etaxonomy.cdm.model.location.NamedArea;
28 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
29 import eu.etaxonomy.cdm.model.metadata.TermSearchField;
30 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
31 import eu.etaxonomy.cdm.model.term.TermCollection;
32 import eu.etaxonomy.cdm.model.term.TermTree;
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.PreferencesUtil;
39 import eu.etaxonomy.taxeditor.store.CdmStore;
40
41 /**
42 * @author n.hoffmann
43 * @created May 11, 2010
44 */
45 public class NamedAreaSelectionDialog extends
46 AbstractFilteredCdmResourceSelectionDialog<NamedArea> {
47
48 protected List<NamedArea> selectedAreas;
49 @SuppressWarnings("rawtypes")
50 protected List<TermCollection> selectedTermCollections;
51 @SuppressWarnings("rawtypes")
52 protected List<TermVocabulary> allVocabularies;
53 protected List<TermTree> allTrees ;
54
55 /**
56 * Creates a filtered selection dialog to select a named area.
57 *
58 * @param shell
59 * The shell for displaying this widget
60 * @param namedArea
61 * A namedArea that should be selected when the dialog opens
62 * @param preferenceId a class which is used for generating the preference key so that every
63 * dialogs can be grouped to have their own preferences depending on this id
64 * @param preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies
65 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
66 */
67 public static NamedArea select(Shell shell,
68 NamedArea namedArea, String preferenceId, UUID... preselectedVocabularyUuids) {
69 NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell,
70 "Choose an area", false, namedArea, preferenceId, preselectedVocabularyUuids);
71 return getSelectionFromDialog(dialog);
72 }
73
74 protected NamedAreaSelectionDialog(Shell shell,
75 String title, boolean multi, NamedArea namedArea, Object preferenceId, UUID... preselectedVocabularyUuids) {
76
77 super(shell, title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
78
79 selectedTermCollections = new ArrayList<>();
80 this.preferenceID = preferenceId;
81
82 if (preselectedVocabularyUuids != null && preselectedVocabularyUuids.length > 0){
83 for(UUID preselectedVocabularyUuid : preselectedVocabularyUuids){
84 TermCollection<?,?> preselectedVocabulary = CdmStore.getService(IVocabularyService.class).findCollection(preselectedVocabularyUuid);
85 selectedTermCollections.add(preselectedVocabulary);
86 }
87 }else{
88 selectedTermCollections = createSelectedVocabularies();
89 }
90 }
91
92 protected List<TermCollection> createSelectedVocabularies() {
93 @SuppressWarnings("rawtypes")
94 List<TermCollection> tempSelectedVocabularies = new ArrayList<>();
95 for(TermCollection<?,?> vocabulary: allVocabularies){
96 if((selectedTermCollections.contains(vocabulary) && !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary)))
97 || !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary))){
98 tempSelectedVocabularies.add(vocabulary);
99 }
100 }
101 return tempSelectedVocabularies;
102 }
103
104 private String getPrefKey(TermCollection<?,?> termCollection){
105 return "hide_"+NamedAreaSelectionDialog.class.getCanonicalName()+termCollection.getUuid()+preferenceID;
106 }
107
108 @Override
109 protected NamedArea getPersistentObject(UUID uuid) {
110
111 DefinedTermBase<?> area = CdmStore.getService(ITermService.class).find(uuid);
112 if (area instanceof NamedArea){
113 return (NamedArea) area;
114 }
115
116 return null;
117 }
118
119 @Override
120 protected void init() {
121 getAvailableVocabularies();
122 }
123
124 private void getAvailableVocabularies(){
125 allVocabularies = new ArrayList<>();
126 allVocabularies.addAll(CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null));
127
128 allTrees = CdmStore.getService(ITermTreeService.class).list(TermType.NamedArea, null, null, null, null);
129 if (allTrees == null) {
130 allTrees = new ArrayList<>();
131 }
132 }
133
134 @Override
135 protected String getTitle(NamedArea namedArea) {
136 try {
137 String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
138 return result;
139 } catch (Exception e) {
140 MessagingUtils.error(NamedAreaSelectionDialog.class, "Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid(), e);
141 return namedArea.getTitleCache();
142 }
143 }
144
145 @Override
146 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
147 return null;
148 }
149
150 @Override
151 protected String[] getNewWizardText() {
152 return null;
153 }
154
155 @Override
156 void createFilterButton(Composite searchAndFilter){
157 filterButton = new Button(searchAndFilter, SWT.NONE);
158 filterButton.setImage(ImageResources.getImage(ImageResources.FUNNEL_ICON));
159 filterButton.addSelectionListener(new SelectionListener(){
160 @Override
161 public void widgetSelected(SelectionEvent e) {
162 Object source = e.getSource();
163 if (source instanceof Button){
164 List<TermCollection> allCollections = new ArrayList<>();
165 allCollections.addAll(allVocabularies);
166 if (allTrees!= null) {
167 allCollections.addAll(allTrees);
168 }
169
170 Dialog dialog = new FilterDialog(getShell(), preferenceID, selectedTermCollections, allCollections);
171 dialog.open();
172 createSelectedVocabularies();
173 search();
174 }
175 }
176
177 @Override
178 public void widgetDefaultSelected(SelectionEvent e) {
179 // TODO Auto-generated method stub
180 }
181 });
182 }
183
184 @Override
185 protected void callService(String pattern) {
186 //label type
187 String abbrevTypeString = PreferencesUtil.getStringValue(PreferencePredicate.NamedAreaSearchForAbbrev.getKey());
188 TermSearchField abbrevType;
189 try{
190 abbrevType = StringUtils.isNotBlank(abbrevTypeString)? TermSearchField.byKey(abbrevTypeString): null;
191 }catch(IllegalArgumentException e){
192 abbrevType = null;
193 }
194
195 model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(NamedArea.class, selectedTermCollections, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage(), abbrevType);
196 }
197 }