1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
14 /*******************************************************************************
15 * Copyright (c) 2000, 2007 IBM Corporation and others.
16 * All rights reserved. This program and the accompanying materials
17 * are made available under the terms of the Eclipse Public License v1.0
18 * which accompanies this distribution, and is available at
19 * http://www.eclipse.org/legal/epl-v10.html
22 * IBM Corporation - initial API and implementation
23 *******************************************************************************/
25 import org.eclipse.core.runtime.Assert;
26 import org.eclipse.core.runtime.ListenerList;
27 import org.eclipse.core.runtime.SafeRunner;
28 import org.eclipse.jface.util.SafeRunnable;
29 import org.eclipse.jface.viewers.IPostSelectionProvider;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.ISelectionChangedListener;
32 import org.eclipse.jface.viewers.ISelectionProvider;
33 import org.eclipse.jface.viewers.SelectionChangedEvent;
34 import org.eclipse.jface.viewers.StructuredSelection;
35 import org.eclipse.ui.IEditorPart;
38 * Manages the current selection in a multi-page editor by tracking the active
39 * nested editor within the multi-page editor. When the selection changes,
40 * notifications are sent to all registered listeners.
42 * This class may be instantiated; it is not intended to be subclassed.
43 * The base implementation of <code>MultiPageEditor.init</code> creates
44 * an instance of this class.
47 public class TabbedPageSelectionProvider implements IPostSelectionProvider {
50 * Registered selection changed listeners (element type:
51 * <code>ISelectionChangedListener</code>).
53 private final ListenerList listeners = new ListenerList();
56 * Registered post selection changed listeners.
58 private final ListenerList postListeners = new ListenerList();
61 * The multi-page editor.
63 private final TabbedPropertyPage multiPageEditor;
66 * Creates a selection provider for the given multi-page editor.
68 * @param multiPageEditor the multi-page editor
70 public TabbedPageSelectionProvider(TabbedPropertyPage multiPageEditor) {
71 Assert.isNotNull(multiPageEditor);
72 this.multiPageEditor = multiPageEditor;
76 * Method declared on <code>ISelectionProvider</code>.
78 public void addSelectionChangedListener(ISelectionChangedListener listener) {
79 listeners.add(listener);
83 * Adds a listener for post selection changes in this multi page selection provider.
85 * @param listener a selection changed listener
88 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
89 postListeners.add(listener);
93 * Notifies all registered selection changed listeners that the editor's
94 * selection has changed. Only listeners registered at the time this method is
95 * called are notified.
97 * @param event the selection changed event
99 public void fireSelectionChanged(final SelectionChangedEvent event) {
100 Object[] listeners = this.listeners.getListeners();
101 fireEventChange(event, listeners);
105 * Notifies all post selection changed listeners that the editor's
106 * selection has changed.
108 * @param event the event to propogate.
111 public void firePostSelectionChanged(final SelectionChangedEvent event) {
112 Object[] listeners = postListeners.getListeners();
113 fireEventChange(event, listeners);
116 private void fireEventChange(final SelectionChangedEvent event, Object[] listeners) {
117 for (int i = 0; i < listeners.length; ++i) {
118 final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i];
119 SafeRunner.run(new SafeRunnable() {
121 l.selectionChanged(event);
128 * Returns the multi-page editor.
129 * @return the multi-page editor.
131 public TabbedPropertyPage getMultiPageEditor() {
132 return multiPageEditor;
136 * Method declared on <code>ISelectionProvider</code>.
138 public ISelection getSelection() {
139 IEditorPart activeEditor = multiPageEditor.getActiveEditor();
140 if (activeEditor != null) {
141 ISelectionProvider selectionProvider = activeEditor.getSite()
142 .getSelectionProvider();
143 if (selectionProvider != null) {
144 return selectionProvider.getSelection();
147 return StructuredSelection.EMPTY;
151 * Method declaed on <code>ISelectionProvider</code>.
153 public void removeSelectionChangedListener(
154 ISelectionChangedListener listener) {
155 listeners.remove(listener);
159 * Removes a listener for post selection changes in this multi page selection provider.
161 * @param listener a selection changed listener
164 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
165 postListeners.remove(listener);
169 * Method declared on <code>ISelectionProvider</code>.
171 public void setSelection(ISelection selection) {
172 IEditorPart activeEditor = multiPageEditor.getActiveEditor();
173 if (activeEditor != null) {
174 ISelectionProvider selectionProvider = activeEditor.getSite()
175 .getSelectionProvider();
176 if (selectionProvider != null) {
177 selectionProvider.setSelection(selection);