01: /*
02:
03: Licensed to the Apache Software Foundation (ASF) under one or more
04: contributor license agreements. See the NOTICE file distributed with
05: this work for additional information regarding copyright ownership.
06: The ASF licenses this file to You under the Apache License, Version 2.0
07: (the "License"); you may not use this file except in compliance with
08: the License. You may obtain a copy of the License at
09:
10: http://www.apache.org/licenses/LICENSE-2.0
11:
12: Unless required by applicable law or agreed to in writing, software
13: distributed under the License is distributed on an "AS IS" BASIS,
14: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: See the License for the specific language governing permissions and
16: limitations under the License.
17:
18: */
19: package org.apache.batik.bridge;
20:
21: import java.util.Calendar;
22:
23: import org.apache.batik.anim.timing.TimedElement;
24: import org.apache.batik.dom.events.DOMTimeEvent;
25: import org.apache.batik.dom.svg.IdContainer;
26: import org.apache.batik.dom.svg.SVGOMAnimationElement;
27: import org.apache.batik.util.XMLConstants;
28:
29: import org.w3c.dom.Element;
30: import org.w3c.dom.Node;
31: import org.w3c.dom.events.DocumentEvent;
32: import org.w3c.dom.events.EventTarget;
33: import org.w3c.dom.smil.TimeEvent;
34:
35: /**
36: * Class that provides utilities for animation support.
37: *
38: * @author <a href="mailto:cam%40mcc%2eid%2eau">Cameron McCormack</a>
39: * @version $Id: AnimationSupport.java 492528 2007-01-04 11:45:47Z cam $
40: */
41: public abstract class AnimationSupport {
42:
43: /**
44: * Fires a {@link TimeEvent} on the given {@link EventTarget}.
45: */
46: public static void fireTimeEvent(EventTarget target,
47: String eventType, Calendar time, int detail) {
48: DocumentEvent de = (DocumentEvent) ((Node) target)
49: .getOwnerDocument();
50: DOMTimeEvent evt = (DOMTimeEvent) de.createEvent("TimeEvent");
51: evt.initTimeEventNS(XMLConstants.XML_EVENTS_NAMESPACE_URI,
52: eventType, null, detail);
53: evt.setTimestamp(time.getTime().getTime());
54: target.dispatchEvent(evt);
55: }
56:
57: /**
58: * Returns the timed element with the given ID, using the given node
59: * as the context for the lookup.
60: */
61: public static TimedElement getTimedElementById(String id, Node n) {
62: Element e = getElementById(id, n);
63: if (e instanceof SVGOMAnimationElement) {
64: SVGAnimationElementBridge b = (SVGAnimationElementBridge) ((SVGOMAnimationElement) e)
65: .getSVGContext();
66: return b.getTimedElement();
67: }
68: return null;
69: }
70:
71: /**
72: * Returns the event target with the given ID, using the given
73: * node as the context for the lookup.
74: */
75: public static EventTarget getEventTargetById(String id, Node n) {
76: return (EventTarget) getElementById(id, n);
77: }
78:
79: /**
80: * Returns the element with the given ID, using the given node
81: * as the context for the lookup.
82: */
83: protected static Element getElementById(String id, Node n) {
84: Node p = n.getParentNode();
85: while (p != null) {
86: n = p;
87: p = n.getParentNode();
88: }
89: if (n instanceof IdContainer) {
90: return ((IdContainer) n).getElementById(id);
91: }
92: return null;
93: }
94: }
|