KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > part > MultiPageSelectionProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.part;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.ListenerList;
15 import org.eclipse.core.runtime.SafeRunner;
16 import org.eclipse.jface.util.SafeRunnable;
17 import org.eclipse.jface.viewers.IPostSelectionProvider;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.ISelectionProvider;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.ui.IEditorPart;
24
25 /**
26  * Manages the current selection in a multi-page editor by tracking the active
27  * nested editor within the multi-page editor. When the selection changes,
28  * notifications are sent to all registered listeners.
29  * <p>
30  * This class may be instantiated; it is not intended to be subclassed.
31  * The base implementation of <code>MultiPageEditor.init</code> creates
32  * an instance of this class.
33  * </p>
34  */

35 public class MultiPageSelectionProvider implements IPostSelectionProvider {
36
37     /**
38      * Registered selection changed listeners (element type:
39      * <code>ISelectionChangedListener</code>).
40      */

41     private ListenerList listeners = new ListenerList();
42     
43     /**
44      * Registered post selection changed listeners.
45      */

46     private ListenerList postListeners = new ListenerList();
47
48     /**
49      * The multi-page editor.
50      */

51     private MultiPageEditorPart multiPageEditor;
52
53     /**
54      * Creates a selection provider for the given multi-page editor.
55      *
56      * @param multiPageEditor the multi-page editor
57      */

58     public MultiPageSelectionProvider(MultiPageEditorPart multiPageEditor) {
59         Assert.isNotNull(multiPageEditor);
60         this.multiPageEditor = multiPageEditor;
61     }
62
63     /* (non-Javadoc)
64      * Method declared on <code>ISelectionProvider</code>.
65      */

66     public void addSelectionChangedListener(ISelectionChangedListener listener) {
67         listeners.add(listener);
68     }
69
70     /**
71      * Adds a listener for post selection changes in this multi page selection provider.
72      *
73      * @param listener a selection changed listener
74      * @since 3.2
75      */

76     public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
77         postListeners.add(listener);
78     }
79
80     /**
81      * Notifies all registered selection changed listeners that the editor's
82      * selection has changed. Only listeners registered at the time this method is
83      * called are notified.
84      *
85      * @param event the selection changed event
86      */

87     public void fireSelectionChanged(final SelectionChangedEvent event) {
88         Object JavaDoc[] listeners = this.listeners.getListeners();
89         fireEventChange(event, listeners);
90     }
91
92     /**
93      * Notifies all post selection changed listeners that the editor's
94      * selection has changed.
95      *
96      * @param event the event to propogate.
97      * @since 3.2
98      */

99     public void firePostSelectionChanged(final SelectionChangedEvent event) {
100         Object JavaDoc[] listeners = postListeners.getListeners();
101         fireEventChange(event, listeners);
102     }
103
104     private void fireEventChange(final SelectionChangedEvent event, Object JavaDoc[] listeners) {
105         for (int i = 0; i < listeners.length; ++i) {
106             final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i];
107             SafeRunner.run(new SafeRunnable() {
108                 public void run() {
109                     l.selectionChanged(event);
110                 }
111             });
112         }
113     }
114     
115     /**
116      * Returns the multi-page editor.
117      * @return the multi-page editor.
118      */

119     public MultiPageEditorPart getMultiPageEditor() {
120         return multiPageEditor;
121     }
122
123     /* (non-Javadoc)
124      * Method declared on <code>ISelectionProvider</code>.
125      */

126     public ISelection getSelection() {
127         IEditorPart activeEditor = multiPageEditor.getActiveEditor();
128         if (activeEditor != null) {
129             ISelectionProvider selectionProvider = activeEditor.getSite()
130                     .getSelectionProvider();
131             if (selectionProvider != null) {
132                 return selectionProvider.getSelection();
133             }
134         }
135         return StructuredSelection.EMPTY;
136     }
137
138     /* (non-JavaDoc)
139      * Method declaed on <code>ISelectionProvider</code>.
140      */

141     public void removeSelectionChangedListener(
142             ISelectionChangedListener listener) {
143         listeners.remove(listener);
144     }
145     
146     /**
147      * Removes a listener for post selection changes in this multi page selection provider.
148      *
149      * @param listener a selection changed listener
150      * @since 3.2
151      */

152     public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
153         postListeners.remove(listener);
154     }
155
156     /* (non-Javadoc)
157      * Method declared on <code>ISelectionProvider</code>.
158      */

159     public void setSelection(ISelection selection) {
160         IEditorPart activeEditor = multiPageEditor.getActiveEditor();
161         if (activeEditor != null) {
162             ISelectionProvider selectionProvider = activeEditor.getSite()
163                     .getSelectionProvider();
164             if (selectionProvider != null) {
165                 selectionProvider.setSelection(selection);
166             }
167         }
168     }
169 }
170
Popular Tags