Project

General

Profile

Download (5.85 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
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.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.ui.dialog.selection;
12

    
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Set;
18
import java.util.UUID;
19

    
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;
26

    
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.TermVocabulary;
32
import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
33
import eu.etaxonomy.cdm.model.location.Country;
34
import eu.etaxonomy.cdm.model.location.NamedArea;
35
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
36
import eu.etaxonomy.taxeditor.store.CdmStore;
37

    
38
/**
39
 * <p>FilteredNamedAreaSelectionDialog class.</p>
40
 *
41
 * @author n.hoffmann
42
 * @created May 11, 2010
43
 * @version 1.0
44
 */
45
public class NamedAreaSelectionDialog extends
46
		AbstractFilteredCdmResourceSelectionDialog<NamedArea> {
47

    
48
	private class IncludeNamedAreaVocabulary extends Action {
49
		private final TermVocabulary<NamedArea> vocabulary;
50

    
51
		/**
52
		 * Creates a new instance of the class.
53
		 */
54
		public IncludeNamedAreaVocabulary(TermVocabulary<NamedArea> vocabulary) {
55
			super(vocabulary.getTitleCache(), IAction.AS_CHECK_BOX);
56
			this.vocabulary = vocabulary;
57
		}
58

    
59
		@Override
60
        public void run(){
61
			if(isChecked()){
62
				selectedVocabularies.add(vocabulary);
63
			}else{
64
				selectedVocabularies.remove(vocabulary);
65
			}
66

    
67
			initModel();
68
		}
69
	}
70

    
71
	private Collection<TermVocabulary<NamedArea>> selectedVocabularies;
72

    
73

    
74
	/**
75
	 * Creates a filtered selection dialog to select a named area.
76
	 *
77
	 * @param shell
78
	 * 				The shell for displaying this widget
79
	 * @param namedArea
80
	 * 				A namedArea that should be selected when the dialog opens
81
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
82
	 * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
83
	 */
84
	public static NamedArea select(Shell shell, ConversationHolder conversation, NamedArea namedArea) {
85
		NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell, conversation,
86
				"Choose an area", false, namedArea);
87
		return getSelectionFromDialog(dialog);
88
	}
89

    
90
	/**
91
	 * <p>Constructor for FilteredNamedAreaSelectionDialog.</p>
92
	 *
93
	 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
94
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
95
	 * @param title a {@link java.lang.String} object.
96
	 * @param multi a boolean.
97
	 * @param namedArea a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
98
	 */
99
	protected NamedAreaSelectionDialog(Shell shell, ConversationHolder conversation, String title, boolean multi, NamedArea namedArea) {
100
		super(shell, conversation, title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
101
	}
102

    
103
	/** {@inheritDoc} */
104
	@Override
105
	protected void fillViewMenu(IMenuManager menuManager) {
106

    
107
		super.fillViewMenu(menuManager);
108

    
109
		for(TermVocabulary<NamedArea> vocabulary : getVocabularies()){
110
			IncludeNamedAreaVocabulary action = new IncludeNamedAreaVocabulary(vocabulary);
111
			menuManager.add(action);
112
			action.setChecked(true);
113
		}
114
	}
115

    
116
	/** {@inheritDoc} */
117
	@Override
118
	protected NamedArea getPersistentObject(UUID uuid) {
119
		for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
120
			for(Object object : vocabulary.getTerms()){
121
				CdmBase cdmBaseObject = (CdmBase) object;
122
				if(uuid.equals(cdmBaseObject.getUuid())){
123
					return CdmBase.deproxy(cdmBaseObject, NamedArea.class);
124
				}
125
			}
126
		}
127
		return null;
128
	}
129

    
130
	/** {@inheritDoc} */
131
	@Override
132
	protected void init() {
133
		selectedVocabularies = getVocabularies();
134
	}
135

    
136
	private List<TermVocabulary<NamedArea>> getVocabularies(){
137
		List<TermVocabulary<NamedArea>> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermClass(NamedArea.class, null, null, null, null);
138
		vocabularies.add(CdmStore.getService(IVocabularyService.class).find(Country.uuidCountryVocabulary));
139
		return vocabularies;
140
	}
141

    
142
	/** {@inheritDoc} */
143
	@Override
144
	protected void initModel() {
145

    
146
		Set<NamedArea> terms = new HashSet<NamedArea>();
147
		for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
148
			terms.addAll(vocabulary.getTermsOrderedByLabels(CdmStore.getDefaultLanguage()));
149
		}
150

    
151
		if(model == null){
152
			model = new ArrayList<UuidAndTitleCache<NamedArea>>();
153
		}
154
		model.clear();
155
		for(Object areaObject : terms){
156
			NamedArea area = (NamedArea) HibernateProxyHelper.deproxy(areaObject);
157
			UuidAndTitleCache<NamedArea> element = new UuidAndTitleCache<NamedArea>(NamedArea.class, area.getUuid(), getTitle(area));
158
			model.add(element);
159
		}
160
	}
161

    
162
	/** {@inheritDoc} */
163
	@Override
164
	protected Control createExtendedContentArea(Composite parent) {
165
		return null;
166
	}
167

    
168
	/** {@inheritDoc} */
169
	@Override
170
	protected String getTitle(NamedArea namedArea) {
171
		try {
172
			String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
173
			return result;
174
		} catch (Exception e) {
175
			//TODO still need to learn how errors are handled in the Tax Editor
176
			System.out.println("Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid());
177
			return namedArea.getTitleCache();
178
		}
179
	}
180

    
181
	/** {@inheritDoc} */
182
	@Override
183
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
184
		return null;
185
	}
186

    
187
	/** {@inheritDoc} */
188
	@Override
189
	protected String getNewWizardLinkText() {
190
		return null;
191
	}
192
}
(16-16/28)