]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.elementclass;\r
13 \r
14 import java.awt.Shape;\r
15 import java.awt.geom.Rectangle2D;\r
16 \r
17 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;\r
18 import org.simantics.g2d.element.IElement;\r
19 import org.simantics.g2d.element.handler.InternalSize;\r
20 import org.simantics.g2d.element.handler.Pick;\r
21 import org.simantics.g2d.element.handler.SceneGraph;\r
22 import org.simantics.g2d.image.Image;\r
23 import org.simantics.g2d.utils.GeometryUtils;\r
24 import org.simantics.scenegraph.g2d.G2DParentNode;\r
25 import org.simantics.utils.datastructures.cache.IProvider;\r
26 import org.simantics.utils.datastructures.cache.StaticProvider;\r
27 \r
28 /**\r
29  * Handles pick and painting of paintable symbols\r
30  * \r
31  * @author Toni Kalajainen\r
32  */\r
33 public class PaintableSymbolHandler implements SceneGraph, Pick, InternalSize {\r
34 \r
35     private static final long serialVersionUID = 8612101011132309291L;\r
36 \r
37     private final Rectangle2D rect;\r
38     private final Double ratio;\r
39     private final Image ps;\r
40 \r
41     private final IProvider<Image> symbolProvider;\r
42 \r
43     public PaintableSymbolHandler(Image symbol)\r
44     {\r
45         this.symbolProvider = StaticProvider.provide(symbol);\r
46         ps = symbolProvider.get();\r
47         rect = ps.getBounds();\r
48         ratio = rect.getWidth() / rect.getHeight();\r
49     }\r
50 \r
51     public PaintableSymbolHandler(IProvider<Image> symbolProvider)\r
52     {\r
53         this.symbolProvider = symbolProvider;\r
54         ps = symbolProvider.get();\r
55         rect = ps.getBounds();\r
56         ratio = rect.getWidth() / rect.getHeight();\r
57     }\r
58 \r
59 \r
60     @Override\r
61     public void cleanup(IElement e) {\r
62     }\r
63 \r
64     @Override\r
65     public void init(IElement e, G2DParentNode parent) {\r
66         System.out.println("PaintableSymbolHandler.paint()");\r
67 //      Graphics2D g = elementGC.getGraphics2D();\r
68 //      Image ps = ctx.getSingleItem(SymbolUtil.class).get(symbolProvider, g.getDeviceConfiguration());\r
69 //      ps.paint(elementGC);\r
70     }\r
71 \r
72     @Override\r
73     public boolean pickTest(IElement e, Shape s, PickPolicy policy) {\r
74         Shape outline = ps.getOutline();\r
75         if (policy == PickPolicy.PICK_CONTAINED_OBJECTS)\r
76             return GeometryUtils.contains(s, outline);\r
77         if (policy == PickPolicy.PICK_INTERSECTING_OBJECTS)\r
78             return GeometryUtils.intersects(s, outline);\r
79         throw new RuntimeException("Unimplemented");\r
80     }\r
81 \r
82     @Override\r
83     public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
84         if (size==null) size = new Rectangle2D.Double();\r
85         size.setFrame(rect);\r
86         return size;\r
87     }\r
88 \r
89 }\r