001package myproject.model.text;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.Observable;
006import myproject.model.AnimatorBuilder;
007import myproject.model.Car;
008import myproject.model.Light;
009import myproject.model.Road;
010import myproject.util.Animator;
011
012/**
013 * A class for building Animators.
014 */
015public class TextAnimatorBuilder implements AnimatorBuilder {
016        TextAnimator animator;
017        public TextAnimatorBuilder() {
018                this.animator = new TextAnimator();
019        }
020        public Animator getAnimator() {
021                if (animator == null) { throw new IllegalStateException(); }
022                Animator returnValue = animator;
023                animator = null;
024                return returnValue;
025        }
026        public void addLight(Light d, int i, int j) {
027                animator.addLight(d,i,j);
028        }
029        public void addHorizontalRoad(Road l, int i, int j, boolean eastToWest) {
030                animator.addRoad(l,i,j);
031        }
032        public void addVerticalRoad(Road l, int i, int j, boolean southToNorth) {
033                animator.addRoad(l,i,j);
034        }
035
036
037        /** Class for drawing the Model. */
038        private static class TextAnimator implements Animator {
039
040                /** Triple of a model element <code>x</code> and coordinates. */
041                private static class Element<T> {
042                        T x;
043                        int i;
044                        int j;
045                        Element(T x, int i, int j) {
046                                this.x = x;
047                                this.i = i;
048                                this.j = j;
049                        }
050                }
051
052                private List<Element<Road>> roadElements;
053                private List<Element<Light>> lightElements;
054                TextAnimator() {
055                        roadElements = new ArrayList<Element<Road>>();
056                        lightElements = new ArrayList<Element<Light>>();
057                }
058                void addLight(Light x, int i, int j) {
059                        lightElements.add(new Element<Light>(x,i,j));
060                }
061                void addRoad(Road x, int i, int j) {
062                        roadElements.add(new Element<Road>(x,i,j));
063                }
064
065                public void dispose() { }
066
067                public void update(Observable o, Object arg) {
068                        for (Element<Light> e : lightElements) {
069                                System.out.print("Light at (" + e.i + "," + e.j + "): ");
070                                if (e.x.getState()) {
071                                        System.out.println("Blue");
072                                } else {
073                                        System.out.println("Yellow");
074                                }
075                        }
076                        for (Element<Road> e : roadElements) {
077                                for (Car d : e.x.getCars()) {
078                                        System.out.print("Road at (" + e.i + "," + e.j + "): ");
079                                        System.out.println("Car at " + d.getPosition());
080                                }
081                        }
082                }
083        }
084}