001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.part;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.ListenerList;
014: import org.eclipse.core.runtime.SafeRunner;
015: import org.eclipse.jface.util.SafeRunnable;
016: import org.eclipse.jface.viewers.IPostSelectionProvider;
017: import org.eclipse.jface.viewers.ISelection;
018: import org.eclipse.jface.viewers.ISelectionChangedListener;
019: import org.eclipse.jface.viewers.ISelectionProvider;
020: import org.eclipse.jface.viewers.SelectionChangedEvent;
021: import org.eclipse.jface.viewers.StructuredSelection;
022: import org.eclipse.ui.IEditorPart;
023:
024: /**
025: * Manages the current selection in a multi-page editor by tracking the active
026: * nested editor within the multi-page editor. When the selection changes,
027: * notifications are sent to all registered listeners.
028: * <p>
029: * This class may be instantiated; it is not intended to be subclassed.
030: * The base implementation of <code>MultiPageEditor.init</code> creates
031: * an instance of this class.
032: * </p>
033: */
034: public class MultiPageSelectionProvider implements
035: IPostSelectionProvider {
036:
037: /**
038: * Registered selection changed listeners (element type:
039: * <code>ISelectionChangedListener</code>).
040: */
041: private ListenerList listeners = new ListenerList();
042:
043: /**
044: * Registered post selection changed listeners.
045: */
046: private ListenerList postListeners = new ListenerList();
047:
048: /**
049: * The multi-page editor.
050: */
051: private MultiPageEditorPart multiPageEditor;
052:
053: /**
054: * Creates a selection provider for the given multi-page editor.
055: *
056: * @param multiPageEditor the multi-page editor
057: */
058: public MultiPageSelectionProvider(
059: MultiPageEditorPart multiPageEditor) {
060: Assert.isNotNull(multiPageEditor);
061: this .multiPageEditor = multiPageEditor;
062: }
063:
064: /* (non-Javadoc)
065: * Method declared on <code>ISelectionProvider</code>.
066: */
067: public void addSelectionChangedListener(
068: ISelectionChangedListener listener) {
069: listeners.add(listener);
070: }
071:
072: /**
073: * Adds a listener for post selection changes in this multi page selection provider.
074: *
075: * @param listener a selection changed listener
076: * @since 3.2
077: */
078: public void addPostSelectionChangedListener(
079: ISelectionChangedListener listener) {
080: postListeners.add(listener);
081: }
082:
083: /**
084: * Notifies all registered selection changed listeners that the editor's
085: * selection has changed. Only listeners registered at the time this method is
086: * called are notified.
087: *
088: * @param event the selection changed event
089: */
090: public void fireSelectionChanged(final SelectionChangedEvent event) {
091: Object[] listeners = this .listeners.getListeners();
092: fireEventChange(event, listeners);
093: }
094:
095: /**
096: * Notifies all post selection changed listeners that the editor's
097: * selection has changed.
098: *
099: * @param event the event to propogate.
100: * @since 3.2
101: */
102: public void firePostSelectionChanged(
103: final SelectionChangedEvent event) {
104: Object[] listeners = postListeners.getListeners();
105: fireEventChange(event, listeners);
106: }
107:
108: private void fireEventChange(final SelectionChangedEvent event,
109: Object[] listeners) {
110: for (int i = 0; i < listeners.length; ++i) {
111: final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i];
112: SafeRunner.run(new SafeRunnable() {
113: public void run() {
114: l.selectionChanged(event);
115: }
116: });
117: }
118: }
119:
120: /**
121: * Returns the multi-page editor.
122: * @return the multi-page editor.
123: */
124: public MultiPageEditorPart getMultiPageEditor() {
125: return multiPageEditor;
126: }
127:
128: /* (non-Javadoc)
129: * Method declared on <code>ISelectionProvider</code>.
130: */
131: public ISelection getSelection() {
132: IEditorPart activeEditor = multiPageEditor.getActiveEditor();
133: if (activeEditor != null) {
134: ISelectionProvider selectionProvider = activeEditor
135: .getSite().getSelectionProvider();
136: if (selectionProvider != null) {
137: return selectionProvider.getSelection();
138: }
139: }
140: return StructuredSelection.EMPTY;
141: }
142:
143: /* (non-JavaDoc)
144: * Method declaed on <code>ISelectionProvider</code>.
145: */
146: public void removeSelectionChangedListener(
147: ISelectionChangedListener listener) {
148: listeners.remove(listener);
149: }
150:
151: /**
152: * Removes a listener for post selection changes in this multi page selection provider.
153: *
154: * @param listener a selection changed listener
155: * @since 3.2
156: */
157: public void removePostSelectionChangedListener(
158: ISelectionChangedListener listener) {
159: postListeners.remove(listener);
160: }
161:
162: /* (non-Javadoc)
163: * Method declared on <code>ISelectionProvider</code>.
164: */
165: public void setSelection(ISelection selection) {
166: IEditorPart activeEditor = multiPageEditor.getActiveEditor();
167: if (activeEditor != null) {
168: ISelectionProvider selectionProvider = activeEditor
169: .getSite().getSelectionProvider();
170: if (selectionProvider != null) {
171: selectionProvider.setSelection(selection);
172: }
173: }
174: }
175: }
|