001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.ui.IPartListener;
013: import org.eclipse.ui.IPartListener2;
014: import org.eclipse.ui.IPartService;
015: import org.eclipse.ui.ISelectionService;
016: import org.eclipse.ui.IWorkbenchPage;
017: import org.eclipse.ui.IWorkbenchPart;
018: import org.eclipse.ui.IWorkbenchPartReference;
019: import org.eclipse.ui.IWorkbenchWindow;
020: import org.eclipse.ui.internal.misc.UIListenerLogging;
021:
022: /**
023: * A part service for a workbench window.
024: */
025: public class WWinPartService implements IPartService {
026: private PartService partService = new PartService(
027: UIListenerLogging.WINDOW_PARTLISTENER_EVENTS,
028: UIListenerLogging.WINDOW_PARTLISTENER2_EVENTS);
029:
030: private WindowSelectionService selectionService;
031:
032: private IWorkbenchPage activePage;
033:
034: private IPartListener2 partListner = new IPartListener2() {
035: public void partActivated(IWorkbenchPartReference ref) {
036: updateActivePart();
037: }
038:
039: public void partBroughtToTop(IWorkbenchPartReference ref) {
040: partService.firePartBroughtToTop(ref);
041: }
042:
043: public void partClosed(IWorkbenchPartReference ref) {
044: partService.firePartClosed(ref);
045: }
046:
047: public void partDeactivated(IWorkbenchPartReference ref) {
048: updateActivePart();
049: }
050:
051: public void partOpened(IWorkbenchPartReference ref) {
052: partService.firePartOpened(ref);
053: }
054:
055: public void partHidden(IWorkbenchPartReference ref) {
056: partService.firePartHidden(ref);
057: }
058:
059: public void partVisible(IWorkbenchPartReference ref) {
060: partService.firePartVisible(ref);
061: }
062:
063: public void partInputChanged(IWorkbenchPartReference ref) {
064: partService.firePartInputChanged(ref);
065: }
066: };
067:
068: /**
069: * Creates a new part service for a workbench window.
070: */
071: public WWinPartService(IWorkbenchWindow window) {
072: selectionService = new WindowSelectionService(window);
073: }
074:
075: /*
076: * (non-Javadoc)
077: * Method declared on IPartService
078: */
079: public void addPartListener(IPartListener l) {
080: partService.addPartListener(l);
081: }
082:
083: /*
084: * (non-Javadoc)
085: * Method declared on IPartService
086: */
087: public void addPartListener(IPartListener2 l) {
088: partService.addPartListener(l);
089: }
090:
091: /*
092: * (non-Javadoc)
093: * Method declared on IPartService
094: */
095: public void removePartListener(IPartListener l) {
096: partService.removePartListener(l);
097: }
098:
099: /*
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)
162: .getOpenParts();
163:
164: for (int i = 0; i < refs.length; i++) {
165: IWorkbenchPartReference reference = refs[i];
166:
167: partService.firePartOpened(reference);
168:
169: IWorkbenchPart part = reference.getPart(false);
170: if (part != null && newPage.isPartVisible(part)) {
171: partService.firePartVisible(reference);
172: }
173: }
174:
175: partService.setActivePart(newPage.getActivePartReference());
176: selectionService.setActivePart(newPage.getActivePart());
177: } else {
178: partService.setActivePart(null);
179: selectionService.setActivePart(null);
180: }
181:
182: // Unhook listener from the old page.
183: reset();
184:
185: // Update active page.
186: activePage = newPage;
187:
188: if (newPage != null) {
189: newPage.addPartListener(partListner);
190: }
191:
192: }
193:
194: /*
195: * Notifies that a page has been closed
196: */
197: void pageClosed(IWorkbenchPage page) {
198: // Unhook listener from the old page.
199: if (page == activePage) {
200: reset();
201: }
202: }
203:
204: /*
205: * Notifies that a page has been opened.
206: */
207: void pageOpened(IWorkbenchPage page) {
208: pageActivated(page);
209: }
210:
211: /*
212: * Resets the part service. The active page, part and selection are
213: * dereferenced.
214: */
215: private void reset() {
216: IWorkbenchPage tempPage = activePage;
217: activePage = null;
218: if (tempPage != null) {
219: WorkbenchPage page = (WorkbenchPage) tempPage;
220:
221: IWorkbenchPartReference[] refs = page.getOpenParts();
222:
223: for (int i = 0; i < refs.length; i++) {
224: IWorkbenchPartReference reference = refs[i];
225:
226: if (page.isPartVisible(reference)) {
227: partService.firePartHidden(reference);
228: }
229:
230: partService.firePartClosed(reference);
231: }
232:
233: tempPage.removePartListener(partListner);
234: }
235:
236: }
237:
238: }
|