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
.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
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
28 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
30 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
31 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
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
;
41 * @created May 11, 2010
44 public class NamedAreaSelectionDialog
extends
45 AbstractFilteredCdmResourceSelectionDialog
<NamedArea
> {
47 private class IncludeNamedAreaVocabulary
extends Action
{
48 private final TermVocabulary
<NamedArea
> vocabulary
;
51 * Creates a new instance of the class.
53 public IncludeNamedAreaVocabulary(TermVocabulary
<NamedArea
> vocabulary
) {
54 super(vocabulary
.getTitleCache(), IAction
.AS_CHECK_BOX
);
55 this.vocabulary
= vocabulary
;
61 selectedVocabularies
.add(vocabulary
);
63 selectedVocabularies
.remove(vocabulary
);
65 PreferencesUtil
.setBooleanValue(getPrefKey(vocabulary
), !isChecked());
70 protected List
<TermVocabulary
> selectedVocabularies
;
71 protected List
<TermVocabulary
> vocabularies
;
76 * Creates a filtered selection dialog to select a named area.
79 * The shell for displaying this widget
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.
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
);
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
);
100 selectedVocabularies
= new ArrayList
<TermVocabulary
>();
101 this.preferenceID
= preferenceId
;
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
);
109 selectedVocabularies
= createSelectedVocabularies();
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
);
122 return tempSelectedVocabularies
;
125 // private static UUID[] createVocabularyUuidList() {
126 // String preselectedVocString = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
127 // if (StringUtils.isBlank(preselectedVocString)){
130 // String[] preselectedVocArray = preselectedVocString.split(";");
131 // UUID[] uuidList = new UUID[preselectedVocArray.length];
133 // for (String uuidString: preselectedVocArray){
134 // uuidList[i]= UUID.fromString(uuidString);
141 private String
getPrefKey(TermVocabulary vocabulary
){
142 return "hide_"+NamedAreaSelectionDialog
.class.getCanonicalName()+vocabulary
.getUuid()+preferenceID
;
147 protected NamedArea
getPersistentObject(UUID uuid
) {
149 DefinedTermBase area
= CdmStore
.getService(ITermService
.class).find(uuid
);
150 if (area
instanceof NamedArea
){
151 return (NamedArea
) area
;
159 protected void init() {
160 vocabularies
= getAvailableVocabularies();
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();
170 // for(int i=0;i<preselectedVocabularyUuids.length;i++){
171 // TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
172 // vocabularies.add(preselectedVocabulary);
175 vocabularies
= CdmStore
.getService(IVocabularyService
.class).listByTermType(TermType
.NamedArea
, true, null, null, null, null);
177 //List<TermVocabulary> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
181 // /** {@inheritDoc} */
183 // protected void search() {
184 // Control control =getSearchField();
185 // String pattern = null;
186 // if (control != null){
187 // pattern = ((Text)control).getText();
190 // if (pattern == null || pattern.equals("?")){
191 // model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, null, PreferencesUtil.getGlobalLanguage());
193 // model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
197 // /** {@inheritDoc} */
199 // protected Control createExtendedContentArea(Composite parent) {
205 protected String
getTitle(NamedArea namedArea
) {
207 String result
= NamedArea
.labelWithLevel(namedArea
, CdmStore
.getDefaultLanguage());
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();
217 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
223 protected String
[] getNewWizardText() {
228 void createFilterButton(Composite searchAndFilter
)
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(){
236 public void widgetSelected(SelectionEvent e
) {
238 Object source
= e
.getSource();
240 if (source
instanceof Button
){
241 Shell shell
= ((Button
)source
).getShell();
242 Dialog dialog
= new FilterDialog(getShell(), preferenceID
, selectedVocabularies
, vocabularies
);
246 createSelectedVocabularies();
256 public void widgetDefaultSelected(SelectionEvent e
) {
257 // TODO Auto-generated method stub
269 * @see eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog#callService(java.lang.String)
272 void callService(String pattern
) {
273 if (selectedVocabularies
== null || selectedVocabularies
.size() == 0){
274 model
= CdmStore
.getService(ITermService
.class).getUuidAndTitleCache(vocabularies
, limitOfInitialElements
, pattern
, PreferencesUtil
.getGlobalLanguage());
276 model
= CdmStore
.getService(ITermService
.class).getUuidAndTitleCache(selectedVocabularies
, limitOfInitialElements
, pattern
, PreferencesUtil
.getGlobalLanguage());