X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils.ui%2Fsrc%2Forg%2Fsimantics%2Futils%2Fui%2Fjface%2FBaseSelectionProvider.java;fp=bundles%2Forg.simantics.utils.ui%2Fsrc%2Forg%2Fsimantics%2Futils%2Fui%2Fjface%2FBaseSelectionProvider.java;h=b2e6d921548618c790aa910bb3ee43713c2bda37;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=3d3a493385afd63db61d7ec87222cc24e005368a;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/BaseSelectionProvider.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/BaseSelectionProvider.java index 3d3a49338..b2e6d9215 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/BaseSelectionProvider.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/BaseSelectionProvider.java @@ -1,128 +1,128 @@ -/******************************************************************************* - * 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 - *******************************************************************************/ -/* - * 23.8.2006 - */ -package org.simantics.utils.ui.jface; - -import org.eclipse.core.runtime.ListenerList; -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; - -/** - * BaseSelectionProvider is a base implementation ISelectionProvider -interface. - *

- * Usage: 1) instantiate 2) call {@link #setAndFireSelection(ISelection)} to - * send a selection event - * - *

- * Contains an empty StructuredSelection by default. - * {@link #setSelection(ISelection)} by default will not notify - * {@link ISelectionChangedListener}s of changes in the selection. - *

- * - * @author Toni Kalajainen - * - * @see ActiveSelectionProvider - */ -public class BaseSelectionProvider implements ISelectionProvider { - - protected ListenerList selectionListeners = new ListenerList(); - - protected ISelection selection = StructuredSelection.EMPTY; - - /** - * Create new BaseSelectionProvider. Selection change notifications will run - * synchronously in the thread that invoked - * {@link ISelectionProvider#setSelection(ISelection)}. - */ - public BaseSelectionProvider() { - } - - public void addSelectionChangedListener(ISelectionChangedListener listener) { - selectionListeners.add(listener); - } - - public ISelection getSelection() { - return selection; - } - - public void removeSelectionChangedListener(ISelectionChangedListener listener) { - selectionListeners.remove(listener); - } - - public void setSelection(ISelection selection) { - setSelectionWithoutFiring(selection); - } - - protected Object[] getListeners() { - return selectionListeners.getListeners(); - } - - /** - * Notify other UIs that selection has changed - * @param selection new selection - */ - public void fireSelection(ISelection selection) { - if (selection == null) - return; - SelectionChangedEvent e = new SelectionChangedEvent(BaseSelectionProvider.this, selection); - for (Object l : getListeners()) - ((ISelectionChangedListener) l).selectionChanged(e); - } - - public void setSelectionWithoutFiring(ISelection selection) { - this.selection = selection; - } - - - /** - * Sets a new selection and always fires a SelectionChangedEvent about it. - * - * @param selection the new selection - */ - public void setAndFireSelection(ISelection selection) { - setSelection(selection); - fireSelection(selection); - } - - /** - * Sets the new selection for this provider and fires all selection change - * listeners if the specified selection differs from the current selection. - * If the selection is either the same object or considered equal to the - * current selection, the listeners are not fired. - * - * @param selection the new selection - */ - public void setAndFireNonEqualSelection(ISelection selection) { - ISelection old = getSelection(); - if (selection == old) - return; - - this.selection = selection; - if (selection != null && !selection.equals(old)) - fireSelection(selection); - } - - public boolean selectionEquals(ISelection s) { - if (s == selection) - return true; - if (s == null) - // Old selection had to be non-null - return true; - return s.equals(selection); - } - -} +/******************************************************************************* + * 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 + *******************************************************************************/ +/* + * 23.8.2006 + */ +package org.simantics.utils.ui.jface; + +import org.eclipse.core.runtime.ListenerList; +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; + +/** + * BaseSelectionProvider is a base implementation ISelectionProvider -interface. + *

+ * Usage: 1) instantiate 2) call {@link #setAndFireSelection(ISelection)} to + * send a selection event + * + *

+ * Contains an empty StructuredSelection by default. + * {@link #setSelection(ISelection)} by default will not notify + * {@link ISelectionChangedListener}s of changes in the selection. + *

+ * + * @author Toni Kalajainen + * + * @see ActiveSelectionProvider + */ +public class BaseSelectionProvider implements ISelectionProvider { + + protected ListenerList selectionListeners = new ListenerList(); + + protected ISelection selection = StructuredSelection.EMPTY; + + /** + * Create new BaseSelectionProvider. Selection change notifications will run + * synchronously in the thread that invoked + * {@link ISelectionProvider#setSelection(ISelection)}. + */ + public BaseSelectionProvider() { + } + + public void addSelectionChangedListener(ISelectionChangedListener listener) { + selectionListeners.add(listener); + } + + public ISelection getSelection() { + return selection; + } + + public void removeSelectionChangedListener(ISelectionChangedListener listener) { + selectionListeners.remove(listener); + } + + public void setSelection(ISelection selection) { + setSelectionWithoutFiring(selection); + } + + protected Object[] getListeners() { + return selectionListeners.getListeners(); + } + + /** + * Notify other UIs that selection has changed + * @param selection new selection + */ + public void fireSelection(ISelection selection) { + if (selection == null) + return; + SelectionChangedEvent e = new SelectionChangedEvent(BaseSelectionProvider.this, selection); + for (Object l : getListeners()) + ((ISelectionChangedListener) l).selectionChanged(e); + } + + public void setSelectionWithoutFiring(ISelection selection) { + this.selection = selection; + } + + + /** + * Sets a new selection and always fires a SelectionChangedEvent about it. + * + * @param selection the new selection + */ + public void setAndFireSelection(ISelection selection) { + setSelection(selection); + fireSelection(selection); + } + + /** + * Sets the new selection for this provider and fires all selection change + * listeners if the specified selection differs from the current selection. + * If the selection is either the same object or considered equal to the + * current selection, the listeners are not fired. + * + * @param selection the new selection + */ + public void setAndFireNonEqualSelection(ISelection selection) { + ISelection old = getSelection(); + if (selection == old) + return; + + this.selection = selection; + if (selection != null && !selection.equals(old)) + fireSelection(selection); + } + + public boolean selectionEquals(ISelection s) { + if (s == selection) + return true; + if (s == null) + // Old selection had to be non-null + return true; + return s.equals(selection); + } + +}