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 org
.eclipse
.core
.runtime
.IProgressMonitor
;
13 import org
.eclipse
.core
.runtime
.IStatus
;
14 import org
.eclipse
.core
.runtime
.ListenerList
;
15 import org
.eclipse
.core
.runtime
.Status
;
16 import org
.eclipse
.core
.runtime
.jobs
.Job
;
17 import org
.eclipse
.jface
.util
.SafeRunnable
;
18 import org
.eclipse
.jface
.viewers
.ISelection
;
19 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
20 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
21 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
22 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
23 import org
.eclipse
.swt
.widgets
.Display
;
26 * <p>SimpleSelectionProvider class.</p>
33 public class SimpleSelectionProvider
implements ISelectionProvider
{
35 private ListenerList selectionChangedListeners
= new ListenerList();
36 private ISelection selection
;
41 * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
44 public void addSelectionChangedListener(
45 ISelectionChangedListener listener
) {
46 selectionChangedListeners
.add(listener
);
51 * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
54 * <p>Getter for the field <code>selection</code>.</p>
56 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
58 public ISelection
getSelection() {
59 if (selection
!= null){
62 // TODO we have to return an empty selection to avoid NPE
63 // I don't really understand why this was not the case before
64 return new StructuredSelection();
69 * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
72 public void removeSelectionChangedListener(
73 ISelectionChangedListener listener
) {
74 selectionChangedListeners
.remove(listener
);
79 * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
82 public void setSelection(ISelection selection
) {
83 this.selection
= selection
;
85 // cancel previous selection setting
92 final SelectionChangedEvent selectionChangedEvent
= new SelectionChangedEvent(this, selection
);
93 final Display display
= Display
.getCurrent();
94 job
= new Job("Setting Selection"){
97 protected IStatus
run(IProgressMonitor monitor
) {
98 monitor
.beginTask("Setting Selection", 10);
100 if(!monitor
.isCanceled()){
101 display
.asyncExec(new Runnable() {
104 fireSelectionChanged(selectionChangedEvent
);
109 return Status
.OK_STATUS
;
114 job
.setPriority(Job
.DECORATE
);
120 private void fireSelectionChanged(final SelectionChangedEvent event
) {
122 Object
[] listeners
= selectionChangedListeners
.getListeners();
123 for (int i
= 0; i
< listeners
.length
; ++i
) {
124 final ISelectionChangedListener l
= (ISelectionChangedListener
) listeners
[i
];
125 SafeRunnable
.run(new SafeRunnable() {
127 l
.selectionChanged(event
);