01: /*
02: * SelectSvgArea.java
03: *
04: * Created on 01 February 2007, 10:54
05: *
06: * To change this template, choose Tools | Template Manager
07: * and open the template in the editor.
08: */
09:
10: package com.xoetrope.svg;
11:
12: import com.kitfox.svg.SVGElement;
13: import com.kitfox.svg.SVGRoot;
14: import com.kitfox.svg.ShapeElement;
15: import com.kitfox.svg.Tspan;
16: import com.xoetrope.svg.XSvgImageMap;
17: import java.awt.geom.Rectangle2D;
18: import java.util.Vector;
19:
20: /**
21: *
22: *
23: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
24: * the GNU Public License (GPL), please see license.txt for more details. If
25: * you make commercial use of this software you must purchase a commercial
26: * license from Xoetrope.</p>
27: * <p> $Revision: 1.2 $</p>
28: */
29: public class XSelectSvgArea {
30: protected XSvgImageMap imageMap;
31: protected Vector shapes;
32:
33: /**
34: * Creates a new instance of SelectSvgArea
35: * @param imageMap <CODE>XSvgImageMap</CODE> instance used by this class for area selection.
36: */
37: public XSelectSvgArea(XSvgImageMap imageMap) {
38: this .imageMap = imageMap;
39: }
40:
41: /**
42: * Method for extracting all svg elements within a bounded area.
43: * @param x <CODE>double</CODE> specifying the x-coordinate of the bounding box.
44: * @param y <CODE>double</CODE> specifying the y-coordinate of the bounding box.
45: * @param width <CODE>double</CODE> specifying the width of the bounding box.
46: * @param height <CODE>double</CODE> specifying the height of the bounding box.
47: */
48: public Vector extractWithinBounds(double x, double y, double width,
49: double height) {
50: SVGRoot root = imageMap.getSvgDiagram().getRoot();
51: Vector parents = new Vector();
52: root.getChildren(parents);
53: extractAllChildren(parents);
54:
55: shapes = new Vector();
56: Rectangle2D.Double bounds = new Rectangle2D.Double(x, y, width,
57: height);
58: for (int i = 0; i < parents.size(); i++) {
59: Object object = parents.get(i);
60: if ((object instanceof ShapeElement)
61: && !(object instanceof Tspan)) {
62: ShapeElement e = (ShapeElement) object;
63:
64: if (bounds.contains(e.getShape().getBounds2D())) {
65: shapes.add(e);
66: }
67: }
68: }
69:
70: return shapes;
71: }
72:
73: /**
74: * private method used by this class to extract all elements from the svg image.
75: * @param parents <CODE>Vector</CODE> passed to this class which will contain all children
76: * when the method has finished executing recursively.
77: */
78: private void extractAllChildren(Vector parents) {
79: for (int i = 0; i < parents.size(); i++) {
80: SVGElement e = (SVGElement) parents.get(i);
81: Vector children = new Vector();
82: e.getChildren(children);
83:
84: if (children.size() != 0) {
85: parents.addAll(children);
86: extractAllChildren(children);
87: }
88: }
89: }
90: }
|