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.*;
042import java.io.Serializable;
043
044/**
045 * Every element contains tracks, which manage the animation.  There is one track
046 * for every parameter with animation, and each track in turn is composed of
047 * many events.
048 *
049 * @author Mark McKay
050 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
051 */
052public class TrackManager implements Serializable
053{
054    public static final long serialVersionUID = 0;
055    
056    static class TrackKey
057    {
058        String name;
059        int type;
060        
061        TrackKey(AnimationElement base)
062        {
063            this(base.getAttribName(), base.getAttribType());
064        }
065        
066        TrackKey(String name, int type)
067        {
068            this.name = name;
069            this.type = type;
070        }
071        
072        public int hashCode()
073        {
074            int hash = name == null ? 0 : name.hashCode();
075            hash = hash * 97 + type;
076            return hash;
077        }
078
079        public boolean equals(Object obj) 
080        {
081            if (!(obj instanceof TrackKey)) return false;
082            TrackKey key = (TrackKey)obj;
083            return key.type == type && key.name.equals(name);
084        }
085    }
086    
087    HashMap tracks = new HashMap();
088    
089    /** Creates a new instance of TrackManager */
090    public TrackManager()
091    {
092    }
093    
094    /**
095     * Adds a new animation element to this track
096     */
097    public void addTrackElement(AnimationElement element) throws SVGElementException
098    {
099        TrackKey key = new TrackKey(element);
100        
101        TrackBase track = (TrackBase)tracks.get(key);
102        
103        if (track == null)
104        {
105            //Create a track for this element
106            if (element instanceof Animate)
107            {
108                switch (((Animate)element).getDataType())
109                {
110                    case Animate.DT_REAL:
111                        track = new TrackDouble(element);
112                        break;
113                    case Animate.DT_COLOR:
114                        track = new TrackColor(element);
115                        break;
116                    case Animate.DT_PATH:
117                        track = new TrackPath(element);
118                        break;
119                    default:
120                        throw new RuntimeException("");
121                }
122            }
123            else if (element instanceof AnimateColor)
124            {
125                track = new TrackColor(element);
126            }
127            else if (element instanceof AnimateTransform || element instanceof AnimateMotion)
128            {
129                track = new TrackTransform(element);
130            }
131            
132            tracks.put(key, track);
133        }
134  
135        track.addElement(element);
136    }
137    
138    public TrackBase getTrack(String name, int type)
139    {
140        //Handle AUTO, which will match either CSS or XML (in that order)
141        if (type == AnimationElement.AT_AUTO)
142        {
143            TrackBase t = getTrack(name, AnimationElement.AT_CSS);
144            if (t != null) return t;
145            t = getTrack(name, AnimationElement.AT_XML);
146            if (t != null) return t;
147            return null;
148        }
149        
150        //Get requested attribute
151        TrackKey key = new TrackKey(name, type);
152        TrackBase t = (TrackBase)tracks.get(key);
153        if (t != null) return t;
154        
155        //If that didn't exist, see if one exists of type AUTO
156        key = new TrackKey(name, AnimationElement.AT_AUTO);
157        return (TrackBase)tracks.get(key);
158    }
159    
160    public int getNumTracks()
161    {
162        return tracks.size();
163    }
164    
165    public Iterator iterator()
166    {
167        return tracks.values().iterator();
168    }
169}