01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.project.ui.tool.selection.provider;
16:
17: import java.util.Set;
18: import java.util.concurrent.CopyOnWriteArraySet;
19:
20: import net.refractions.udig.project.ui.tool.IMapEditorSelectionProvider;
21:
22: import org.eclipse.jface.viewers.ISelection;
23: import org.eclipse.jface.viewers.ISelectionChangedListener;
24: import org.eclipse.jface.viewers.ISelectionProvider;
25: import org.eclipse.jface.viewers.SelectionChangedEvent;
26: import org.eclipse.jface.viewers.StructuredSelection;
27: import org.eclipse.swt.widgets.Display;
28:
29: /**
30: * Implements the basic functionality of a MapEditorSelectionProvider.
31: *
32: * @author Jesse
33: * @since 1.1.0
34: */
35: public abstract class AbstractMapEditorSelectionProvider implements
36: IMapEditorSelectionProvider {
37: protected StructuredSelection selection;
38: protected CopyOnWriteArraySet<ISelectionChangedListener> listeners = new CopyOnWriteArraySet<ISelectionChangedListener>();
39:
40: public Set<ISelectionChangedListener> getListeners() {
41: return listeners;
42: }
43:
44: protected void notifyListeners() {
45: if (Display.getCurrent() == null) {
46: final ISelectionProvider p = this ;
47: Display.getDefault().asyncExec(new Runnable() {
48: public void run() {
49: SelectionChangedEvent event = new SelectionChangedEvent(
50: p, selection);
51: for (ISelectionChangedListener l : listeners) {
52: l.selectionChanged(event);
53: }
54: }
55: });
56: } else {
57: SelectionChangedEvent event = new SelectionChangedEvent(
58: this , selection);
59: for (ISelectionChangedListener l : listeners) {
60: l.selectionChanged(event);
61: }
62: }
63: }
64:
65: public void addSelectionChangedListener(
66: ISelectionChangedListener listener) {
67: listeners.add(listener);
68: }
69:
70: public ISelection getSelection() {
71: return selection;
72: }
73:
74: public void removeSelectionChangedListener(
75: ISelectionChangedListener listener) {
76: listeners.remove(listener);
77: }
78:
79: public void setSelection(ISelection selection) {
80: throw new UnsupportedOperationException();
81: }
82:
83: }
|