// XAAL toolkit // Copyright (C) 2009 Ville Karavirta // // 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 3 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, see . package xaal.objects; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import xaal.objects.animation.AddAnimationOperation; import xaal.objects.animation.AnimationOperation; import xaal.objects.animation.NoSuchOperationException; import xaal.objects.graphical.GraphicalPrimitive; import xaal.objects.structures.Structure; public class Animation extends Initial implements AddAnimationOperation { private LinkedList operations; private int position = 0; private HashMap state; public Animation() { super(); operations = new LinkedList(); } /** * */ public Animation(Xaal x) { //super(x); this(); setRoot(x); } public void setRoot(Xaal x) { super.setRoot(x); x.setAnimation(this); } public void addAnimationOperation(AnimationOperation ao) { operations.addLast(ao); } public Iterator getOperations() { return operations.iterator(); } public int getNumOperations() { return operations.size(); } public String toString() { return "Animation:\n -operations:"+operations.toString() + "\n -graphics:"+(getGraphicals().hasNext()?getGraphicals().next():""); } public Iterator applyNextOperation() throws NoSuchOperationException { if (state == null) { state = new HashMap(); if (getRoot().getInitial() != null) { Iterator i = getRoot().getInitial().getStructures(); while (i.hasNext()) { Structure str = (Structure) i.next(); System.out.println("init "+str); state.put(str.getId(), str); } i = getRoot().getInitial().getGraphicals(); while (i.hasNext()) { GraphicalPrimitive gp = (GraphicalPrimitive) i.next(); state.put(gp.getId(), gp); } } } if (position == operations.size()) throw new NoSuchOperationException("No next operation"); else { ((AnimationOperation) operations.get(position)).apply(state); position++; } return state.values().iterator(); } public Iterator undoPreviousOperation() throws NoSuchOperationException { if (position == 0) throw new NoSuchOperationException("No previous operation"); else { throw new NoSuchOperationException("Undoing operations not yet supported"); } } public boolean hasNextOperation() { return (position < operations.size()); } public AnimationOperation getCurrentOperation() { return (AnimationOperation) operations.get(position); } public List getOperationsList() { return new ArrayList(operations); } }