3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collection
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import org
.eclipse
.jface
.action
.Action
;
21 import org
.eclipse
.jface
.action
.IAction
;
22 import org
.eclipse
.jface
.action
.IMenuManager
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.Shell
;
27 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
28 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
29 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
32 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
33 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
34 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
35 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
36 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
37 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
38 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
42 * @created May 11, 2010
45 public class NamedAreaSelectionDialog
extends
46 AbstractFilteredCdmResourceSelectionDialog
<NamedArea
> {
48 private class IncludeNamedAreaVocabulary
extends Action
{
49 private final TermVocabulary
<NamedArea
> vocabulary
;
52 * Creates a new instance of the class.
54 public IncludeNamedAreaVocabulary(TermVocabulary
<NamedArea
> vocabulary
) {
55 super(vocabulary
.getTitleCache(), IAction
.AS_CHECK_BOX
);
56 this.vocabulary
= vocabulary
;
62 selectedVocabularies
.add(vocabulary
);
64 selectedVocabularies
.remove(vocabulary
);
66 PreferencesUtil
.getPreferenceStore().setValue(getPrefKey(vocabulary
), isChecked());
71 protected Collection
<TermVocabulary
> selectedVocabularies
;
72 protected ArrayList
<TermVocabulary
> preselectedVocabularies
;
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 preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies
84 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
86 public static NamedArea
select(Shell shell
, ConversationHolder conversation
, NamedArea namedArea
, UUID
... preselectedVocabularyUuids
) {
87 NamedAreaSelectionDialog dialog
= new NamedAreaSelectionDialog(shell
, conversation
,
88 "Choose an area", false, namedArea
, preselectedVocabularyUuids
);
89 return getSelectionFromDialog(dialog
);
92 protected NamedAreaSelectionDialog(Shell shell
, ConversationHolder conversation
, String title
, boolean multi
, NamedArea namedArea
, UUID
... preselectedVocabularyUuids
) {
93 super(shell
, conversation
, title
, multi
, NamedAreaSelectionDialog
.class.getCanonicalName(), namedArea
);
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 Collection
<TermVocabulary
> tempSelectedVocabularies
= new HashSet
<>();
100 for(TermVocabulary vocabulary
:selectedVocabularies
){
101 if(preselectedVocabularies
.contains(vocabulary
)
102 || PreferencesUtil
.getPreferenceStore().getBoolean(getPrefKey(vocabulary
))){
103 tempSelectedVocabularies
.add(vocabulary
);
106 selectedVocabularies
= tempSelectedVocabularies
;
107 initModel();//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 NamedAreaSelectionDialog
.class.toString()+vocabulary
.getUuid();
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();
150 for(TermVocabulary vocabulary
:selectedVocabularies
){
151 PreferencesUtil
.getPreferenceStore().setDefault(getPrefKey(vocabulary
), true);
155 private List
<TermVocabulary
> getAvailableVocabularies(){
156 List
<TermVocabulary
> vocabularies
= CdmStore
.getService(IVocabularyService
.class).listByTermType(TermType
.NamedArea
, true, null, null, null, null);
162 protected void initModel() {
164 Set
<NamedArea
> terms
= new HashSet
<NamedArea
>();
165 for(TermVocabulary
<NamedArea
> vocabulary
: selectedVocabularies
){
166 terms
.addAll(vocabulary
.getTermsOrderedByLabels(CdmStore
.getDefaultLanguage()));
170 model
= new ArrayList
<UuidAndTitleCache
<NamedArea
>>();
173 for(Object areaObject
: terms
){
174 NamedArea area
= (NamedArea
) HibernateProxyHelper
.deproxy(areaObject
);
175 UuidAndTitleCache
<NamedArea
> element
= new UuidAndTitleCache
<NamedArea
>(NamedArea
.class, area
.getUuid(), area
.getId(), getTitle(area
));
182 protected Control
createExtendedContentArea(Composite parent
) {
188 protected String
getTitle(NamedArea namedArea
) {
190 String result
= NamedArea
.labelWithLevel(namedArea
, CdmStore
.getDefaultLanguage());
192 } catch (Exception e
) {
193 MessagingUtils
.error(NamedAreaSelectionDialog
.class, "Error occurred when trying retrieve title for Named Area: " + namedArea
.getUuid(), e
);
194 return namedArea
.getTitleCache();
200 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
206 protected String
getNewWizardLinkText() {