/******************************************************************************* * Copyright (c) 2007, 2018 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.core.runtime.SafeRunner; import org.eclipse.jface.util.SafeRunnable; import org.eclipse.jface.viewers.IPostSelectionProvider; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.SelectionChangedEvent; /** * 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. *

* * @author Toni Kalajainen * @author Tuukka Lehtonen */ public class BasePostSelectionProvider extends BaseSelectionProvider implements IPostSelectionProvider { protected ListenerList postSelectionListeners = new ListenerList<>(); public void clearListeners() { super.clearListeners(); clearPostSelectionChangedListeners(); } public void clearPostSelectionChangedListeners() { postSelectionListeners.clear(); } @Override public void addPostSelectionChangedListener(ISelectionChangedListener listener) { postSelectionListeners.add(listener); } @Override public void removePostSelectionChangedListener(ISelectionChangedListener listener) { postSelectionListeners.remove(listener); } protected Object[] getPostListeners() { return postSelectionListeners.getListeners(); } /** * Notify other UIs that selection has changed * @param selection new selection */ public void firePostSelection(ISelection selection) { if (selection == null) return; final SelectionChangedEvent e = new SelectionChangedEvent(this, selection); for (Object o : postSelectionListeners.getListeners()) { final ISelectionChangedListener l = (ISelectionChangedListener) o; SafeRunner.run(new SafeRunnable() { @Override public void run() throws Exception { l.selectionChanged(e); } }); } } }