]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/BasePostSelectionProvider.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / jface / BasePostSelectionProvider.java
index 2ed41c2af5235dcff59a08b7aba143962f518090..c5ca0e754cada161035bbcee68289b4dc135062a 100644 (file)
-/*******************************************************************************\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.core.runtime.SafeRunner;\r
-import org.eclipse.jface.util.SafeRunnable;\r
-import org.eclipse.jface.viewers.IPostSelectionProvider;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.jface.viewers.ISelectionChangedListener;\r
-import org.eclipse.jface.viewers.SelectionChangedEvent;\r
-import org.eclipse.jface.viewers.StructuredSelection;\r
-import org.simantics.utils.ObjectUtils;\r
-\r
-/**\r
- * BaseSelectionProvider is a base implementation ISelectionProvider -interface.\r
- * <p>\r
- * Usage:\r
- *  1) instantiate\r
- *  2) call {@link #setAndFireSelection(ISelection)} to send a selection event\r
- * \r
- * <p>\r
- * Contains an empty StructuredSelection by default.\r
- * </p>\r
- * \r
- * @author Toni Kalajainen\r
- */\r
-public class BasePostSelectionProvider implements IPostSelectionProvider {\r
-\r
-    protected ListenerList selectionListeners = new ListenerList();\r
-\r
-    protected ListenerList postSelectionListeners = new ListenerList();\r
-\r
-    protected ISelection selection = StructuredSelection.EMPTY;\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 clearListeners() {\r
-        selectionListeners.clear();\r
-        postSelectionListeners.clear();\r
-    }\r
-\r
-    public void clearSelectionChangedListeners() {\r
-        postSelectionListeners.clear();\r
-    }\r
-\r
-    public void clearPostSelectionChangedListeners() {\r
-        postSelectionListeners.clear();\r
-    }\r
-\r
-    public void removeSelectionChangedListener(ISelectionChangedListener listener) {\r
-        selectionListeners.remove(listener);\r
-    }\r
-\r
-    @Override\r
-    public void addPostSelectionChangedListener(ISelectionChangedListener listener) {\r
-        postSelectionListeners.add(listener);\r
-    }\r
-\r
-    @Override\r
-    public void removePostSelectionChangedListener(ISelectionChangedListener listener) {\r
-        postSelectionListeners.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
-    protected Object[] getPostListeners() {\r
-        return postSelectionListeners.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(this, selection);\r
-        for (Object l : getListeners())\r
-            ((ISelectionChangedListener) l).selectionChanged(e);\r
-    }\r
-\r
-    /**\r
-     * Notify other UIs that selection has changed\r
-     * @param selection new selection\r
-     */\r
-    public void firePostSelection(ISelection selection) {\r
-        if (selection == null)\r
-            return;\r
-        final SelectionChangedEvent e = new SelectionChangedEvent(this, selection);\r
-        for (Object o : postSelectionListeners.getListeners()) {\r
-            final ISelectionChangedListener l = (ISelectionChangedListener) o;\r
-            SafeRunner.run(new SafeRunnable() {\r
-                @Override\r
-                public void run() throws Exception {\r
-                    l.selectionChanged(e);\r
-                }\r
-            });\r
-        }\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 (ObjectUtils.objectEquals(old, selection))\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 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;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.simantics.utils.ObjectUtils;
+
+/**
+ * BaseSelectionProvider is a base implementation ISelectionProvider -interface.
+ * <p>
+ * Usage:
+ *  1) instantiate
+ *  2) call {@link #setAndFireSelection(ISelection)} to send a selection event
+ * 
+ * <p>
+ * Contains an empty StructuredSelection by default.
+ * </p>
+ * 
+ * @author Toni Kalajainen
+ */
+public class BasePostSelectionProvider implements IPostSelectionProvider {
+
+    protected ListenerList selectionListeners = new ListenerList();
+
+    protected ListenerList postSelectionListeners = new ListenerList();
+
+    protected ISelection selection = StructuredSelection.EMPTY;
+
+    public void addSelectionChangedListener(ISelectionChangedListener listener) {
+        selectionListeners.add(listener);
+    }
+
+    public ISelection getSelection() {
+        return selection;
+    }
+
+    public void clearListeners() {
+        selectionListeners.clear();
+        postSelectionListeners.clear();
+    }
+
+    public void clearSelectionChangedListeners() {
+        postSelectionListeners.clear();
+    }
+
+    public void clearPostSelectionChangedListeners() {
+        postSelectionListeners.clear();
+    }
+
+    public void removeSelectionChangedListener(ISelectionChangedListener listener) {
+        selectionListeners.remove(listener);
+    }
+
+    @Override
+    public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
+        postSelectionListeners.add(listener);
+    }
+
+    @Override
+    public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
+        postSelectionListeners.remove(listener);
+    }
+
+    public void setSelection(ISelection selection) {
+        setSelectionWithoutFiring(selection);
+    }
+
+    protected Object[] getListeners() {
+        return selectionListeners.getListeners();
+    }
+
+    protected Object[] getPostListeners() {
+        return postSelectionListeners.getListeners();
+    }
+
+    /**
+     * Notify other UIs that selection has changed
+     * @param selection new selection
+     */
+    public void fireSelection(ISelection selection) {
+        if (selection == null)
+            return;
+        SelectionChangedEvent e = new SelectionChangedEvent(this, selection);
+        for (Object l : getListeners())
+            ((ISelectionChangedListener) l).selectionChanged(e);
+    }
+
+    /**
+     * 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);
+                }
+            });
+        }
+    }
+
+    public void setSelectionWithoutFiring(ISelection selection) {
+        this.selection = selection;
+    }
+
+
+    /**
+     * Sets a new selection and always fires a SelectionChangedEvent about it.
+     * 
+     * @param selection the new selection
+     */
+    public void setAndFireSelection(ISelection selection) {
+        setSelection(selection);
+        fireSelection(selection);
+    }
+
+    /**
+     * Sets the new selection for this provider and fires all selection change
+     * listeners if the specified selection differs from the current selection.
+     * If the selection is either the same object or considered equal to the
+     * current selection, the listeners are not fired.
+     * 
+     * @param selection the new selection
+     */
+    public void setAndFireNonEqualSelection(ISelection selection) {
+        ISelection old = getSelection();
+        if (ObjectUtils.objectEquals(old, selection))
+            return;
+
+        this.selection = selection;
+        if (selection != null && !selection.equals(old))
+            fireSelection(selection);
+    }
+
+    public boolean selectionEquals(ISelection s) {
+        if (s == selection)
+            return true;
+        if (s == null)
+            // Old selection had to be non-null
+            return true;
+        return s.equals(selection);
+    }
+
+}