]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/PopulateSheetDropParticipant.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / PopulateSheetDropParticipant.java
diff --git a/bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/PopulateSheetDropParticipant.java b/bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/PopulateSheetDropParticipant.java
new file mode 100644 (file)
index 0000000..0d456b5
--- /dev/null
@@ -0,0 +1,70 @@
+/*******************************************************************************\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.spreadsheet.ui;\r
+\r
+import java.awt.geom.AffineTransform;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.util.URIStringUtils;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.diagram.adapter.GraphToDiagramSynchronizer;\r
+import org.simantics.diagram.participant.PopulateSelectionDropParticipant;\r
+import org.simantics.g2d.dnd.ElementClassDragItem;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+public class PopulateSheetDropParticipant extends PopulateSelectionDropParticipant {\r
+\r
+    public PopulateSheetDropParticipant(GraphToDiagramSynchronizer synchronizer) {\r
+        super(synchronizer);\r
+    }\r
+\r
+    @Override\r
+    public List<ElementClassDragItem> getElements(Session session, IStructuredSelection selection) throws DatabaseException {\r
+\r
+        final Resource resource = ISelectionUtils.filterSingleSelection(selection, Resource.class);\r
+        if(resource == null) return Collections.emptyList();\r
+\r
+        return session.syncRequest(new Read<List<ElementClassDragItem>>() {\r
+\r
+            @Override\r
+            public List<ElementClassDragItem> perform(ReadGraph graph) throws DatabaseException {\r
+\r
+                SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
+                if(graph.isInstanceOf(resource, sr.Spreadsheet)) {\r
+                    Layer0 L0 = Layer0.getInstance(graph);\r
+                    ElementClassDragItem item = new ElementClassDragItem(SheetFactory.createSheetClass(sr.SpreadsheetElement));\r
+                    item.getHintContext().setHint(SheetClass.KEY_SHEET, resource);\r
+                    String rvi = "/" + URIStringUtils.escape((String)graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING));\r
+                    item.getHintContext().setHint(SheetClass.KEY_RVI, rvi);\r
+                    item.getHintContext().setHint(ElementHints.KEY_TRANSFORM, AffineTransform.getScaleInstance(1,1));\r
+                    return Collections.singletonList(item);\r
+                } else {\r
+                    return Collections.emptyList();\r
+                }\r
+\r
+            }\r
+\r
+        });\r
+\r
+    }\r
+\r
+}
\ No newline at end of file