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.algorithmanimator.core.atoms;
005    
006    import graphlab.graph.graph.EdgeModel;
007    import graphlab.graph.graph.VertexModel;
008    import graphlab.library.BaseGraph;
009    import graphlab.library.BaseVertex;
010    import graphlab.library.event.Event;
011    import graphlab.library.event.PostWorkEvent;
012    import graphlab.library.event.PreWorkEvent;
013    import graphlab.platform.core.BlackBoard;
014    import graphlab.plugins.algorithmanimator.core.AtomAnimator;
015    
016    import java.util.Collection;
017    
018    /**
019     * @author Azin Azadi
020     */
021    public class PrePostWork implements AtomAnimator<Event> {
022        private PostWorkEvent t;
023        PreWorkEvent tt;
024    
025        public boolean isAnimatable(Event event) {
026            if (event instanceof PreWorkEvent)
027                return true;
028            return event instanceof PostWorkEvent;
029        }
030    
031        public Event animate(Event event, BlackBoard b) {
032            if (event instanceof PreWorkEvent) {
033                tt = (PreWorkEvent) event;
034                visit(tt.from, tt.to, tt.graph);
035                return tt;
036            }
037            if (event instanceof PostWorkEvent) {
038                t = (PostWorkEvent) event;
039                leave(t.to);
040                return t;
041            }
042            return null;
043        }
044    
045        private void visit(BaseVertex from, BaseVertex v, BaseGraph graph) {
046            if (from == v)
047                return;
048    //        tt.setMessage("visit:" + v.getId());
049            VertexModel v2 = ((VertexModel) v);
050            VertexModel v1 = ((VertexModel) from);
051            if (graph == null) {
052                System.out.println("graph = null");
053                return;
054            }
055            Collection edges = graph.getEdges(v1, v2);
056            if (edges == null)
057                return;
058            EdgeModel ee = ((EdgeModel) edges.iterator().next());
059            if (ee != null)
060                ee.setColor(2);
061            v2.setColor(3);
062    //        try {
063    //            Thread.sleep(100);
064    //            wait(400);
065    //        } catch (Exception e) {
066    //            e.printStackTrace();
067    //        }
068        }
069    
070        private void leave(BaseVertex v) {
071    //        t.setMessage("leave:" + v.getId());
072    
073            VertexModel vv = ((VertexModel) v);
074            vv.setColor(4);
075            // vv.view.repaint();
076    
077            try {
078                Thread.sleep(100);
079    
080            } catch (Exception e) {
081                e.printStackTrace();
082            }
083        }
084    
085    }