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
.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
;
41 import org
.eclipse
.ui
.dialogs
.FilteredItemsSelectionDialog
;
43 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
44 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
45 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
46 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
47 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
48 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
49 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
50 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
51 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
52 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
53 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
54 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
57 * <p>Abstract AbstractFilteredCdmResourceSelectionDialog class.</p>
63 public abstract class AbstractFilteredCdmResourceSelectionDialog
<T
extends ICdmBase
> extends
64 FilteredItemsSelectionDialog
implements IConversationEnabled
{
66 private final ConversationHolder conversation
;
68 protected List
<UuidAndTitleCache
<T
>> model
;
69 private final Set
<T
> transientCdmObjects
= new HashSet
<T
>();
70 private final String settings
;
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
;
98 String objectTitle
= getTitle(cdmObject
);
99 if (objectTitle
!= null) {
100 setInitialPattern(objectTitle
);
103 setListLabelProvider(createListLabelProvider());
104 setDetailsLabelProvider(createDetailsLabelProvider());
106 setSelectionHistory(new ResourceSelectionHistory());
110 * By default, we are returning the standard list label provider
112 * Override in subclasses if you want different behavior
116 protected ILabelProvider
createDetailsLabelProvider() {
117 return createListLabelProvider();
124 protected ILabelProvider
createListLabelProvider() {
125 return new FilteredCdmResourceLabelProvider();
129 * Override in subclasses.
130 * Will run before initModel()
132 protected void init() {
137 * <p>getSelectionFromDialog</p>
139 * @param dialog a {@link eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog} object.
140 * @param <TYPE> a TYPE object.
141 * @return a TYPE object.
143 protected static <TYPE
extends CdmBase
> TYPE
getSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog
<TYPE
> dialog
) {
144 //dialog.setInitialPattern("");
145 int result
= dialog
.open();
147 if (result
== Window
.CANCEL
) {
151 UUID uuid
= dialog
.getSelectedUuidAndTitleCache().getUuid();
155 return dialog
.getCdmObjectByUuid(uuid
);
159 * Check if object was created during the life of this dialog. If not,
160 * retrieve it from the CdmStore.
162 * @param cdmUuid a {@link java.util.UUID} object.
163 * @return a T object.
165 protected T
getCdmObjectByUuid(UUID cdmUuid
) {
166 for (T cdmObject
: transientCdmObjects
) {
167 if (cdmObject
.getUuid().equals(cdmUuid
)) {
171 return getPersistentObject(cdmUuid
);
175 * <p>getPersistentObject</p>
177 * @param uuid a {@link java.util.UUID} object.
178 * @return a T object.
180 abstract protected T
getPersistentObject(UUID uuid
);
185 // protected void addObjectToModel(T cdmObject) {
186 // model.add(new UuidAndTitleCache(cdmObject.getClass(), cdmObject.getUuid(), getTitle(cdmObject)));
187 // transientCdmObjects.add(cdmObject);
191 * <p>isObjectTransient</p>
193 * @param cdmObject a T object.
196 protected boolean isObjectTransient(T cdmObject
) {
197 return (getPersistentObject(cdmObject
.getUuid()) == null);
203 * @param cdmObject a T object.
204 * @return a {@link java.lang.String} object.
206 protected String
getTitle(T cdmObject
) {
207 if(cdmObject
== null){
211 if (cdmObject
instanceof IIdentifiableEntity
) {
212 return ((IIdentifiableEntity
) cdmObject
).getTitleCache();
215 throw new IllegalArgumentException("Generic method only" +
216 " supports cdmObject of type IIdentifiableEntity." +
217 " Please implement specific method in subclass.");
223 public void refresh() {
225 filterExcludedObjects();
232 abstract protected void initModel();
235 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
239 protected ItemsFilter
createFilter() {
240 return new ItemsFilter() {
243 * Always returns false to enforce refiltering even if the pattern is equal
246 public boolean equalsFilter(ItemsFilter filter
) {
251 public boolean isConsistentItem(Object item
) {
256 public boolean matchItem(Object item
) {
258 if(item
instanceof UuidAndTitleCache
){
259 text
= ((UuidAndTitleCache
) item
).getTitleCache();
260 }else if(item
instanceof String
){
261 text
= (String
) item
;
263 return text
!= null ?
matches(text
) : false;
271 * Set the filter input to the Agent's title cache
273 * @param cdmObject a T object.
275 protected void setPattern(T cdmObject
) {
276 // FilteredSelection does some very tricky caching to make sure it
277 // runs with high performance.
278 // This works for most use cases, but we want to change the model while the dialog is open
279 // and all the clever caching prevents the content provider from knowing that the model has changed
280 // I am aware, that this is a hack, but the FilteredSelectionDialog API does not offer a convenient
281 // way to solve the problem.
283 Field lastCompletedFilter
= this.getClass().getSuperclass().getSuperclass().getDeclaredField("lastCompletedFilter");
284 lastCompletedFilter
.setAccessible(true);
285 lastCompletedFilter
.set(this, null);
286 } catch (SecurityException e
) {
287 MessagingUtils
.error(getClass(), e
);
288 } catch (NoSuchFieldException e
) {
289 MessagingUtils
.error(getClass(), e
);
290 } catch (IllegalArgumentException e
) {
291 MessagingUtils
.error(getClass(), e
);
292 } catch (IllegalAccessException e
) {
293 MessagingUtils
.error(getClass(), e
);
296 // this also is not the nicest way to do it.
297 // I am still amazed, that FilteredSelectionDialog does not offer any methods to change its data
298 // once it was opened. Am I doing it wrong?
299 String pattern
= getTitle(cdmObject
);
300 ((Text
) getPatternControl()).setText(pattern
);
304 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider, org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor)
308 protected void fillContentProvider(AbstractContentProvider contentProvider
,
309 ItemsFilter itemsFilter
, IProgressMonitor progressMonitor
)
310 throws CoreException
{
313 progressMonitor
.beginTask("Looking for entities", model
.size());
314 for(UuidAndTitleCache
<T
> element
: model
){
315 contentProvider
.add(element
, itemsFilter
);
316 if (progressMonitor
.isCanceled()) {
317 throw new OperationCanceledException();
319 progressMonitor
.worked(1);
322 MessagingUtils
.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
326 progressMonitor
.done();
331 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
335 protected IDialogSettings
getDialogSettings() {
336 IDialogSettings settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().getSection(getSettings());
338 if (settings
== null) {
339 settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().addNewSection(getSettings());
345 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
349 public String
getElementName(Object item
) {
350 return ((UuidAndTitleCache
) item
).getTitleCache();
354 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
358 protected Comparator
getItemsComparator() {
359 return new Comparator
<UuidAndTitleCache
>() {
361 public int compare(UuidAndTitleCache entity1
,
362 UuidAndTitleCache entity2
) {
363 Collator collator
= Collator
.getInstance();
364 return collator
.compare(entity1
.getTitleCache(), entity2
.getTitleCache());
370 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#validateItem(java.lang.Object)
374 protected IStatus
validateItem(Object item
) {
375 return Status
.OK_STATUS
;
379 * <p>getSelectedUuidAndTitleCache</p>
381 * @return a {@link eu.etaxonomy.cdm.model.common.UuidAndTitleCache} object.
383 protected UuidAndTitleCache
getSelectedUuidAndTitleCache() {
384 Object
[] result
= getResult();
385 return result
[0] == null ?
null : (UuidAndTitleCache
) result
[0];
389 * <p>Getter for the field <code>settings</code>.</p>
391 * @return a {@link java.lang.String} object.
393 public String
getSettings() {
394 if(settings
== null){
395 throw new IllegalStateException("No SETTINGS set.");
403 * @created Oct 19, 2009
406 private class ResourceSelectionHistory
extends SelectionHistory
{
408 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#restoreItemFromMemento(org.eclipse.ui.IMemento)
411 protected Object
restoreItemFromMemento(IMemento element
) {
412 return element
.getString("resource"); //$NON-NLS-1$
415 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#storeItemToMemento(java.lang.Object,
416 * org.eclipse.ui.IMemento)
419 protected void storeItemToMemento(Object item
, IMemento element
) {
420 element
.putString("resource", item
.toString()); //$NON-NLS-1$
425 * <p>getNewWizardLinkText</p>
427 * @return a {@link java.lang.String} object.
429 protected abstract String
getNewWizardLinkText();
432 * <p>getNewEntityWizard</p>
434 * @return a {@link eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard} object.
436 protected abstract AbstractNewEntityWizard
getNewEntityWizard(String parameter
);
438 public class FilteredCdmResourceLabelProvider
extends LabelProvider
{
440 public String
getText(Object element
) {
441 if (element
== null) {
444 UuidAndTitleCache uuidAndTitleCache
= (UuidAndTitleCache
) element
;
445 String titleCache
= uuidAndTitleCache
.getTitleCache();
446 if(PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG
)){
447 titleCache
+= " ["+uuidAndTitleCache
.getId()+"]";
454 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
458 protected Control
createExtendedContentArea(Composite parent
) {
459 String newWizardLinkText
= getNewWizardLinkText();
460 if(newWizardLinkText
!= null){
461 Link link
= new Link(parent
, SWT
.NONE
);
462 link
.setText(newWizardLinkText
);
463 link
.addSelectionListener(getNewWizardLinkSelectionListener());
469 protected SelectionListener
getNewWizardLinkSelectionListener(){
470 return new SelectionAdapter() {
473 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
476 public void widgetSelected(SelectionEvent e
) {
478 AbstractNewEntityWizard wizard
= getNewEntityWizard(e
.text
);
480 wizard
.init(null, null);
481 if(wizard
.getEntity() != null) {
482 WizardDialog dialog
= new WizardDialog(getShell(), wizard
);
483 int status
= dialog
.open();
485 if (status
== IStatus
.OK
) {
487 T entity
= (T
) wizard
.getEntity();
488 model
.add(new UuidAndTitleCache
<T
>(entity
.getUuid(),
493 getConversationHolder().bind();
495 //FIXME : Need to make sure this is a stable fix (ticket 3822)
496 getConversationHolder().commit();
503 * <p>getConversationHolder</p>
505 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
508 public ConversationHolder
getConversationHolder() {
514 public void update(CdmDataChangeMap changeEvents
) {}
517 * Don't want to add for example a taxon or synonym to itself
518 * so filter the list to remove the taxon in question
519 * (<code>cdmBaseToBeFiltered</code>)
520 * so it is not available in the filtered list.
522 private void filterExcludedObjects() {
523 if (model
!= null && cdmBaseToBeFiltered
!= null) {
525 UuidAndTitleCache uuidAndTitleCacheToRemove
= null;
527 for (UuidAndTitleCache uuidAndTitleCache
: model
){
528 if ((cdmBaseToBeFiltered
.getUuid()).equals(uuidAndTitleCache
.getUuid())) {
529 uuidAndTitleCacheToRemove
= uuidAndTitleCache
;
532 model
.remove(uuidAndTitleCacheToRemove
);