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