KickJava   Java API By Example, From Geeks To Geeks.

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


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.jface.viewers.IPostSelectionProvider;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.ISelectionChangedListener;
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.jface.viewers.SelectionChangedEvent;
18 import org.eclipse.ui.IPartListener;
19 import org.eclipse.ui.IViewPart;
20 import org.eclipse.ui.IWorkbenchPage;
21 import org.eclipse.ui.IWorkbenchPart;
22
23 /**
24  * Provides debug view selection management/notification for
25  * a debug view in a specific workbench page. This selection
26  * provider sheilds clients from a debug view openning and closing,
27  * and still provides selection notification/information even
28  * when the debug view is not the active part.
29  */

30 public class PagePartSelectionTracker extends AbstractPartSelectionTracker
31         implements IPartListener, ISelectionChangedListener {
32
33     /**
34      * The workbench page for which this is tracking selection.
35      */

36     private IWorkbenchPage fPage;
37
38     /**
39      * The part in this tracker's page, or <code>null</code> if one is not open.
40      */

41     private IWorkbenchPart fPart;
42
43     private ISelectionChangedListener selectionListener = new ISelectionChangedListener() {
44         public void selectionChanged(SelectionChangedEvent event) {
45             fireSelection(getPart(), event.getSelection());
46         }
47     };
48
49     private ISelectionChangedListener postSelectionListener = new ISelectionChangedListener() {
50         public void selectionChanged(SelectionChangedEvent event) {
51             firePostSelection(getPart(), event.getSelection());
52         }
53     };
54
55     public PagePartSelectionTracker(IWorkbenchPage page, String JavaDoc partId) {
56         super(partId);
57         setPage(page);
58         page.addPartListener(this);
59         IViewPart part = page.findView(partId);
60         if (part != null) {
61             setPart(part, false);
62         }
63     }
64
65     /**
66      * Disposes this selection provider - removes all listeners
67      * currently registered.
68      */

69     public void dispose() {
70         setPart(null, false);
71         setPage(null);
72         super.dispose();
73     }
74
75     /*
76      * @see IPartListener#partActivated(IWorkbenchPart)
77      */

78     public void partActivated(IWorkbenchPart part) {
79     }
80
81     /*
82      * @see IPartListener#partBroughtToTop(IWorkbenchPart)
83      */

84     public void partBroughtToTop(IWorkbenchPart part) {
85     }
86
87     /**
88      * @see IPartListener#partClosed(IWorkbenchPart)
89      */

90     public void partClosed(IWorkbenchPart part) {
91         if (getPartId(part).equals(getPartId())) {
92             setPart(null, true);
93         }
94     }
95
96     /*
97      * @see IPartListener#partDeactivated(IWorkbenchPart)
98      */

99     public void partDeactivated(IWorkbenchPart part) {
100     }
101
102     /**
103      * @see IPartListener#partOpened(IWorkbenchPart)
104      */

105     public void partOpened(IWorkbenchPart part) {
106         if (getPartId(part).equals(getPartId())) {
107             setPart(part, true);
108         }
109     }
110
111     /**
112      * Returns the id for the given part, taking into account
113      * multi-view instances which may have a secondary id.
114      *
115      * @since 3.0
116      */

117     private Object JavaDoc getPartId(IWorkbenchPart part) {
118         String JavaDoc id = part.getSite().getId();
119         if (part instanceof IViewPart) {
120             String JavaDoc secondaryId = ((IViewPart) part).getViewSite()
121                     .getSecondaryId();
122             if (secondaryId != null) {
123                 id = id + ':' + secondaryId;
124             }
125         }
126         return id;
127     }
128
129     /**
130      * The selection has changed in the part being tracked.
131      * Forward it to the listeners.
132      *
133      * @see ISelectionChangedListener#selectionChanged
134      */

135     public void selectionChanged(SelectionChangedEvent event) {
136         fireSelection(getPart(), event.getSelection());
137     }
138
139     /**
140      * Sets the page this selection provider works for
141      *
142      * @param page workbench page
143      */

144     private void setPage(IWorkbenchPage page) {
145         fPage = page;
146     }
147
148     /**
149      * Returns the page this selection provider works for
150      *
151      * @return workbench page
152      */

153     protected IWorkbenchPage getPage() {
154         return fPage;
155     }
156
157     /**
158      * Returns the part this is tracking,
159      * or <code>null</code> if it is not open
160      *
161      * @return part., or <code>null</code>
162      */

163     protected IWorkbenchPart getPart() {
164         return fPart;
165     }
166
167     /*
168      * @see AbstractPartSelectionTracker#getSelection()
169      */

170     public ISelection getSelection() {
171         IWorkbenchPart part = getPart();
172         if (part != null) {
173             ISelectionProvider sp = part.getSite().getSelectionProvider();
174             if (sp != null) {
175                 return sp.getSelection();
176             }
177         }
178         return null;
179     }
180
181     /**
182      * @see AbstractDebugSelectionProvider#getSelectionProvider()
183      */

184     protected ISelectionProvider getSelectionProvider() {
185         IWorkbenchPart part = getPart();
186         if (part != null) {
187             return part.getSite().getSelectionProvider();
188         }
189         return null;
190     }
191
192     /**
193      * Sets the part for this selection tracker.
194      *
195      * @param part the part
196      * @param notify whether to send notification that the selection has changed.
197      */

198     private void setPart(IWorkbenchPart part, boolean notify) {
199         if (fPart != null) {
200             // remove myself as a listener from the existing part
201
ISelectionProvider sp = fPart.getSite().getSelectionProvider();
202             if (sp != null) {
203                 sp.removeSelectionChangedListener(selectionListener);
204                 if (sp instanceof IPostSelectionProvider) {
205                     ((IPostSelectionProvider) sp)
206                             .removePostSelectionChangedListener(postSelectionListener);
207                 } else {
208                     sp.removeSelectionChangedListener(postSelectionListener);
209                 }
210             }
211         }
212         fPart = part;
213         ISelection sel = null;
214         if (part != null) {
215             ISelectionProvider sp = part.getSite().getSelectionProvider();
216             if (sp != null) {
217                 sp.addSelectionChangedListener(selectionListener);
218                 if (sp instanceof IPostSelectionProvider) {
219                     ((IPostSelectionProvider) sp)
220                             .addPostSelectionChangedListener(postSelectionListener);
221                 } else {
222                     sp.addSelectionChangedListener(postSelectionListener);
223                 }
224                 if (notify) {
225                     // get the selection to send below
226
sel = sp.getSelection();
227                 }
228             }
229         }
230         if (notify) {
231             fireSelection(part, sel);
232             firePostSelection(part, sel);
233         }
234     }
235 }
236
Popular Tags