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.element.handler.impl;
14 import java.awt.Shape;
15 import java.awt.geom.AffineTransform;
16 import java.awt.geom.NoninvertibleTransformException;
17 import java.awt.geom.Rectangle2D;
18 import java.util.List;
20 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
21 import org.simantics.g2d.element.IElement;
22 import org.simantics.g2d.element.handler.ElementHandler;
23 import org.simantics.g2d.element.handler.Outline;
24 import org.simantics.g2d.element.handler.Pick;
25 import org.simantics.g2d.element.handler.Transform;
26 import org.simantics.g2d.utils.GeometryUtils;
29 * @author Tuukka Lehtonen
31 public class OutlinePick implements Pick {
33 public static final ElementHandler INSTANCE = new OutlinePick();
35 private static final long serialVersionUID = 1L;
38 public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
39 Rectangle2D pickArea = null;
40 if (s instanceof Rectangle2D)
41 pickArea = (Rectangle2D) s;
43 // FIXME: suboptimal, but works.
44 pickArea = s.getBounds2D();
46 // Pick with shape handler(s)
47 List<Outline> shapeHandlers = e.getElementClass().getItemsByClass(Outline.class);
48 if (!shapeHandlers.isEmpty()) {
49 // Convert pick shape to element coordinates
50 Transform t = e.getElementClass().getSingleItem(Transform.class);
51 AffineTransform canvasToElement = t.getTransform(e);
52 AffineTransform elementToCanvas;
54 elementToCanvas = canvasToElement.createInverse();
55 } catch (NoninvertibleTransformException e1) {
56 throw new RuntimeException(e1);
58 Shape pickShapeInElementCoords = GeometryUtils.transformShape(pickArea, elementToCanvas);
60 // Intersection with one shape is enough
62 case PICK_INTERSECTING_OBJECTS: {
63 for (Outline es : shapeHandlers) {
64 Shape elementShape = es.getElementShape(e);
65 if (elementShape == null)
67 if (GeometryUtils.intersects(pickShapeInElementCoords, elementShape))
72 case PICK_CONTAINED_OBJECTS: {
73 for (Outline es : shapeHandlers) {
74 Shape elementShape = es.getElementShape(e);
75 if (GeometryUtils.contains(pickShapeInElementCoords, elementShape))