]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/ShapePick.java
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         PathIterator iter = es.getPathIterator(null);
43         Collection<double[]> segments = new ArrayList<double[]>();
44         PathUtils.toLineSegments(iter, segments);
45
46         switch (policy) {
47             case PICK_CONTAINED_OBJECTS:
48                 for (double[] seg : segments) {
49                     if (!pickRect.contains(seg[0], seg[1]) || !pickRect.contains(seg[2], seg[3]))
50                         return false;
51                 }
52                 return true;
53
54             case PICK_INTERSECTING_OBJECTS:
55                 for (double[] seg : segments) {
56                     if (pickRect.intersectsLine(seg[0], seg[1], seg[2], seg[3]))
57                         return true;
58                 }
59                 return false;
60         }
61
62         return false;
63     }
64
65 }