3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
;
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
;
19 import java
.util
.UUID
;
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
;
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
;
58 * <p>Abstract AbstractFilteredCdmResourceSelectionDialog class.</p>
64 public abstract class AbstractFilteredCdmResourceSelectionDialog
<T
extends ICdmBase
> extends
65 FilteredItemsSelectionDialog
implements IConversationEnabled
{
67 private final ConversationHolder conversation
;
69 protected List
<UuidAndTitleCache
<T
>> model
;
70 private final Set
<T
> transientCdmObjects
= new HashSet
<T
>();
71 private final String settings
;
73 protected T cdmBaseToBeFiltered
;
77 * <p>Constructor for AbstractFilteredCdmResourceSelectionDialog.</p>
79 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
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.
87 protected AbstractFilteredCdmResourceSelectionDialog(Shell shell
, ConversationHolder conversation
, String title
, boolean multi
, String settings
, T cdmObject
) {
90 setMessage("Use * for wildcard, or ? to see all entries");
91 this.settings
= settings
;
93 this.conversation
= conversation
;
94 this.cdmBaseToBeFiltered
= cdmObject
;
95 Cursor cursor
= shell
.getCursor();
96 shell
.setCursor(shell
.getDisplay().getSystemCursor(SWT
.CURSOR_WAIT
));
99 shell
.setCursor(cursor
);
100 String objectTitle
= getTitle(cdmObject
);
101 if (objectTitle
!= null) {
102 setInitialPattern(objectTitle
);
105 setListLabelProvider(createListLabelProvider());
106 setDetailsLabelProvider(createDetailsLabelProvider());
108 setSelectionHistory(new ResourceSelectionHistory());
112 * By default, we are returning the standard list label provider
114 * Override in subclasses if you want different behavior
118 protected ILabelProvider
createDetailsLabelProvider() {
119 return createListLabelProvider();
126 protected ILabelProvider
createListLabelProvider() {
127 return new FilteredCdmResourceLabelProvider();
131 * Override in subclasses.
132 * Will run before initModel()
134 protected void init() {
139 * <p>getSelectionFromDialog</p>
141 * @param dialog a {@link eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog} object.
142 * @param <TYPE> a TYPE object.
143 * @return a TYPE object.
145 protected static <TYPE
extends CdmBase
> TYPE
getSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog
<TYPE
> dialog
) {
146 //dialog.setInitialPattern("");
147 int result
= dialog
.open();
149 if (result
== Window
.CANCEL
) {
153 UUID uuid
= dialog
.getSelectedUuidAndTitleCache().getUuid();
157 return dialog
.getCdmObjectByUuid(uuid
);
161 * Check if object was created during the life of this dialog. If not,
162 * retrieve it from the CdmStore.
164 * @param cdmUuid a {@link java.util.UUID} object.
165 * @return a T object.
167 protected T
getCdmObjectByUuid(UUID cdmUuid
) {
168 for (T cdmObject
: transientCdmObjects
) {
169 if (cdmObject
.getUuid().equals(cdmUuid
)) {
173 return getPersistentObject(cdmUuid
);
177 * <p>getPersistentObject</p>
179 * @param uuid a {@link java.util.UUID} object.
180 * @return a T object.
182 abstract protected T
getPersistentObject(UUID uuid
);
187 // protected void addObjectToModel(T cdmObject) {
188 // model.add(new UuidAndTitleCache(cdmObject.getClass(), cdmObject.getUuid(), getTitle(cdmObject)));
189 // transientCdmObjects.add(cdmObject);
193 * <p>isObjectTransient</p>
195 * @param cdmObject a T object.
198 protected boolean isObjectTransient(T cdmObject
) {
199 return (getPersistentObject(cdmObject
.getUuid()) == null);
205 * @param cdmObject a T object.
206 * @return a {@link java.lang.String} object.
208 protected String
getTitle(T cdmObject
) {
209 if(cdmObject
== null){
213 if (cdmObject
instanceof IIdentifiableEntity
) {
214 return ((IIdentifiableEntity
) cdmObject
).getTitleCache();
217 throw new IllegalArgumentException("Generic method only" +
218 " supports cdmObject of type IIdentifiableEntity." +
219 " Please implement specific method in subclass.");
225 public void refresh() {
227 filterExcludedObjects();
234 abstract protected void initModel();
237 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
241 protected ItemsFilter
createFilter() {
242 return new ItemsFilter() {
245 * Always returns false to enforce refiltering even if the pattern is equal
248 public boolean equalsFilter(ItemsFilter filter
) {
253 public boolean isConsistentItem(Object item
) {
258 public boolean matchItem(Object item
) {
260 if(item
instanceof UuidAndTitleCache
){
261 text
= ((UuidAndTitleCache
) item
).getTitleCache();
262 }else if(item
instanceof String
){
263 text
= (String
) item
;
265 return text
!= null ?
matches(text
) : false;
273 * Set the filter input to the Agent's title cache
275 * @param cdmObject a T object.
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.
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
);
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
);
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)
310 protected void fillContentProvider(AbstractContentProvider contentProvider
,
311 ItemsFilter itemsFilter
, IProgressMonitor progressMonitor
)
312 throws CoreException
{
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();
321 progressMonitor
.worked(1);
324 MessagingUtils
.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
328 progressMonitor
.done();
333 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
337 protected IDialogSettings
getDialogSettings() {
338 IDialogSettings settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().getSection(getSettings());
340 if (settings
== null) {
341 settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().addNewSection(getSettings());
347 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
351 public String
getElementName(Object item
) {
352 return ((UuidAndTitleCache
) item
).getTitleCache();
356 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
360 protected Comparator
getItemsComparator() {
361 return new Comparator
<UuidAndTitleCache
>() {
363 public int compare(UuidAndTitleCache entity1
,
364 UuidAndTitleCache entity2
) {
365 Collator collator
= Collator
.getInstance();
366 return collator
.compare(entity1
.getTitleCache(), entity2
.getTitleCache());
372 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#validateItem(java.lang.Object)
376 protected IStatus
validateItem(Object item
) {
377 return Status
.OK_STATUS
;
381 * <p>getSelectedUuidAndTitleCache</p>
383 * @return a {@link eu.etaxonomy.cdm.model.common.UuidAndTitleCache} object.
385 protected UuidAndTitleCache
getSelectedUuidAndTitleCache() {
386 Object
[] result
= getResult();
387 return result
[0] == null ?
null : (UuidAndTitleCache
) result
[0];
391 * <p>Getter for the field <code>settings</code>.</p>
393 * @return a {@link java.lang.String} object.
395 public String
getSettings() {
396 if(settings
== null){
397 throw new IllegalStateException("No SETTINGS set.");
405 * @created Oct 19, 2009
408 private class ResourceSelectionHistory
extends SelectionHistory
{
410 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#restoreItemFromMemento(org.eclipse.ui.IMemento)
413 protected Object
restoreItemFromMemento(IMemento element
) {
414 return element
.getString("resource"); //$NON-NLS-1$
417 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#storeItemToMemento(java.lang.Object,
418 * org.eclipse.ui.IMemento)
421 protected void storeItemToMemento(Object item
, IMemento element
) {
422 element
.putString("resource", item
.toString()); //$NON-NLS-1$
427 * <p>getNewWizardLinkText</p>
429 * @return a {@link java.lang.String} object.
431 protected abstract String
getNewWizardLinkText();
434 * <p>getNewEntityWizard</p>
436 * @return a {@link eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard} object.
438 protected abstract AbstractNewEntityWizard
getNewEntityWizard(String parameter
);
440 public class FilteredCdmResourceLabelProvider
extends LabelProvider
{
442 public String
getText(Object element
) {
443 if (element
== null) {
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()+"]";
456 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
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());
471 protected SelectionListener
getNewWizardLinkSelectionListener(){
472 return new SelectionAdapter() {
475 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
478 public void widgetSelected(SelectionEvent e
) {
480 AbstractNewEntityWizard wizard
= getNewEntityWizard(e
.text
);
482 wizard
.init(null, null);
483 if(wizard
.getEntity() != null) {
484 WizardDialog dialog
= new WizardDialog(getShell(), wizard
);
485 int status
= dialog
.open();
487 if (status
== IStatus
.OK
) {
489 T entity
= (T
) wizard
.getEntity();
490 model
.add(new UuidAndTitleCache
<T
>(entity
.getUuid(),
495 getConversationHolder().bind();
497 //FIXME : Need to make sure this is a stable fix (ticket 3822)
498 getConversationHolder().commit();
505 * <p>getConversationHolder</p>
507 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
510 public ConversationHolder
getConversationHolder() {
516 public void update(CdmDataChangeMap changeEvents
) {}
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.
524 private void filterExcludedObjects() {
525 if (model
!= null && cdmBaseToBeFiltered
!= null) {
527 UuidAndTitleCache uuidAndTitleCacheToRemove
= null;
529 for (UuidAndTitleCache uuidAndTitleCache
: model
){
530 if ((cdmBaseToBeFiltered
.getUuid()).equals(uuidAndTitleCache
.getUuid())) {
531 uuidAndTitleCacheToRemove
= uuidAndTitleCache
;
534 model
.remove(uuidAndTitleCacheToRemove
);