KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.IPartListener;
14 import org.eclipse.ui.IPartListener2;
15 import org.eclipse.ui.IPartService;
16 import org.eclipse.ui.ISelectionService;
17 import org.eclipse.ui.IWorkbenchPage;
18 import org.eclipse.ui.IWorkbenchPart;
19 import org.eclipse.ui.IWorkbenchPartReference;
20 import org.eclipse.ui.IWorkbenchWindow;
21 import org.eclipse.ui.internal.misc.UIListenerLogging;
22
23 /**
24  * A part service for a workbench window.
25  */

26 public class WWinPartService implements IPartService {
27     private PartService partService = new PartService(UIListenerLogging.WINDOW_PARTLISTENER_EVENTS,
28             UIListenerLogging.WINDOW_PARTLISTENER2_EVENTS);
29
30     private WindowSelectionService selectionService;
31
32     private IWorkbenchPage activePage;
33     
34     private IPartListener2 partListner = new IPartListener2() {
35         public void partActivated(IWorkbenchPartReference ref) {
36             updateActivePart();
37         }
38
39         public void partBroughtToTop(IWorkbenchPartReference ref) {
40             partService.firePartBroughtToTop(ref);
41         }
42
43         public void partClosed(IWorkbenchPartReference ref) {
44             partService.firePartClosed(ref);
45         }
46
47         public void partDeactivated(IWorkbenchPartReference ref) {
48             updateActivePart();
49         }
50
51         public void partOpened(IWorkbenchPartReference ref) {
52             partService.firePartOpened(ref);
53         }
54
55         public void partHidden(IWorkbenchPartReference ref) {
56             partService.firePartHidden(ref);
57         }
58
59         public void partVisible(IWorkbenchPartReference ref) {
60             partService.firePartVisible(ref);
61         }
62
63         public void partInputChanged(IWorkbenchPartReference ref) {
64             partService.firePartInputChanged(ref);
65         }
66     };
67
68     /**
69      * Creates a new part service for a workbench window.
70      */

71     public WWinPartService(IWorkbenchWindow window) {
72         selectionService = new WindowSelectionService(window);
73     }
74
75     /*
76      * (non-Javadoc)
77      * Method declared on IPartService
78      */

79     public void addPartListener(IPartListener l) {
80         partService.addPartListener(l);
81     }
82
83     /*
84      * (non-Javadoc)
85      * Method declared on IPartService
86      */

87     public void addPartListener(IPartListener2 l) {
88         partService.addPartListener(l);
89     }
90
91     /*
92      * (non-Javadoc)
93      * Method declared on IPartService
94      */

95     public void removePartListener(IPartListener l) {
96         partService.removePartListener(l);
97     }
98
99     /*
100      * (non-Javadoc)
101      * Method declared on IPartService
102      */

103     public void removePartListener(IPartListener2 l) {
104         partService.removePartListener(l);
105     }
106
107     /*
108      * (non-Javadoc)
109      * Method declared on IPartService
110      */

111     public IWorkbenchPart getActivePart() {
112         return partService.getActivePart();
113     }
114     
115     private void updateActivePart() {
116         IWorkbenchPartReference activeRef = null;
117         IWorkbenchPart activePart = null;
118         
119         if (activePage != null) {
120             activePart = activePage.getActivePart();
121             activeRef = activePage.getActivePartReference();
122         }
123         
124         partService.setActivePart(activeRef);
125         selectionService.setActivePart(activePart);
126     }
127
128     /*
129      * (non-Javadoc)
130      * Method declared on IPartService
131      */

132     public IWorkbenchPartReference getActivePartReference() {
133         return partService.getActivePartReference();
134     }
135
136     /*
137      * Returns the selection service.
138      */

139     ISelectionService getSelectionService() {
140         return selectionService;
141     }
142
143     /*
144      * Notifies that a page has been activated.
145      */

146     void pageActivated(IWorkbenchPage newPage) {
147         // Optimize.
148
if (newPage == activePage) {
149             return;
150         }
151
152         // Fire events in the following order:
153

154         // 1. For each open part in the new page, open it and then (if applicable) make it visible
155
// 2. Deactivate old active part
156
// 3. Activate the new active part
157
// 4. For each open part in the old page, make it invisible then close it
158

159         // Hook listener on the new page.
160
if (newPage != null) {
161             IWorkbenchPartReference[] refs = ((WorkbenchPage)newPage).getOpenParts();
162             
163             for (int i = 0; i < refs.length; i++) {
164                 IWorkbenchPartReference reference = refs[i];
165                 
166                 partService.firePartOpened(reference);
167                 
168                 IWorkbenchPart part = reference.getPart(false);
169                 if (part != null && newPage.isPartVisible(part)) {
170                     partService.firePartVisible(reference);
171                 }
172             }
173
174             partService.setActivePart(newPage.getActivePartReference());
175             selectionService.setActivePart(newPage.getActivePart());
176         } else {
177             partService.setActivePart(null);
178             selectionService.setActivePart(null);
179         }
180
181         // Unhook listener from the old page.
182
reset();
183
184         // Update active page.
185
activePage = newPage;
186
187         if (newPage != null) {
188             newPage.addPartListener(partListner);
189         }
190         
191     }
192
193     /*
194      * Notifies that a page has been closed
195      */

196     void pageClosed(IWorkbenchPage page) {
197         // Unhook listener from the old page.
198
if (page == activePage) {
199             reset();
200         }
201     }
202
203     /*
204      * Notifies that a page has been opened.
205      */

206     void pageOpened(IWorkbenchPage page) {
207         pageActivated(page);
208     }
209
210     /*
211      * Resets the part service. The active page, part and selection are
212      * dereferenced.
213      */

214     private void reset() {
215         IWorkbenchPage tempPage = activePage;
216         activePage = null;
217         if (tempPage != null) {
218             WorkbenchPage page = (WorkbenchPage)tempPage;
219             
220             IWorkbenchPartReference[] refs = page.getOpenParts();
221             
222             for (int i = 0; i < refs.length; i++) {
223                 IWorkbenchPartReference reference = refs[i];
224                 
225                 if (page.isPartVisible(reference)) {
226                     partService.firePartHidden(reference);
227                 }
228                 
229                 partService.firePartClosed(reference);
230             }
231                     
232             tempPage.removePartListener(partListner);
233         }
234         
235     }
236     
237 }
238
Popular Tags