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.core.runtime.SafeRunner;
19 import org.eclipse.jface.util.SafeRunnable;
20 import org.eclipse.jface.viewers.IPostSelectionProvider;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionChangedListener;
23 import org.eclipse.jface.viewers.SelectionChangedEvent;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.simantics.utils.ObjectUtils;
28 * BaseSelectionProvider is a base implementation ISelectionProvider -interface.
32 * 2) call {@link #setAndFireSelection(ISelection)} to send a selection event
35 * Contains an empty StructuredSelection by default.
38 * @author Toni Kalajainen
40 public class BasePostSelectionProvider implements IPostSelectionProvider {
42 protected ListenerList selectionListeners = new ListenerList();
44 protected ListenerList postSelectionListeners = new ListenerList();
46 protected ISelection selection = StructuredSelection.EMPTY;
48 public void addSelectionChangedListener(ISelectionChangedListener listener) {
49 selectionListeners.add(listener);
52 public ISelection getSelection() {
56 public void clearListeners() {
57 selectionListeners.clear();
58 postSelectionListeners.clear();
61 public void clearSelectionChangedListeners() {
62 postSelectionListeners.clear();
65 public void clearPostSelectionChangedListeners() {
66 postSelectionListeners.clear();
69 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
70 selectionListeners.remove(listener);
74 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
75 postSelectionListeners.add(listener);
79 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
80 postSelectionListeners.remove(listener);
83 public void setSelection(ISelection selection) {
84 setSelectionWithoutFiring(selection);
87 protected Object[] getListeners() {
88 return selectionListeners.getListeners();
91 protected Object[] getPostListeners() {
92 return postSelectionListeners.getListeners();
96 * Notify other UIs that selection has changed
97 * @param selection new selection
99 public void fireSelection(ISelection selection) {
100 if (selection == null)
102 SelectionChangedEvent e = new SelectionChangedEvent(this, selection);
103 for (Object l : getListeners())
104 ((ISelectionChangedListener) l).selectionChanged(e);
108 * Notify other UIs that selection has changed
109 * @param selection new selection
111 public void firePostSelection(ISelection selection) {
112 if (selection == null)
114 final SelectionChangedEvent e = new SelectionChangedEvent(this, selection);
115 for (Object o : postSelectionListeners.getListeners()) {
116 final ISelectionChangedListener l = (ISelectionChangedListener) o;
117 SafeRunner.run(new SafeRunnable() {
119 public void run() throws Exception {
120 l.selectionChanged(e);
126 public void setSelectionWithoutFiring(ISelection selection) {
127 this.selection = selection;
132 * Sets a new selection and always fires a SelectionChangedEvent about it.
134 * @param selection the new selection
136 public void setAndFireSelection(ISelection selection) {
137 setSelection(selection);
138 fireSelection(selection);
142 * Sets the new selection for this provider and fires all selection change
143 * listeners if the specified selection differs from the current selection.
144 * If the selection is either the same object or considered equal to the
145 * current selection, the listeners are not fired.
147 * @param selection the new selection
149 public void setAndFireNonEqualSelection(ISelection selection) {
150 ISelection old = getSelection();
151 if (ObjectUtils.objectEquals(old, selection))
154 this.selection = selection;
155 if (selection != null && !selection.equals(old))
156 fireSelection(selection);
159 public boolean selectionEquals(ISelection s) {
163 // Old selection had to be non-null
165 return s.equals(selection);