1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
15 package org.simantics.utils.ui.jface;
\r
17 import org.eclipse.core.runtime.ListenerList;
\r
18 import org.eclipse.jface.viewers.ISelection;
\r
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
\r
20 import org.eclipse.jface.viewers.ISelectionProvider;
\r
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
\r
22 import org.eclipse.jface.viewers.StructuredSelection;
\r
25 * BaseSelectionProvider is a base implementation ISelectionProvider -interface.
\r
27 * Usage: 1) instantiate 2) call {@link #setAndFireSelection(ISelection)} to
\r
28 * send a selection event
\r
31 * Contains an empty StructuredSelection by default.
\r
32 * {@link #setSelection(ISelection)} by default will not notify
\r
33 * {@link ISelectionChangedListener}s of changes in the selection.
\r
36 * @author Toni Kalajainen
\r
38 * @see ActiveSelectionProvider
\r
40 public class BaseSelectionProvider implements ISelectionProvider {
\r
42 protected ListenerList selectionListeners = new ListenerList();
\r
44 protected ISelection selection = StructuredSelection.EMPTY;
\r
47 * Create new BaseSelectionProvider. Selection change notifications will run
\r
48 * synchronously in the thread that invoked
\r
49 * {@link ISelectionProvider#setSelection(ISelection)}.
\r
51 public BaseSelectionProvider() {
\r
54 public void addSelectionChangedListener(ISelectionChangedListener listener) {
\r
55 selectionListeners.add(listener);
\r
58 public ISelection getSelection() {
\r
62 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
\r
63 selectionListeners.remove(listener);
\r
66 public void setSelection(ISelection selection) {
\r
67 setSelectionWithoutFiring(selection);
\r
70 protected Object[] getListeners() {
\r
71 return selectionListeners.getListeners();
\r
75 * Notify other UIs that selection has changed
\r
76 * @param selection new selection
\r
78 public void fireSelection(ISelection selection) {
\r
79 if (selection == null)
\r
81 SelectionChangedEvent e = new SelectionChangedEvent(BaseSelectionProvider.this, selection);
\r
82 for (Object l : getListeners())
\r
83 ((ISelectionChangedListener) l).selectionChanged(e);
\r
86 public void setSelectionWithoutFiring(ISelection selection) {
\r
87 this.selection = selection;
\r
92 * Sets a new selection and always fires a SelectionChangedEvent about it.
\r
94 * @param selection the new selection
\r
96 public void setAndFireSelection(ISelection selection) {
\r
97 setSelection(selection);
\r
98 fireSelection(selection);
\r
102 * Sets the new selection for this provider and fires all selection change
\r
103 * listeners if the specified selection differs from the current selection.
\r
104 * If the selection is either the same object or considered equal to the
\r
105 * current selection, the listeners are not fired.
\r
107 * @param selection the new selection
\r
109 public void setAndFireNonEqualSelection(ISelection selection) {
\r
110 ISelection old = getSelection();
\r
111 if (selection == old)
\r
114 this.selection = selection;
\r
115 if (selection != null && !selection.equals(old))
\r
116 fireSelection(selection);
\r
119 public boolean selectionEquals(ISelection s) {
\r
120 if (s == selection)
\r
123 // Old selection had to be non-null
\r
125 return s.equals(selection);
\r