| 1 |
/** |
| 2 |
* Copyright (c) 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.edit.ui.provider; |
| 12 |
|
| 13 |
import java.util.List; |
| 14 |
|
| 15 |
import org.eclipse.emf.common.util.UniqueEList; |
| 16 |
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; |
| 17 |
import org.eclipse.jface.viewers.ISelection; |
| 18 |
import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 19 |
import org.eclipse.jface.viewers.ISelectionProvider; |
| 20 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 21 |
import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 22 |
import org.eclipse.jface.viewers.StructuredSelection; |
| 23 |
|
| 24 |
/** |
| 25 |
* @since 2.3 |
| 26 |
*/ |
| 27 |
public class UnwrappingSelectionProvider implements ISelectionProvider |
| 28 |
{ |
| 29 |
protected ISelection selection; |
| 30 |
protected List<ISelectionChangedListener> listeners = new UniqueEList.FastCompare<ISelectionChangedListener>(); |
| 31 |
protected ISelectionChangedListener selectionChangedListener = |
| 32 |
new ISelectionChangedListener() |
| 33 |
{ |
| 34 |
public void selectionChanged(SelectionChangedEvent event) |
| 35 |
{ |
| 36 |
setSelection(event.getSelection()); |
| 37 |
} |
| 38 |
}; |
| 39 |
|
| 40 |
public UnwrappingSelectionProvider(ISelectionProvider selectionProvider) |
| 41 |
{ |
| 42 |
selectionProvider.addSelectionChangedListener(selectionChangedListener); |
| 43 |
setSelection(selectionProvider.getSelection()); |
| 44 |
} |
| 45 |
|
| 46 |
public void addSelectionChangedListener(ISelectionChangedListener listener) |
| 47 |
{ |
| 48 |
listeners.add(listener); |
| 49 |
} |
| 50 |
|
| 51 |
public ISelection getSelection() |
| 52 |
{ |
| 53 |
return selection; |
| 54 |
} |
| 55 |
|
| 56 |
public void removeSelectionChangedListener(ISelectionChangedListener listener) |
| 57 |
{ |
| 58 |
listeners.remove(listener); |
| 59 |
} |
| 60 |
|
| 61 |
public void setSelection(ISelection selection) |
| 62 |
{ |
| 63 |
if (selection instanceof IStructuredSelection) |
| 64 |
{ |
| 65 |
Object [] objects = ((IStructuredSelection)selection).toArray(); |
| 66 |
for (int i = 0; i < objects.length; ++i) |
| 67 |
{ |
| 68 |
objects[i] = unwrap(objects[i]); |
| 69 |
} |
| 70 |
this.selection = new StructuredSelection(objects); |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
this.selection = selection; |
| 75 |
} |
| 76 |
fireSelectionChanged(); |
| 77 |
} |
| 78 |
|
| 79 |
protected Object unwrap(Object object) |
| 80 |
{ |
| 81 |
return AdapterFactoryEditingDomain.unwrap(object); |
| 82 |
} |
| 83 |
|
| 84 |
protected void fireSelectionChanged() |
| 85 |
{ |
| 86 |
for (ISelectionChangedListener selectionChangedListener : listeners) |
| 87 |
{ |
| 88 |
selectionChangedListener.selectionChanged(new SelectionChangedEvent(this, selection)); |
| 89 |
} |
| 90 |
} |
| 91 |
} |