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
.text
.Collator
;
13 import java
.util
.Comparator
;
14 import java
.util
.HashSet
;
15 import java
.util
.Iterator
;
16 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.core
.runtime
.IStatus
;
23 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
24 import org
.eclipse
.core
.runtime
.Status
;
25 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
26 import org
.eclipse
.jface
.viewers
.ILabelProvider
;
27 import org
.eclipse
.jface
.viewers
.LabelProvider
;
28 import org
.eclipse
.jface
.window
.Window
;
29 import org
.eclipse
.jface
.wizard
.WizardDialog
;
30 import org
.eclipse
.swt
.SWT
;
31 import org
.eclipse
.swt
.events
.SelectionAdapter
;
32 import org
.eclipse
.swt
.events
.SelectionEvent
;
33 import org
.eclipse
.swt
.events
.SelectionListener
;
34 import org
.eclipse
.swt
.graphics
.Cursor
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Control
;
37 import org
.eclipse
.swt
.widgets
.Link
;
38 import org
.eclipse
.swt
.widgets
.Shell
;
39 import org
.eclipse
.swt
.widgets
.Text
;
40 import org
.eclipse
.ui
.IMemento
;
42 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
43 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
44 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
45 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
46 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
47 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
48 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
49 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
50 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
51 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
52 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
53 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
56 * <p>Abstract AbstractFilteredCdmResourceSelectionDialog class.</p>
62 public abstract class AbstractFilteredCdmResourceSelectionDialog
<T
extends ICdmBase
> extends
63 CdmFilteredItemsSelectionDialog
implements IConversationEnabled
{
65 private final ConversationHolder conversation
;
67 protected List
<UuidAndTitleCache
<T
>> model
;
68 private final Set
<T
> transientCdmObjects
= new HashSet
<T
>();
69 private final String settings
;
70 protected final int limitOfInitialElements
= 100;
72 protected T cdmBaseToBeFiltered
;
76 * <p>Constructor for AbstractFilteredCdmResourceSelectionDialog.</p>
78 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
80 * @param title a {@link java.lang.String} object.
81 * @param multi a boolean.
82 * @param settings a {@link java.lang.String} object.
83 * @param cdmObject a T object.
84 * @param <T> a T object.
86 protected AbstractFilteredCdmResourceSelectionDialog(Shell shell
, ConversationHolder conversation
, String title
, boolean multi
, String settings
, T cdmObject
) {
89 setMessage("Use * for wildcard, or ? to see all entries");
90 this.settings
= settings
;
92 this.conversation
= conversation
;
93 this.cdmBaseToBeFiltered
= cdmObject
;
94 Cursor cursor
= shell
.getCursor();
95 shell
.setCursor(shell
.getDisplay().getSystemCursor(SWT
.CURSOR_WAIT
));
98 shell
.setCursor(cursor
);
99 String objectTitle
= getTitle(cdmObject
);
100 // if (objectTitle != null) {
101 // setInitialPattern(objectTitle);
104 setListLabelProvider(createListLabelProvider());
105 setDetailsLabelProvider(createDetailsLabelProvider());
107 setSelectionHistory(new ResourceSelectionHistory());
111 * By default, we are returning the standard list label provider
113 * Override in subclasses if you want different behavior
117 protected ILabelProvider
createDetailsLabelProvider() {
118 return createListLabelProvider();
125 protected ILabelProvider
createListLabelProvider() {
126 return new FilteredCdmResourceLabelProvider();
130 * Override in subclasses.
131 * Will run before initModel()
133 protected void init() {
138 * <p>getSelectionFromDialog</p>
140 * @param dialog a {@link eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog} object.
141 * @param <TYPE> a TYPE object.
142 * @return a TYPE object.
144 protected static <TYPE
extends CdmBase
> TYPE
getSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog
<TYPE
> dialog
) {
145 //dialog.setInitialPattern("");
146 int result
= dialog
.open();
148 if (result
== Window
.CANCEL
) {
152 UUID uuid
= dialog
.getSelectedUuidAndTitleCache().getUuid();
156 return dialog
.getCdmObjectByUuid(uuid
);
160 * Check if object was created during the life of this dialog. If not,
161 * retrieve it from the CdmStore.
163 * @param cdmUuid a {@link java.util.UUID} object.
164 * @return a T object.
166 protected T
getCdmObjectByUuid(UUID cdmUuid
) {
167 for (T cdmObject
: transientCdmObjects
) {
168 if (cdmObject
.getUuid().equals(cdmUuid
)) {
172 return getPersistentObject(cdmUuid
);
176 * <p>getPersistentObject</p>
178 * @param uuid a {@link java.util.UUID} object.
179 * @return a T object.
181 abstract protected T
getPersistentObject(UUID uuid
);
186 // protected void addObjectToModel(T cdmObject) {
187 // model.add(new UuidAndTitleCache(cdmObject.getClass(), cdmObject.getUuid(), getTitle(cdmObject)));
188 // transientCdmObjects.add(cdmObject);
192 * <p>isObjectTransient</p>
194 * @param cdmObject a T object.
197 protected boolean isObjectTransient(T cdmObject
) {
198 return (getPersistentObject(cdmObject
.getUuid()) == null);
204 * @param cdmObject a T object.
205 * @return a {@link java.lang.String} object.
207 protected String
getTitle(T cdmObject
) {
208 if(cdmObject
== null){
212 if (cdmObject
instanceof IIdentifiableEntity
) {
213 return ((IIdentifiableEntity
) cdmObject
).getTitleCache();
216 throw new IllegalArgumentException("Generic method only" +
217 " supports cdmObject of type IIdentifiableEntity." +
218 " Please implement specific method in subclass.");
224 public void refresh() {
231 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
235 protected ItemsFilter
createFilter() {
236 return new ItemsFilter() {
239 * Always returns false to enforce refiltering even if the pattern is equal
242 public boolean equalsFilter(ItemsFilter filter
) {
247 public boolean isConsistentItem(Object item
) {
252 public boolean matchItem(Object item
) {
254 if(item
instanceof UuidAndTitleCache
){
255 text
= ((UuidAndTitleCache
) item
).getTitleCache();
256 }else if(item
instanceof String
){
257 text
= (String
) item
;
259 return text
!= null ?
matches(text
) : false;
267 * Set the filter input to the Agent's title cache
269 * @param cdmObject a T object.
271 protected void setPattern(T cdmObject
) {
272 // FilteredSelection does some very tricky caching to make sure it
273 // runs with high performance.
274 // This works for most use cases, but we want to change the model while the dialog is open
275 // and all the clever caching prevents the content provider from knowing that the model has changed
276 // I am aware, that this is a hack, but the FilteredSelectionDialog API does not offer a convenient
277 // way to solve the problem.
279 // Field lastCompletedFilter = this.getClass().getSuperclass().getSuperclass().getDeclaredField("lastCompletedFilter");
280 // lastCompletedFilter.setAccessible(true);
281 // lastCompletedFilter.set(this, null);
282 // } catch (SecurityException e) {
283 // MessagingUtils.error(getClass(), e);
284 // } catch (NoSuchFieldException e) {
285 // MessagingUtils.error(getClass(), e);
286 // } catch (IllegalArgumentException e) {
287 // MessagingUtils.error(getClass(), e);
288 // } catch (IllegalAccessException e) {
289 // MessagingUtils.error(getClass(), e);
292 // this also is not the nicest way to do it.
293 // I am still amazed, that FilteredSelectionDialog does not offer any methods to change its data
294 // once it was opened. Am I doing it wrong?
295 String pattern
= getTitle(cdmObject
);
296 ((Text
) getPatternControl()).setText(pattern
);
302 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider, org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor)
306 protected void fillContentProvider(AbstractContentProvider contentProvider
,
307 ItemsFilter itemsFilter
, IProgressMonitor progressMonitor
)
308 throws CoreException
{
311 progressMonitor
.beginTask("Looking for entities", model
.size());
312 filterExcludedObjects();
313 Iterator
<UuidAndTitleCache
<T
>> iterator
= model
.iterator();
314 UuidAndTitleCache
<T
> element
;
315 while(iterator
.hasNext()){
316 element
= iterator
.next();
317 if (!element
.equals(cdmBaseToBeFiltered
)){
318 contentProvider
.add(element
, itemsFilter
);
320 if (progressMonitor
.isCanceled()) {
321 throw new OperationCanceledException();
323 progressMonitor
.worked(1);
326 MessagingUtils
.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
330 progressMonitor
.done();
335 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
339 protected IDialogSettings
getDialogSettings() {
340 IDialogSettings settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().getSection(getSettings());
342 if (settings
== null) {
343 settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().addNewSection(getSettings());
349 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
353 public String
getElementName(Object item
) {
354 return ((UuidAndTitleCache
) item
).getTitleCache();
358 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
362 protected Comparator
getItemsComparator() {
363 return new Comparator
<UuidAndTitleCache
>() {
365 public int compare(UuidAndTitleCache entity1
,
366 UuidAndTitleCache entity2
) {
367 Collator collator
= Collator
.getInstance();
368 return collator
.compare(entity1
.getTitleCache(), entity2
.getTitleCache());
374 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#validateItem(java.lang.Object)
378 protected IStatus
validateItem(Object item
) {
379 return Status
.OK_STATUS
;
383 * <p>getSelectedUuidAndTitleCache</p>
385 * @return a {@link eu.etaxonomy.cdm.model.common.UuidAndTitleCache} object.
387 protected UuidAndTitleCache
getSelectedUuidAndTitleCache() {
388 Object
[] result
= getResult();
389 return result
[0] == null ?
null : (UuidAndTitleCache
) result
[0];
393 * <p>Getter for the field <code>settings</code>.</p>
395 * @return a {@link java.lang.String} object.
397 public String
getSettings() {
398 if(settings
== null){
399 throw new IllegalStateException("No SETTINGS set.");
407 * @created Oct 19, 2009
410 private class ResourceSelectionHistory
extends SelectionHistory
{
412 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#restoreItemFromMemento(org.eclipse.ui.IMemento)
415 protected Object
restoreItemFromMemento(IMemento element
) {
416 return element
.getString("resource"); //$NON-NLS-1$
419 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#storeItemToMemento(java.lang.Object,
420 * org.eclipse.ui.IMemento)
423 protected void storeItemToMemento(Object item
, IMemento element
) {
424 element
.putString("resource", item
.toString()); //$NON-NLS-1$
429 * <p>getNewWizardLinkText</p>
431 * @return a {@link java.lang.String} object.
433 protected abstract String
getNewWizardLinkText();
436 * <p>getNewEntityWizard</p>
438 * @return a {@link eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard} object.
440 protected abstract AbstractNewEntityWizard
getNewEntityWizard(String parameter
);
442 public class FilteredCdmResourceLabelProvider
extends LabelProvider
{
444 public String
getText(Object element
) {
445 if (element
== null) {
448 UuidAndTitleCache uuidAndTitleCache
= (UuidAndTitleCache
) element
;
449 String titleCache
= uuidAndTitleCache
.getTitleCache();
450 if(PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG
)){
451 titleCache
+= " ["+uuidAndTitleCache
.getId()+"]";
458 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
462 protected Control
createExtendedContentArea(Composite parent
) {
463 String newWizardLinkText
= getNewWizardLinkText();
464 if(newWizardLinkText
!= null){
465 Link link
= new Link(parent
, SWT
.NONE
);
466 link
.setText(newWizardLinkText
);
467 link
.addSelectionListener(getNewWizardLinkSelectionListener());
473 protected SelectionListener
getNewWizardLinkSelectionListener(){
474 return new SelectionAdapter() {
477 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
480 public void widgetSelected(SelectionEvent e
) {
482 AbstractNewEntityWizard wizard
= getNewEntityWizard(e
.text
);
484 wizard
.init(null, null);
485 if(wizard
.getEntity() != null) {
486 WizardDialog dialog
= new WizardDialog(getShell(), wizard
);
487 int status
= dialog
.open();
489 if (status
== IStatus
.OK
) {
491 T entity
= (T
) wizard
.getEntity();
492 // model.add(new UuidAndTitleCache<T>(entity.getUuid(), entity.getId(), getTitle(entity)));
495 getConversationHolder().bind();
497 //FIXME : Need to make sure this is a stable fix (ticket 3822)
498 getConversationHolder().commit();
506 * <p>getConversationHolder</p>
508 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
511 public ConversationHolder
getConversationHolder() {
517 public void update(CdmDataChangeMap changeEvents
) {}
520 * Don't want to add for example a taxon or synonym to itself
521 * so filter the list to remove the taxon in question
522 * (<code>cdmBaseToBeFiltered</code>)
523 * so it is not available in the filtered list.
525 private void filterExcludedObjects() {
526 if (model
!= null && cdmBaseToBeFiltered
!= null) {
528 UuidAndTitleCache uuidAndTitleCacheToRemove
= null;
530 for (UuidAndTitleCache uuidAndTitleCache
: model
){
531 if ((cdmBaseToBeFiltered
.getUuid()).equals(uuidAndTitleCache
.getUuid())) {
532 uuidAndTitleCacheToRemove
= uuidAndTitleCache
;
535 model
.remove(uuidAndTitleCacheToRemove
);