X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FTabbedPageSelectionProvider.java;fp=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FTabbedPageSelectionProvider.java;h=f5da3126bdbd4adb8e7147367635423b1a2f273d;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TabbedPageSelectionProvider.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TabbedPageSelectionProvider.java new file mode 100644 index 000000000..f5da3126b --- /dev/null +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TabbedPageSelectionProvider.java @@ -0,0 +1,181 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.browsing.ui.swt; + +/******************************************************************************* + * Copyright (c) 2000, 2007 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.ListenerList; +import org.eclipse.core.runtime.SafeRunner; +import org.eclipse.jface.util.SafeRunnable; +import org.eclipse.jface.viewers.IPostSelectionProvider; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionChangedListener; +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.ui.IEditorPart; + +/** + * Manages the current selection in a multi-page editor by tracking the active + * nested editor within the multi-page editor. When the selection changes, + * notifications are sent to all registered listeners. + *

+ * This class may be instantiated; it is not intended to be subclassed. + * The base implementation of MultiPageEditor.init creates + * an instance of this class. + *

+ */ +public class TabbedPageSelectionProvider implements IPostSelectionProvider { + + /** + * Registered selection changed listeners (element type: + * ISelectionChangedListener). + */ + private final ListenerList listeners = new ListenerList(); + + /** + * Registered post selection changed listeners. + */ + private final ListenerList postListeners = new ListenerList(); + + /** + * The multi-page editor. + */ + private final TabbedPropertyPage multiPageEditor; + + /** + * Creates a selection provider for the given multi-page editor. + * + * @param multiPageEditor the multi-page editor + */ + public TabbedPageSelectionProvider(TabbedPropertyPage multiPageEditor) { + Assert.isNotNull(multiPageEditor); + this.multiPageEditor = multiPageEditor; + } + + /* (non-Javadoc) + * Method declared on ISelectionProvider. + */ + public void addSelectionChangedListener(ISelectionChangedListener listener) { + listeners.add(listener); + } + + /** + * Adds a listener for post selection changes in this multi page selection provider. + * + * @param listener a selection changed listener + * @since 3.2 + */ + public void addPostSelectionChangedListener(ISelectionChangedListener listener) { + postListeners.add(listener); + } + + /** + * Notifies all registered selection changed listeners that the editor's + * selection has changed. Only listeners registered at the time this method is + * called are notified. + * + * @param event the selection changed event + */ + public void fireSelectionChanged(final SelectionChangedEvent event) { + Object[] listeners = this.listeners.getListeners(); + fireEventChange(event, listeners); + } + + /** + * Notifies all post selection changed listeners that the editor's + * selection has changed. + * + * @param event the event to propogate. + * @since 3.2 + */ + public void firePostSelectionChanged(final SelectionChangedEvent event) { + Object[] listeners = postListeners.getListeners(); + fireEventChange(event, listeners); + } + + private void fireEventChange(final SelectionChangedEvent event, Object[] listeners) { + for (int i = 0; i < listeners.length; ++i) { + final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i]; + SafeRunner.run(new SafeRunnable() { + public void run() { + l.selectionChanged(event); + } + }); + } + } + + /** + * Returns the multi-page editor. + * @return the multi-page editor. + */ + public TabbedPropertyPage getMultiPageEditor() { + return multiPageEditor; + } + + /* (non-Javadoc) + * Method declared on ISelectionProvider. + */ + public ISelection getSelection() { + IEditorPart activeEditor = multiPageEditor.getActiveEditor(); + if (activeEditor != null) { + ISelectionProvider selectionProvider = activeEditor.getSite() + .getSelectionProvider(); + if (selectionProvider != null) { + return selectionProvider.getSelection(); + } + } + return StructuredSelection.EMPTY; + } + + /* (non-JavaDoc) + * Method declaed on ISelectionProvider. + */ + public void removeSelectionChangedListener( + ISelectionChangedListener listener) { + listeners.remove(listener); + } + + /** + * Removes a listener for post selection changes in this multi page selection provider. + * + * @param listener a selection changed listener + * @since 3.2 + */ + public void removePostSelectionChangedListener(ISelectionChangedListener listener) { + postListeners.remove(listener); + } + + /* (non-Javadoc) + * Method declared on ISelectionProvider. + */ + public void setSelection(ISelection selection) { + IEditorPart activeEditor = multiPageEditor.getActiveEditor(); + if (activeEditor != null) { + ISelectionProvider selectionProvider = activeEditor.getSite() + .getSelectionProvider(); + if (selectionProvider != null) { + selectionProvider.setSelection(selection); + } + } + } +}