001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.project.ui.internal.adapters;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.concurrent.CopyOnWriteArrayList;
023:
024: import net.refractions.udig.project.UDIGAdaptableDecorator;
025:
026: import org.eclipse.core.runtime.IAdaptable;
027: import org.eclipse.core.runtime.Platform;
028: import org.eclipse.jface.viewers.ISelection;
029: import org.eclipse.jface.viewers.ISelectionChangedListener;
030: import org.eclipse.jface.viewers.ISelectionProvider;
031: import org.eclipse.jface.viewers.IStructuredSelection;
032: import org.eclipse.jface.viewers.SelectionChangedEvent;
033: import org.eclipse.jface.viewers.StructuredSelection;
034: import org.eclipse.swt.widgets.Display;
035:
036: /**
037: * A SelectionProvider Decorator that wraps a selection provider and attempts (using the Platform's
038: * Adapter Factory mechanism) to adapt all non-adaptable objects to adaptable objects.
039: *
040: * @author jeichar
041: * @since 0.3
042: */
043: public class UDIGAdapterSelectionProvider implements
044: ISelectionProvider, ISelectionChangedListener {
045:
046: private ISelectionProvider provider;
047: CopyOnWriteArrayList<ISelectionChangedListener> listeners = new CopyOnWriteArrayList<ISelectionChangedListener>();
048:
049: /**
050: * Construct <code>UDIGAdapterSelectionProvider</code>.*
051: *
052: * @param provider
053: */
054: public UDIGAdapterSelectionProvider(ISelectionProvider provider) {
055: this .provider = provider;
056: provider.addSelectionChangedListener(this );
057: }
058:
059: /**
060: * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
061: */
062: public void addSelectionChangedListener(
063: ISelectionChangedListener listener) {
064: listeners.add(listener);
065: }
066:
067: /**
068: * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
069: */
070: public ISelection getSelection() {
071: ISelection selection = provider.getSelection();
072: List<Object> newSelection = new ArrayList<Object>();
073: if (selection instanceof IStructuredSelection) {
074: for (Iterator iter = ((IStructuredSelection) selection)
075: .iterator(); iter.hasNext();) {
076: Object object = iter.next();
077: if (object instanceof IAdaptable) {
078: newSelection.add(object);
079: continue;
080: }
081:
082: Object adaptable = Platform.getAdapterManager()
083: .getAdapter(object, IAdaptable.class);
084: if (adaptable == null)
085: adaptable = object;
086: newSelection.add(adaptable);
087: }
088: selection = new StructuredSelection(newSelection);
089: }
090:
091: return selection;
092: }
093:
094: /**
095: * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
096: */
097: public void removeSelectionChangedListener(
098: ISelectionChangedListener listener) {
099: listeners.add(listener);
100: }
101:
102: /**
103: * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
104: */
105: public void setSelection(ISelection selection) {
106: List<Object> newSelection = new ArrayList<Object>();
107: if (selection instanceof IStructuredSelection) {
108: for (Iterator iter = ((IStructuredSelection) selection)
109: .iterator(); iter.hasNext();) {
110: Object object = iter.next();
111: if (object instanceof UDIGAdaptableDecorator) {
112: newSelection.add(((UDIGAdaptableDecorator) object)
113: .getObject());
114: continue;
115: }
116:
117: newSelection.add(object);
118: }
119: selection = new StructuredSelection(newSelection);
120: }
121:
122: provider.setSelection(selection);
123: }
124:
125: /**
126: * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
127: */
128: public void selectionChanged(SelectionChangedEvent event) {
129: final SelectionChangedEvent newEvent = new SelectionChangedEvent(
130: this , getSelection());
131: for (final ISelectionChangedListener listener : listeners) {
132: Display.getDefault().asyncExec(new Runnable() {
133: /**
134: * @see java.lang.Runnable#run()
135: */
136: public void run() {
137: listener.selectionChanged(newEvent);
138: }
139: });
140: }
141: }
142:
143: /**
144: * This is ONLY for testing
145: * @return Returns the provider.
146: */
147: public ISelectionProvider testGetProvider() {
148: return provider;
149: }
150:
151: }
|