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
.ArrayList
;
14 import java
.util
.Collections
;
15 import java
.util
.Comparator
;
16 import java
.util
.HashSet
;
17 import java
.util
.Iterator
;
18 import java
.util
.List
;
20 import java
.util
.UUID
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.IStatus
;
24 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
25 import org
.eclipse
.jface
.viewers
.ILabelProvider
;
26 import org
.eclipse
.jface
.viewers
.LabelProvider
;
27 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
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
.Button
;
36 import org
.eclipse
.swt
.widgets
.Composite
;
37 import org
.eclipse
.swt
.widgets
.Control
;
38 import org
.eclipse
.swt
.widgets
.Shell
;
39 import org
.eclipse
.swt
.widgets
.Text
;
41 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
42 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
43 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
44 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
45 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
46 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
47 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
48 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
49 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
50 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
53 * <p>Abstract AbstractFilteredCdmResourceSelectionDialog class.</p>
59 public abstract class AbstractFilteredCdmResourceSelectionDialog
<T
extends ICdmBase
> extends
60 SearchDialog
{//implements IConversationEnabled {
62 // private final ConversationHolder conversation = null;
64 protected List
<UuidAndTitleCache
<T
>> model
;
65 private final Set
<T
> transientCdmObjects
= new HashSet
<T
>();
66 private final String settings
;
67 protected final int limitOfInitialElements
= 100;
69 private T selectedObject
;
71 protected T cdmBaseToBeFiltered
;
75 * <p>Constructor for AbstractFilteredCdmResourceSelectionDialog.</p>
77 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
79 * @param title a {@link java.lang.String} object.
80 * @param multi a boolean.
81 * @param settings a {@link java.lang.String} object.
82 * @param cdmObject a T object.
83 * @param <T> a T object.
85 protected AbstractFilteredCdmResourceSelectionDialog(Shell shell
, //ConversationHolder conversation,
86 String title
, boolean multi
, String settings
, T cdmObject
) {
88 setShellStyle(SWT
.DIALOG_TRIM
);
90 setMessage(Messages
.SearchDialog_patternLabel
);
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
));
98 shell
.setCursor(cursor
);
99 setListLabelProvider(createListLabelProvider());
104 * By default, we are returning the standard list label provider
106 * Override in subclasses if you want different behavior
110 protected ILabelProvider
createDetailsLabelProvider() {
111 return createListLabelProvider();
118 protected ILabelProvider
createListLabelProvider() {
119 return new FilteredCdmResourceLabelProvider();
123 * Override in subclasses.
124 * Will run before initModel()
126 protected void init() {
131 * <p>getSelectionFromDialog</p>
133 * @param dialog a {@link eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog} object.
134 * @param <TYPE> a TYPE object.
135 * @return a TYPE object.
137 protected static <TYPE
extends CdmBase
> TYPE
getSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog
<TYPE
> dialog
) {
138 int result
= dialog
.open();
140 if (result
== Window
.CANCEL
) {
144 UuidAndTitleCache uuid
= dialog
.getSelectedUuidAndTitleCache();
148 return dialog
.getCdmObjectByUuid(uuid
.getUuid());
152 * Check if object was created during the life of this dialog. If not,
153 * retrieve it from the CdmStore.
155 * @param cdmUuid a {@link java.util.UUID} object.
156 * @return a T object.
158 protected T
getCdmObjectByUuid(UUID cdmUuid
) {
159 for (T cdmObject
: transientCdmObjects
) {
160 if (cdmObject
.getUuid().equals(cdmUuid
)) {
164 return getPersistentObject(cdmUuid
);
168 * <p>getPersistentObject</p>
170 * @param uuid a {@link java.util.UUID} object.
171 * @return a T object.
173 abstract protected T
getPersistentObject(UUID uuid
);
177 * <p>isObjectTransient</p>
179 * @param cdmObject a T object.
182 protected boolean isObjectTransient(T cdmObject
) {
183 return (getPersistentObject(cdmObject
.getUuid()) == null);
189 * @param cdmObject a T object.
190 * @return a {@link java.lang.String} object.
192 protected String
getTitle(T cdmObject
) {
193 if(cdmObject
== null){
197 if (cdmObject
instanceof IIdentifiableEntity
) {
198 return ((IIdentifiableEntity
) cdmObject
).getTitleCache();
201 throw new IllegalArgumentException("Generic method only" +
202 " supports cdmObject of type IIdentifiableEntity." +
203 " Please implement specific method in subclass.");
209 * Set the filter input to the Agent's title cache
211 * @param cdmObject a T object.
213 protected void setPattern(T cdmObject
) {
214 String pattern
= getTitle(cdmObject
);
215 getSearchField().setText(pattern
);
221 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider, org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor)
226 protected void fillContentProvider(IProgressMonitor progressMonitor
)
230 model
= new ArrayList
<UuidAndTitleCache
<T
>>();
233 if (progressMonitor
!= null){
234 progressMonitor
.beginTask("Looking for entities", model
.size());
236 Collections
.sort(model
, getItemsComparator());
237 contentProvider
.reset();
238 Iterator
<UuidAndTitleCache
<T
>> iterator
= model
.iterator();
239 UuidAndTitleCache
<T
> element
;
240 while(iterator
.hasNext()){
241 element
= iterator
.next();
242 if (!element
.equals(cdmBaseToBeFiltered
)){
243 contentProvider
.add(element
);
245 if (progressMonitor
!= null){
246 if (progressMonitor
.isCanceled()) {
249 progressMonitor
.worked(1);
255 MessagingUtils
.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
259 if (progressMonitor
!= null) {
260 progressMonitor
.done();
266 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
270 protected IDialogSettings
getDialogSettings() {
271 IDialogSettings settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().getSection(getSettings());
273 if (settings
== null) {
274 settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().addNewSection(getSettings());
280 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
284 public String
getElementName(Object item
) {
285 return ((UuidAndTitleCache
) item
).getTitleCache();
289 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
294 protected Comparator
getItemsComparator() {
295 return new Comparator
<UuidAndTitleCache
>() {
297 public int compare(UuidAndTitleCache entity1
,
298 UuidAndTitleCache entity2
) {
299 Collator collator
= Collator
.getInstance();
300 if (entity1
== entity2
){
304 if (entity1
== null && entity2
!= null){
307 if (entity2
== null && entity1
!= null){
310 if (entity1
.getUuid().equals(entity2
.getUuid())){
313 if (entity1
.getTitleCache() == null && entity2
.getTitleCache() != null){
316 if (entity2
.getTitleCache() == null){
319 int result
= collator
.compare(entity1
.getTitleCache(), entity2
.getTitleCache());
321 result
= entity1
.getUuid().compareTo(entity2
.getUuid());
330 * <p>getSelectedUuidAndTitleCache</p>
332 * @return a {@link eu.etaxonomy.cdm.model.common.UuidAndTitleCache} object.
334 protected UuidAndTitleCache
getSelectedUuidAndTitleCache() {
335 Object result
= getResult();
336 if (result
instanceof UuidAndTitleCache
){
337 return (UuidAndTitleCache
) result
;
345 private Object
getResult() {
346 StructuredSelection selection
= getCurrentSelection();
347 if (selection
== null){
350 return selection
.getFirstElement();
354 * <p>Getter for the field <code>settings</code>.</p>
356 * @return a {@link java.lang.String} object.
358 public String
getSettings() {
359 if(settings
== null){
360 throw new IllegalStateException("No SETTINGS set.");
368 * <p>getNewWizardLinkText</p>
370 * @return a {@link java.lang.String} object.
372 protected abstract String
[] getNewWizardText();
375 * <p>getNewEntityWizard</p>
377 * @return a {@link eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard} object.
379 protected abstract AbstractNewEntityWizard
getNewEntityWizard(String parameter
);
381 public class FilteredCdmResourceLabelProvider
extends LabelProvider
{
383 public String
getText(Object element
) {
384 if (element
== null) {
387 UuidAndTitleCache uuidAndTitleCache
= (UuidAndTitleCache
) element
;
388 String titleCache
= uuidAndTitleCache
.getTitleCache();
389 if(PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG
)){
390 titleCache
+= " ["+uuidAndTitleCache
.getId()+"]";
397 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
402 // protected Control createExtendedContentArea(Composite parent) {
403 // String newWizardLinkText = getNewWizardLinkText();
404 //// if(newWizardLinkText != null){
405 //// newButton1 = this.createButton(this.getShell(), new_id, newWizardLinkText, false);
407 //// newButton1.addSelectionListener(getNewWizardLinkSelectionListener());
408 //// return newButton1;
414 protected void createButtonsForButtonBar(Composite parent
) {
415 String
[] newButtonText
= getNewWizardText();
416 if (newButtonText
!= null){
417 this.newButton1
= createButton(parent
, this.new_id
, newButtonText
[0], false);
418 newButton1
.addSelectionListener(getNewWizardButtonSelectionListener());
419 if (newButtonText
.length
> 1){
420 newButton2
= createButton(parent
, this.new_id2
, newButtonText
[1], false);
421 newButton2
.addSelectionListener(getNewWizardButtonSelectionListener());
424 //Label label = new Label(parent, SWT.NONE);
425 super.createButtonsForButtonBar(parent
);
428 protected SelectionListener
getNewWizardButtonSelectionListener(){
429 return new SelectionAdapter() {
432 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
435 public void widgetSelected(SelectionEvent e
) {
436 Object source
= e
.getSource();
438 if (source
instanceof Button
){
439 Button sourceButton
= (Button
) source
;
440 text
= sourceButton
.getText();
442 AbstractNewEntityWizard wizard
= getNewEntityWizard(text
);
444 wizard
.init(null, null);
445 if(wizard
.getEntity() != null) {
446 WizardDialog dialog
= new WizardDialog(getShell(), wizard
);
447 int status
= dialog
.open();
449 if (status
== IStatus
.OK
) {
451 T entity
= (T
) wizard
.getEntity();
454 // if (getConversationHolder() != null){
455 // getConversationHolder().bind();
458 //FIXME : Need to make sure this is a stable fix (ticket 3822)
459 // if (getConversationHolder() != null){
460 // getConversationHolder().commit();
469 * <p>getConversationHolder</p>
471 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
474 // public ConversationHolder getConversationHolder() {
475 // return conversation;
480 // public void update(CdmDataChangeMap changeEvents) {}
483 * Don't want to add for example a taxon or synonym to itself
484 * so filter the list to remove the taxon in question
485 * (<code>cdmBaseToBeFiltered</code>)
486 * so it is not available in the filtered list.
488 private void filterExcludedObjects() {
489 if (model
!= null && cdmBaseToBeFiltered
!= null) {
491 UuidAndTitleCache uuidAndTitleCacheToRemove
= null;
493 for (UuidAndTitleCache uuidAndTitleCache
: model
){
494 if ((cdmBaseToBeFiltered
.getUuid()).equals(uuidAndTitleCache
.getUuid())) {
495 uuidAndTitleCacheToRemove
= uuidAndTitleCache
;
498 model
.remove(uuidAndTitleCacheToRemove
);
502 void createFilterButton(Composite searchAndFilter
){
503 //as default no filter button available
508 protected void search() {
509 Control control
=getSearchField();
510 String pattern
= null;
511 if (control
!= null){
512 pattern
= ((Text
)control
).getText();
513 if (pattern
.equals("*") || pattern
.equals("?")){
515 }else if (pattern
!= null){
516 callService(pattern
);
518 fillContentProvider(null);
521 // if (pattern.equals("?")){
522 // model = CdmStore.getService(INameService.class).getUuidAndTitleCache(null, null);
523 // }else if (pattern != null){
524 // model = CdmStore.getService(INameService.class).getUuidAndTitleCache(limitOfInitialElements, pattern);
528 abstract void callService(String pattern
);