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
.apache
.commons
.lang
.StringUtils
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
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
.viewers
.StructuredSelection
;
30 import org
.eclipse
.jface
.window
.Window
;
31 import org
.eclipse
.jface
.wizard
.WizardDialog
;
32 import org
.eclipse
.swt
.SWT
;
33 import org
.eclipse
.swt
.events
.SelectionAdapter
;
34 import org
.eclipse
.swt
.events
.SelectionEvent
;
35 import org
.eclipse
.swt
.events
.SelectionListener
;
36 import org
.eclipse
.swt
.graphics
.Cursor
;
37 import org
.eclipse
.swt
.layout
.GridData
;
38 import org
.eclipse
.swt
.layout
.GridLayout
;
39 import org
.eclipse
.swt
.widgets
.Button
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.swt
.widgets
.Control
;
42 import org
.eclipse
.swt
.widgets
.Shell
;
43 import org
.eclipse
.swt
.widgets
.Text
;
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
.taxeditor
.l10n
.Messages
;
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 SearchDialog
{//implements IConversationEnabled {
66 // private final ConversationHolder conversation = null;
68 protected List
<UuidAndTitleCache
<T
>> model
;
69 private final Set
<T
> transientCdmObjects
= new HashSet
<T
>();
70 private final String settings
;
71 protected final int limitOfInitialElements
= 100;
73 private T selectedObject
;
75 protected Set
<UUID
> cdmBaseToBeFiltered
;
79 * <p>Constructor for AbstractFilteredCdmResourceSelectionDialog.</p>
81 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
83 * @param title a {@link java.lang.String} object.
84 * @param multi a boolean.
85 * @param settings a {@link java.lang.String} object.
86 * @param cdmObject a T object.
87 * @param <T> a T object.
89 protected AbstractFilteredCdmResourceSelectionDialog(Shell shell
, //ConversationHolder conversation,
90 String title
, boolean multi
, String settings
, T cdmObject
) {
92 setShellStyle(SWT
.DIALOG_TRIM
);
93 setMessage(Messages
.SearchDialog_patternLabel
);
94 this.settings
= settings
;
95 if (cdmObject
!= null){
96 this.cdmBaseToBeFiltered
= new HashSet
<>();
97 this.cdmBaseToBeFiltered
.add(cdmObject
.getUuid());
99 Cursor cursor
= shell
.getCursor();
100 shell
.setCursor(shell
.getDisplay().getSystemCursor(SWT
.CURSOR_WAIT
));
102 shell
.setCursor(cursor
);
103 setListLabelProvider(createListLabelProvider());
108 * By default, we are returning the standard list label provider
110 * Override in subclasses if you want different behavior
114 protected ILabelProvider
createDetailsLabelProvider() {
115 return createListLabelProvider();
122 protected ILabelProvider
createListLabelProvider() {
123 return new FilteredCdmResourceLabelProvider();
127 * Override in subclasses.
128 * Will run before initModel()
130 protected void init() {
135 * <p>getSelectionFromDialog</p>
137 * @param dialog a {@link eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog} object.
138 * @param <TYPE> a TYPE object.
139 * @return a TYPE object.
141 protected static <TYPE
extends CdmBase
> TYPE
getSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog
<TYPE
> dialog
) {
142 UuidAndTitleCache result
= getUuidAndTitleCacheSelectionFromDialog(dialog
);
144 return dialog
.getCdmObjectByUuid(result
.getUuid());
151 * <p>getSelectionFromDialog</p>
153 * @param dialog a {@link eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog} object.
154 * @param <TYPE> a TYPE object.
155 * @return a TYPE object.
157 protected static UuidAndTitleCache
getUuidAndTitleCacheSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog dialog
) {
161 int result
= dialog
.open();
163 if (result
== Window
.CANCEL
) {
167 UuidAndTitleCache uuid
= dialog
.getSelectedUuidAndTitleCache();
173 * Check if object was created during the life of this dialog. If not,
174 * retrieve it from the CdmStore.
176 * @param cdmUuid a {@link java.util.UUID} object.
177 * @return a T object.
179 protected T
getCdmObjectByUuid(UUID cdmUuid
) {
180 for (T cdmObject
: transientCdmObjects
) {
181 if (cdmObject
.getUuid().equals(cdmUuid
)) {
185 return getPersistentObject(cdmUuid
);
189 * <p>getPersistentObject</p>
191 * @param uuid a {@link java.util.UUID} object.
192 * @return a T object.
194 abstract protected T
getPersistentObject(UUID uuid
);
198 * <p>isObjectTransient</p>
200 * @param cdmObject a T object.
203 protected boolean isObjectTransient(T cdmObject
) {
204 return (getPersistentObject(cdmObject
.getUuid()) == null);
210 * @param cdmObject a T object.
211 * @return a {@link java.lang.String} object.
213 protected String
getTitle(T cdmObject
) {
214 if(cdmObject
== null){
218 if (cdmObject
instanceof IIdentifiableEntity
) {
219 return ((IIdentifiableEntity
) cdmObject
).getTitleCache();
222 throw new IllegalArgumentException("Generic method only" +
223 " supports cdmObject of type IIdentifiableEntity." +
224 " Please implement specific method in subclass.");
230 * Set the filter input to the Agent's title cache
232 * @param cdmObject a T object.
234 protected void setPattern(T cdmObject
) {
235 String pattern
= getTitle(cdmObject
);
236 getSearchField().setText(pattern
);
242 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider, org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor)
247 protected void fillContentProvider(IProgressMonitor progressMonitor
)
251 model
= new ArrayList
<UuidAndTitleCache
<T
>>();
254 if (progressMonitor
!= null){
255 progressMonitor
.beginTask("Looking for entities", model
.size());
259 contentProvider
.reset();
260 Iterator
<UuidAndTitleCache
<T
>> iterator
= model
.iterator();
261 UuidAndTitleCache
<T
> element
;
262 while(iterator
.hasNext()){
263 element
= iterator
.next();
265 if (cdmBaseToBeFiltered
== null || !cdmBaseToBeFiltered
.contains(element
.getUuid())){
266 contentProvider
.add(element
);
268 if (progressMonitor
!= null){
269 if (progressMonitor
.isCanceled()) {
272 progressMonitor
.worked(1);
278 MessagingUtils
.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
282 if (progressMonitor
!= null) {
283 progressMonitor
.done();
289 protected void sort() {
290 Collections
.sort(model
, getItemsComparator());
294 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
298 protected IDialogSettings
getDialogSettings() {
299 IDialogSettings settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().getSection(getSettings());
301 if (settings
== null) {
302 settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().addNewSection(getSettings());
308 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
312 public String
getElementName(Object item
) {
313 return ((UuidAndTitleCache
) item
).getTitleCache();
317 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
322 protected Comparator
getItemsComparator() {
323 return new Comparator
<UuidAndTitleCache
>() {
325 public int compare(UuidAndTitleCache entity1
,
326 UuidAndTitleCache entity2
) {
327 Collator collator
= Collator
.getInstance();
328 if (entity1
== entity2
){
332 if (entity1
== null && entity2
!= null){
335 if (entity2
== null && entity1
!= null){
338 if (entity1
.getUuid().equals(entity2
.getUuid())){
341 if (entity1
.getTitleCache() == null && entity2
.getTitleCache() != null){
344 if (entity2
.getTitleCache() == null){
347 int result
= collator
.compare(entity1
.getTitleCache(), entity2
.getTitleCache());
349 result
= entity1
.getUuid().compareTo(entity2
.getUuid());
358 * <p>getSelectedUuidAndTitleCache</p>
360 * @return a {@link eu.etaxonomy.cdm.model.common.UuidAndTitleCache} object.
362 protected UuidAndTitleCache
getSelectedUuidAndTitleCache() {
363 Object result
= getResult();
364 if (result
instanceof UuidAndTitleCache
){
365 return (UuidAndTitleCache
) result
;
373 private Object
getResult() {
374 StructuredSelection selection
= getCurrentSelection();
375 if (selection
== null){
378 return selection
.getFirstElement();
382 * <p>Getter for the field <code>settings</code>.</p>
384 * @return a {@link java.lang.String} object.
386 public String
getSettings() {
387 if(settings
== null){
388 throw new IllegalStateException("No SETTINGS set.");
396 * <p>getNewWizardLinkText</p>
398 * @return a {@link java.lang.String} object.
400 protected abstract String
[] getNewWizardText();
403 * <p>getNewEntityWizard</p>
405 * @return a {@link eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard} object.
407 protected abstract AbstractNewEntityWizard
getNewEntityWizard(String parameter
);
409 public class FilteredCdmResourceLabelProvider
extends LabelProvider
{
411 public String
getText(Object element
) {
412 if (element
== null) {
415 UuidAndTitleCache uuidAndTitleCache
= (UuidAndTitleCache
) element
;
416 String titleCache
= uuidAndTitleCache
.getTitleCache();
417 if(PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG
)){
418 titleCache
+= " ["+uuidAndTitleCache
.getId()+"]";
425 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
430 // protected Control createExtendedContentArea(Composite parent) {
431 // String newWizardLinkText = getNewWizardLinkText();
432 //// if(newWizardLinkText != null){
433 //// newButton1 = this.createButton(this.getShell(), new_id, newWizardLinkText, false);
435 //// newButton1.addSelectionListener(getNewWizardLinkSelectionListener());
436 //// return newButton1;
442 protected void createButtonsForButtonBar(Composite parent
) {
443 String
[] newButtonText
= getNewWizardText();
445 if (newButtonText
!= null){
446 this.newButton1
= createButton(parent
, this.new_id
, newButtonText
[0], false);
447 newButton1
.addSelectionListener(getNewWizardButtonSelectionListener());
450 if (newButtonText
.length
> 1){
451 newButton2
= createButton(parent
, this.new_id2
, newButtonText
[1], false);
452 newButton2
.addSelectionListener(getNewWizardButtonSelectionListener());
457 Button space
= createButton(parent
, this.space_id
, " ", false);
458 space
.setEnabled(false);
459 space
.setVisible(false);
460 GridData gridData
= new GridData();
461 gridData
.grabExcessHorizontalSpace
= false;
462 gridData
.widthHint
= 3;
463 space
.setLayoutData(gridData
);
464 GridLayout gridLayout
= new GridLayout();
465 gridLayout
.makeColumnsEqualWidth
= false;
466 if (newButtonText
!= null){
467 gridLayout
.numColumns
=newButtonText
.length
+2;
469 gridLayout
.numColumns
=2;
471 parent
.setLayout(gridLayout
);
473 super.createButtonsForButtonBar(parent
);
474 super.getButton(IDialogConstants
.OK_ID
).setEnabled(false);
477 protected SelectionListener
getNewWizardButtonSelectionListener(){
478 return new SelectionAdapter() {
481 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
484 public void widgetSelected(SelectionEvent e
) {
485 Object source
= e
.getSource();
487 if (source
instanceof Button
){
488 Button sourceButton
= (Button
) source
;
489 text
= sourceButton
.getText();
491 AbstractNewEntityWizard wizard
= getNewEntityWizard(text
);
493 if (wizard
.getEntity() == null){
494 wizard
.init(null, null);
496 if(wizard
.getEntity() != null) {
497 WizardDialog dialog
= new WizardDialog(getShell(), wizard
);
498 int status
= dialog
.open();
500 if (status
== IStatus
.OK
) {
502 T entity
= (T
) wizard
.getEntity();
506 // if (getConversationHolder() != null){
507 // getConversationHolder().bind();
510 //FIXME : Need to make sure this is a stable fix (ticket 3822)
511 // if (getConversationHolder() != null){
512 // getConversationHolder().commit();
521 * <p>getConversationHolder</p>
523 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
526 // public ConversationHolder getConversationHolder() {
527 // return conversation;
532 // public void update(CdmDataChangeMap changeEvents) {}
535 * Don't want to add for example a taxon or synonym to itself
536 * so filter the list to remove the taxon in question
537 * (<code>cdmBaseToBeFiltered</code>)
538 * so it is not available in the filtered list.
540 private void filterExcludedObjects() {
541 if (model
!= null && cdmBaseToBeFiltered
!= null) {
543 UuidAndTitleCache uuidAndTitleCacheToRemove
= null;
545 for (UuidAndTitleCache uuidAndTitleCache
: model
){
546 if (cdmBaseToBeFiltered
!= null && cdmBaseToBeFiltered
.contains(uuidAndTitleCache
.getUuid())) {
547 uuidAndTitleCacheToRemove
= uuidAndTitleCache
;
550 model
.remove(uuidAndTitleCacheToRemove
);
554 void createFilterButton(Composite searchAndFilter
){
555 //as default no filter button available
560 protected void search() {
561 Control control
=getSearchField();
562 String pattern
= null;
563 if (control
!= null){
564 pattern
= ((Text
)control
).getText();
565 if (pattern
.equals("*") || pattern
.equals("?")){
567 }else if (StringUtils
.isNotBlank(pattern
)){
568 callService(pattern
);
570 fillContentProvider(null);
573 // if (pattern.equals("?")){
574 // model = CdmStore.getService(INameService.class).getUuidAndTitleCache(null, null);
575 // }else if (pattern != null){
576 // model = CdmStore.getService(INameService.class).getUuidAndTitleCache(limitOfInitialElements, pattern);
580 abstract void callService(String pattern
);