1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.editor;
|
11
|
|
12
|
import java.util.HashSet;
|
13
|
import java.util.Set;
|
14
|
|
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;
|
26
|
|
27
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
28
|
|
29
|
/**
|
30
|
* <p>SimpleSelectionProvider class.</p>
|
31
|
*
|
32
|
* @author p.ciardelli
|
33
|
* @author n.hoffmann
|
34
|
* @created 16.05.2008
|
35
|
*/
|
36
|
public class SimpleSelectionProvider implements ISelectionProvider {
|
37
|
|
38
|
private static final String SETTING_SELECTION = Messages.SimpleSelectionProvider_SETTING_SELECTION;
|
39
|
private Set<ISelectionChangedListener> selectionChangedListeners = new HashSet<ISelectionChangedListener>();
|
40
|
private ISelection selection;
|
41
|
private Job job;
|
42
|
|
43
|
@Override
|
44
|
public void addSelectionChangedListener(
|
45
|
ISelectionChangedListener listener) {
|
46
|
selectionChangedListeners.add(listener);
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* <p>Getter for the field <code>selection</code>.</p>
|
51
|
*
|
52
|
* @return a {@link org.eclipse.jface.viewers.ISelection} object.
|
53
|
*/
|
54
|
@Override
|
55
|
public ISelection getSelection() {
|
56
|
if (selection != null){
|
57
|
return selection;
|
58
|
}
|
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();
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
public void removeSelectionChangedListener(
|
66
|
ISelectionChangedListener listener) {
|
67
|
selectionChangedListeners.remove(listener);
|
68
|
}
|
69
|
|
70
|
@Override
|
71
|
public void setSelection(ISelection selection) {
|
72
|
this.selection = selection;
|
73
|
|
74
|
// cancel previous selection setting
|
75
|
if(job != null){
|
76
|
if(job.getState() != Job.NONE){
|
77
|
job.cancel();
|
78
|
}
|
79
|
job = null;
|
80
|
}
|
81
|
|
82
|
if(job == null){
|
83
|
final SelectionChangedEvent selectionChangedEvent = new SelectionChangedEvent(this, selection);
|
84
|
final Display display = Display.getCurrent();
|
85
|
job = new Job(SETTING_SELECTION){
|
86
|
|
87
|
@Override
|
88
|
protected IStatus run(IProgressMonitor monitor) {
|
89
|
monitor.beginTask(SETTING_SELECTION, 10);
|
90
|
|
91
|
if(!monitor.isCanceled()){
|
92
|
display.asyncExec(new Runnable() {
|
93
|
|
94
|
@Override
|
95
|
public void run() {
|
96
|
SimpleSelectionProvider.this.fireSelectionChanged(selectionChangedEvent);
|
97
|
}
|
98
|
});
|
99
|
}
|
100
|
|
101
|
return Status.OK_STATUS;
|
102
|
}
|
103
|
|
104
|
};
|
105
|
|
106
|
job.setPriority(Job.DECORATE);
|
107
|
job.schedule();
|
108
|
}
|
109
|
|
110
|
}
|
111
|
|
112
|
private void fireSelectionChanged(final SelectionChangedEvent event) {
|
113
|
|
114
|
for (final ISelectionChangedListener listener : selectionChangedListeners) {
|
115
|
SafeRunnable.run(new SafeRunnable() {
|
116
|
@Override
|
117
|
public void run() {
|
118
|
listener.selectionChanged(event);
|
119
|
}
|
120
|
});
|
121
|
}
|
122
|
}
|
123
|
}
|