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
.model
.common
.UuidAndTitleCache
;
49 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
50 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
51 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
52 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
55 * <p>Abstract AbstractFilteredCdmResourceSelectionDialog class.</p>
61 public abstract class AbstractFilteredCdmResourceSelectionDialog
<T
extends ICdmBase
> extends
62 FilteredItemsSelectionDialog
implements IConversationEnabled
{
64 private ConversationHolder conversation
;
66 protected List
<UuidAndTitleCache
<T
>> model
;
67 private Set
<T
> transientCdmObjects
= new HashSet
<T
>();
68 private String settings
;
70 protected T cdmBaseToBeFiltered
;
73 * <p>Constructor for AbstractFilteredCdmResourceSelectionDialog.</p>
75 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
77 * @param title a {@link java.lang.String} object.
78 * @param multi a boolean.
79 * @param settings a {@link java.lang.String} object.
80 * @param cdmObject a T object.
81 * @param <T> a T object.
83 protected AbstractFilteredCdmResourceSelectionDialog(Shell shell
, ConversationHolder conversation
, String title
, boolean multi
, String settings
, T cdmObject
) {
86 setMessage("Use * for wildcard, or ? to see all entries");
87 this.settings
= settings
;
89 this.conversation
= conversation
;
95 String objectTitle
= getTitle(cdmObject
);
96 if (objectTitle
!= null) {
97 setInitialPattern(objectTitle
);
100 setListLabelProvider(createListLabelProvider());
101 setDetailsLabelProvider(createDetailsLabelProvider());
103 setSelectionHistory(new ResourceSelectionHistory());
107 * By default, we are returning the standard list label provider
109 * Override in subclasses if you want different behavior
113 protected ILabelProvider
createDetailsLabelProvider() {
114 return createListLabelProvider();
121 protected ILabelProvider
createListLabelProvider() {
122 return new FilteredCdmResourceLabelProvider();
126 * Override in subclasses.
127 * Will run before initModel()
129 protected void init() {
134 * <p>getSelectionFromDialog</p>
136 * @param dialog a {@link eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog} object.
137 * @param <TYPE> a TYPE object.
138 * @return a TYPE object.
140 protected static <TYPE
extends CdmBase
> TYPE
getSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog
<TYPE
> dialog
) {
141 //dialog.setInitialPattern("");
142 int result
= dialog
.open();
144 if (result
== Window
.CANCEL
) {
148 UUID uuid
= dialog
.getSelectedUuidAndTitleCache().getUuid();
152 return dialog
.getCdmObjectByUuid(uuid
);
156 * Check if object was created during the life of this dialog. If not,
157 * retrieve it from the CdmStore.
159 * @param cdmUuid a {@link java.util.UUID} object.
160 * @return a T object.
162 protected T
getCdmObjectByUuid(UUID cdmUuid
) {
163 for (T cdmObject
: transientCdmObjects
) {
164 if (cdmObject
.getUuid().equals(cdmUuid
)) {
168 return getPersistentObject(cdmUuid
);
172 * <p>getPersistentObject</p>
174 * @param uuid a {@link java.util.UUID} object.
175 * @return a T object.
177 abstract protected T
getPersistentObject(UUID uuid
);
182 // protected void addObjectToModel(T cdmObject) {
183 // model.add(new UuidAndTitleCache(cdmObject.getClass(), cdmObject.getUuid(), getTitle(cdmObject)));
184 // transientCdmObjects.add(cdmObject);
188 * <p>isObjectTransient</p>
190 * @param cdmObject a T object.
193 protected boolean isObjectTransient(T cdmObject
) {
194 return (getPersistentObject(cdmObject
.getUuid()) == null);
200 * @param cdmObject a T object.
201 * @return a {@link java.lang.String} object.
203 protected String
getTitle(T cdmObject
) {
204 if(cdmObject
== null){
208 if (cdmObject
instanceof IIdentifiableEntity
) {
209 return ((IIdentifiableEntity
) cdmObject
).getTitleCache();
212 throw new IllegalArgumentException("Generic method only" +
213 " supports cdmObject of type IIdentifiableEntity." +
214 " Please implement specific method in subclass.");
220 public void refresh() {
222 filterExcludedObjects();
229 abstract protected void initModel();
232 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
236 protected ItemsFilter
createFilter() {
237 return new ItemsFilter() {
240 * Always returns false to enforce refiltering even if the pattern is equal
243 public boolean equalsFilter(ItemsFilter filter
) {
248 public boolean isConsistentItem(Object item
) {
253 public boolean matchItem(Object item
) {
255 if(item
instanceof UuidAndTitleCache
){
256 text
= ((UuidAndTitleCache
) item
).getTitleCache();
257 }else if(item
instanceof String
){
258 text
= (String
) item
;
260 return text
!= null ?
matches(text
) : false;
268 * Set the filter input to the Agent's title cache
270 * @param cdmObject a T object.
272 protected void setPattern(T cdmObject
) {
273 // FilteredSelection does some very tricky caching to make sure it
274 // runs with high performance.
275 // This works for most use cases, but we want to change the model while the dialog is open
276 // and all the clever caching prevents the content provider from knowing that the model has changed
277 // I am aware, that this is a hack, but the FilteredSelectionDialog API does not offer a convenient
278 // way to solve the problem.
280 Field lastCompletedFilter
= this.getClass().getSuperclass().getSuperclass().getDeclaredField("lastCompletedFilter");
281 lastCompletedFilter
.setAccessible(true);
282 lastCompletedFilter
.set(this, null);
283 } catch (SecurityException e
) {
284 StoreUtil
.error(getClass(), e
);
285 } catch (NoSuchFieldException e
) {
286 StoreUtil
.error(getClass(), e
);
287 } catch (IllegalArgumentException e
) {
288 StoreUtil
.error(getClass(), e
);
289 } catch (IllegalAccessException e
) {
290 StoreUtil
.error(getClass(), e
);
293 // this also is not the nicest way to do it.
294 // I am still amazed, that FilteredSelectionDialog does not offer any methods to change its data
295 // once it was opened. Am I doing it wrong?
296 String pattern
= getTitle(cdmObject
);
297 ((Text
) getPatternControl()).setText(pattern
);
301 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider, org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor)
305 protected void fillContentProvider(AbstractContentProvider contentProvider
,
306 ItemsFilter itemsFilter
, IProgressMonitor progressMonitor
)
307 throws CoreException
{
310 progressMonitor
.beginTask("Looking for entities", model
.size());
311 for(UuidAndTitleCache
<T
> element
: model
){
312 contentProvider
.add(element
, itemsFilter
);
313 if (progressMonitor
.isCanceled()) {
314 throw new OperationCanceledException();
316 progressMonitor
.worked(1);
319 StoreUtil
.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
323 progressMonitor
.done();
328 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
332 protected IDialogSettings
getDialogSettings() {
333 IDialogSettings settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().getSection(getSettings());
335 if (settings
== null) {
336 settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().addNewSection(getSettings());
342 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
346 public String
getElementName(Object item
) {
347 return ((UuidAndTitleCache
) item
).getTitleCache();
351 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
355 protected Comparator
getItemsComparator() {
356 return new Comparator
<UuidAndTitleCache
>() {
357 public int compare(UuidAndTitleCache entity1
,
358 UuidAndTitleCache entity2
) {
359 Collator collator
= Collator
.getInstance();
360 return collator
.compare(entity1
.getTitleCache(), entity2
.getTitleCache());
366 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#validateItem(java.lang.Object)
370 protected IStatus
validateItem(Object item
) {
371 return Status
.OK_STATUS
;
375 * <p>getSelectedUuidAndTitleCache</p>
377 * @return a {@link eu.etaxonomy.cdm.model.common.UuidAndTitleCache} object.
379 protected UuidAndTitleCache
getSelectedUuidAndTitleCache() {
380 Object
[] result
= getResult();
381 return result
[0] == null ?
null : (UuidAndTitleCache
) result
[0];
385 * <p>Getter for the field <code>settings</code>.</p>
387 * @return a {@link java.lang.String} object.
389 public String
getSettings() {
390 if(settings
== null){
391 throw new IllegalStateException("No SETTINGS set.");
399 * @created Oct 19, 2009
402 private class ResourceSelectionHistory
extends SelectionHistory
{
404 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#restoreItemFromMemento(org.eclipse.ui.IMemento)
406 protected Object
restoreItemFromMemento(IMemento element
) {
407 return element
.getString("resource"); //$NON-NLS-1$
410 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#storeItemToMemento(java.lang.Object,
411 * org.eclipse.ui.IMemento)
413 protected void storeItemToMemento(Object item
, IMemento element
) {
414 element
.putString("resource", item
.toString()); //$NON-NLS-1$
419 * <p>getNewWizardLinkText</p>
421 * @return a {@link java.lang.String} object.
423 protected abstract String
getNewWizardLinkText();
426 * <p>getNewEntityWizard</p>
428 * @return a {@link eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard} object.
430 protected abstract AbstractNewEntityWizard
getNewEntityWizard(String parameter
);
432 public class FilteredCdmResourceLabelProvider
extends LabelProvider
{
433 public String
getText(Object element
) {
434 if (element
== null) {
437 return ((UuidAndTitleCache
) element
).getTitleCache();
442 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
446 protected Control
createExtendedContentArea(Composite parent
) {
447 if(getNewWizardLinkText() != null){
448 Link link
= new Link(parent
, SWT
.NONE
);
449 link
.setText(getNewWizardLinkText());
450 link
.addSelectionListener(getNewWizardLinkSelectionListener());
456 protected SelectionListener
getNewWizardLinkSelectionListener(){
457 return new SelectionAdapter() {
460 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
463 public void widgetSelected(SelectionEvent e
) {
465 AbstractNewEntityWizard wizard
= getNewEntityWizard(e
.text
);
466 wizard
.init(null, null);
467 WizardDialog dialog
= new WizardDialog(getShell(), wizard
);
468 int status
= dialog
.open();
470 if (status
== IStatus
.OK
) {
472 T entity
= (T
) wizard
.getEntity();
474 // addObjectToModel(teamOrPerson);
477 getConversationHolder().bind();
484 * <p>getConversationHolder</p>
486 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
488 public ConversationHolder
getConversationHolder() {
493 public void update(CdmDataChangeMap changeEvents
) {}
496 * Don't want to add a taxon or synonym to itself so filter the list to remove the taxon in question
497 * so it is not available to add as a concept relation
499 private void filterExcludedObjects() {
500 if (model
!= null && cdmBaseToBeFiltered
!= null) {
502 UuidAndTitleCache uuidAndTitleCacheToRemove
= null;
504 for (UuidAndTitleCache uuidAndTitleCache
: model
){
505 if ((cdmBaseToBeFiltered
.getUuid()).equals(uuidAndTitleCache
.getUuid())) {
506 uuidAndTitleCacheToRemove
= uuidAndTitleCache
;
509 model
.remove(uuidAndTitleCacheToRemove
);