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.PathIterator;
16 import java.awt.geom.Rectangle2D;
17 import java.util.ArrayList;
18 import java.util.Collection;
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;
26 public class ShapePick implements Pick {
28 public final static ShapePick INSTANCE = new ShapePick();
30 private static final long serialVersionUID = 1L;
33 public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
34 Rectangle2D pickRect = null;
35 if (s instanceof Rectangle2D)
36 pickRect = (Rectangle2D) s;
38 // FIXME: suboptimal, but works.
39 pickRect = s.getBounds2D();
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);
49 case PICK_CONTAINED_OBJECTS:
50 for (double[] seg : segments) {
51 if (!pickRect.contains(seg[0], seg[1]) || !pickRect.contains(seg[2], seg[3]))
56 case PICK_INTERSECTING_OBJECTS:
57 for (double[] seg : segments) {
58 if (pickRect.intersectsLine(seg[0], seg[1], seg[2], seg[3]))
60 if (pickRect.contains(seg[0], seg[1]))
62 if (pickRect.contains(seg[2], seg[3]))