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.jface.viewers.IPostSelectionProvider;
013: import org.eclipse.jface.viewers.ISelection;
014: import org.eclipse.jface.viewers.ISelectionChangedListener;
015: import org.eclipse.jface.viewers.ISelectionProvider;
016: import org.eclipse.jface.viewers.SelectionChangedEvent;
017: import org.eclipse.ui.IPartListener;
018: import org.eclipse.ui.IViewPart;
019: import org.eclipse.ui.IWorkbenchPage;
020: import org.eclipse.ui.IWorkbenchPart;
021:
022: /**
023: * Provides debug view selection management/notification for
024: * a debug view in a specific workbench page. This selection
025: * provider sheilds clients from a debug view openning and closing,
026: * and still provides selection notification/information even
027: * when the debug view is not the active part.
028: */
029: public class PagePartSelectionTracker extends
030: AbstractPartSelectionTracker implements IPartListener,
031: ISelectionChangedListener {
032:
033: /**
034: * The workbench page for which this is tracking selection.
035: */
036: private IWorkbenchPage fPage;
037:
038: /**
039: * The part in this tracker's page, or <code>null</code> if one is not open.
040: */
041: private IWorkbenchPart fPart;
042:
043: private ISelectionChangedListener selectionListener = new ISelectionChangedListener() {
044: public void selectionChanged(SelectionChangedEvent event) {
045: fireSelection(getPart(), event.getSelection());
046: }
047: };
048:
049: private ISelectionChangedListener postSelectionListener = new ISelectionChangedListener() {
050: public void selectionChanged(SelectionChangedEvent event) {
051: firePostSelection(getPart(), event.getSelection());
052: }
053: };
054:
055: public PagePartSelectionTracker(IWorkbenchPage page, String partId) {
056: super (partId);
057: setPage(page);
058: page.addPartListener(this );
059: IViewPart part = page.findView(partId);
060: if (part != null) {
061: setPart(part, false);
062: }
063: }
064:
065: /**
066: * Disposes this selection provider - removes all listeners
067: * currently registered.
068: */
069: public void dispose() {
070: setPart(null, false);
071: setPage(null);
072: super .dispose();
073: }
074:
075: /*
076: * @see IPartListener#partActivated(IWorkbenchPart)
077: */
078: public void partActivated(IWorkbenchPart part) {
079: }
080:
081: /*
082: * @see IPartListener#partBroughtToTop(IWorkbenchPart)
083: */
084: public void partBroughtToTop(IWorkbenchPart part) {
085: }
086:
087: /**
088: * @see IPartListener#partClosed(IWorkbenchPart)
089: */
090: public void partClosed(IWorkbenchPart part) {
091: if (getPartId(part).equals(getPartId())) {
092: setPart(null, true);
093: }
094: }
095:
096: /*
097: * @see IPartListener#partDeactivated(IWorkbenchPart)
098: */
099: 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 getPartId(IWorkbenchPart part) {
118: String id = part.getSite().getId();
119: if (part instanceof IViewPart) {
120: String 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()
174: .getSelectionProvider();
175: if (sp != null) {
176: return sp.getSelection();
177: }
178: }
179: return null;
180: }
181:
182: /**
183: * @see AbstractDebugSelectionProvider#getSelectionProvider()
184: */
185: protected ISelectionProvider getSelectionProvider() {
186: IWorkbenchPart part = getPart();
187: if (part != null) {
188: return part.getSite().getSelectionProvider();
189: }
190: return null;
191: }
192:
193: /**
194: * Sets the part for this selection tracker.
195: *
196: * @param part the part
197: * @param notify whether to send notification that the selection has changed.
198: */
199: private void setPart(IWorkbenchPart part, boolean notify) {
200: if (fPart != null) {
201: // remove myself as a listener from the existing part
202: ISelectionProvider sp = fPart.getSite()
203: .getSelectionProvider();
204: if (sp != null) {
205: sp.removeSelectionChangedListener(selectionListener);
206: if (sp instanceof IPostSelectionProvider) {
207: ((IPostSelectionProvider) sp)
208: .removePostSelectionChangedListener(postSelectionListener);
209: } else {
210: sp
211: .removeSelectionChangedListener(postSelectionListener);
212: }
213: }
214: }
215: fPart = part;
216: ISelection sel = null;
217: if (part != null) {
218: ISelectionProvider sp = part.getSite()
219: .getSelectionProvider();
220: if (sp != null) {
221: sp.addSelectionChangedListener(selectionListener);
222: if (sp instanceof IPostSelectionProvider) {
223: ((IPostSelectionProvider) sp)
224: .addPostSelectionChangedListener(postSelectionListener);
225: } else {
226: sp
227: .addSelectionChangedListener(postSelectionListener);
228: }
229: if (notify) {
230: // get the selection to send below
231: sel = sp.getSelection();
232: }
233: }
234: }
235: if (notify) {
236: fireSelection(part, sel);
237: firePostSelection(part, sel);
238: }
239: }
240: }
|