Upgraded to cdmlib-3.0.9, working on error warning for unsavable editor
[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 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;
24
25 /**
26 * <p>SimpleSelectionProvider class.</p>
27 *
28 * @author p.ciardelli
29 * @author n.hoffmann
30 * @created 16.05.2008
31 * @version 1.0
32 */
33 public class SimpleSelectionProvider implements ISelectionProvider {
34
35 private ListenerList selectionChangedListeners = new ListenerList();
36 private ISelection selection;
37 private Job job;
38
39 /*
40 * (non-Javadoc)
41 * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
42 */
43 /** {@inheritDoc} */
44 public void addSelectionChangedListener(
45 ISelectionChangedListener listener) {
46 selectionChangedListeners.add(listener);
47 }
48
49 /*
50 * (non-Javadoc)
51 * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
52 */
53 /**
54 * <p>Getter for the field <code>selection</code>.</p>
55 *
56 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
57 */
58 public ISelection getSelection() {
59 if (selection != null){
60 return selection;
61 }
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();
65 }
66
67 /*
68 * (non-Javadoc)
69 * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
70 */
71 /** {@inheritDoc} */
72 public void removeSelectionChangedListener(
73 ISelectionChangedListener listener) {
74 selectionChangedListeners.remove(listener);
75 }
76
77 /*
78 * (non-Javadoc)
79 * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
80 */
81 /** {@inheritDoc} */
82 public void setSelection(ISelection selection) {
83 this.selection = selection;
84
85 // cancel previous selection setting
86 if(job != null){
87 job.cancel();
88 job = null;
89 }
90
91 if(job == null){
92 final SelectionChangedEvent selectionChangedEvent = new SelectionChangedEvent(this, selection);
93 final Display display = Display.getCurrent();
94 job = new Job("Setting Selection"){
95
96 @Override
97 protected IStatus run(IProgressMonitor monitor) {
98 monitor.beginTask("Setting Selection", 10);
99
100 if(!monitor.isCanceled()){
101 display.asyncExec(new Runnable() {
102
103 public void run() {
104 fireSelectionChanged(selectionChangedEvent);
105 }
106 });
107 }
108
109 return Status.OK_STATUS;
110 }
111
112 };
113
114 job.setPriority(Job.DECORATE);
115 job.schedule();
116 }
117
118 }
119
120 private void fireSelectionChanged(final SelectionChangedEvent event) {
121
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() {
126 public void run() {
127 l.selectionChanged(event);
128 }
129 });
130 }
131 }
132 }