001/* 002 * SVG Salamander 003 * Copyright (c) 2004, Mark McKay 004 * All rights reserved. 005 * 006 * Redistribution and use in source and binary forms, with or 007 * without modification, are permitted provided that the following 008 * conditions are met: 009 * 010 * - Redistributions of source code must retain the above 011 * copyright notice, this list of conditions and the following 012 * disclaimer. 013 * - Redistributions in binary form must reproduce the above 014 * copyright notice, this list of conditions and the following 015 * disclaimer in the documentation and/or other materials 016 * provided with the distribution. 017 * 018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 029 * OF THE POSSIBILITY OF SUCH DAMAGE. 030 * 031 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other 032 * projects can be found at http://www.kitfox.com 033 * 034 * Created on August 15, 2004, 11:34 PM 035 */ 036 037package com.kitfox.svg.animation; 038 039import java.util.*; 040 041import com.kitfox.svg.*; 042 043import java.io.Serializable; 044 045/** 046 * Every element contains tracks, which manage the animation. There is one track 047 * for every parameter with animation, and each track in turn is composed of 048 * many events. 049 * 050 * @author Mark McKay 051 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a> 052 */ 053public class TrackManager implements Serializable 054{ 055 public static final long serialVersionUID = 0; 056 057 static class TrackKey 058 { 059 String name; 060 int type; 061 062 TrackKey(AnimationElement base) 063 { 064 this(base.getAttribName(), base.getAttribType()); 065 } 066 067 TrackKey(String name, int type) 068 { 069 this.name = name; 070 this.type = type; 071 } 072 073 @Override 074 public int hashCode() 075 { 076 int hash = name == null ? 0 : name.hashCode(); 077 hash = hash * 97 + type; 078 return hash; 079 } 080 081 @Override 082 public boolean equals(Object obj) 083 { 084 if (!(obj instanceof TrackKey)) return false; 085 TrackKey key = (TrackKey)obj; 086 return key.type == type && key.name.equals(name); 087 } 088 } 089 090 HashMap<TrackKey, TrackBase> tracks = new HashMap<TrackKey, TrackBase>(); 091 092 /** Creates a new instance of TrackManager */ 093 public TrackManager() 094 { 095 } 096 097 /** 098 * Adds a new animation element to this track 099 */ 100 public void addTrackElement(AnimationElement element) throws SVGElementException 101 { 102 TrackKey key = new TrackKey(element); 103 104 TrackBase track = (TrackBase)tracks.get(key); 105 106 if (track == null) 107 { 108 //Create a track for this element 109 if (element instanceof Animate) 110 { 111 switch (((Animate)element).getDataType()) 112 { 113 case Animate.DT_REAL: 114 track = new TrackDouble(element); 115 break; 116 case Animate.DT_COLOR: 117 track = new TrackColor(element); 118 break; 119 case Animate.DT_PATH: 120 track = new TrackPath(element); 121 break; 122 default: 123 throw new RuntimeException(""); 124 } 125 } 126 else if (element instanceof AnimateColor) 127 { 128 track = new TrackColor(element); 129 } 130 else if (element instanceof AnimateTransform || element instanceof AnimateMotion) 131 { 132 track = new TrackTransform(element); 133 } 134 135 tracks.put(key, track); 136 } 137 138 track.addElement(element); 139 } 140 141 public TrackBase getTrack(String name, int type) 142 { 143 //Handle AUTO, which will match either CSS or XML (in that order) 144 if (type == AnimationElement.AT_AUTO) 145 { 146 TrackBase t = getTrack(name, AnimationElement.AT_CSS); 147 if (t != null) return t; 148 t = getTrack(name, AnimationElement.AT_XML); 149 if (t != null) return t; 150 return null; 151 } 152 153 //Get requested attribute 154 TrackKey key = new TrackKey(name, type); 155 TrackBase t = (TrackBase)tracks.get(key); 156 if (t != null) return t; 157 158 //If that didn't exist, see if one exists of type AUTO 159 key = new TrackKey(name, AnimationElement.AT_AUTO); 160 return (TrackBase)tracks.get(key); 161 } 162 163 public int getNumTracks() 164 { 165 return tracks.size(); 166 } 167 168 public Iterator<TrackBase> iterator() 169 { 170 return tracks.values().iterator(); 171 } 172}