KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > ConvertingSelectionProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.ListenerList;
20
21 import org.eclipse.core.resources.IResource;
22
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.ISelectionChangedListener;
25 import org.eclipse.jface.viewers.ISelectionProvider;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.SelectionChangedEvent;
28 import org.eclipse.jface.viewers.StructuredSelection;
29
30 import org.eclipse.jdt.core.IJavaElement;
31
32
33
34 /**
35  * A converting selection provider is a special selection provider which converts
36  * a selection before notifying any listeners. Additional it converts the selection
37  * on <code>getSelection</code> and <code>setSelection</code>. The default strategy
38  * used to adapt the elements of the selection to {@link IJavaElement} or a {@link IResource},
39  * but implementors can override this behavior.
40  *
41  * @since 3.2
42  */

43 public class ConvertingSelectionProvider implements ISelectionProvider {
44
45     private final ISelectionProvider fProvider;
46     private SelectionChangedListener fListener;
47     
48     private class SelectionChangedListener implements ISelectionChangedListener {
49         
50         ListenerList fListeners= new ListenerList();
51         
52         public void selectionChanged(SelectionChangedEvent event) {
53             ISelection selection= convertFrom(event.getSelection());
54             SelectionChangedEvent newEvent= new SelectionChangedEvent(ConvertingSelectionProvider.this, selection);
55             Object JavaDoc[] listeners= fListeners.getListeners();
56             for (int i= 0; i < listeners.length; i++) {
57                 ((ISelectionChangedListener)listeners[i]).selectionChanged(newEvent);
58             }
59         }
60         public void addListener(ISelectionChangedListener listener) {
61             fListeners.add(listener);
62         }
63         public void removeListener(ISelectionChangedListener listener) {
64             fListeners.remove(listener);
65         }
66         public boolean isEmpty() {
67             return fListeners.isEmpty();
68         }
69     }
70     
71     /**
72      * Creates a {@link ConvertingSelectionProvider} to convert from a given selection provider
73      * using the default mechanism.
74      *
75      * @param provider the provider to covert from and to
76      */

77     public ConvertingSelectionProvider(ISelectionProvider provider) {
78         Assert.isNotNull(provider);
79         fProvider= provider;
80     }
81     
82     /**
83      * Converts the given original viewer selection into a new
84      * selection. The default behavior adapts the elements in the selection
85      * first to {@link IJavaElement} then to {@link IResource}.
86      * Implementors want to override this method.
87      *
88      * @param viewerSelection the original viewer selection
89      *
90      * @return the new selection to be used
91      */

92     public ISelection convertFrom(ISelection viewerSelection) {
93         return convertFromUsingDefaultMechanism(viewerSelection);
94     }
95
96
97     private ISelection convertFromUsingDefaultMechanism(ISelection viewerSelection) {
98         if (! (viewerSelection instanceof IStructuredSelection))
99             return viewerSelection;
100         IStructuredSelection selection= (IStructuredSelection)viewerSelection;
101         List JavaDoc result= new ArrayList JavaDoc(selection.size());
102         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
103             Object JavaDoc element= iter.next();
104             if (element instanceof IResource || element instanceof IJavaElement) {
105                 result.add(element);
106             } else if (element instanceof IAdaptable) {
107                 IAdaptable adaptable= (IAdaptable)element;
108                 IJavaElement jElement= (IJavaElement)adaptable.getAdapter(IJavaElement.class);
109                 if (jElement != null) {
110                     result.add(jElement);
111                 } else {
112                     IResource resource= (IResource)adaptable.getAdapter(IResource.class);
113                     if (resource != null) {
114                         result.add(resource);
115                     } else {
116                         result.add(element);
117                     }
118                 }
119             } else {
120                 result.add(element);
121             }
122         }
123         return new StructuredSelection(result);
124     }
125
126     /**
127      * Converts a selection to a viewer selection. The default implementation does not convert
128      * the selection. Implementors want to override this behavior.
129      *
130      * @param selection the selection to convert
131      *
132      * @return a viewer selection
133      */

134     public ISelection convertTo(ISelection selection) {
135         return selection;
136     }
137     
138     
139     /**
140      * {@inheritDoc}
141      */

142     public final ISelection getSelection() {
143         return convertFrom(fProvider.getSelection());
144     }
145
146     /**
147      * {@inheritDoc}
148      */

149     public final void setSelection(ISelection selection) {
150         fProvider.setSelection(convertTo(selection));
151     }
152     
153     /**
154      * {@inheritDoc}
155      */

156     public void addSelectionChangedListener(ISelectionChangedListener listener) {
157         if (fListener == null) {
158             fListener= new SelectionChangedListener();
159             fProvider.addSelectionChangedListener(fListener);
160         }
161         fListener.addListener(listener);
162     }
163     
164     /**
165      * {@inheritDoc}
166      */

167     public void removeSelectionChangedListener(ISelectionChangedListener listener) {
168         if (fListener == null)
169             return;
170         fListener.removeListener(listener);
171         if (fListener.isEmpty()) {
172             fProvider.removeSelectionChangedListener(fListener);
173             fListener= null;
174         }
175     }
176 }
177
Popular Tags