1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 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 *******************************************************************************/
15 package org.simantics.utils.ui.jface;
17 import org.eclipse.core.runtime.ListenerList;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.ISelectionProvider;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.simantics.utils.ObjectUtils;
26 * BaseSelectionProvider is a base implementation ISelectionProvider -interface.
28 * Usage: 1) instantiate 2) call {@link #setAndFireSelection(ISelection)} to
29 * send a selection event
32 * Contains an empty StructuredSelection by default.
33 * {@link #setSelection(ISelection)} by default will not notify
34 * {@link ISelectionChangedListener}s of changes in the selection.
37 * @author Toni Kalajainen
39 * @see ActiveSelectionProvider
41 public class BaseSelectionProvider implements ISelectionProvider {
43 protected ListenerList<ISelectionChangedListener> selectionListeners = new ListenerList<>();
45 protected ISelection selection = StructuredSelection.EMPTY;
48 * Create new BaseSelectionProvider. Selection change notifications will run
49 * synchronously in the thread that invoked
50 * {@link ISelectionProvider#setSelection(ISelection)}.
52 public BaseSelectionProvider() {
55 public void addSelectionChangedListener(ISelectionChangedListener listener) {
56 selectionListeners.add(listener);
59 public ISelection getSelection() {
63 public void clearListeners() {
64 clearSelectionChangedListeners();
67 public void clearSelectionChangedListeners() {
68 selectionListeners.clear();
71 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
72 selectionListeners.remove(listener);
75 public void setSelection(ISelection selection) {
76 setSelectionWithoutFiring(selection);
79 protected Object[] getListeners() {
80 return selectionListeners.getListeners();
84 * Notify other UIs that selection has changed
85 * @param selection new selection
87 public void fireSelection(ISelection selection) {
88 if (selection == null)
90 SelectionChangedEvent e = new SelectionChangedEvent(BaseSelectionProvider.this, selection);
91 for (Object l : getListeners())
92 ((ISelectionChangedListener) l).selectionChanged(e);
95 public void setSelectionWithoutFiring(ISelection selection) {
96 this.selection = selection;
101 * Sets a new selection and always fires a SelectionChangedEvent about it.
103 * @param selection the new selection
105 public void setAndFireSelection(ISelection selection) {
106 setSelection(selection);
107 fireSelection(selection);
111 * Sets the new selection for this provider and fires all selection change
112 * listeners if the specified selection differs from the current selection.
113 * If the selection is either the same object or considered equal to the
114 * current selection, the listeners are not fired.
116 * @param selection the new selection
118 public boolean setAndFireNonEqualSelection(ISelection selection) {
119 ISelection old = getSelection();
120 if (ObjectUtils.objectEquals(old, selection))
123 this.selection = selection;
124 if (selection != null)
125 fireSelection(selection);
129 public boolean selectionEquals(ISelection s) {
133 // Old selection had to be non-null
135 return s.equals(selection);