| 1 | /*
|
|---|
| 2 | * SVG Salamander
|
|---|
| 3 | * Copyright (c) 2004, Mark McKay
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or
|
|---|
| 7 | * without modification, are permitted provided that the following
|
|---|
| 8 | * conditions are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above
|
|---|
| 11 | * copyright notice, this list of conditions and the following
|
|---|
| 12 | * disclaimer.
|
|---|
| 13 | * - Redistributions in binary form must reproduce the above
|
|---|
| 14 | * copyright notice, this list of conditions and the following
|
|---|
| 15 | * disclaimer in the documentation and/or other materials
|
|---|
| 16 | * provided with the distribution.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|---|
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|---|
| 22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|---|
| 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|---|
| 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|---|
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|---|
| 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|---|
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|---|
| 29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 30 | *
|
|---|
| 31 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
|---|
| 32 | * projects can be found at http://www.kitfox.com
|
|---|
| 33 | *
|
|---|
| 34 | * Created on February 18, 2004, 5:04 PM
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | package com.kitfox.svg;
|
|---|
| 38 |
|
|---|
| 39 | import java.awt.Graphics2D;
|
|---|
| 40 | import java.awt.Rectangle;
|
|---|
| 41 | import java.awt.geom.AffineTransform;
|
|---|
| 42 | import java.awt.geom.Point2D;
|
|---|
| 43 | import java.awt.geom.Rectangle2D;
|
|---|
| 44 | import java.io.Serializable;
|
|---|
| 45 | import java.net.URI;
|
|---|
| 46 | import java.util.ArrayList;
|
|---|
| 47 | import java.util.HashMap;
|
|---|
| 48 | import java.util.List;
|
|---|
| 49 | import java.util.logging.Level;
|
|---|
| 50 | import java.util.logging.Logger;
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Top level structure in an SVG tree.
|
|---|
| 55 | *
|
|---|
| 56 | * @author Mark McKay
|
|---|
| 57 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
|---|
| 58 | */
|
|---|
| 59 | public class SVGDiagram implements Serializable
|
|---|
| 60 | {
|
|---|
| 61 | public static final long serialVersionUID = 0;
|
|---|
| 62 |
|
|---|
| 63 | //Indexes elements within this SVG diagram
|
|---|
| 64 | final HashMap idMap = new HashMap();
|
|---|
| 65 |
|
|---|
| 66 | SVGRoot root;
|
|---|
| 67 | final SVGUniverse universe;
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * This is used by the SVGRoot to determine the width of the
|
|---|
| 71 | */
|
|---|
| 72 | private Rectangle deviceViewport = new Rectangle(100, 100);
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * If true, no attempt will be made to discard geometry based on it being
|
|---|
| 76 | * out of bounds. This trades potentially drawing many out of bounds
|
|---|
| 77 | * shapes with having to recalculate bounding boxes every animation iteration.
|
|---|
| 78 | */
|
|---|
| 79 | protected boolean ignoreClipHeuristic = false;
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * URL which uniquely identifies this document
|
|---|
| 83 | */
|
|---|
| 84 | // final URI docRoot;
|
|---|
| 85 |
|
|---|
| 86 | /**
|
|---|
| 87 | * URI that uniquely identifies this document. Also used to resolve
|
|---|
| 88 | * relative urls. Default base for document.
|
|---|
| 89 | */
|
|---|
| 90 | final URI xmlBase;
|
|---|
| 91 |
|
|---|
| 92 | /** Creates a new instance of SVGDiagram */
|
|---|
| 93 | public SVGDiagram(URI xmlBase, SVGUniverse universe)
|
|---|
| 94 | {
|
|---|
| 95 | this.universe = universe;
|
|---|
| 96 | // this.docRoot = docRoot;
|
|---|
| 97 | this.xmlBase = xmlBase;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * Draws this diagram to the passed graphics context
|
|---|
| 102 | */
|
|---|
| 103 | public void render(Graphics2D g) throws SVGException
|
|---|
| 104 | {
|
|---|
| 105 | root.renderToViewport(g);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * Searches thorough the scene graph for all RenderableElements that have
|
|---|
| 110 | * shapes that contain the passed point.
|
|---|
| 111 | *
|
|---|
| 112 | * For every shape which contains the pick point, a List containing the
|
|---|
| 113 | * path to the node is added to the return list. That is, the result of
|
|---|
| 114 | * SVGElement.getPath() is added for each entry.
|
|---|
| 115 | *
|
|---|
| 116 | * @return the passed in list
|
|---|
| 117 | */
|
|---|
| 118 | public List pick(Point2D point, List retVec) throws SVGException
|
|---|
| 119 | {
|
|---|
| 120 | return pick(point, false, retVec);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | public List pick(Point2D point, boolean boundingBox, List retVec) throws SVGException
|
|---|
| 124 | {
|
|---|
| 125 | if (retVec == null)
|
|---|
| 126 | {
|
|---|
| 127 | retVec = new ArrayList();
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | root.pick(point, boundingBox, retVec);
|
|---|
| 131 |
|
|---|
| 132 | return retVec;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | public List pick(Rectangle2D pickArea, List retVec) throws SVGException
|
|---|
| 136 | {
|
|---|
| 137 | return pick(pickArea, false, retVec);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | public List pick(Rectangle2D pickArea, boolean boundingBox, List retVec) throws SVGException
|
|---|
| 141 | {
|
|---|
| 142 | if (retVec == null)
|
|---|
| 143 | {
|
|---|
| 144 | retVec = new ArrayList();
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | root.pick(pickArea, new AffineTransform(), boundingBox, retVec);
|
|---|
| 148 |
|
|---|
| 149 | return retVec;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | public SVGUniverse getUniverse()
|
|---|
| 153 | {
|
|---|
| 154 | return universe;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | public URI getXMLBase()
|
|---|
| 158 | {
|
|---|
| 159 | return xmlBase;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | // public URL getDocRoot()
|
|---|
| 163 | // {
|
|---|
| 164 | // return docRoot;
|
|---|
| 165 | // }
|
|---|
| 166 |
|
|---|
| 167 | public float getWidth()
|
|---|
| 168 | {
|
|---|
| 169 | if (root == null) return 0;
|
|---|
| 170 | return root.getDeviceWidth();
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | public float getHeight()
|
|---|
| 174 | {
|
|---|
| 175 | if (root == null) return 0;
|
|---|
| 176 | return root.getDeviceHeight();
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /**
|
|---|
| 180 | * Returns the viewing rectangle of this diagram in device coordinates.
|
|---|
| 181 | */
|
|---|
| 182 | public Rectangle2D getViewRect(Rectangle2D rect)
|
|---|
| 183 | {
|
|---|
| 184 | if (root != null) return root.getDeviceRect(rect);
|
|---|
| 185 | return rect;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | public Rectangle2D getViewRect()
|
|---|
| 189 | {
|
|---|
| 190 | return getViewRect(new Rectangle2D.Double());
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | public SVGElement getElement(String name)
|
|---|
| 194 | {
|
|---|
| 195 | return (SVGElement)idMap.get(name);
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | public void setElement(String name, SVGElement node)
|
|---|
| 199 | {
|
|---|
| 200 | idMap.put(name, node);
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | public void removeElement(String name)
|
|---|
| 204 | {
|
|---|
| 205 | idMap.remove(name);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | public SVGRoot getRoot()
|
|---|
| 209 | {
|
|---|
| 210 | return root;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | public void setRoot(SVGRoot root)
|
|---|
| 214 | {
|
|---|
| 215 | this.root = root;
|
|---|
| 216 | root.setDiagram(this);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | public boolean ignoringClipHeuristic() { return ignoreClipHeuristic; }
|
|---|
| 220 |
|
|---|
| 221 | public void setIgnoringClipHeuristic(boolean ignoreClipHeuristic) { this.ignoreClipHeuristic = ignoreClipHeuristic; }
|
|---|
| 222 |
|
|---|
| 223 | /**
|
|---|
| 224 | * Updates all attributes in this diagram associated with a time event.
|
|---|
| 225 | * Ie, all attributes with track information.
|
|---|
| 226 | */
|
|---|
| 227 | public void updateTime(double curTime) throws SVGException
|
|---|
| 228 | {
|
|---|
| 229 | if (root == null) return;
|
|---|
| 230 | root.updateTime(curTime);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | public Rectangle getDeviceViewport()
|
|---|
| 234 | {
|
|---|
| 235 | return deviceViewport;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /**
|
|---|
| 239 | * Sets the dimensions of the device being rendered into. This is used by
|
|---|
| 240 | * SVGRoot when its x, y, width or height parameters are specified as
|
|---|
| 241 | * percentages.
|
|---|
| 242 | */
|
|---|
| 243 | public void setDeviceViewport(Rectangle deviceViewport)
|
|---|
| 244 | {
|
|---|
| 245 | this.deviceViewport.setBounds(deviceViewport);
|
|---|
| 246 | if (root != null)
|
|---|
| 247 | {
|
|---|
| 248 | try
|
|---|
| 249 | {
|
|---|
| 250 | root.build();
|
|---|
| 251 | } catch (SVGException ex)
|
|---|
| 252 | {
|
|---|
| 253 | Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
|
|---|
| 254 | "Could not build document", ex);
|
|---|
| 255 | }
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|