fix multiple representation by removing conversation of selection dialog
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / NamedAreaSelectionDialog.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.ui.dialog.selection;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.UUID;
15
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;
29
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;
40
41 /**
42 * @author n.hoffmann
43 * @created May 11, 2010
44 * @version 1.0
45 */
46 public class NamedAreaSelectionDialog extends
47 AbstractFilteredCdmResourceSelectionDialog<NamedArea> {
48
49 private class IncludeNamedAreaVocabulary extends Action {
50 private final TermVocabulary<NamedArea> vocabulary;
51
52 /**
53 * Creates a new instance of the class.
54 */
55 public IncludeNamedAreaVocabulary(TermVocabulary<NamedArea> vocabulary) {
56 super(vocabulary.getTitleCache(), IAction.AS_CHECK_BOX);
57 this.vocabulary = vocabulary;
58 }
59
60 @Override
61 public void run(){
62 if(isChecked()){
63 selectedVocabularies.add(vocabulary);
64 }else{
65 selectedVocabularies.remove(vocabulary);
66 }
67 PreferencesUtil.getPreferenceStore().setValue(getPrefKey(vocabulary), !isChecked());
68 search();
69 }
70 }
71
72 protected List<TermVocabulary> selectedVocabularies;
73 protected ArrayList<TermVocabulary> preselectedVocabularies;
74
75
76
77 /**
78 * Creates a filtered selection dialog to select a named area.
79 *
80 * @param shell
81 * The shell for displaying this widget
82 * @param namedArea
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.
89 */
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);
95 }
96
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);
106 }
107
108
109 selectedVocabularies = createSelectedVocabularies();;
110 // search;//re-init to consider pre-selected vocabularies
111 }
112
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);
119 }
120 }
121 return tempSelectedVocabularies;
122 }
123
124 /** {@inheritDoc} */
125 // @Override
126 // protected void fillViewMenu(IMenuManager menuManager) {
127 //
128 //// super.fillViewMenu(menuManager);
129 //
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);
135 // }
136 // else{
137 // action.setChecked(!PreferencesUtil.getPreferenceStore().getBoolean(getPrefKey(vocabulary)));
138 // }
139 // }
140 // }
141
142 private String getPrefKey(TermVocabulary vocabulary){
143 return "hide_"+NamedAreaSelectionDialog.class.getCanonicalName()+vocabulary.getUuid()+preferenceID;
144 }
145
146 /** {@inheritDoc} */
147 @Override
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);
154 }
155 }
156 }
157 return null;
158 }
159
160 /** {@inheritDoc} */
161 @Override
162 protected void init() {
163 selectedVocabularies = getAvailableVocabularies();
164 }
165
166 private List<TermVocabulary> getAvailableVocabularies(){
167 List<TermVocabulary> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
168 return vocabularies;
169 }
170
171 /** {@inheritDoc} */
172 @Override
173 protected void search() {
174 Control control =getSearchField();
175 String pattern = null;
176 if (control != null){
177 pattern = ((Text)control).getText();
178 }
179
180 if (pattern == null || pattern.equals("?")){
181 model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, null, PreferencesUtil.getGlobalLanguage());
182 }else{
183 model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
184 }
185 try {
186 fillContentProvider(null);
187 } catch (CoreException e) {
188 // TODO Auto-generated catch block
189 e.printStackTrace();
190 }
191 // Set<NamedArea> terms = new HashSet<NamedArea>();
192 // for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
193 // terms.addAll(vocabulary.getTermsOrderedByLabels(CdmStore.getDefaultLanguage()));
194 // }
195 //
196 // if(model == null){
197 // model = new ArrayList<UuidAndTitleCache<NamedArea>>();
198 // }
199 // model.clear();
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);
204 // }
205 }
206
207 // /** {@inheritDoc} */
208 // @Override
209 // protected Control createExtendedContentArea(Composite parent) {
210 // return null;
211 // }
212
213 /** {@inheritDoc} */
214 @Override
215 protected String getTitle(NamedArea namedArea) {
216 try {
217 String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
218 return result;
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();
222 }
223 }
224
225 /** {@inheritDoc} */
226 @Override
227 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
228 return null;
229 }
230
231 /** {@inheritDoc} */
232 @Override
233 protected String[] getNewWizardText() {
234 return null;
235 }
236
237 @Override
238 void createFilterButton(Composite searchAndFilter)
239 {
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(){
245 @Override
246 public void widgetSelected(SelectionEvent e) {
247
248 Object source = e.getSource();
249 String text = null;
250 if (source instanceof Button){
251 Shell shell = ((Button)source).getShell();
252 Dialog dialog = new FilterDialog(getShell(), preferenceID, selectedVocabularies);
253 if(dialog!=null){
254 dialog.open();
255 }
256 createSelectedVocabularies();
257 }
258
259
260
261
262 }
263
264 @Override
265 public void widgetDefaultSelected(SelectionEvent e) {
266 // TODO Auto-generated method stub
267
268 }
269
270
271 });
272
273 }
274
275 /**
276 *
277 */
278 public void setSelectedVocabularies() {
279 // TODO Auto-generated method stub
280
281 }
282
283 }