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
.editor
;
12 import java
.util
.HashSet
;
15 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
16 import org
.eclipse
.core
.runtime
.IStatus
;
17 import org
.eclipse
.core
.runtime
.Status
;
18 import org
.eclipse
.core
.runtime
.jobs
.Job
;
19 import org
.eclipse
.jface
.util
.SafeRunnable
;
20 import org
.eclipse
.jface
.viewers
.ISelection
;
21 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
22 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
23 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
24 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
25 import org
.eclipse
.swt
.widgets
.Display
;
27 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
30 * <p>SimpleSelectionProvider class.</p>
36 public class SimpleSelectionProvider
implements ISelectionProvider
{
38 private static final String SETTING_SELECTION
= Messages
.SimpleSelectionProvider_SETTING_SELECTION
;
39 private Set
<ISelectionChangedListener
> selectionChangedListeners
= new HashSet
<ISelectionChangedListener
>();
40 private ISelection selection
;
44 public void addSelectionChangedListener(
45 ISelectionChangedListener listener
) {
46 selectionChangedListeners
.add(listener
);
50 * <p>Getter for the field <code>selection</code>.</p>
52 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
55 public ISelection
getSelection() {
56 if (selection
!= null){
59 // TODO we have to return an empty selection to avoid NPE
60 // I don't really understand why this was not the case before
61 return new StructuredSelection();
65 public void removeSelectionChangedListener(
66 ISelectionChangedListener listener
) {
67 selectionChangedListeners
.remove(listener
);
71 public void setSelection(ISelection selection
) {
72 this.selection
= selection
;
74 // cancel previous selection setting
76 if(job
.getState() != Job
.NONE
){
83 final SelectionChangedEvent selectionChangedEvent
= new SelectionChangedEvent(this, selection
);
84 final Display display
= Display
.getCurrent();
85 job
= new Job(SETTING_SELECTION
){
88 protected IStatus
run(IProgressMonitor monitor
) {
89 monitor
.beginTask(SETTING_SELECTION
, 10);
91 if(!monitor
.isCanceled()){
92 display
.asyncExec(new Runnable() {
96 SimpleSelectionProvider
.this.fireSelectionChanged(selectionChangedEvent
);
101 return Status
.OK_STATUS
;
106 job
.setPriority(Job
.DECORATE
);
112 private void fireSelectionChanged(final SelectionChangedEvent event
) {
114 for (final ISelectionChangedListener listener
: selectionChangedListeners
) {
115 SafeRunnable
.run(new SafeRunnable() {
118 listener
.selectionChanged(event
);