]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TabbedPageSelectionProvider.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / TabbedPageSelectionProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
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
20  *
21  * Contributors:
22  *     IBM Corporation - initial API and implementation
23  *******************************************************************************/
24
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;
36
37 /**
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.
41  * <p>
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.
45  * </p>
46  */
47 public class TabbedPageSelectionProvider implements IPostSelectionProvider {
48
49     /**
50      * Registered selection changed listeners (element type:
51      * <code>ISelectionChangedListener</code>).
52      */
53     private final ListenerList listeners = new ListenerList();
54
55     /**
56      * Registered post selection changed listeners.
57      */
58     private final ListenerList postListeners = new ListenerList();
59
60     /**
61      * The multi-page editor.
62      */
63     private final TabbedPropertyPage multiPageEditor;
64
65     /**
66      * Creates a selection provider for the given multi-page editor.
67      *
68      * @param multiPageEditor the multi-page editor
69      */
70     public TabbedPageSelectionProvider(TabbedPropertyPage multiPageEditor) {
71         Assert.isNotNull(multiPageEditor);
72         this.multiPageEditor = multiPageEditor;
73     }
74
75     /* (non-Javadoc)
76      * Method declared on <code>ISelectionProvider</code>.
77      */
78     public void addSelectionChangedListener(ISelectionChangedListener listener) {
79         listeners.add(listener);
80     }
81
82     /**
83      * Adds a listener for post selection changes in this multi page selection provider.
84      *
85      * @param listener a selection changed listener
86      * @since 3.2
87      */
88     public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
89         postListeners.add(listener);
90     }
91
92     /**
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.
96      *
97      * @param event the selection changed event
98      */
99     public void fireSelectionChanged(final SelectionChangedEvent event) {
100         Object[] listeners = this.listeners.getListeners();
101         fireEventChange(event, listeners);
102     }
103
104     /**
105      * Notifies all post selection changed listeners that the editor's
106      * selection has changed.
107      * 
108      * @param event the event to propogate.
109      * @since 3.2
110      */
111     public void firePostSelectionChanged(final SelectionChangedEvent event) {
112         Object[] listeners = postListeners.getListeners();
113         fireEventChange(event, listeners);
114     }
115
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() {
120                 public void run() {
121                     l.selectionChanged(event);
122                 }
123             });
124         }
125     }
126
127     /**
128      * Returns the multi-page editor.
129      * @return the multi-page editor.
130      */
131     public TabbedPropertyPage getMultiPageEditor() {
132         return multiPageEditor;
133     }
134
135     /* (non-Javadoc)
136      * Method declared on <code>ISelectionProvider</code>.
137      */
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();
145             }
146         }
147         return StructuredSelection.EMPTY;
148     }
149
150     /* (non-JavaDoc)
151      * Method declaed on <code>ISelectionProvider</code>.
152      */
153     public void removeSelectionChangedListener(
154             ISelectionChangedListener listener) {
155         listeners.remove(listener);
156     }
157
158     /**
159      * Removes a listener for post selection changes in this multi page selection provider.
160      *
161      * @param listener a selection changed listener
162      * @since 3.2
163      */
164     public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
165         postListeners.remove(listener);
166     }
167
168     /* (non-Javadoc)
169      * Method declared on <code>ISelectionProvider</code>.
170      */
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);
178             }
179         }
180     }
181 }