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
;
73 private Object preferenceID
;
77 * Creates a filtered selection dialog to select a named area.
80 * The shell for displaying this widget
82 * A namedArea that should be selected when the dialog opens
83 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
84 * @param preferenceId a class which is used for generating the preference key so that every
85 * dialogs can be grouped to have their own preferences depending on this id
86 * @param preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies
87 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
89 public static NamedArea
select(Shell shell
, ConversationHolder conversation
, 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
, String title
, boolean multi
, NamedArea namedArea
, Object preferenceId
, UUID
... preselectedVocabularyUuids
) {
96 super(shell
, conversation
, title
, multi
, NamedAreaSelectionDialog
.class.getCanonicalName(), namedArea
);
97 this.preferenceID
= preferenceId
;
98 preselectedVocabularies
= new ArrayList
<TermVocabulary
>();
99 for(int i
=0;i
<preselectedVocabularyUuids
.length
;i
++){
100 TermVocabulary preselectedVocabulary
= CdmStore
.getService(IVocabularyService
.class).find(preselectedVocabularyUuids
[i
]);
101 preselectedVocabularies
.add(preselectedVocabulary
);
103 Collection
<TermVocabulary
> tempSelectedVocabularies
= new HashSet
<>();
104 for(TermVocabulary vocabulary
:selectedVocabularies
){
105 if(preselectedVocabularies
.contains(vocabulary
)
106 || !PreferencesUtil
.getPreferenceStore().getBoolean(getPrefKey(vocabulary
))){
107 tempSelectedVocabularies
.add(vocabulary
);
110 selectedVocabularies
= tempSelectedVocabularies
;
111 initModel();//re-init to consider pre-selected vocabularies
116 protected void fillViewMenu(IMenuManager menuManager
) {
118 super.fillViewMenu(menuManager
);
120 for(TermVocabulary
<NamedArea
> vocabulary
: getAvailableVocabularies()){
121 IncludeNamedAreaVocabulary action
= new IncludeNamedAreaVocabulary(vocabulary
);
122 menuManager
.add(action
);
123 if(preselectedVocabularies
.contains(vocabulary
)) {
124 action
.setChecked(true);
127 action
.setChecked(!PreferencesUtil
.getPreferenceStore().getBoolean(getPrefKey(vocabulary
)));
132 private String
getPrefKey(TermVocabulary vocabulary
){
133 return "hide_"+NamedAreaSelectionDialog
.class.getCanonicalName()+vocabulary
.getUuid()+preferenceID
;
138 protected NamedArea
getPersistentObject(UUID uuid
) {
139 for(TermVocabulary
<NamedArea
> vocabulary
: selectedVocabularies
){
140 for(Object object
: vocabulary
.getTerms()){
141 CdmBase cdmBaseObject
= (CdmBase
) object
;
142 if(uuid
.equals(cdmBaseObject
.getUuid())){
143 return CdmBase
.deproxy(cdmBaseObject
, NamedArea
.class);
152 protected void init() {
153 selectedVocabularies
= getAvailableVocabularies();
156 private List
<TermVocabulary
> getAvailableVocabularies(){
157 List
<TermVocabulary
> vocabularies
= CdmStore
.getService(IVocabularyService
.class).listByTermType(TermType
.NamedArea
, true, null, null, null, null);
163 protected void initModel() {
165 Set
<NamedArea
> terms
= new HashSet
<NamedArea
>();
166 for(TermVocabulary
<NamedArea
> vocabulary
: selectedVocabularies
){
167 terms
.addAll(vocabulary
.getTermsOrderedByLabels(CdmStore
.getDefaultLanguage()));
171 model
= new ArrayList
<UuidAndTitleCache
<NamedArea
>>();
174 for(Object areaObject
: terms
){
175 NamedArea area
= (NamedArea
) HibernateProxyHelper
.deproxy(areaObject
);
176 UuidAndTitleCache
<NamedArea
> element
= new UuidAndTitleCache
<NamedArea
>(NamedArea
.class, area
.getUuid(), area
.getId(), getTitle(area
));
183 protected Control
createExtendedContentArea(Composite parent
) {
189 protected String
getTitle(NamedArea namedArea
) {
191 String result
= NamedArea
.labelWithLevel(namedArea
, CdmStore
.getDefaultLanguage());
193 } catch (Exception e
) {
194 MessagingUtils
.error(NamedAreaSelectionDialog
.class, "Error occurred when trying retrieve title for Named Area: " + namedArea
.getUuid(), e
);
195 return namedArea
.getTitleCache();
201 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
207 protected String
getNewWizardLinkText() {