8bfa50dd67f542f256411f7858a241e01b48bfef
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / AbstractFilteredCdmResourceSelectionDialog.java
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.lang.reflect.Field;
14 import java.text.Collator;
15 import java.util.Comparator;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.UUID;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.OperationCanceledException;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.jface.dialogs.IDialogSettings;
27 import org.eclipse.jface.viewers.ILabelProvider;
28 import org.eclipse.jface.viewers.LabelProvider;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.jface.wizard.WizardDialog;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.events.SelectionListener;
35 import org.eclipse.swt.graphics.Cursor;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Link;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.ui.IMemento;
42 import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;
43
44 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
45 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
46 import eu.etaxonomy.cdm.model.common.CdmBase;
47 import eu.etaxonomy.cdm.model.common.ICdmBase;
48 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
49 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
50 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
51 import eu.etaxonomy.taxeditor.model.MessagingUtils;
52 import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
53 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
54 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
55 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
56
57 /**
58 * <p>Abstract AbstractFilteredCdmResourceSelectionDialog class.</p>
59 *
60 * @author n.hoffmann
61 * @created 04.06.2009
62 * @version 1.0
63 */
64 public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmBase> extends
65 FilteredItemsSelectionDialog implements IConversationEnabled {
66
67 private final ConversationHolder conversation;
68
69 protected List<UuidAndTitleCache<T>> model;
70 private final Set<T> transientCdmObjects = new HashSet<T>();
71 private final String settings;
72
73 protected T cdmBaseToBeFiltered;
74
75
76 /**
77 * <p>Constructor for AbstractFilteredCdmResourceSelectionDialog.</p>
78 *
79 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
80 * @param conversation
81 * @param title a {@link java.lang.String} object.
82 * @param multi a boolean.
83 * @param settings a {@link java.lang.String} object.
84 * @param cdmObject a T object.
85 * @param <T> a T object.
86 */
87 protected AbstractFilteredCdmResourceSelectionDialog(Shell shell, ConversationHolder conversation, String title, boolean multi, String settings, T cdmObject) {
88 super(shell, multi);
89 setTitle(title);
90 setMessage("Use * for wildcard, or ? to see all entries");
91 this.settings = settings;
92
93 this.conversation = conversation;
94
95 Cursor cursor = shell.getCursor();
96 shell.setCursor(shell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT));
97 init();
98 initModel();
99 shell.setCursor(cursor);
100 String objectTitle = getTitle(cdmObject);
101 if (objectTitle != null) {
102 setInitialPattern(objectTitle);
103 }
104
105 setListLabelProvider(createListLabelProvider());
106 setDetailsLabelProvider(createDetailsLabelProvider());
107
108 setSelectionHistory(new ResourceSelectionHistory());
109 }
110
111 /**
112 * By default, we are returning the standard list label provider
113 *
114 * Override in subclasses if you want different behavior
115 *
116 * @return
117 */
118 protected ILabelProvider createDetailsLabelProvider() {
119 return createListLabelProvider();
120 }
121
122 /**
123 *
124 * @return
125 */
126 protected ILabelProvider createListLabelProvider() {
127 return new FilteredCdmResourceLabelProvider();
128 }
129
130 /**
131 * Override in subclasses.
132 * Will run before initModel()
133 */
134 protected void init() {
135
136 }
137
138 /**
139 * <p>getSelectionFromDialog</p>
140 *
141 * @param dialog a {@link eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog} object.
142 * @param <TYPE> a TYPE object.
143 * @return a TYPE object.
144 */
145 protected static <TYPE extends CdmBase> TYPE getSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog<TYPE> dialog) {
146 //dialog.setInitialPattern("");
147 int result = dialog.open();
148
149 if (result == Window.CANCEL) {
150 return null;
151 }
152
153 UUID uuid = dialog.getSelectedUuidAndTitleCache().getUuid();
154 if(uuid == null){
155 return null;
156 }
157 return dialog.getCdmObjectByUuid(uuid);
158 }
159
160 /**
161 * Check if object was created during the life of this dialog. If not,
162 * retrieve it from the CdmStore.
163 *
164 * @param cdmUuid a {@link java.util.UUID} object.
165 * @return a T object.
166 */
167 protected T getCdmObjectByUuid(UUID cdmUuid) {
168 for (T cdmObject : transientCdmObjects) {
169 if (cdmObject.getUuid().equals(cdmUuid)) {
170 return cdmObject;
171 }
172 }
173 return getPersistentObject(cdmUuid);
174 }
175
176 /**
177 * <p>getPersistentObject</p>
178 *
179 * @param uuid a {@link java.util.UUID} object.
180 * @return a T object.
181 */
182 abstract protected T getPersistentObject(UUID uuid);
183
184 /**
185 * @param cdmObject
186 */
187 // protected void addObjectToModel(T cdmObject) {
188 // model.add(new UuidAndTitleCache(cdmObject.getClass(), cdmObject.getUuid(), getTitle(cdmObject)));
189 // transientCdmObjects.add(cdmObject);
190 // }
191
192 /**
193 * <p>isObjectTransient</p>
194 *
195 * @param cdmObject a T object.
196 * @return a boolean.
197 */
198 protected boolean isObjectTransient(T cdmObject) {
199 return (getPersistentObject(cdmObject.getUuid()) == null);
200 }
201
202 /**
203 * <p>getTitle</p>
204 *
205 * @param cdmObject a T object.
206 * @return a {@link java.lang.String} object.
207 */
208 protected String getTitle(T cdmObject) {
209 if(cdmObject == null){
210 return "";
211 }
212
213 if (cdmObject instanceof IIdentifiableEntity) {
214 return ((IIdentifiableEntity) cdmObject).getTitleCache();
215 }
216
217 throw new IllegalArgumentException("Generic method only" +
218 " supports cdmObject of type IIdentifiableEntity." +
219 " Please implement specific method in subclass.");
220 }
221
222
223 /** {@inheritDoc} */
224 @Override
225 public void refresh() {
226 //initModel();
227 filterExcludedObjects();
228 super.refresh();
229 }
230
231 /**
232 * <p>initModel</p>
233 */
234 abstract protected void initModel();
235
236 /* (non-Javadoc)
237 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
238 */
239 /** {@inheritDoc} */
240 @Override
241 protected ItemsFilter createFilter() {
242 return new ItemsFilter() {
243
244 /**
245 * Always returns false to enforce refiltering even if the pattern is equal
246 */
247 @Override
248 public boolean equalsFilter(ItemsFilter filter) {
249 return false;
250 }
251
252 @Override
253 public boolean isConsistentItem(Object item) {
254 return false;
255 }
256
257 @Override
258 public boolean matchItem(Object item) {
259 String text = null;
260 if(item instanceof UuidAndTitleCache){
261 text = ((UuidAndTitleCache) item).getTitleCache();
262 }else if(item instanceof String){
263 text = (String) item;
264 }
265 return text != null ? matches(text) : false;
266 }
267
268 };
269 }
270
271
272 /**
273 * Set the filter input to the Agent's title cache
274 *
275 * @param cdmObject a T object.
276 */
277 protected void setPattern(T cdmObject) {
278 // FilteredSelection does some very tricky caching to make sure it
279 // runs with high performance.
280 // This works for most use cases, but we want to change the model while the dialog is open
281 // and all the clever caching prevents the content provider from knowing that the model has changed
282 // I am aware, that this is a hack, but the FilteredSelectionDialog API does not offer a convenient
283 // way to solve the problem.
284 try {
285 Field lastCompletedFilter = this.getClass().getSuperclass().getSuperclass().getDeclaredField("lastCompletedFilter");
286 lastCompletedFilter.setAccessible(true);
287 lastCompletedFilter.set(this, null);
288 } catch (SecurityException e) {
289 MessagingUtils.error(getClass(), e);
290 } catch (NoSuchFieldException e) {
291 MessagingUtils.error(getClass(), e);
292 } catch (IllegalArgumentException e) {
293 MessagingUtils.error(getClass(), e);
294 } catch (IllegalAccessException e) {
295 MessagingUtils.error(getClass(), e);
296 }
297
298 // this also is not the nicest way to do it.
299 // I am still amazed, that FilteredSelectionDialog does not offer any methods to change its data
300 // once it was opened. Am I doing it wrong?
301 String pattern = getTitle(cdmObject);
302 ((Text) getPatternControl()).setText(pattern);
303 }
304
305 /* (non-Javadoc)
306 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider, org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor)
307 */
308 /** {@inheritDoc} */
309 @Override
310 protected void fillContentProvider(AbstractContentProvider contentProvider,
311 ItemsFilter itemsFilter, IProgressMonitor progressMonitor)
312 throws CoreException {
313 try {
314 if(model != null){
315 progressMonitor.beginTask("Looking for entities", model.size());
316 for(UuidAndTitleCache<T> element : model){
317 contentProvider.add(element, itemsFilter);
318 if (progressMonitor.isCanceled()) {
319 throw new OperationCanceledException();
320 }
321 progressMonitor.worked(1);
322 }
323 }else{
324 MessagingUtils.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
325 }
326 }
327 finally {
328 progressMonitor.done();
329 }
330 }
331
332 /* (non-Javadoc)
333 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
334 */
335 /** {@inheritDoc} */
336 @Override
337 protected IDialogSettings getDialogSettings() {
338 IDialogSettings settings = TaxeditorStorePlugin.getDefault().getDialogSettings().getSection(getSettings());
339
340 if (settings == null) {
341 settings = TaxeditorStorePlugin.getDefault().getDialogSettings().addNewSection(getSettings());
342 }
343 return settings;
344 }
345
346 /* (non-Javadoc)
347 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
348 */
349 /** {@inheritDoc} */
350 @Override
351 public String getElementName(Object item) {
352 return ((UuidAndTitleCache) item).getTitleCache();
353 }
354
355 /* (non-Javadoc)
356 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
357 */
358 /** {@inheritDoc} */
359 @Override
360 protected Comparator getItemsComparator() {
361 return new Comparator<UuidAndTitleCache>() {
362 @Override
363 public int compare(UuidAndTitleCache entity1,
364 UuidAndTitleCache entity2) {
365 Collator collator = Collator.getInstance();
366 return collator.compare(entity1.getTitleCache(), entity2.getTitleCache());
367 }
368 };
369 }
370
371 /* (non-Javadoc)
372 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#validateItem(java.lang.Object)
373 */
374 /** {@inheritDoc} */
375 @Override
376 protected IStatus validateItem(Object item) {
377 return Status.OK_STATUS;
378 }
379
380 /**
381 * <p>getSelectedUuidAndTitleCache</p>
382 *
383 * @return a {@link eu.etaxonomy.cdm.model.common.UuidAndTitleCache} object.
384 */
385 protected UuidAndTitleCache getSelectedUuidAndTitleCache() {
386 Object[] result = getResult();
387 return result[0] == null ? null : (UuidAndTitleCache) result[0];
388 }
389
390 /**
391 * <p>Getter for the field <code>settings</code>.</p>
392 *
393 * @return a {@link java.lang.String} object.
394 */
395 public String getSettings() {
396 if(settings == null){
397 throw new IllegalStateException("No SETTINGS set.");
398 }
399 return settings;
400 }
401
402 /**
403 *
404 * @author n.hoffmann
405 * @created Oct 19, 2009
406 * @version 1.0
407 */
408 private class ResourceSelectionHistory extends SelectionHistory {
409 /*
410 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#restoreItemFromMemento(org.eclipse.ui.IMemento)
411 */
412 @Override
413 protected Object restoreItemFromMemento(IMemento element) {
414 return element.getString("resource"); //$NON-NLS-1$
415 }
416 /*
417 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#storeItemToMemento(java.lang.Object,
418 * org.eclipse.ui.IMemento)
419 */
420 @Override
421 protected void storeItemToMemento(Object item, IMemento element) {
422 element.putString("resource", item.toString()); //$NON-NLS-1$
423 }
424 }
425
426 /**
427 * <p>getNewWizardLinkText</p>
428 *
429 * @return a {@link java.lang.String} object.
430 */
431 protected abstract String getNewWizardLinkText();
432
433 /**
434 * <p>getNewEntityWizard</p>
435 * @param parameter
436 * @return a {@link eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard} object.
437 */
438 protected abstract AbstractNewEntityWizard getNewEntityWizard(String parameter);
439
440 public class FilteredCdmResourceLabelProvider extends LabelProvider {
441 @Override
442 public String getText(Object element) {
443 if (element == null) {
444 return null;
445 }
446 UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
447 String titleCache = uuidAndTitleCache.getTitleCache();
448 if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
449 titleCache += " ["+uuidAndTitleCache.getId()+"]";
450 }
451 return titleCache;
452 }
453 };
454
455 /* (non-Javadoc)
456 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
457 */
458 /** {@inheritDoc} */
459 @Override
460 protected Control createExtendedContentArea(Composite parent) {
461 String newWizardLinkText = getNewWizardLinkText();
462 if(newWizardLinkText != null){
463 Link link = new Link(parent, SWT.NONE);
464 link.setText(newWizardLinkText);
465 link.addSelectionListener(getNewWizardLinkSelectionListener());
466 return link;
467 }
468 return null;
469 }
470
471 protected SelectionListener getNewWizardLinkSelectionListener(){
472 return new SelectionAdapter() {
473
474 /* (non-Javadoc)
475 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
476 */
477 @Override
478 public void widgetSelected(SelectionEvent e) {
479
480 AbstractNewEntityWizard wizard = getNewEntityWizard(e.text);
481
482 wizard.init(null, null);
483 if(wizard.getEntity() != null) {
484 WizardDialog dialog = new WizardDialog(getShell(), wizard);
485 int status = dialog.open();
486
487 if (status == IStatus.OK) {
488
489 T entity = (T) wizard.getEntity();
490 model.add(new UuidAndTitleCache<T>(entity.getUuid(),
491 entity.getId(),
492 getTitle(entity)));
493 refresh();
494 setPattern(entity);
495 getConversationHolder().bind();
496 }
497 //FIXME : Need to make sure this is a stable fix (ticket 3822)
498 getConversationHolder().commit();
499 }
500 }
501 };
502 }
503
504 /**
505 * <p>getConversationHolder</p>
506 *
507 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
508 */
509 @Override
510 public ConversationHolder getConversationHolder() {
511 return conversation;
512 }
513
514 /** {@inheritDoc} */
515 @Override
516 public void update(CdmDataChangeMap changeEvents) {}
517
518 /**
519 * Don't want to add for example a taxon or synonym to itself
520 * so filter the list to remove the taxon in question
521 * (<code>cdmBaseToBeFiltered</code>)
522 * so it is not available in the filtered list.
523 */
524 private void filterExcludedObjects() {
525 if (model != null && cdmBaseToBeFiltered != null) {
526
527 UuidAndTitleCache uuidAndTitleCacheToRemove = null;
528
529 for (UuidAndTitleCache uuidAndTitleCache : model){
530 if ((cdmBaseToBeFiltered.getUuid()).equals(uuidAndTitleCache.getUuid())) {
531 uuidAndTitleCacheToRemove = uuidAndTitleCache;
532 }
533 }
534 model.remove(uuidAndTitleCacheToRemove);
535 }
536 }
537 }