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.
10 package eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
14 import java
.util
.UUID
;
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
;
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
;
44 * @created May 11, 2010
47 public class NamedAreaSelectionDialog
extends
48 AbstractFilteredCdmResourceSelectionDialog
<NamedArea
> {
50 private class IncludeNamedAreaVocabulary
extends Action
{
51 private final TermVocabulary
<NamedArea
> vocabulary
;
54 * Creates a new instance of the class.
56 public IncludeNamedAreaVocabulary(TermVocabulary
<NamedArea
> vocabulary
) {
57 super(vocabulary
.getTitleCache(), IAction
.AS_CHECK_BOX
);
58 this.vocabulary
= vocabulary
;
64 selectedVocabularies
.add(vocabulary
);
66 selectedVocabularies
.remove(vocabulary
);
68 PreferencesUtil
.setBooleanValue(getPrefKey(vocabulary
), !isChecked());
72 protected List
<NamedArea
> selectedAreas
;
73 protected List
<TermVocabulary
> selectedVocabularies
;
74 protected List
<TermVocabulary
> vocabularies
;
79 * Creates a filtered selection dialog to select a named area.
82 * The shell for displaying this widget
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.
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
);
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
);
103 selectedVocabularies
= new ArrayList
<TermVocabulary
>();
104 this.preferenceID
= preferenceId
;
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
);
112 selectedVocabularies
= createSelectedVocabularies();
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
);
125 return tempSelectedVocabularies
;
128 // private static UUID[] createVocabularyUuidList() {
129 // String preselectedVocString = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
130 // if (StringUtils.isBlank(preselectedVocString)){
133 // String[] preselectedVocArray = preselectedVocString.split(";");
134 // UUID[] uuidList = new UUID[preselectedVocArray.length];
136 // for (String uuidString: preselectedVocArray){
137 // uuidList[i]= UUID.fromString(uuidString);
144 private String
getPrefKey(TermVocabulary vocabulary
){
145 return "hide_"+NamedAreaSelectionDialog
.class.getCanonicalName()+vocabulary
.getUuid()+preferenceID
;
150 protected NamedArea
getPersistentObject(UUID uuid
) {
152 DefinedTermBase area
= CdmStore
.getService(ITermService
.class).find(uuid
);
153 if (area
instanceof NamedArea
){
154 return (NamedArea
) area
;
162 protected void init() {
163 vocabularies
= getAvailableVocabularies();
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);
177 protected String
getTitle(NamedArea namedArea
) {
179 String result
= NamedArea
.labelWithLevel(namedArea
, CdmStore
.getDefaultLanguage());
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();
189 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
195 protected String
[] getNewWizardText() {
200 void createFilterButton(Composite searchAndFilter
)
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(){
208 public void widgetSelected(SelectionEvent e
) {
210 Object source
= e
.getSource();
212 if (source
instanceof Button
){
213 Shell shell
= ((Button
)source
).getShell();
214 Dialog dialog
= new FilterDialog(getShell(), preferenceID
, selectedVocabularies
, vocabularies
);
218 createSelectedVocabularies();
228 public void widgetDefaultSelected(SelectionEvent e
) {
229 // TODO Auto-generated method stub
241 * @see eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog#callService(java.lang.String)
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
);
251 model
= CdmStore
.getService(ITermService
.class).getUuidAndTitleCacheNamedArea(vocabularies
, limitOfInitialElements
, pattern
, PreferencesUtil
.getGlobalLanguage());
254 if (abbrevType
!= null && !abbrevType
.equals(NamedAreaSearchField
.NoAbbrev
)){
255 model
= CdmStore
.getService(ITermService
.class).getUuidAndTitleCacheNamedAreaByAbbrev(selectedVocabularies
, limitOfInitialElements
, pattern
, PreferencesUtil
.getGlobalLanguage(), abbrevType
);
257 model
= CdmStore
.getService(ITermService
.class).getUuidAndTitleCacheNamedArea(selectedVocabularies
, limitOfInitialElements
, pattern
, PreferencesUtil
.getGlobalLanguage());