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 *******************************************************************************/
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;
25 * BaseSelectionProvider is a base implementation ISelectionProvider -interface.
27 * Usage: 1) instantiate 2) call {@link #setAndFireSelection(ISelection)} to
28 * send a selection event
31 * Contains an empty StructuredSelection by default.
32 * {@link #setSelection(ISelection)} by default will not notify
33 * {@link ISelectionChangedListener}s of changes in the selection.
36 * @author Toni Kalajainen
38 * @see ActiveSelectionProvider
40 public class BaseSelectionProvider implements ISelectionProvider {
42 protected ListenerList selectionListeners = new ListenerList();
44 protected ISelection selection = StructuredSelection.EMPTY;
47 * Create new BaseSelectionProvider. Selection change notifications will run
48 * synchronously in the thread that invoked
49 * {@link ISelectionProvider#setSelection(ISelection)}.
51 public BaseSelectionProvider() {
54 public void addSelectionChangedListener(ISelectionChangedListener listener) {
55 selectionListeners.add(listener);
58 public ISelection getSelection() {
62 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
63 selectionListeners.remove(listener);
66 public void setSelection(ISelection selection) {
67 setSelectionWithoutFiring(selection);
70 protected Object[] getListeners() {
71 return selectionListeners.getListeners();
75 * Notify other UIs that selection has changed
76 * @param selection new selection
78 public void fireSelection(ISelection selection) {
79 if (selection == null)
81 SelectionChangedEvent e = new SelectionChangedEvent(BaseSelectionProvider.this, selection);
82 for (Object l : getListeners())
83 ((ISelectionChangedListener) l).selectionChanged(e);
86 public void setSelectionWithoutFiring(ISelection selection) {
87 this.selection = selection;
92 * Sets a new selection and always fires a SelectionChangedEvent about it.
94 * @param selection the new selection
96 public void setAndFireSelection(ISelection selection) {
97 setSelection(selection);
98 fireSelection(selection);
102 * Sets the new selection for this provider and fires all selection change
103 * listeners if the specified selection differs from the current selection.
104 * If the selection is either the same object or considered equal to the
105 * current selection, the listeners are not fired.
107 * @param selection the new selection
109 public void setAndFireNonEqualSelection(ISelection selection) {
110 ISelection old = getSelection();
111 if (selection == old)
114 this.selection = selection;
115 if (selection != null && !selection.equals(old))
116 fireSelection(selection);
119 public boolean selectionEquals(ISelection s) {
123 // Old selection had to be non-null
125 return s.equals(selection);