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
.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
);
69 private Collection
<TermVocabulary
> selectedVocabularies
;
70 private ArrayList
<TermVocabulary
> preselectedVocabularies
;
74 * Creates a filtered selection dialog to select a named area.
77 * The shell for displaying this widget
79 * A namedArea that should be selected when the dialog opens
80 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
81 * @param preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies
82 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
84 public static NamedArea
select(Shell shell
, ConversationHolder conversation
, NamedArea namedArea
, UUID
... preselectedVocabularyUuids
) {
85 NamedAreaSelectionDialog dialog
= new NamedAreaSelectionDialog(shell
, conversation
,
86 "Choose an area", false, namedArea
, preselectedVocabularyUuids
);
87 return getSelectionFromDialog(dialog
);
90 protected NamedAreaSelectionDialog(Shell shell
, ConversationHolder conversation
, String title
, boolean multi
, NamedArea namedArea
, UUID
... preselectedVocabularyUuids
) {
91 super(shell
, conversation
, title
, multi
, NamedAreaSelectionDialog
.class.getCanonicalName(), namedArea
);
92 if(preselectedVocabularyUuids
.length
>0){
93 preselectedVocabularies
= new ArrayList
<TermVocabulary
>();
94 for(int i
=0;i
<preselectedVocabularyUuids
.length
;i
++){
95 TermVocabulary preselectedVocabulary
= CdmStore
.getService(IVocabularyService
.class).find(preselectedVocabularyUuids
[i
]);
96 preselectedVocabularies
.add(preselectedVocabulary
);
98 selectedVocabularies
.retainAll(preselectedVocabularies
);
99 initModel();//re-init to consider pre-selected vocabularies
105 protected void fillViewMenu(IMenuManager menuManager
) {
107 super.fillViewMenu(menuManager
);
109 for(TermVocabulary
<NamedArea
> vocabulary
: getAvailableVocabularies()){
110 IncludeNamedAreaVocabulary action
= new IncludeNamedAreaVocabulary(vocabulary
);
111 menuManager
.add(action
);
112 if(preselectedVocabularies
==null || preselectedVocabularies
.isEmpty() || preselectedVocabularies
.contains(vocabulary
)) {
113 action
.setChecked(true);
120 protected NamedArea
getPersistentObject(UUID uuid
) {
121 for(TermVocabulary
<NamedArea
> vocabulary
: selectedVocabularies
){
122 for(Object object
: vocabulary
.getTerms()){
123 CdmBase cdmBaseObject
= (CdmBase
) object
;
124 if(uuid
.equals(cdmBaseObject
.getUuid())){
125 return CdmBase
.deproxy(cdmBaseObject
, NamedArea
.class);
134 protected void init() {
135 selectedVocabularies
= getAvailableVocabularies();
138 private List
<TermVocabulary
> getAvailableVocabularies(){
139 List
<TermVocabulary
> vocabularies
= CdmStore
.getService(IVocabularyService
.class).listByTermType(TermType
.NamedArea
, true, null, null, null, null);
145 protected void initModel() {
147 Set
<NamedArea
> terms
= new HashSet
<NamedArea
>();
148 for(TermVocabulary
<NamedArea
> vocabulary
: selectedVocabularies
){
149 terms
.addAll(vocabulary
.getTermsOrderedByLabels(CdmStore
.getDefaultLanguage()));
153 model
= new ArrayList
<UuidAndTitleCache
<NamedArea
>>();
156 for(Object areaObject
: terms
){
157 NamedArea area
= (NamedArea
) HibernateProxyHelper
.deproxy(areaObject
);
158 UuidAndTitleCache
<NamedArea
> element
= new UuidAndTitleCache
<NamedArea
>(NamedArea
.class, area
.getUuid(), area
.getId(), getTitle(area
));
165 protected Control
createExtendedContentArea(Composite parent
) {
171 protected String
getTitle(NamedArea namedArea
) {
173 String result
= NamedArea
.labelWithLevel(namedArea
, CdmStore
.getDefaultLanguage());
175 } catch (Exception e
) {
176 MessagingUtils
.error(NamedAreaSelectionDialog
.class, "Error occurred when trying retrieve title for Named Area: " + namedArea
.getUuid(), e
);
177 return namedArea
.getTitleCache();
183 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
189 protected String
getNewWizardLinkText() {