blob: 371a15e6c2e9b51cb2c619e6ebfd27c8e1aae983 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
/*******************************************************************************
* Copyright (c) 2006 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.viewsupport;
import java.util.ArrayList;
import java.util.Iterator;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
/**
* A selection provider that adapts the elements of structured selections
* to a requested type.
* @author markus.schorn@windriver.com
*/
public class AdaptingSelectionProvider implements ISelectionProvider, ISelectionChangedListener {
private Class<?> fTargetType;
private ListenerList fListenerList;
private ISelectionProvider fProvider;
public AdaptingSelectionProvider(Class<?> targetType, ISelectionProvider provider) {
fProvider= provider;
fTargetType= targetType;
fListenerList= new ListenerList();
}
private ISelection convertSelection(ISelection selection) {
if (selection != null) {
if (selection instanceof IStructuredSelection) {
IStructuredSelection ss= (IStructuredSelection) selection;
ArrayList<Object> adapted= new ArrayList<Object>();
for (Iterator<?> iter = ss.iterator(); iter.hasNext(); ) {
Object elem= adaptElem(iter.next());
if (elem != null) {
adapted.add(elem);
}
}
return new StructuredSelection(adapted);
}
}
return selection;
}
private Object adaptElem(Object elem) {
if (fTargetType.isInstance(elem)) {
return elem;
}
if (elem instanceof IAdaptable) {
return ((IAdaptable) elem).getAdapter(fTargetType);
}
return null;
}
public void addSelectionChangedListener(ISelectionChangedListener listener) {
if (fListenerList.isEmpty()) {
fProvider.addSelectionChangedListener(this);
}
fListenerList.add(listener);
}
public ISelection getSelection() {
return convertSelection(fProvider.getSelection());
}
public void removeSelectionChangedListener(ISelectionChangedListener listener) {
fListenerList.remove(listener);
if (fListenerList.isEmpty()) {
fProvider.removeSelectionChangedListener(this);
}
}
public void setSelection(ISelection selection) {
throw new UnsupportedOperationException();
}
public void selectionChanged(SelectionChangedEvent event) {
SelectionChangedEvent event2= new SelectionChangedEvent(this, convertSelection(event.getSelection()));
Object[] listeners= fListenerList.getListeners();
for (Object listener : listeners) {
ISelectionChangedListener l= (ISelectionChangedListener) listener;
l.selectionChanged(event2);
}
}
}
|