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
.Collection
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.eclipse
.jface
.action
.Action
;
20 import org
.eclipse
.jface
.action
.IAction
;
21 import org
.eclipse
.jface
.action
.IMenuManager
;
22 import org
.eclipse
.swt
.widgets
.Composite
;
23 import org
.eclipse
.swt
.widgets
.Control
;
24 import org
.eclipse
.swt
.widgets
.Shell
;
26 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
28 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
31 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
32 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
33 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
34 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
35 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
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
.getPreferenceStore().setValue(getPrefKey(vocabulary
), !isChecked());
70 protected Collection
<TermVocabulary
> selectedVocabularies
;
71 protected ArrayList
<TermVocabulary
> preselectedVocabularies
;
72 private Object preferenceID
;
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
, NamedArea namedArea
, String preferenceId
, UUID
... preselectedVocabularyUuids
) {
89 NamedAreaSelectionDialog dialog
= new NamedAreaSelectionDialog(shell
, conversation
,
90 "Choose an area", false, namedArea
, preferenceId
, preselectedVocabularyUuids
);
91 return getSelectionFromDialog(dialog
);
94 protected NamedAreaSelectionDialog(Shell shell
, ConversationHolder conversation
, String title
, boolean multi
, NamedArea namedArea
, Object preferenceId
, UUID
... preselectedVocabularyUuids
) {
95 super(shell
, conversation
, title
, multi
, NamedAreaSelectionDialog
.class.getCanonicalName(), namedArea
);
96 this.preferenceID
= preferenceId
;
97 preselectedVocabularies
= new ArrayList
<TermVocabulary
>();
98 for(int i
=0;i
<preselectedVocabularyUuids
.length
;i
++){
99 TermVocabulary preselectedVocabulary
= CdmStore
.getService(IVocabularyService
.class).find(preselectedVocabularyUuids
[i
]);
100 preselectedVocabularies
.add(preselectedVocabulary
);
102 Collection
<TermVocabulary
> tempSelectedVocabularies
= new HashSet
<>();
103 for(TermVocabulary vocabulary
:selectedVocabularies
){
104 if(preselectedVocabularies
.contains(vocabulary
)
105 || !PreferencesUtil
.getPreferenceStore().getBoolean(getPrefKey(vocabulary
))){
106 tempSelectedVocabularies
.add(vocabulary
);
109 selectedVocabularies
= tempSelectedVocabularies
;
110 initModel();//re-init to consider pre-selected vocabularies
115 protected void fillViewMenu(IMenuManager menuManager
) {
117 super.fillViewMenu(menuManager
);
119 for(TermVocabulary
<NamedArea
> vocabulary
: getAvailableVocabularies()){
120 IncludeNamedAreaVocabulary action
= new IncludeNamedAreaVocabulary(vocabulary
);
121 menuManager
.add(action
);
122 if(preselectedVocabularies
.contains(vocabulary
)) {
123 action
.setChecked(true);
126 action
.setChecked(!PreferencesUtil
.getPreferenceStore().getBoolean(getPrefKey(vocabulary
)));
131 private String
getPrefKey(TermVocabulary vocabulary
){
132 return "hide_"+NamedAreaSelectionDialog
.class.getCanonicalName()+vocabulary
.getUuid()+preferenceID
;
137 protected NamedArea
getPersistentObject(UUID uuid
) {
138 for(TermVocabulary
<NamedArea
> vocabulary
: selectedVocabularies
){
139 for(Object object
: vocabulary
.getTerms()){
140 CdmBase cdmBaseObject
= (CdmBase
) object
;
141 if(uuid
.equals(cdmBaseObject
.getUuid())){
142 return CdmBase
.deproxy(cdmBaseObject
, NamedArea
.class);
151 protected void init() {
152 selectedVocabularies
= getAvailableVocabularies();
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() {