]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/ShapePick.java
Merge "Counting of pending nodes does not work if null text is saved as """
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / ShapePick.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.PathIterator;
16 import java.awt.geom.Rectangle2D;
17 import java.util.ArrayList;
18 import java.util.Collection;
19
20 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
21 import org.simantics.g2d.element.ElementUtils;
22 import org.simantics.g2d.element.IElement;
23 import org.simantics.g2d.element.handler.Pick;
24 import org.simantics.g2d.utils.PathUtils;
25
26 public class ShapePick implements Pick {
27
28     public final static ShapePick INSTANCE = new ShapePick();
29
30     private static final long serialVersionUID = 1L;
31
32     @Override
33     public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
34         Rectangle2D pickRect = null;
35         if (s instanceof Rectangle2D)
36             pickRect = (Rectangle2D) s;
37         else
38             // FIXME: suboptimal, but works.
39             pickRect = s.getBounds2D();
40
41         Shape es = ElementUtils.getElementShapeOrBounds(e);
42         // getElementShapeOrBounds returns shape or bounds in local coords!
43         es = ElementUtils.getTransform(e).createTransformedShape(es);
44         PathIterator iter = es.getPathIterator(null);
45         Collection<double[]> segments = new ArrayList<double[]>();
46         PathUtils.toLineSegments(iter, segments);
47
48         switch (policy) {
49             case PICK_CONTAINED_OBJECTS:
50                 for (double[] seg : segments) {
51                     if (!pickRect.contains(seg[0], seg[1]) || !pickRect.contains(seg[2], seg[3]))
52                         return false;
53                 }
54                 return true;
55
56             case PICK_INTERSECTING_OBJECTS:
57                 for (double[] seg : segments) {
58                     if (pickRect.intersectsLine(seg[0], seg[1], seg[2], seg[3]))
59                         return true;
60                     if (pickRect.contains(seg[0], seg[1]))
61                         return true;
62                     if (pickRect.contains(seg[2], seg[3]))
63                         return true;
64                 }
65                 
66                 return false;
67         }
68
69         return false;
70     }
71
72 }