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.internal;
16:
17: import java.util.Set;
18: import java.util.concurrent.CopyOnWriteArraySet;
19:
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:
25: /**
26: * Since a MapEditor can have a different selection provider depending on the tool that is selected, this class
27: * is needed to manage the changing selection providers.
28: *
29: * @author Jesse
30: * @since 1.1.0
31: */
32: class ReplaceableSelectionProvider implements ISelectionProvider {
33:
34: private static final ISelection EMPTY = new ISelection() {
35:
36: public boolean isEmpty() {
37: return true;
38: }
39:
40: };
41: private Set<ISelectionChangedListener> listeners = new CopyOnWriteArraySet<ISelectionChangedListener>();
42: private ISelectionProvider wrapped;
43: private ISelectionChangedListener listener = new ISelectionChangedListener() {
44:
45: public void selectionChanged(SelectionChangedEvent event) {
46: if (event.getSelectionProvider() != wrapped) {
47: event.getSelectionProvider()
48: .removeSelectionChangedListener(this );
49: }
50: notifyChange(event.getSelection());
51: }
52:
53: };
54:
55: public void addSelectionChangedListener(
56: ISelectionChangedListener listener) {
57: listeners.add(listener);
58: }
59:
60: public ISelection getSelection() {
61: if (wrapped != null)
62: return wrapped.getSelection();
63: else
64: return EMPTY;
65: }
66:
67: public void removeSelectionChangedListener(
68: ISelectionChangedListener listener) {
69: listeners.remove(listener);
70: }
71:
72: public void setSelection(ISelection selection) {
73: if (wrapped != null)
74: wrapped.setSelection(selection);
75: }
76:
77: public void setProvider(ISelectionProvider newProvider) {
78: if (wrapped != null)
79: wrapped.removeSelectionChangedListener(listener);
80: newProvider.addSelectionChangedListener(listener);
81: wrapped = newProvider;
82:
83: if (newProvider.getSelection() != null)
84: notifyChange(newProvider.getSelection());
85: }
86:
87: private void notifyChange(ISelection selection) {
88: SelectionChangedEvent event = new SelectionChangedEvent(this ,
89: selection);
90: for (ISelectionChangedListener l : listeners) {
91: l.selectionChanged(event);
92: }
93: }
94:
95: public ISelectionProvider getSelectionProvider() {
96: return wrapped;
97: }
98:
99: }
|