KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.ListenerList;
14 import org.eclipse.core.runtime.Platform;
15 import org.eclipse.jface.util.SafeRunnable;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.ui.INullSelectionListener;
18 import org.eclipse.ui.ISelectionListener;
19 import org.eclipse.ui.IWorkbenchPart;
20
21 /**
22  * Provides per-part selection tracking for the selection service.
23  */

24 public abstract class AbstractPartSelectionTracker {
25     /**
26      * List of selection listeners for this tracker
27      */

28     private ListenerList fListeners = new ListenerList();
29
30     /**
31      * List of post selection listeners for this tracker
32      */

33     private ListenerList postListeners = new ListenerList();
34
35     /**
36      * The id of the part this tracls
37      */

38     private String JavaDoc fPartId;
39
40     /**
41      * Constructs a part selection tracker for the part with the given id.
42      *
43      * @param id part identifier
44      */

45     public AbstractPartSelectionTracker(String JavaDoc partId) {
46         setPartId(partId);
47     }
48
49     /**
50      * Adds a selection listener to this tracker
51      *
52      * @param listener the listener to add
53      */

54     public void addSelectionListener(ISelectionListener listener) {
55         fListeners.add(listener);
56     }
57
58     /**
59      * Adds a post selection listener to this tracker
60      *
61      * @param listener the listener to add
62      */

63     public void addPostSelectionListener(ISelectionListener listener) {
64         postListeners.add(listener);
65     }
66
67     /**
68      * Returns the selection from the part being tracked,
69      * or <code>null</code> if the part is closed or has no selection.
70      */

71     public abstract ISelection getSelection();
72
73     /**
74      * Removes a selection listener from this tracker.
75      *
76      * @param listener the listener to remove
77      */

78     public void removeSelectionListener(ISelectionListener listener) {
79         fListeners.remove(listener);
80     }
81
82     /**
83      * Removes a post selection listener from this tracker.
84      *
85      * @param listener the listener to remove
86      */

87     public void removePostSelectionListener(ISelectionListener listener) {
88         postListeners.remove(listener);
89     }
90
91     /**
92      * Disposes this selection tracker. This removes all listeners currently registered.
93      */

94     public void dispose() {
95         synchronized (fListeners) {
96             Object JavaDoc[] listeners = fListeners.getListeners();
97             for (int i = 0; i < listeners.length; i++) {
98                 fListeners.remove(listeners[i]);
99                 postListeners.remove(listeners[i]);
100             }
101         }
102     }
103
104     /**
105      * Fires a selection event to the listeners.
106      *
107      * @param part the part or <code>null</code> if no active part
108      * @param sel the selection or <code>null</code> if no active selection
109      * @param listeners the list of listeners to notify
110      */

111     protected void fireSelection(final IWorkbenchPart part, final ISelection sel) {
112         Object JavaDoc[] array = fListeners.getListeners();
113         for (int i = 0; i < array.length; i++) {
114             final ISelectionListener l = (ISelectionListener) array[i];
115             if ((part != null && sel != null)
116                     || l instanceof INullSelectionListener) {
117                 Platform.run(new SafeRunnable() {
118                     public void run() {
119                         l.selectionChanged(part, sel);
120                     }
121                 });
122             }
123         }
124     }
125
126     /**
127      * Fires a post selection event to the listeners.
128      *
129      * @param part the part or <code>null</code> if no active part
130      * @param sel the selection or <code>null</code> if no active selection
131      * @param listeners the list of listeners to notify
132      */

133     protected void firePostSelection(final IWorkbenchPart part,
134             final ISelection sel) {
135         Object JavaDoc[] array = postListeners.getListeners();
136         for (int i = 0; i < array.length; i++) {
137             final ISelectionListener l = (ISelectionListener) array[i];
138             if ((part != null && sel != null)
139                     || l instanceof INullSelectionListener) {
140                 Platform.run(new SafeRunnable() {
141                     public void run() {
142                         l.selectionChanged(part, sel);
143                     }
144                 });
145             }
146         }
147     }
148
149     /**
150      * Sets the id of the part that this tracks.
151      *
152      * @param id view identifier
153      */

154     private void setPartId(String JavaDoc partId) {
155         fPartId = partId;
156     }
157
158     /**
159      * Returns the id of the part that this tracks.
160      *
161      * @return part identifier
162      */

163     protected String JavaDoc getPartId() {
164         return fPartId;
165     }
166
167 }
168
Popular Tags