001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.plugins.visualization.corebasics.animator;
005    
006    import graphlab.graph.graph.*;
007    import graphlab.platform.core.BlackBoard;
008    
009    import java.awt.geom.Point2D;
010    import java.util.HashMap;
011    import java.util.Iterator;
012    import java.util.Vector;
013    
014    /**
015     * @author Rouzbeh Ebrahimi  Ebrahimi ruzbehus@yahoo.com
016     */
017    public class GeneralAnimator implements Runnable {
018    
019        public HashMap<VertexModel, Point2D> vertexDestinations = new HashMap<VertexModel, Point2D>();
020        public HashMap<EdgeModel, Vector<Point2D>> edgeBendPoints = new HashMap<EdgeModel, Vector<Point2D>>();
021        public boolean supportBendedEdge;
022    
023        GraphModel g;
024        AbstractGraphRenderer gv;
025        public Thread animate;
026        private BlackBoard blackboard;
027    
028        public GeneralAnimator(HashMap<VertexModel, Point2D> vertexDestinations, GraphModel g, BlackBoard blackboard) {
029            this.vertexDestinations = vertexDestinations;
030            this.g = g;
031            this.blackboard = blackboard;
032        }
033    
034        public GeneralAnimator(HashMap<VertexModel, Point2D> vertexDestinations, HashMap<EdgeModel, Vector<Point2D>> edgeBendPoints, GraphModel g, BlackBoard blackboard) {
035            this.vertexDestinations = vertexDestinations;
036            this.edgeBendPoints = edgeBendPoints;
037            this.g = g;
038            supportBendedEdge = true;
039            this.blackboard = blackboard;
040        }
041    
042        public void start() {
043            if (animate == null) {
044                if (supportBendedEdge)
045                    animate = new Thread(new GeneralAnimator(vertexDestinations, edgeBendPoints, g, blackboard));
046                else
047                    animate = new Thread(new GeneralAnimator(vertexDestinations, g, blackboard));
048    
049                animate.start();
050            }
051        }
052    
053        public void run() {
054            final Thread current = Thread.currentThread();
055            Iterator<VertexModel> v = vertexDestinations.keySet().iterator();
056            final Vector<Point2D> movements = new Vector<Point2D>();
057            final Vector<Point2D> initials = new Vector<Point2D>();
058            for (; v.hasNext();) {
059                VertexModel vertex = v.next();
060                double initalX = vertex.getLocation().getX();
061                Point2D point2D = vertexDestinations.get(vertex);
062                double totalXMovement = (point2D.getX() - initalX);
063                double initialY = vertex.getLocation().getY();
064                double totalYMovement = (point2D.getY() - initialY);
065                initials.add(new Point2D.Double(initalX, initialY));
066                movements.add(new Point2D.Double(totalXMovement, totalYMovement));
067            }
068    
069            Iterator<Point2D> m;
070            Iterator<Point2D> i;
071            final int k = 21;
072            for (int j = 1; j != k; j++) {
073                AbstractGraphRenderer ren = blackboard.getData(AbstractGraphRenderer.EVENT_KEY);
074                final int j1 = j;
075                ren.ignoreRepaints(new Runnable() {
076                    public void run() {
077                        doAnimateStep(movements, initials, j1, k, current);
078                    }
079                });
080            }
081            if (supportBendedEdge) {
082                paintEdges();
083            }
084        }
085    
086        private void doAnimateStep(Vector<Point2D> movements, Vector<Point2D> initials, int j, int k, Thread current) {
087            Iterator<VertexModel> v;
088            Iterator<Point2D> m;
089            Iterator<Point2D> i;
090            v = vertexDestinations.keySet().iterator();
091    
092            m = movements.iterator();
093            i = initials.iterator();
094            for (; v.hasNext();) {
095                VertexModel vertex = v.next();
096                Point2D movement = m.next();
097                Point2D initial = i.next();
098                //                vertex.setLabel(initial.getY()+"");
099                vertex.setLocation(new GraphPoint(initial.getX() + j * movement.getX() / k, initial.getY() + j * movement.getY() / k));
100    
101            }
102            try {
103                Thread.sleep(100);
104            } catch (InterruptedException e) {
105                e.printStackTrace();
106            }
107            while (animate == current) {
108                //todo
109                //                g.view.repaint();
110            }
111        }
112    
113        public void paintEdges() {
114            Iterator<EdgeModel> ei = g.edgeIterator();
115            for (; ei.hasNext();) {
116                EdgeModel e = ei.next();
117    //            e.view.ssetBendedEdge(true);
118    //            e.view.setBendPoints(edgeBendPoints.get(e));
119            }
120    //todo        g.repaint();
121        }
122    }