]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/OutlinePick.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / OutlinePick.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.element.handler.impl;
13
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;
19
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;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class OutlinePick implements Pick {
32
33     public static final ElementHandler INSTANCE         = new OutlinePick();
34
35     private static final long          serialVersionUID = 1L;
36
37     @Override
38     public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
39         Rectangle2D pickArea = null;
40         if (s instanceof Rectangle2D)
41             pickArea = (Rectangle2D) s;
42         else
43             // FIXME: suboptimal, but works.
44             pickArea = s.getBounds2D();
45
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;
53             try {
54                 elementToCanvas = canvasToElement.createInverse();
55             } catch (NoninvertibleTransformException e1) {
56                 throw new RuntimeException(e1);
57             }
58             Shape pickShapeInElementCoords = GeometryUtils.transformShape(pickArea, elementToCanvas);
59
60             // Intersection with one shape is enough
61             switch (policy) {
62                 case PICK_INTERSECTING_OBJECTS: {
63                     for (Outline es : shapeHandlers) {
64                         Shape elementShape = es.getElementShape(e);
65                         if (elementShape == null)
66                             return false;
67                         if (GeometryUtils.intersects(pickShapeInElementCoords, elementShape))
68                             return true;
69                     }
70                     return false;
71                 }
72                 case PICK_CONTAINED_OBJECTS: {
73                     for (Outline es : shapeHandlers) {
74                         Shape elementShape = es.getElementShape(e);
75                         if (GeometryUtils.contains(pickShapeInElementCoords, elementShape))
76                             return true;
77                     }
78                     return false;
79                 }
80             }
81         }
82         return false;
83     }
84 }