]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/WorkbenchSelectionInputSource.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / WorkbenchSelectionInputSource.java
diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/WorkbenchSelectionInputSource.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/WorkbenchSelectionInputSource.java
new file mode 100644 (file)
index 0000000..f72ac24
--- /dev/null
@@ -0,0 +1,117 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 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
+package org.simantics.browsing.ui.graph.impl;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.ui.ISelectionListener;\r
+import org.eclipse.ui.ISelectionService;\r
+import org.eclipse.ui.IWorkbenchPage;\r
+import org.eclipse.ui.IWorkbenchPart;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.eclipse.ui.IWorkbenchWindow;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.db.Disposable;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.utils.ObjectUtils;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+public class WorkbenchSelectionInputSource implements WorkbenchSessionContextInputSource, ObservableInputSource,\r
+        ISelectionListener, Disposable {\r
+\r
+    protected ISelectionService   service;\r
+    protected Object              selection;\r
+    protected IWorkbenchPart      part;\r
+    protected IWorkbenchPart      ownPart;\r
+    protected InputSourceListener listener;\r
+\r
+    @Override\r
+    public void init(IWorkbenchSite site, IWorkbenchPart ownPart) {\r
+        this.ownPart = ownPart;\r
+        attachToWorkbench();\r
+    }\r
+\r
+    /**\r
+     * @thread SWT\r
+     */\r
+    protected void attachToWorkbench() {\r
+        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
+        if (window == null)\r
+            return;\r
+        service = window.getSelectionService();\r
+        ISelection initialSelection = service.getSelection();\r
+        IWorkbenchPart activePart = null;\r
+        if (initialSelection != null) {\r
+            IWorkbenchPage activePage = window.getActivePage();\r
+            if (activePage != null)\r
+                activePart = activePage.getActivePart();\r
+        }\r
+        selectionChanged(activePart, initialSelection);\r
+        service.addPostSelectionListener(this);\r
+    }\r
+\r
+    /**\r
+     * @thread SWT\r
+     */\r
+    @Override\r
+    public void dispose() {\r
+        if (service != null)\r
+            service.removePostSelectionListener(this);\r
+        service = null;\r
+        selection = null;\r
+        listener = null;\r
+    }\r
+\r
+    /**\r
+     * @thread SWT\r
+     */\r
+    @Override\r
+    public void selectionChanged(IWorkbenchPart part, ISelection selection) {\r
+       // Do not process selections from self\r
+       if(ObjectUtils.objectEquals(ownPart, part)) return;\r
+        Object old = this.selection;\r
+        this.selection = selection;\r
+        this.part = part;\r
+        //System.err.println("WorkbenchSelectionInputSource.selectionChanged(" + part + ", " + this.selection + ")");\r
+        fireIfInputChanged(old, selection);\r
+    }\r
+\r
+    /**\r
+     * @param oldSelection\r
+     * @param newSelection\r
+     * @thread SWT\r
+     */\r
+    protected void fireIfInputChanged(Object oldSelection, Object newSelection) {\r
+        InputSourceListener l = listener;\r
+        if (l != null)\r
+            if (!ObjectUtils.objectEquals(oldSelection, newSelection))\r
+                l.inputChanged(this);\r
+    }\r
+\r
+    @Override\r
+    public Object get(ISessionContext ctx) {\r
+        return selection != null ? selection : GraphExplorer.EMPTY_INPUT;\r
+    }\r
+    \r
+    @Override\r
+    public IWorkbenchPart getProvider() {\r
+       return this.part;\r
+    }\r
+\r
+    @Override\r
+    public void setListener(InputSourceListener listener) {\r
+        this.listener = listener;\r
+    }\r
+\r
+}\r