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
.core
.runtime
.CoreException
;
17 import org
.eclipse
.jface
.action
.Action
;
18 import org
.eclipse
.jface
.action
.IAction
;
19 import org
.eclipse
.jface
.dialogs
.Dialog
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.SelectionEvent
;
22 import org
.eclipse
.swt
.events
.SelectionListener
;
23 import org
.eclipse
.swt
.widgets
.Button
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Control
;
26 import org
.eclipse
.swt
.widgets
.Shell
;
27 import org
.eclipse
.swt
.widgets
.Text
;
28 import org
.eclipse
.wb
.swt
.ResourceManager
;
30 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
31 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
34 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
35 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
36 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
37 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
38 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
39 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
43 * @created May 11, 2010
46 public class NamedAreaSelectionDialog
extends
47 AbstractFilteredCdmResourceSelectionDialog
<NamedArea
> {
49 private class IncludeNamedAreaVocabulary
extends Action
{
50 private final TermVocabulary
<NamedArea
> vocabulary
;
53 * Creates a new instance of the class.
55 public IncludeNamedAreaVocabulary(TermVocabulary
<NamedArea
> vocabulary
) {
56 super(vocabulary
.getTitleCache(), IAction
.AS_CHECK_BOX
);
57 this.vocabulary
= vocabulary
;
63 selectedVocabularies
.add(vocabulary
);
65 selectedVocabularies
.remove(vocabulary
);
67 PreferencesUtil
.getPreferenceStore().setValue(getPrefKey(vocabulary
), !isChecked());
72 protected List
<TermVocabulary
> selectedVocabularies
;
73 protected ArrayList
<TermVocabulary
> preselectedVocabularies
;
78 * Creates a filtered selection dialog to select a named area.
81 * The shell for displaying this widget
83 * A namedArea that should be selected when the dialog opens
84 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
85 * @param preferenceId a class which is used for generating the preference key so that every
86 * dialogs can be grouped to have their own preferences depending on this id
87 * @param preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies
88 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
90 public static NamedArea
select(Shell shell
, //ConversationHolder conversation,
91 NamedArea namedArea
, String preferenceId
, UUID
... preselectedVocabularyUuids
) {
92 NamedAreaSelectionDialog dialog
= new NamedAreaSelectionDialog(shell
, //conversation,
93 "Choose an area", false, namedArea
, preferenceId
, preselectedVocabularyUuids
);
94 return getSelectionFromDialog(dialog
);
97 protected NamedAreaSelectionDialog(Shell shell
, //ConversationHolder conversation,
98 String title
, boolean multi
, NamedArea namedArea
, Object preferenceId
, UUID
... preselectedVocabularyUuids
) {
99 super(shell
, //conversation,
100 title
, multi
, NamedAreaSelectionDialog
.class.getCanonicalName(), namedArea
);
101 this.preferenceID
= preferenceId
;
102 preselectedVocabularies
= new ArrayList
<TermVocabulary
>();
103 for(int i
=0;i
<preselectedVocabularyUuids
.length
;i
++){
104 TermVocabulary preselectedVocabulary
= CdmStore
.getService(IVocabularyService
.class).find(preselectedVocabularyUuids
[i
]);
105 preselectedVocabularies
.add(preselectedVocabulary
);
109 selectedVocabularies
= createSelectedVocabularies();;
110 // search;//re-init to consider pre-selected vocabularies
113 private List
<TermVocabulary
> createSelectedVocabularies() {
114 List
<TermVocabulary
> tempSelectedVocabularies
= new ArrayList
<TermVocabulary
>();
115 for(TermVocabulary vocabulary
:selectedVocabularies
){
116 if(preselectedVocabularies
.contains(vocabulary
)
117 || !PreferencesUtil
.getPreferenceStore().getBoolean(getPrefKey(vocabulary
))){
118 tempSelectedVocabularies
.add(vocabulary
);
121 return tempSelectedVocabularies
;
126 // protected void fillViewMenu(IMenuManager menuManager) {
128 //// super.fillViewMenu(menuManager);
130 // for(TermVocabulary<NamedArea> vocabulary : getAvailableVocabularies()){
131 // IncludeNamedAreaVocabulary action = new IncludeNamedAreaVocabulary(vocabulary);
132 // menuManager.add(action);
133 // if(preselectedVocabularies.contains(vocabulary)) {
134 // action.setChecked(true);
137 // action.setChecked(!PreferencesUtil.getPreferenceStore().getBoolean(getPrefKey(vocabulary)));
142 private String
getPrefKey(TermVocabulary vocabulary
){
143 return "hide_"+NamedAreaSelectionDialog
.class.getCanonicalName()+vocabulary
.getUuid()+preferenceID
;
148 protected NamedArea
getPersistentObject(UUID uuid
) {
149 for(TermVocabulary
<NamedArea
> vocabulary
: selectedVocabularies
){
150 for(Object object
: vocabulary
.getTerms()){
151 CdmBase cdmBaseObject
= (CdmBase
) object
;
152 if(uuid
.equals(cdmBaseObject
.getUuid())){
153 return CdmBase
.deproxy(cdmBaseObject
, NamedArea
.class);
162 protected void init() {
163 selectedVocabularies
= getAvailableVocabularies();
166 private List
<TermVocabulary
> getAvailableVocabularies(){
167 List
<TermVocabulary
> vocabularies
= CdmStore
.getService(IVocabularyService
.class).listByTermType(TermType
.NamedArea
, true, null, null, null, null);
173 protected void search() {
174 Control control
=getSearchField();
175 String pattern
= null;
176 if (control
!= null){
177 pattern
= ((Text
)control
).getText();
180 if (pattern
== null || pattern
.equals("?")){
181 model
= CdmStore
.getService(ITermService
.class).getUuidAndTitleCache(selectedVocabularies
, limitOfInitialElements
, null, PreferencesUtil
.getGlobalLanguage());
183 model
= CdmStore
.getService(ITermService
.class).getUuidAndTitleCache(selectedVocabularies
, limitOfInitialElements
, pattern
, PreferencesUtil
.getGlobalLanguage());
186 fillContentProvider(null);
187 } catch (CoreException e
) {
188 // TODO Auto-generated catch block
191 // Set<NamedArea> terms = new HashSet<NamedArea>();
192 // for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
193 // terms.addAll(vocabulary.getTermsOrderedByLabels(CdmStore.getDefaultLanguage()));
196 // if(model == null){
197 // model = new ArrayList<UuidAndTitleCache<NamedArea>>();
200 // for(Object areaObject : terms){
201 // NamedArea area = (NamedArea) HibernateProxyHelper.deproxy(areaObject);
202 // UuidAndTitleCache<NamedArea> element = new UuidAndTitleCache<NamedArea>(NamedArea.class, area.getUuid(), area.getId(), getTitle(area));
203 // model.add(element);
207 // /** {@inheritDoc} */
209 // protected Control createExtendedContentArea(Composite parent) {
215 protected String
getTitle(NamedArea namedArea
) {
217 String result
= NamedArea
.labelWithLevel(namedArea
, CdmStore
.getDefaultLanguage());
219 } catch (Exception e
) {
220 MessagingUtils
.error(NamedAreaSelectionDialog
.class, "Error occurred when trying retrieve title for Named Area: " + namedArea
.getUuid(), e
);
221 return namedArea
.getTitleCache();
227 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
233 protected String
[] getNewWizardText() {
238 void createFilterButton(Composite searchAndFilter
)
240 filterButton
= new Button(searchAndFilter
, SWT
.NONE
);
241 // filterButton.setText("Filter");
242 filterButton
.setImage(ResourceManager
.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/funnel-icon.png"));
243 // SelectionListener filterSelectionListener = new FilterSelectionListener(preferenceID, this);
244 filterButton
.addSelectionListener(new SelectionListener(){
246 public void widgetSelected(SelectionEvent e
) {
248 Object source
= e
.getSource();
250 if (source
instanceof Button
){
251 Shell shell
= ((Button
)source
).getShell();
252 Dialog dialog
= new FilterDialog(getShell(), preferenceID
, selectedVocabularies
);
256 createSelectedVocabularies();
265 public void widgetDefaultSelected(SelectionEvent e
) {
266 // TODO Auto-generated method stub
278 public void setSelectedVocabularies() {
279 // TODO Auto-generated method stub