1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.elementclass;
14 import java.awt.Shape;
15 import java.awt.geom.Rectangle2D;
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;
29 * Handles pick and painting of paintable symbols
31 * @author Toni Kalajainen
33 public class PaintableSymbolHandler implements SceneGraph, Pick, InternalSize {
35 private static final long serialVersionUID = 8612101011132309291L;
37 private final Rectangle2D rect;
38 private final Double ratio;
39 private final Image ps;
41 private final IProvider<Image> symbolProvider;
43 public PaintableSymbolHandler(Image symbol)
45 this.symbolProvider = StaticProvider.provide(symbol);
46 ps = symbolProvider.get();
47 rect = ps.getBounds();
48 ratio = rect.getWidth() / rect.getHeight();
51 public PaintableSymbolHandler(IProvider<Image> symbolProvider)
53 this.symbolProvider = symbolProvider;
54 ps = symbolProvider.get();
55 rect = ps.getBounds();
56 ratio = rect.getWidth() / rect.getHeight();
61 public void cleanup(IElement e) {
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);
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");
83 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
84 if (size==null) size = new Rectangle2D.Double();