EffectingVideo.java :  » Graphic-Library » apollo » org » apollo » datamodel » Java Open Source

Java Open Source » Graphic Library » apollo 
apollo » org » apollo » datamodel » EffectingVideo.java
/*
 * Apollo - Motion capture and animation system
 * Copyright (c) 2005 Apollo
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @author Giovane.Kuhn - brain@netuno.com.br
 *
 */
package org.apollo.datamodel;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.apollo.ApolloUtil;

/**
 * Class represents a video that was transformed with some effects.
 * @author Giovane.Kuhn on 03/05/2005
 */
public final class EffectingVideo implements Serializable {

    private static final long serialVersionUID = 1L;

    /** List of effects to be applied in the video */
    private List<VideoEffect> effects;

    /** Effecting video file name */
    private String relativeVideoFile;

    /** Effecting video frames, that contain the markers info */
    private Map<Integer, EffectingFrame> frames;

    /** Project that's owner */
    private Project project;

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
        if (effects != null) {
            for (VideoEffect effect : effects) {
                effect.setProject(project);
            }
        }
    }

    public List<VideoEffect> getEffects() {
        return effects;
    }

    public void setEffects(List<VideoEffect> effects) {
        this.effects = effects;
        if (effects != null) {
            for (VideoEffect effect : effects) {
                effect.setProject(project);
            }
        }
    }

    public boolean addEffect(VideoEffect effect) {
        if (effects == null) {
            effects = new ArrayList<VideoEffect>();
        }
        boolean ret = effects.add(effect);
        effect.setProject(project);
        project.setChanged(EffectingVideo.class);
        return ret;
    }

    public boolean removeEffect(VideoEffect effect) {
        if (effects == null) {
            return false;
        }
        boolean ret = effects.remove(effect);
        effect.setProject(null);
        project.setChanged(EffectingVideo.class);
        return ret;
    }

    public boolean removeEffects(List<VideoEffect> effect) {
        if (effects == null) {
            return false;
        }
        boolean ret = effects.removeAll(effect);
        for (VideoEffect e : effect) {
            e.setProject(null);
        }
        project.setChanged(EffectingVideo.class);
        return ret;
    }

    public boolean replaceAllEffects(List<VideoEffect> newEffects) {
        if (effects != null) {
            for (VideoEffect effect : effects) {
                effect.setProject(null);
            }
        }
        this.setEffects(new ArrayList<VideoEffect>(newEffects));
        if (project != null) {
            project.setChanged(EffectingVideo.class);
        }
        return true;
    }

    public boolean changeEffectOrder(VideoEffect effect, int sum) {
        if (effects == null || sum == 0) {
            // are not change order
            return false;
        }
        int index = effects.indexOf(effect);
        if (index < 0 || sum == 0) {
            // effect doesn't exists
            return false;
        }
        if (index == effects.size() - 1 && sum > 0) {
            // effect is already last
            return false;
        }
        if (index == 0 && sum < 0) {
            // effect is already first
            return false;
        }
        int newIndex = index + sum;
        VideoEffect other = effects.get(newIndex);
        assert other != null;
        effects.set(newIndex, effect);
        effects.set(index, other);
        if (project != null) {
            project.setChanged(EffectingVideo.class);
        }
        return true;
    }

    public String getCanonicalVideoFile() {
        if (relativeVideoFile == null) {
            return null;
        }
        try {
            return new File(ApolloUtil.getPathName(project.getProjectFile()), relativeVideoFile).getCanonicalPath();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String getRelativeVideoFile() {
        return relativeVideoFile;
    }

    public void setRelativeVideoFile(String relativeVideoFile) {
        boolean changed = (this.relativeVideoFile != relativeVideoFile || (relativeVideoFile != null && !relativeVideoFile.equalsIgnoreCase(this.relativeVideoFile)));
        this.relativeVideoFile = relativeVideoFile;
        if (project != null && changed) {
            project.setChanged(EffectingVideo.class);
        }
    }

    public Map<Integer, EffectingFrame> getFrames() {
        if (frames == null) {
            return Collections.emptyMap();
        }
        return frames;
    }

    public List<EffectingFrame> getOrderedFrames() {
        if (frames == null) {
            return Collections.emptyList();
        }
        List<EffectingFrame> ret = new ArrayList<EffectingFrame>(frames.values());
        Collections.sort(ret, new Comparator<EffectingFrame>() {

            public int compare(EffectingFrame o1, EffectingFrame o2) {
                return o1.getSequence() - o2.getSequence();
            }

        });
        return ret;
    }

    public void setFrames(Map<Integer, EffectingFrame> frames) {
        this.frames = frames;
    }

    public EffectingFrame getFrame(Integer sequence) {
        if (frames == null) {
            return null;
        }
        return frames.get(sequence);
    }

    public void reset(boolean toOriginalMarkers) {
        if (frames == null) {
            return;
        }
        for (EffectingFrame f : frames.values()) {
            f.reset(toOriginalMarkers);
        }
    }

    public void uncolorizeMarkers() {
        if (frames == null) {
            return;
        }
        for (EffectingFrame f : frames.values()) {
            f.uncolorizeMarkers();
        }
    }

    public String toString() {
        return ApolloUtil.getFileName(relativeVideoFile, true);
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.