Project

General

Profile

Download (3.59 KB) Statistics
| Branch: | Tag: | Revision:
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
		// 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();
64
	}
65

    
66
	/*
67
	 * (non-Javadoc)
68
	 * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
69
	 */
70
	/** {@inheritDoc} */
71
	public void removeSelectionChangedListener(
72
			ISelectionChangedListener listener) {
73
		selectionChangedListeners.remove(listener);
74
	}
75

    
76
	/*
77
	 * (non-Javadoc)
78
	 * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
79
	 */
80
	/** {@inheritDoc} */
81
	public void setSelection(ISelection selection) {
82
		this.selection = selection;
83
		
84
		// cancel previous selection setting
85
		if(job != null){
86
			job.cancel();
87
			job = null;
88
		}
89
		
90
		if(job == null){
91
			final SelectionChangedEvent selectionChangedEvent = new SelectionChangedEvent(this, selection);
92
			final Display display = Display.getCurrent();
93
			job = new Job("Setting Selection"){
94
	
95
				@Override
96
				protected IStatus run(IProgressMonitor monitor) {
97
					monitor.beginTask("Setting Selection", 10);
98
					
99
					if(!monitor.isCanceled()){
100
						display.asyncExec(new Runnable() {
101
					 
102
							public void run() {
103
								fireSelectionChanged(selectionChangedEvent);
104
							}
105
						});
106
					}
107
					
108
					return Status.OK_STATUS;
109
				}
110
				
111
			};
112
			
113
			job.setPriority(Job.DECORATE);
114
			job.schedule();
115
		}
116
		
117
	}
118

    
119
	private void fireSelectionChanged(final SelectionChangedEvent event) {
120
		
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() {
125
				public void run() {
126
					l.selectionChanged(event);
127
				}
128
			});
129
		}
130
	}
131
}
(11-11/15)