KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > AbstractSelectionService


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.ui.internal;
12
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.core.runtime.ListenerList;
16 import org.eclipse.jface.viewers.IPostSelectionProvider;
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.SelectionChangedEvent;
21 import org.eclipse.ui.INullSelectionListener;
22 import org.eclipse.ui.ISelectionListener;
23 import org.eclipse.ui.ISelectionService;
24 import org.eclipse.ui.IWorkbenchPart;
25
26 /**
27  * Abstract selection service.
28  */

29 public abstract class AbstractSelectionService implements ISelectionService {
30
31     /**
32      * The list of selection listeners (not per-part).
33      */

34     private ListenerList listeners = new ListenerList();
35
36     /**
37      * The list of post selection listeners (not per-part).
38      */

39     private ListenerList postListeners = new ListenerList();
40
41     /**
42      * The currently active part.
43      */

44     private IWorkbenchPart activePart;
45
46     /**
47      * The active part's selection provider, remembered in case the part
48      * replaces its selection provider after we hooked a listener.
49      */

50     private ISelectionProvider activeProvider;
51
52     /**
53      * Map from part id (String) to per-part tracker (AbstractPartSelectionTracker).
54      */

55     private Hashtable JavaDoc perPartTrackers;
56
57     /**
58      * The JFace selection listener to hook on the active part's selection provider.
59      */

60     private ISelectionChangedListener selListener = new ISelectionChangedListener() {
61         public void selectionChanged(SelectionChangedEvent event) {
62             fireSelection(activePart, event.getSelection());
63         }
64     };
65
66     /**
67      * The JFace post selection listener to hook on the active part's selection provider.
68      */

69     private ISelectionChangedListener postSelListener = new ISelectionChangedListener() {
70         public void selectionChanged(SelectionChangedEvent event) {
71             firePostSelection(activePart, event.getSelection());
72         }
73     };
74
75     /**
76      * Creates a new SelectionService.
77      */

78     protected AbstractSelectionService() {
79     }
80
81     /* (non-Javadoc)
82      * Method declared on ISelectionService.
83      */

84     public void addSelectionListener(ISelectionListener l) {
85         listeners.add(l);
86     }
87
88     /* (non-Javadoc)
89      * Method declared on ISelectionService.
90      */

91     public void addSelectionListener(String JavaDoc partId, ISelectionListener listener) {
92         getPerPartTracker(partId).addSelectionListener(listener);
93     }
94
95     /* (non-Javadoc)
96      * Method declared on ISelectionService.
97      */

98     public void addPostSelectionListener(ISelectionListener l) {
99         postListeners.add(l);
100     }
101
102     /* (non-Javadoc)
103      * Method declared on ISelectionService.
104      */

105     public void addPostSelectionListener(String JavaDoc partId,
106             ISelectionListener listener) {
107         getPerPartTracker(partId).addPostSelectionListener(listener);
108     }
109
110     /* (non-Javadoc)
111      * Method declared on ISelectionService.
112      */

113     public void removeSelectionListener(ISelectionListener l) {
114         listeners.remove(l);
115     }
116
117     /*
118      * (non-Javadoc)
119      * Method declared on ISelectionListener.
120      */

121     public void removePostSelectionListener(String JavaDoc partId,
122             ISelectionListener listener) {
123         getPerPartTracker(partId).removePostSelectionListener(listener);
124     }
125
126     /* (non-Javadoc)
127      * Method declared on ISelectionService.
128      */

129     public void removePostSelectionListener(ISelectionListener l) {
130         postListeners.remove(l);
131     }
132
133     /*
134      * (non-Javadoc)
135      * Method declared on ISelectionListener.
136      */

137     public void removeSelectionListener(String JavaDoc partId,
138             ISelectionListener listener) {
139         getPerPartTracker(partId).removeSelectionListener(listener);
140     }
141
142     /**
143      * Fires a selection event to the given listeners.
144      *
145      * @param part the part or <code>null</code> if no active part
146      * @param sel the selection or <code>null</code> if no active selection
147      */

148     protected void fireSelection(final IWorkbenchPart part, final ISelection sel) {
149         Object JavaDoc[] array = listeners.getListeners();
150         for (int i = 0; i < array.length; i++) {
151             final ISelectionListener l = (ISelectionListener) array[i];
152             if ((part != null && sel != null)
153                     || l instanceof INullSelectionListener) {
154                 
155                 try {
156                     l.selectionChanged(part, sel);
157                 } catch (Exception JavaDoc e) {
158                     WorkbenchPlugin.log(e);
159                 }
160             }
161         }
162     }
163
164     /**
165      * Fires a selection event to the given listeners.
166      *
167      * @param part the part or <code>null</code> if no active part
168      * @param sel the selection or <code>null</code> if no active selection
169      */

170     protected void firePostSelection(final IWorkbenchPart part,
171             final ISelection sel) {
172         Object JavaDoc[] array = postListeners.getListeners();
173         for (int i = 0; i < array.length; i++) {
174             final ISelectionListener l = (ISelectionListener) array[i];
175             if ((part != null && sel != null)
176                     || l instanceof INullSelectionListener) {
177                 
178                 try {
179                     l.selectionChanged(part, sel);
180                 } catch (Exception JavaDoc e) {
181                     WorkbenchPlugin.log(e);
182                 }
183             }
184         }
185     }
186
187     /**
188      * Returns the per-part selection tracker for the given part id.
189      *
190      * @param partId part identifier
191      * @return per-part selection tracker
192      */

193     protected AbstractPartSelectionTracker getPerPartTracker(String JavaDoc partId) {
194         if (perPartTrackers == null) {
195             perPartTrackers = new Hashtable JavaDoc(4);
196         }
197         AbstractPartSelectionTracker tracker = (AbstractPartSelectionTracker) perPartTrackers
198                 .get(partId);
199         if (tracker == null) {
200             tracker = createPartTracker(partId);
201             perPartTrackers.put(partId, tracker);
202         }
203         return tracker;
204     }
205
206     /**
207      * Creates a new per-part selection tracker for the given part id.
208      *
209      * @param partId part identifier
210      * @return per-part selection tracker
211      */

212     protected abstract AbstractPartSelectionTracker createPartTracker(
213             String JavaDoc partId);
214
215     /**
216      * Returns the selection.
217      */

218     public ISelection getSelection() {
219         if (activeProvider != null) {
220             return activeProvider.getSelection();
221         } else {
222             return null;
223         }
224     }
225
226     /*
227      * @see ISelectionService#getSelection(String)
228      */

229     public ISelection getSelection(String JavaDoc partId) {
230         return getPerPartTracker(partId).getSelection();
231     }
232
233     /**
234      * Sets the current-active part (or null if none)
235      *
236      * @since 3.1
237      *
238      * @param newPart the new active part (or null if none)
239      */

240     public void setActivePart(IWorkbenchPart newPart) {
241         // Optimize.
242
if (newPart == activePart) {
243             return;
244         }
245         
246         ISelectionProvider selectionProvider = null;
247         
248         if (newPart != null) {
249             selectionProvider = newPart.getSite().getSelectionProvider();
250             
251             if (selectionProvider == null) {
252                 newPart = null;
253             }
254         }
255         
256         if (newPart == activePart) {
257             return;
258         }
259         
260         if (activePart != null) {
261             if (activeProvider != null) {
262                 activeProvider.removeSelectionChangedListener(selListener);
263                 if (activeProvider instanceof IPostSelectionProvider) {
264                     ((IPostSelectionProvider) activeProvider)
265                             .removePostSelectionChangedListener(postSelListener);
266                 } else {
267                     activeProvider
268                             .removeSelectionChangedListener(postSelListener);
269                 }
270                 activeProvider = null;
271             }
272             activePart = null;
273         }
274
275         activePart = newPart;
276         
277         if (newPart != null) {
278             activeProvider = selectionProvider;
279             // Fire an event if there's an active provider
280
activeProvider.addSelectionChangedListener(selListener);
281             ISelection sel = activeProvider.getSelection();
282             fireSelection(newPart, sel);
283             if (activeProvider instanceof IPostSelectionProvider) {
284                 ((IPostSelectionProvider) activeProvider)
285                         .addPostSelectionChangedListener(postSelListener);
286             } else {
287                 activeProvider.addSelectionChangedListener(postSelListener);
288             }
289             firePostSelection(newPart, sel);
290         } else {
291             fireSelection(null, null);
292             firePostSelection(null, null);
293         }
294     }
295     
296 // /**
297
// * Notifies the listener that a part has been activated.
298
// */
299
// public void partActivated(IWorkbenchPart newPart) {
300
// // Optimize.
301
// if (newPart == activePart)
302
// return;
303
//
304
// // Unhook selection from the old part.
305
// reset();
306
//
307
// // Update active part.
308
// activePart = newPart;
309
//
310
// // Hook selection on the new part.
311
// if (activePart != null) {
312
// activeProvider = activePart.getSite().getSelectionProvider();
313
// if (activeProvider != null) {
314
// // Fire an event if there's an active provider
315
// activeProvider.addSelectionChangedListener(selListener);
316
// ISelection sel = activeProvider.getSelection();
317
// fireSelection(newPart, sel);
318
// if (activeProvider instanceof IPostSelectionProvider)
319
// ((IPostSelectionProvider) activeProvider)
320
// .addPostSelectionChangedListener(postSelListener);
321
// else
322
// activeProvider.addSelectionChangedListener(postSelListener);
323
// firePostSelection(newPart, sel);
324
// } else {
325
// //Reset active part. activeProvider may not be null next time this method is called.
326
// activePart = null;
327
// }
328
// }
329
// // No need to fire an event if no active provider, since this was done in reset()
330
// }
331
//
332
// /**
333
// * Notifies the listener that a part has been brought to the front.
334
// */
335
// public void partBroughtToTop(IWorkbenchPart newPart) {
336
// // do nothing, the active part has not changed,
337
// // so the selection is unaffected
338
// }
339
//
340
// /**
341
// * Notifies the listener that a part has been closed
342
// */
343
// public void partClosed(IWorkbenchPart part) {
344
// // Unhook selection from the part.
345
// if (part == activePart) {
346
// reset();
347
// }
348
// }
349
//
350
// /**
351
// * Notifies the listener that a part has been deactivated.
352
// */
353
// public void partDeactivated(IWorkbenchPart part) {
354
// // Unhook selection from the part.
355
// if (part == activePart) {
356
// reset();
357
// }
358
// }
359
//
360
// /**
361
// * Notifies the listener that a part has been opened.
362
// */
363
// public void partOpened(IWorkbenchPart part) {
364
// // Wait for activation.
365
// }
366
//
367
// /**
368
// * Notifies the listener that a part has been opened.
369
// */
370
// public void partInputChanged(IWorkbenchPart part) {
371
// // 36501 - only process if part is active
372
// if (activePart == part) {
373
// reset();
374
// partActivated(part);
375
// }
376
// }
377
//
378
// /**
379
// * Resets the service. The active part and selection provider are
380
// * dereferenced.
381
// */
382
// public void reset() {
383
// if (activePart != null) {
384
// fireSelection(null, null);
385
// firePostSelection(null, null);
386
// if (activeProvider != null) {
387
// activeProvider.removeSelectionChangedListener(selListener);
388
// if (activeProvider instanceof IPostSelectionProvider)
389
// ((IPostSelectionProvider) activeProvider)
390
// .removePostSelectionChangedListener(postSelListener);
391
// else
392
// activeProvider
393
// .removeSelectionChangedListener(postSelListener);
394
// activeProvider = null;
395
// }
396
// activePart = null;
397
// }
398
// }
399

400 }
401
Popular Tags