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)
61 // TODO we have to return an empty selection to avoid NPE
62 // I don't really understand why this was not the case before
63 return new StructuredSelection();
68 * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
71 public void removeSelectionChangedListener(
72 ISelectionChangedListener listener
) {
73 selectionChangedListeners
.remove(listener
);
78 * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
81 public void setSelection(ISelection selection
) {
82 this.selection
= selection
;
84 // cancel previous selection setting
91 final SelectionChangedEvent selectionChangedEvent
= new SelectionChangedEvent(this, selection
);
92 final Display display
= Display
.getCurrent();
93 job
= new Job("Setting Selection"){
96 protected IStatus
run(IProgressMonitor monitor
) {
97 monitor
.beginTask("Setting Selection", 10);
99 if(!monitor
.isCanceled()){
100 display
.asyncExec(new Runnable() {
103 fireSelectionChanged(selectionChangedEvent
);
108 return Status
.OK_STATUS
;
113 job
.setPriority(Job
.DECORATE
);
119 private void fireSelectionChanged(final SelectionChangedEvent event
) {
121 Object
[] listeners
= selectionChangedListeners
.getListeners();
122 for (int i
= 0; i
< listeners
.length
; ++i
) {
123 final ISelectionChangedListener l
= (ISelectionChangedListener
) listeners
[i
];
124 SafeRunnable
.run(new SafeRunnable() {
126 l
.selectionChanged(event
);