]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/WorkbenchStructuralSelectionProvider.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / handlers / WorkbenchStructuralSelectionProvider.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/WorkbenchStructuralSelectionProvider.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/WorkbenchStructuralSelectionProvider.java
new file mode 100644 (file)
index 0000000..4b46b9b
--- /dev/null
@@ -0,0 +1,65 @@
+/*******************************************************************************\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
+package org.simantics.modeling.ui.diagramEditor.handlers;\r
+\r
+import java.util.ArrayList;\r
+\r
+import org.eclipse.jface.viewers.IPostSelectionProvider;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.StructuredSelection;\r
+import org.eclipse.ui.IWorkbenchPartSite;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.diagram.ui.WorkbenchSelectionProvider;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.utils.threads.IThreadWorkQueue;\r
+\r
+/**\r
+ * A canvas participant that listens to the #0 mouse selection and provides it\r
+ * forward through the {@link IPostSelectionProvider} interface.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class WorkbenchStructuralSelectionProvider extends WorkbenchSelectionProvider {\r
+\r
+    ResourceArray                                   suffix;\r
+\r
+    public WorkbenchStructuralSelectionProvider(IThreadWorkQueue swt, ResourceArray appendToSelections) {\r
+        this(swt, null, appendToSelections);\r
+    }\r
+\r
+    public WorkbenchStructuralSelectionProvider(IThreadWorkQueue swt, IWorkbenchPartSite site, ResourceArray appendToSelections) {\r
+        super(swt, site);\r
+        this.suffix = appendToSelections;\r
+    }\r
+\r
+    protected ISelection constructAdaptableSelection(Iterable<?> selection) {\r
+        ArrayList<Object> objects = new ArrayList<Object>();\r
+        for (Object o : selection) {\r
+            if (o instanceof IElement) {\r
+                IElement e = (IElement) o;\r
+                Object object = e.getHint(ElementHints.KEY_OBJECT);\r
+                if (object instanceof Resource) {\r
+                    objects.add(suffix.prepended((Resource) object));\r
+                } else {\r
+                    objects.add(object);\r
+                    objects.add(suffix);\r
+                }\r
+            } else {\r
+                System.out.println("  unrecognized selection: " + o.getClass() + ": " + o);\r
+            }\r
+        }\r
+        return new StructuredSelection(objects);\r
+    }\r
+\r
+}\r