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
.action
.IMenuManager
;
19 import org
.eclipse
.swt
.widgets
.Composite
;
20 import org
.eclipse
.swt
.widgets
.Control
;
21 import org
.eclipse
.swt
.widgets
.Shell
;
22 import org
.eclipse
.swt
.widgets
.Text
;
24 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
29 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
30 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
31 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
32 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
33 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
34 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
38 * @created May 11, 2010
41 public class NamedAreaSelectionDialog
extends
42 AbstractFilteredCdmResourceSelectionDialog
<NamedArea
> {
44 private class IncludeNamedAreaVocabulary
extends Action
{
45 private final TermVocabulary
<NamedArea
> vocabulary
;
48 * Creates a new instance of the class.
50 public IncludeNamedAreaVocabulary(TermVocabulary
<NamedArea
> vocabulary
) {
51 super(vocabulary
.getTitleCache(), IAction
.AS_CHECK_BOX
);
52 this.vocabulary
= vocabulary
;
58 selectedVocabularies
.add(vocabulary
);
60 selectedVocabularies
.remove(vocabulary
);
62 PreferencesUtil
.getPreferenceStore().setValue(getPrefKey(vocabulary
), !isChecked());
67 protected List
<TermVocabulary
> selectedVocabularies
;
68 protected ArrayList
<TermVocabulary
> preselectedVocabularies
;
69 private Object preferenceID
;
73 * Creates a filtered selection dialog to select a named area.
76 * The shell for displaying this widget
78 * A namedArea that should be selected when the dialog opens
79 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
80 * @param preferenceId a class which is used for generating the preference key so that every
81 * dialogs can be grouped to have their own preferences depending on this id
82 * @param preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies
83 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
85 public static NamedArea
select(Shell shell
, ConversationHolder conversation
, NamedArea namedArea
, String preferenceId
, UUID
... preselectedVocabularyUuids
) {
86 NamedAreaSelectionDialog dialog
= new NamedAreaSelectionDialog(shell
, conversation
,
87 "Choose an area", false, namedArea
, preferenceId
, preselectedVocabularyUuids
);
88 return getSelectionFromDialog(dialog
);
91 protected NamedAreaSelectionDialog(Shell shell
, ConversationHolder conversation
, String title
, boolean multi
, NamedArea namedArea
, Object preferenceId
, UUID
... preselectedVocabularyUuids
) {
92 super(shell
, conversation
, title
, multi
, NamedAreaSelectionDialog
.class.getCanonicalName(), namedArea
);
93 this.preferenceID
= preferenceId
;
94 preselectedVocabularies
= new ArrayList
<TermVocabulary
>();
95 for(int i
=0;i
<preselectedVocabularyUuids
.length
;i
++){
96 TermVocabulary preselectedVocabulary
= CdmStore
.getService(IVocabularyService
.class).find(preselectedVocabularyUuids
[i
]);
97 preselectedVocabularies
.add(preselectedVocabulary
);
99 List
<TermVocabulary
> tempSelectedVocabularies
= new ArrayList
<>();
100 for(TermVocabulary vocabulary
:selectedVocabularies
){
101 if(preselectedVocabularies
.contains(vocabulary
)
102 || !PreferencesUtil
.getPreferenceStore().getBoolean(getPrefKey(vocabulary
))){
103 tempSelectedVocabularies
.add(vocabulary
);
106 selectedVocabularies
= tempSelectedVocabularies
;
107 // search;//re-init to consider pre-selected vocabularies
112 protected void fillViewMenu(IMenuManager menuManager
) {
114 // super.fillViewMenu(menuManager);
116 for(TermVocabulary
<NamedArea
> vocabulary
: getAvailableVocabularies()){
117 IncludeNamedAreaVocabulary action
= new IncludeNamedAreaVocabulary(vocabulary
);
118 menuManager
.add(action
);
119 if(preselectedVocabularies
.contains(vocabulary
)) {
120 action
.setChecked(true);
123 action
.setChecked(!PreferencesUtil
.getPreferenceStore().getBoolean(getPrefKey(vocabulary
)));
128 private String
getPrefKey(TermVocabulary vocabulary
){
129 return "hide_"+NamedAreaSelectionDialog
.class.getCanonicalName()+vocabulary
.getUuid()+preferenceID
;
134 protected NamedArea
getPersistentObject(UUID uuid
) {
135 for(TermVocabulary
<NamedArea
> vocabulary
: selectedVocabularies
){
136 for(Object object
: vocabulary
.getTerms()){
137 CdmBase cdmBaseObject
= (CdmBase
) object
;
138 if(uuid
.equals(cdmBaseObject
.getUuid())){
139 return CdmBase
.deproxy(cdmBaseObject
, NamedArea
.class);
148 protected void init() {
149 selectedVocabularies
= getAvailableVocabularies();
152 private List
<TermVocabulary
> getAvailableVocabularies(){
153 List
<TermVocabulary
> vocabularies
= CdmStore
.getService(IVocabularyService
.class).listByTermType(TermType
.NamedArea
, true, null, null, null, null);
159 protected void search() {
160 Control control
=getSearchField();
161 String pattern
= null;
162 if (control
!= null){
163 pattern
= ((Text
)control
).getText();
166 if (pattern
== null || pattern
.equals("?")){
167 model
= CdmStore
.getService(ITermService
.class).getUuidAndTitleCache(selectedVocabularies
, limitOfInitialElements
, null, PreferencesUtil
.getGlobalLanguage());
169 model
= CdmStore
.getService(ITermService
.class).getUuidAndTitleCache(selectedVocabularies
, limitOfInitialElements
, pattern
, PreferencesUtil
.getGlobalLanguage());
172 // Set<NamedArea> terms = new HashSet<NamedArea>();
173 // for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
174 // terms.addAll(vocabulary.getTermsOrderedByLabels(CdmStore.getDefaultLanguage()));
177 // if(model == null){
178 // model = new ArrayList<UuidAndTitleCache<NamedArea>>();
181 // for(Object areaObject : terms){
182 // NamedArea area = (NamedArea) HibernateProxyHelper.deproxy(areaObject);
183 // UuidAndTitleCache<NamedArea> element = new UuidAndTitleCache<NamedArea>(NamedArea.class, area.getUuid(), area.getId(), getTitle(area));
184 // model.add(element);
190 protected Control
createExtendedContentArea(Composite parent
) {
196 protected String
getTitle(NamedArea namedArea
) {
198 String result
= NamedArea
.labelWithLevel(namedArea
, CdmStore
.getDefaultLanguage());
200 } catch (Exception e
) {
201 MessagingUtils
.error(NamedAreaSelectionDialog
.class, "Error occurred when trying retrieve title for Named Area: " + namedArea
.getUuid(), e
);
202 return namedArea
.getTitleCache();
208 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
214 protected String
getNewWizardLinkText() {