KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > loskutov > bco > views > BCOViewSelectionProvider


1 package de.loskutov.bco.views;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.eclipse.core.runtime.IStatus;
7 import org.eclipse.jface.viewers.IPostSelectionProvider;
8 import org.eclipse.jface.viewers.ISelection;
9 import org.eclipse.jface.viewers.ISelectionChangedListener;
10
11 import de.loskutov.bco.BytecodeOutlinePlugin;
12
13 /**
14  * Adapter for different selection provider in one view - text control and table control.
15  * See <a HREF="http://forge.objectweb.org/tracker/?func=detail&atid=100023&aid=304424&group_id=23">bug 304424</a>
16  * The main problem is, that
17  * <pre>
18  * getSite().setSelectionProvider(viewSelectionProvider);
19  * </pre>
20  * could be set only once per view, so that we cannot switch existing
21  * selection provider on the fly (or I have no idea how to do this simplier way).
22  * @author Andrei
23  */

24 class BCOViewSelectionProvider implements IPostSelectionProvider {
25
26     private IPostSelectionProvider realProvider;
27     private List JavaDoc selProviders;
28     private ISelection selection;
29
30     public BCOViewSelectionProvider(){
31         super();
32         selProviders = new ArrayList JavaDoc();
33     }
34
35     public void setCurrentSelectionProvider(IPostSelectionProvider provider){
36         if(!selProviders.contains(provider)){
37             BytecodeOutlinePlugin.log(
38                 new Exception JavaDoc(
39                     "Current selection provider is not registered yet"),
40                 IStatus.WARNING);
41             return;
42         }
43         realProvider = provider;
44         if(selection != null) {
45             realProvider.setSelection(selection);
46         }
47     }
48
49     public void registerSelectionProvider(IPostSelectionProvider provider) {
50         if(!selProviders.contains(provider)){
51             selProviders.add(provider);
52         }
53     }
54
55     /* (non-Javadoc)
56      * @see org.eclipse.jface.viewers.IPostSelectionProvider#addPostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
57      */

58     public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
59         for (int i = 0; i < selProviders.size(); i++) {
60             IPostSelectionProvider provider = (IPostSelectionProvider) selProviders.get(i);
61             provider.addPostSelectionChangedListener(listener);
62         }
63     }
64
65     /* (non-Javadoc)
66      * @see org.eclipse.jface.viewers.IPostSelectionProvider#removePostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
67      */

68     public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
69         for (int i = 0; i < selProviders.size(); i++) {
70             IPostSelectionProvider provider = (IPostSelectionProvider) selProviders.get(i);
71             provider.removePostSelectionChangedListener(listener);
72         }
73     }
74
75     /* (non-Javadoc)
76      * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
77      */

78     public void addSelectionChangedListener(ISelectionChangedListener listener) {
79         for (int i = 0; i < selProviders.size(); i++) {
80             IPostSelectionProvider provider = (IPostSelectionProvider) selProviders.get(i);
81             provider.addSelectionChangedListener(listener);
82         }
83     }
84
85     /* (non-Javadoc)
86      * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
87      */

88     public ISelection getSelection() {
89         return realProvider != null? realProvider.getSelection() : null;
90     }
91
92     /* (non-Javadoc)
93      * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
94      */

95     public void removeSelectionChangedListener(ISelectionChangedListener listener) {
96         for (int i = 0; i < selProviders.size(); i++) {
97             IPostSelectionProvider provider = (IPostSelectionProvider) selProviders.get(i);
98             provider.removeSelectionChangedListener(listener);
99         }
100     }
101
102     /* (non-Javadoc)
103      * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
104      */

105     public void setSelection(ISelection selection) {
106         this.selection = selection;
107         if(realProvider != null) {
108             realProvider.setSelection(selection);
109         }
110     }
111
112 }
Popular Tags