KickJava   Java API By Example, From Geeks To Geeks.

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


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.ISelection;
14 import org.eclipse.ui.INullSelectionListener;
15 import org.eclipse.ui.IPageListener;
16 import org.eclipse.ui.IWorkbenchPage;
17 import org.eclipse.ui.IWorkbenchPart;
18 import org.eclipse.ui.IWorkbenchWindow;
19
20 /**
21  * Provides part selection tracking for a part with a specific id
22  * in all pages of a specific workbench window. This tracker shields
23  * clients from a part opening and closing, and still provides selection
24  * notification/information even when the part is not active.
25  */

26 public class WindowPartSelectionTracker extends AbstractPartSelectionTracker
27         implements IPageListener {
28     /**
29      * The window this selection tracker is working in
30      */

31     private IWorkbenchWindow fWindow;
32
33     /**
34      * Part selection listener.
35      */

36     private final INullSelectionListener selListener = new INullSelectionListener() {
37         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
38             fireSelection(part, selection);
39         }
40     };
41
42     /**
43      * Part post selection listener
44      */

45     private final INullSelectionListener postSelListener = new INullSelectionListener() {
46         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
47             firePostSelection(part, selection);
48         }
49     };
50
51     /**
52      * Constructs a new selection tracker for the given window and part id.
53      *
54      * @param window workbench window
55      * @param partId part identifier
56      */

57     public WindowPartSelectionTracker(IWorkbenchWindow window, String JavaDoc partId) {
58         super(partId);
59         setWindow(window);
60         window.addPageListener(this);
61         IWorkbenchPage[] pages = window.getPages();
62         for (int i = 0; i < pages.length; i++) {
63             pageOpened(pages[i]);
64         }
65     }
66
67     /*
68      * @see IPageListener#pageActivated(IWorkbenchPage)
69      */

70     public void pageActivated(IWorkbenchPage page) {
71     }
72
73     /*
74      * @see IPageListener#pageClosed(IWorkbenchPage)
75      */

76     public void pageClosed(IWorkbenchPage page) {
77         page.removeSelectionListener(getPartId(), selListener);
78         page.removePostSelectionListener(getPartId(), postSelListener);
79     }
80
81     /*
82      * @see IPageListener#pageOpened(IWorkbenchPage)
83      */

84     public void pageOpened(IWorkbenchPage page) {
85         page.addSelectionListener(getPartId(), selListener);
86         page.addPostSelectionListener(getPartId(), postSelListener);
87     }
88
89     /**
90      * Sets the window this tracker is working in.
91      *
92      * @param window workbench window
93      */

94     private void setWindow(IWorkbenchWindow window) {
95         fWindow = window;
96     }
97
98     /**
99      * Returns the window this tracker is working in.
100      *
101      * @return workbench window
102      */

103     protected IWorkbenchWindow getWindow() {
104         return fWindow;
105     }
106
107     /**
108      * @see AbstractPartSelectionTracker#dispose()
109      */

110     public void dispose() {
111         super.dispose();
112         fWindow = null;
113     }
114
115     /*
116      * @see AbstractPartSelectionTracker#getSelection()
117      */

118     public ISelection getSelection() {
119         IWorkbenchPage page = getWindow().getActivePage();
120         if (page != null) {
121             return page.getSelection(getPartId());
122         }
123         return null;
124     }
125 }
126
Popular Tags