2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
;
11 import java
.util
.ArrayList
;
12 import java
.util
.List
;
13 import java
.util
.UUID
;
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
;
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
;
43 * @created May 11, 2010
45 public class NamedAreaSelectionDialog
extends
46 AbstractFilteredCdmResourceSelectionDialog
<NamedArea
> {
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
;
56 * Creates a filtered selection dialog to select a named area.
59 * The shell for displaying this widget
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.
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
);
74 protected NamedAreaSelectionDialog(Shell shell
,
75 String title
, boolean multi
, NamedArea namedArea
, Object preferenceId
, UUID
... preselectedVocabularyUuids
) {
77 super(shell
, title
, multi
, NamedAreaSelectionDialog
.class.getCanonicalName(), namedArea
);
79 selectedTermCollections
= new ArrayList
<>();
80 this.preferenceID
= preferenceId
;
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
);
88 selectedTermCollections
= createSelectedVocabularies();
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
);
101 return tempSelectedVocabularies
;
104 private String
getPrefKey(TermCollection
<?
,?
> termCollection
){
105 return "hide_"+NamedAreaSelectionDialog
.class.getCanonicalName()+termCollection
.getUuid()+preferenceID
;
109 protected NamedArea
getPersistentObject(UUID uuid
) {
111 DefinedTermBase
<?
> area
= CdmStore
.getService(ITermService
.class).find(uuid
);
112 if (area
instanceof NamedArea
){
113 return (NamedArea
) area
;
120 protected void init() {
121 getAvailableVocabularies();
124 private void getAvailableVocabularies(){
125 allVocabularies
= new ArrayList
<>();
126 allVocabularies
.addAll(CdmStore
.getService(IVocabularyService
.class).listByTermType(TermType
.NamedArea
, true, null, null, null, null));
128 allTrees
= CdmStore
.getService(ITermTreeService
.class).list(TermType
.NamedArea
, null, null, null, null);
129 if (allTrees
== null) {
130 allTrees
= new ArrayList
<>();
135 protected String
getTitle(NamedArea namedArea
) {
137 String result
= NamedArea
.labelWithLevel(namedArea
, CdmStore
.getDefaultLanguage());
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();
146 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
151 protected String
[] getNewWizardText() {
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(){
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
);
170 Dialog dialog
= new FilterDialog(getShell(), preferenceID
, selectedTermCollections
, allCollections
);
172 createSelectedVocabularies();
178 public void widgetDefaultSelected(SelectionEvent e
) {
179 // TODO Auto-generated method stub
185 protected void callService(String pattern
) {
187 String abbrevTypeString
= PreferencesUtil
.getStringValue(PreferencePredicate
.NamedAreaSearchForAbbrev
.getKey());
188 TermSearchField abbrevType
;
190 abbrevType
= StringUtils
.isNotBlank(abbrevTypeString
)? TermSearchField
.byKey(abbrevTypeString
): null;
191 }catch(IllegalArgumentException e
){
195 model
= CdmStore
.getService(ITermService
.class).getUuidAndTitleCache(NamedArea
.class, selectedTermCollections
, limitOfInitialElements
, pattern
, PreferencesUtil
.getGlobalLanguage(), abbrevType
);