001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004, Refractions Research Inc. This
003: * library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
004: * License as published by the Free Software Foundation; version 2.1 of the License. This library is distributed in the
005: * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
006: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
007: */
008: package net.refractions.udig.project.ui.internal;
009:
010: import java.util.Set;
011: import java.util.concurrent.CopyOnWriteArraySet;
012:
013: import net.refractions.udig.project.IMap;
014: import net.refractions.udig.project.ui.tool.IMapEditorSelectionProvider;
015: import net.refractions.udig.ui.PlatformGIS;
016:
017: import org.eclipse.core.runtime.Platform;
018: import org.eclipse.emf.common.notify.Notification;
019: import org.eclipse.emf.common.notify.impl.AdapterImpl;
020: import org.eclipse.jface.util.SafeRunnable;
021: import org.eclipse.jface.viewers.ISelection;
022: import org.eclipse.jface.viewers.ISelectionChangedListener;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.jface.viewers.SelectionChangedEvent;
025: import org.eclipse.jface.viewers.StructuredSelection;
026: import org.eclipse.ui.IEditorPart;
027:
028: /**
029: * Uses the EditManager and SelectionModel to provide a curent selection.
030: * <p>
031: * Listen to EditManager and SelectionModel events and notify the Eclipse Platform of selection
032: * changes.
033: * </p>
034: * <p>
035: * Responsibilities:
036: * <ul>
037: * <li>Listen for EditLayer and EditFeature changes in LayerManager</li>
038: * <li>Listen for SelectionModel selection changes</li>
039: * <li>set the selection of the eclipse Platform to one that reflects the state of SelectionModel
040: * and LayerManager </li>
041: * </ul>
042: *
043: * @author Jesse
044: * @since 0.6
045: */
046: public class MapEditorSelectionProvider extends AdapterImpl implements
047: IMapEditorSelectionProvider {
048:
049: /**
050: * Construct <code>MapEditorSelectionProvider</code>.
051: */
052: public MapEditorSelectionProvider() {
053: }
054:
055: IStructuredSelection selection;
056: CopyOnWriteArraySet<ISelectionChangedListener> list = new CopyOnWriteArraySet<ISelectionChangedListener>();
057:
058: protected void fireSelectionChangedEvent(
059: final SelectionChangedEvent event) {
060: Object source = event.getSource();
061: if (source == this )
062: return;
063: for (final ISelectionChangedListener l : list) {
064: PlatformGIS.run(new SafeRunnable() {
065: public void run() {
066: l.selectionChanged(event);
067: }
068: });
069: }
070: }
071:
072: /**
073: * @see org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.eclipse.emf.common.notify.Notification)
074: */
075: @Override
076: public void notifyChanged(Notification msg) {
077: fireSelectionChangedEvent(new SelectionChangedEvent(this ,
078: selection));
079: }
080:
081: /**
082: * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
083: */
084: public void addSelectionChangedListener(
085: ISelectionChangedListener listener) {
086: list.add(listener);
087: }
088:
089: /**
090: * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
091: */
092: public ISelection getSelection() {
093: return selection;
094: }
095:
096: /**
097: * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
098: */
099: public void removeSelectionChangedListener(
100: ISelectionChangedListener listener) {
101: list.remove(listener);
102: }
103:
104: /**
105: * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
106: */
107: public void setSelection(ISelection selection) {
108: if (selection instanceof IStructuredSelection) {
109: this .selection = (IStructuredSelection) selection;
110: fireSelectionChangedEvent(new SelectionChangedEvent(this ,
111: selection));
112: }
113: }
114:
115: public void setActiveMap(IMap map2, final IEditorPart editor) {
116: selection = new StructuredSelection(map2);
117: }
118:
119: public Set<ISelectionChangedListener> getListeners() {
120: return list;
121: }
122:
123: }
|