KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > SelectionProviderMediator


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.internal.ui.viewsupport;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.ListenerList;
15
16 import org.eclipse.swt.events.FocusEvent;
17 import org.eclipse.swt.events.FocusListener;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Widget;
20
21 import org.eclipse.jface.viewers.IPostSelectionProvider;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.jface.viewers.ISelectionProvider;
25 import org.eclipse.jface.viewers.SelectionChangedEvent;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.jface.viewers.StructuredViewer;
28
29 /**
30  * A selection provider for view parts with more that one viewer.
31  * Tracks the focus of the viewers to provide the correct selection.
32  */

33 public class SelectionProviderMediator implements IPostSelectionProvider {
34
35     private class InternalListener implements ISelectionChangedListener, FocusListener {
36         /*
37          * @see ISelectionChangedListener#selectionChanged
38          */

39         public void selectionChanged(SelectionChangedEvent event) {
40             doSelectionChanged(event);
41         }
42         
43         /*
44          * @see FocusListener#focusGained
45          */

46         public void focusGained(FocusEvent e) {
47             doFocusChanged(e.widget);
48         }
49     
50         /*
51          * @see FocusListener#focusLost
52          */

53         public void focusLost(FocusEvent e) {
54             // do not reset due to focus behavior on GTK
55
//fViewerInFocus= null;
56
}
57     }
58     
59     private class InternalPostSelectionListener implements ISelectionChangedListener {
60         public void selectionChanged(SelectionChangedEvent event) {
61             doPostSelectionChanged(event);
62         }
63         
64     }
65     
66     private StructuredViewer[] fViewers;
67
68     private StructuredViewer fViewerInFocus;
69     private ListenerList fSelectionChangedListeners;
70     private ListenerList fPostSelectionChangedListeners;
71     
72     /**
73      * @param viewers All viewers that can provide a selection
74      * @param viewerInFocus the viewer currently in focus or <code>null</code>
75      */

76     public SelectionProviderMediator(StructuredViewer[] viewers, StructuredViewer viewerInFocus) {
77         Assert.isNotNull(viewers);
78         fViewers= viewers;
79         InternalListener listener= new InternalListener();
80         fSelectionChangedListeners= new ListenerList();
81         fPostSelectionChangedListeners= new ListenerList();
82         fViewerInFocus= viewerInFocus;
83
84         for (int i= 0; i < fViewers.length; i++) {
85             StructuredViewer viewer= fViewers[i];
86             viewer.addSelectionChangedListener(listener);
87             viewer.addPostSelectionChangedListener(new InternalPostSelectionListener());
88             Control control= viewer.getControl();
89             control.addFocusListener(listener);
90         }
91     }
92     
93     private void doFocusChanged(Widget control) {
94         for (int i= 0; i < fViewers.length; i++) {
95             if (fViewers[i].getControl() == control) {
96                 propagateFocusChanged(fViewers[i]);
97                 return;
98             }
99         }
100     }
101     
102     final void doPostSelectionChanged(SelectionChangedEvent event) {
103         ISelectionProvider provider= event.getSelectionProvider();
104         if (provider == fViewerInFocus) {
105             firePostSelectionChanged();
106         }
107     }
108     
109     final void doSelectionChanged(SelectionChangedEvent event) {
110         ISelectionProvider provider= event.getSelectionProvider();
111         if (provider == fViewerInFocus) {
112             fireSelectionChanged();
113         }
114     }
115     
116     final void propagateFocusChanged(StructuredViewer viewer) {
117         if (viewer != fViewerInFocus) { // OK to compare by identity
118
fViewerInFocus= viewer;
119             fireSelectionChanged();
120             firePostSelectionChanged();
121         }
122     }
123     
124     private void fireSelectionChanged() {
125         if (fSelectionChangedListeners != null) {
126             SelectionChangedEvent event= new SelectionChangedEvent(this, getSelection());
127             
128             Object JavaDoc[] listeners= fSelectionChangedListeners.getListeners();
129             for (int i= 0; i < listeners.length; i++) {
130                 ISelectionChangedListener listener= (ISelectionChangedListener) listeners[i];
131                 listener.selectionChanged(event);
132             }
133         }
134     }
135     
136     private void firePostSelectionChanged() {
137         if (fPostSelectionChangedListeners != null) {
138             SelectionChangedEvent event= new SelectionChangedEvent(this, getSelection());
139             
140             Object JavaDoc[] listeners= fPostSelectionChangedListeners.getListeners();
141             for (int i= 0; i < listeners.length; i++) {
142                 ISelectionChangedListener listener= (ISelectionChangedListener) listeners[i];
143                 listener.selectionChanged(event);
144             }
145         }
146     }
147     
148     /*
149      * @see ISelectionProvider#addSelectionChangedListener
150      */

151     public void addSelectionChangedListener(ISelectionChangedListener listener) {
152         fSelectionChangedListeners.add(listener);
153     }
154     
155     /*
156      * @see ISelectionProvider#removeSelectionChangedListener
157      */

158     public void removeSelectionChangedListener(ISelectionChangedListener listener) {
159         fSelectionChangedListeners.remove(listener);
160     }
161     
162     /* (non-Javadoc)
163      * @see org.eclipse.jface.viewers.IPostSelectionProvider#addPostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
164      */

165     public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
166         fPostSelectionChangedListeners.add(listener);
167     }
168
169
170     /* (non-Javadoc)
171      * @see org.eclipse.jface.viewers.IPostSelectionProvider#removePostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
172      */

173     public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
174         fPostSelectionChangedListeners.remove(listener);
175     }
176         
177     /*
178      * @see ISelectionProvider#getSelection
179      */

180     public ISelection getSelection() {
181         if (fViewerInFocus != null) {
182             return fViewerInFocus.getSelection();
183         }
184         return StructuredSelection.EMPTY;
185     }
186     
187     /*
188      * @see ISelectionProvider#setSelection
189      */

190     public void setSelection(ISelection selection) {
191         if (fViewerInFocus != null) {
192             fViewerInFocus.setSelection(selection);
193         }
194     }
195
196     public void setSelection(ISelection selection, boolean reveal) {
197         if (fViewerInFocus != null) {
198             fViewerInFocus.setSelection(selection, reveal);
199         }
200     }
201     
202     /**
203      * Returns the viewer in focus or null if no viewer has the focus
204      */

205     public StructuredViewer getViewerInFocus() {
206         return fViewerInFocus;
207     }
208 }
209
Popular Tags