]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/PaintableSymbolHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / PaintableSymbolHandler.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/PaintableSymbolHandler.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/PaintableSymbolHandler.java
new file mode 100644 (file)
index 0000000..8340420
--- /dev/null
@@ -0,0 +1,89 @@
+/*******************************************************************************\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.g2d.elementclass;\r
+\r
+import java.awt.Shape;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.InternalSize;\r
+import org.simantics.g2d.element.handler.Pick;\r
+import org.simantics.g2d.element.handler.SceneGraph;\r
+import org.simantics.g2d.image.Image;\r
+import org.simantics.g2d.utils.GeometryUtils;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.utils.datastructures.cache.IProvider;\r
+import org.simantics.utils.datastructures.cache.StaticProvider;\r
+\r
+/**\r
+ * Handles pick and painting of paintable symbols\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class PaintableSymbolHandler implements SceneGraph, Pick, InternalSize {\r
+\r
+    private static final long serialVersionUID = 8612101011132309291L;\r
+\r
+    private final Rectangle2D rect;\r
+    private final Double ratio;\r
+    private final Image ps;\r
+\r
+    private final IProvider<Image> symbolProvider;\r
+\r
+    public PaintableSymbolHandler(Image symbol)\r
+    {\r
+        this.symbolProvider = StaticProvider.provide(symbol);\r
+        ps = symbolProvider.get();\r
+        rect = ps.getBounds();\r
+        ratio = rect.getWidth() / rect.getHeight();\r
+    }\r
+\r
+    public PaintableSymbolHandler(IProvider<Image> symbolProvider)\r
+    {\r
+        this.symbolProvider = symbolProvider;\r
+        ps = symbolProvider.get();\r
+        rect = ps.getBounds();\r
+        ratio = rect.getWidth() / rect.getHeight();\r
+    }\r
+\r
+\r
+    @Override\r
+    public void cleanup(IElement e) {\r
+    }\r
+\r
+    @Override\r
+    public void init(IElement e, G2DParentNode parent) {\r
+        System.out.println("PaintableSymbolHandler.paint()");\r
+//      Graphics2D g = elementGC.getGraphics2D();\r
+//      Image ps = ctx.getSingleItem(SymbolUtil.class).get(symbolProvider, g.getDeviceConfiguration());\r
+//      ps.paint(elementGC);\r
+    }\r
+\r
+    @Override\r
+    public boolean pickTest(IElement e, Shape s, PickPolicy policy) {\r
+        Shape outline = ps.getOutline();\r
+        if (policy == PickPolicy.PICK_CONTAINED_OBJECTS)\r
+            return GeometryUtils.contains(s, outline);\r
+        if (policy == PickPolicy.PICK_INTERSECTING_OBJECTS)\r
+            return GeometryUtils.intersects(s, outline);\r
+        throw new RuntimeException("Unimplemented");\r
+    }\r
+\r
+    @Override\r
+    public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
+        if (size==null) size = new Rectangle2D.Double();\r
+        size.setFrame(rect);\r
+        return size;\r
+    }\r
+\r
+}\r