--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ * 23.8.2006\r
+ */\r
+package org.simantics.utils.ui.jface;\r
+\r
+import org.eclipse.core.runtime.ListenerList;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.ISelectionChangedListener;\r
+import org.eclipse.jface.viewers.ISelectionProvider;\r
+import org.eclipse.jface.viewers.SelectionChangedEvent;\r
+import org.eclipse.jface.viewers.StructuredSelection;\r
+\r
+/**\r
+ * BaseSelectionProvider is a base implementation ISelectionProvider -interface.\r
+ * <p>\r
+ * Usage: 1) instantiate 2) call {@link #setAndFireSelection(ISelection)} to\r
+ * send a selection event\r
+ * \r
+ * <p>\r
+ * Contains an empty StructuredSelection by default.\r
+ * {@link #setSelection(ISelection)} by default will not notify\r
+ * {@link ISelectionChangedListener}s of changes in the selection.\r
+ * </p>\r
+ * \r
+ * @author Toni Kalajainen\r
+ * \r
+ * @see ActiveSelectionProvider\r
+ */\r
+public class BaseSelectionProvider implements ISelectionProvider {\r
+\r
+ protected ListenerList selectionListeners = new ListenerList();\r
+\r
+ protected ISelection selection = StructuredSelection.EMPTY;\r
+\r
+ /**\r
+ * Create new BaseSelectionProvider. Selection change notifications will run\r
+ * synchronously in the thread that invoked\r
+ * {@link ISelectionProvider#setSelection(ISelection)}.\r
+ */\r
+ public BaseSelectionProvider() {\r
+ }\r
+\r
+ public void addSelectionChangedListener(ISelectionChangedListener listener) {\r
+ selectionListeners.add(listener);\r
+ }\r
+\r
+ public ISelection getSelection() {\r
+ return selection;\r
+ }\r
+\r
+ public void removeSelectionChangedListener(ISelectionChangedListener listener) {\r
+ selectionListeners.remove(listener);\r
+ }\r
+\r
+ public void setSelection(ISelection selection) {\r
+ setSelectionWithoutFiring(selection);\r
+ }\r
+\r
+ protected Object[] getListeners() {\r
+ return selectionListeners.getListeners();\r
+ }\r
+\r
+ /**\r
+ * Notify other UIs that selection has changed\r
+ * @param selection new selection\r
+ */\r
+ public void fireSelection(ISelection selection) {\r
+ if (selection == null)\r
+ return;\r
+ SelectionChangedEvent e = new SelectionChangedEvent(BaseSelectionProvider.this, selection);\r
+ for (Object l : getListeners())\r
+ ((ISelectionChangedListener) l).selectionChanged(e);\r
+ }\r
+\r
+ public void setSelectionWithoutFiring(ISelection selection) {\r
+ this.selection = selection;\r
+ }\r
+\r
+\r
+ /**\r
+ * Sets a new selection and always fires a SelectionChangedEvent about it.\r
+ * \r
+ * @param selection the new selection\r
+ */\r
+ public void setAndFireSelection(ISelection selection) {\r
+ setSelection(selection);\r
+ fireSelection(selection);\r
+ }\r
+\r
+ /**\r
+ * Sets the new selection for this provider and fires all selection change\r
+ * listeners if the specified selection differs from the current selection.\r
+ * If the selection is either the same object or considered equal to the\r
+ * current selection, the listeners are not fired.\r
+ * \r
+ * @param selection the new selection\r
+ */\r
+ public void setAndFireNonEqualSelection(ISelection selection) {\r
+ ISelection old = getSelection();\r
+ if (selection == old)\r
+ return;\r
+\r
+ this.selection = selection;\r
+ if (selection != null && !selection.equals(old))\r
+ fireSelection(selection);\r
+ }\r
+\r
+ public boolean selectionEquals(ISelection s) {\r
+ if (s == selection)\r
+ return true;\r
+ if (s == null)\r
+ // Old selection had to be non-null\r
+ return true;\r
+ return s.equals(selection);\r
+ }\r
+\r
+}\r