merging in latest changes from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / SimpleSelectionProvider.java
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 /**
28 * <p>SimpleSelectionProvider class.</p>
29 *
30 * @author p.ciardelli
31 * @author n.hoffmann
32 * @created 16.05.2008
33 * @version 1.0
34 */
35 public class SimpleSelectionProvider implements ISelectionProvider {
36
37 private Set<ISelectionChangedListener> selectionChangedListeners = new HashSet<ISelectionChangedListener>();
38 private ISelection selection;
39 private Job job;
40
41 /*
42 * (non-Javadoc)
43 * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
44 */
45 /** {@inheritDoc} */
46 public void addSelectionChangedListener(
47 ISelectionChangedListener listener) {
48 selectionChangedListeners.add(listener);
49 }
50
51 /*
52 * (non-Javadoc)
53 * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
54 */
55 /**
56 * <p>Getter for the field <code>selection</code>.</p>
57 *
58 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
59 */
60 public ISelection getSelection() {
61 if (selection != null){
62 return selection;
63 }
64 // TODO we have to return an empty selection to avoid NPE
65 // I don't really understand why this was not the case before
66 return new StructuredSelection();
67 }
68
69 /*
70 * (non-Javadoc)
71 * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
72 */
73 /** {@inheritDoc} */
74 public void removeSelectionChangedListener(
75 ISelectionChangedListener listener) {
76 selectionChangedListeners.remove(listener);
77 }
78
79 /*
80 * (non-Javadoc)
81 * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
82 */
83 /** {@inheritDoc} */
84 public void setSelection(ISelection selection) {
85 this.selection = selection;
86
87 // cancel previous selection setting
88 if(job != null){
89 if(job.getState() != Job.NONE){
90 job.cancel();
91 }
92 job = null;
93 }
94
95 if(job == null){
96 final SelectionChangedEvent selectionChangedEvent = new SelectionChangedEvent(this, selection);
97 final Display display = Display.getCurrent();
98 job = new Job("Setting Selection"){
99
100 @Override
101 protected IStatus run(IProgressMonitor monitor) {
102 monitor.beginTask("Setting Selection", 10);
103
104 if(!monitor.isCanceled()){
105 display.asyncExec(new Runnable() {
106
107 public void run() {
108 SimpleSelectionProvider.this.fireSelectionChanged(selectionChangedEvent);
109 }
110 });
111 }
112
113 return Status.OK_STATUS;
114 }
115
116 };
117
118 job.setPriority(Job.DECORATE);
119 job.schedule();
120 }
121
122 }
123
124 private void fireSelectionChanged(final SelectionChangedEvent event) {
125
126 for (final ISelectionChangedListener listener : selectionChangedListeners) {
127 SafeRunnable.run(new SafeRunnable() {
128 public void run() {
129 listener.selectionChanged(event);
130 }
131 });
132 }
133 }
134 }