3 package eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
;
5 import java
.lang
.reflect
.Field
;
6 import java
.text
.Collator
;
7 import java
.util
.Comparator
;
10 import org
.eclipse
.core
.runtime
.CoreException
;
11 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
12 import org
.eclipse
.core
.runtime
.IStatus
;
13 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
14 import org
.eclipse
.core
.runtime
.Status
;
15 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
16 import org
.eclipse
.jface
.viewers
.ILabelProvider
;
17 import org
.eclipse
.jface
.viewers
.LabelProvider
;
18 import org
.eclipse
.jface
.window
.Window
;
19 import org
.eclipse
.jface
.wizard
.WizardDialog
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.SelectionAdapter
;
22 import org
.eclipse
.swt
.events
.SelectionEvent
;
23 import org
.eclipse
.swt
.events
.SelectionListener
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Control
;
26 import org
.eclipse
.swt
.widgets
.Link
;
27 import org
.eclipse
.swt
.widgets
.Shell
;
28 import org
.eclipse
.swt
.widgets
.Text
;
29 import org
.eclipse
.ui
.IMemento
;
30 import org
.eclipse
.ui
.dialogs
.FilteredItemsSelectionDialog
;
32 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
33 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
34 import eu
.etaxonomy
.cdm
.model
.common
.IEnumTerm
;
35 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
36 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
37 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
38 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
41 * <p>Abstract AbstractFilteredCdmEnumSelectionDialog class.</p>
47 public abstract class AbstractFilteredCdmEnumSelectionDialog
<T
extends IEnumTerm
> extends
48 FilteredItemsSelectionDialog
implements IConversationEnabled
{
50 private ConversationHolder conversation
;
52 protected List
<T
> model
;
53 private String settings
;
58 * <p>Constructor for AbstractFilteredCdmResourceSelectionDialog.</p>
60 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
62 * @param title a {@link java.lang.String} object.
63 * @param multi a boolean.
64 * @param settings a {@link java.lang.String} object.
65 * @param cdmEnum a T object.
66 * @param <T> a T object.
68 protected AbstractFilteredCdmEnumSelectionDialog(Shell shell
,
69 ConversationHolder conversation
,
76 setMessage("Use * for wildcard, or ? to see all entries");
77 this.settings
= settings
;
79 this.conversation
= conversation
;
85 String objectTitle
= getTitle(cdmEnum
);
86 if (objectTitle
!= null) {
87 setInitialPattern(objectTitle
);
90 setListLabelProvider(createListLabelProvider());
91 setDetailsLabelProvider(createDetailsLabelProvider());
93 setSelectionHistory(new ResourceSelectionHistory());
97 * By default, we are returning the standard list label provider
99 * Override in subclasses if you want different behavior
103 protected ILabelProvider
createDetailsLabelProvider() {
104 return createListLabelProvider();
111 protected ILabelProvider
createListLabelProvider() {
112 return new FilteredCdmResourceLabelProvider();
116 * Override in subclasses.
117 * Will run before initModel()
119 protected void init() {
124 * <p>getSelectionFromDialog</p>
126 * @param dialog a {@link eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog} object.
127 * @param <TYPE> a TYPE object.
128 * @return a TYPE object.
130 protected static <TYPE
extends IEnumTerm
> TYPE
getSelectionFromDialog(AbstractFilteredCdmEnumSelectionDialog
<TYPE
> dialog
) {
132 int result
= dialog
.open();
134 if (result
== Window
.CANCEL
) {
138 return dialog
.getSelectedEnumTerm();
145 * @param cdmObject a T object.
146 * @return a {@link java.lang.String} object.
148 protected String
getTitle(T cdmEnum
) {
149 return cdmEnum
.getMessage();
155 public void refresh() {
163 abstract protected void initModel();
166 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
170 protected ItemsFilter
createFilter() {
171 return new ItemsFilter() {
174 * Always returns false to enforce refiltering even if the pattern is equal
177 public boolean equalsFilter(ItemsFilter filter
) {
182 public boolean isConsistentItem(Object item
) {
187 public boolean matchItem(Object item
) {
189 if(item
instanceof IEnumTerm
){
190 text
= ((IEnumTerm
) item
).getMessage();
191 }else if(item
instanceof String
){
192 text
= (String
) item
;
194 return text
!= null ?
matches(text
) : false;
202 * Set the filter input to the Agent's title cache
204 * @param cdmObject a T object.
206 protected void setPattern(T cdmObject
) {
207 // FilteredSelection does some very tricky caching to make sure it
208 // runs with high performance.
209 // This works for most use cases, but we want to change the model while the dialog is open
210 // and all the clever caching prevents the content provider from knowing that the model has changed
211 // I am aware, that this is a hack, but the FilteredSelectionDialog API does not offer a convenient
212 // way to solve the problem.
214 Field lastCompletedFilter
= this.getClass().getSuperclass().getSuperclass().getDeclaredField("lastCompletedFilter");
215 lastCompletedFilter
.setAccessible(true);
216 lastCompletedFilter
.set(this, null);
217 } catch (SecurityException e
) {
218 MessagingUtils
.error(getClass(), e
);
219 } catch (NoSuchFieldException e
) {
220 MessagingUtils
.error(getClass(), e
);
221 } catch (IllegalArgumentException e
) {
222 MessagingUtils
.error(getClass(), e
);
223 } catch (IllegalAccessException e
) {
224 MessagingUtils
.error(getClass(), e
);
227 // this also is not the nicest way to do it.
228 // I am still amazed, that FilteredSelectionDialog does not offer any methods to change its data
229 // once it was opened. Am I doing it wrong?
230 String pattern
= getTitle(cdmObject
);
231 ((Text
) getPatternControl()).setText(pattern
);
235 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider, org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor)
239 protected void fillContentProvider(AbstractContentProvider contentProvider
,
240 ItemsFilter itemsFilter
, IProgressMonitor progressMonitor
)
241 throws CoreException
{
244 progressMonitor
.beginTask("Looking for entities", model
.size());
245 for(T element
: model
){
246 contentProvider
.add(element
, itemsFilter
);
247 if (progressMonitor
.isCanceled()) {
248 throw new OperationCanceledException();
250 progressMonitor
.worked(1);
253 MessagingUtils
.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
257 progressMonitor
.done();
262 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
266 protected IDialogSettings
getDialogSettings() {
267 IDialogSettings settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().getSection(getSettings());
269 if (settings
== null) {
270 settings
= TaxeditorStorePlugin
.getDefault().getDialogSettings().addNewSection(getSettings());
276 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
280 public String
getElementName(Object item
) {
281 return ((IEnumTerm
) item
).getMessage();
285 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
289 protected Comparator
getItemsComparator() {
290 return new Comparator
<IEnumTerm
>() {
292 public int compare(IEnumTerm entity1
,
294 if (entity1
.equals(entity2
)){
297 Collator collator
= Collator
.getInstance();
299 int result
= collator
.compare(entity1
.getMessage(), entity2
.getMessage());
301 return entity1
.getUuid().compareTo(entity2
.getUuid());
309 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#validateItem(java.lang.Object)
313 protected IStatus
validateItem(Object item
) {
314 return Status
.OK_STATUS
;
318 * <p>getSelectedUuidAndTitleCache</p>
320 * @return a {@link eu.etaxonomy.cdm.model.common.UuidAndTitleCache} object.
322 protected T
getSelectedEnumTerm() {
323 Object
[] result
= getResult();
324 return result
[0] == null ?
null : (T
) result
[0];
328 * <p>Getter for the field <code>settings</code>.</p>
330 * @return a {@link java.lang.String} object.
332 public String
getSettings() {
333 if(settings
== null){
334 throw new IllegalStateException("No SETTINGS set.");
342 * @created Oct 19, 2009
345 private class ResourceSelectionHistory
extends SelectionHistory
{
347 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#restoreItemFromMemento(org.eclipse.ui.IMemento)
350 protected Object
restoreItemFromMemento(IMemento element
) {
351 return element
.getString("resource"); //$NON-NLS-1$
354 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#storeItemToMemento(java.lang.Object,
355 * org.eclipse.ui.IMemento)
358 protected void storeItemToMemento(Object item
, IMemento element
) {
359 element
.putString("resource", item
.toString()); //$NON-NLS-1$
364 * <p>getNewWizardLinkText</p>
366 * @return a {@link java.lang.String} object.
368 protected abstract String
getNewWizardLinkText();
371 * <p>getNewEntityWizard</p>
373 * @return a {@link eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard} object.
375 protected abstract AbstractNewEntityWizard
getNewEntityWizard(String parameter
);
377 public class FilteredCdmResourceLabelProvider
extends LabelProvider
{
379 public String
getText(Object element
) {
380 if (element
== null) {
383 return ((IEnumTerm
) element
).getMessage();
388 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
392 protected Control
createExtendedContentArea(Composite parent
) {
393 if(getNewWizardLinkText() != null){
394 Link link
= new Link(parent
, SWT
.NONE
);
395 link
.setText(getNewWizardLinkText());
396 link
.addSelectionListener(getNewWizardLinkSelectionListener());
402 protected SelectionListener
getNewWizardLinkSelectionListener(){
403 return new SelectionAdapter() {
406 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
409 public void widgetSelected(SelectionEvent e
) {
411 AbstractNewEntityWizard wizard
= getNewEntityWizard(e
.text
);
412 wizard
.init(null, null);
413 WizardDialog dialog
= new WizardDialog(getShell(), wizard
);
414 int status
= dialog
.open();
416 if (status
== IStatus
.OK
) {
418 T entity
= (T
) wizard
.getEntity();
422 getConversationHolder().bind();
429 * <p>getConversationHolder</p>
431 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
434 public ConversationHolder
getConversationHolder() {
440 public void update(CdmDataChangeMap changeEvents
) {}