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.main.select;
005    
006    import graphlab.graph.graph.EdgeModel;
007    import graphlab.graph.graph.SubGraph;
008    import graphlab.graph.graph.VertexModel;
009    import graphlab.platform.core.AbstractAction;
010    import graphlab.platform.core.BlackBoard;
011    
012    import java.util.Vector;
013    
014    /**
015     * this class is do the update of the graph after the selection changes, it sets the selection of vertices and edges, and
016     * repaints the graph if necessary
017     *
018     * @author Azin Azadi
019     */
020    public class SelectUpdater extends AbstractAction {
021        /**
022         * constructor
023         *
024         * @param bb the blackboard of the action
025         */
026        public SelectUpdater(BlackBoard bb) {
027            super(bb);
028            listen4Event(Select.EVENT_KEY);
029        }
030    
031        SubGraph last = new SubGraph();
032    
033        public void performAction(String eventName, Object value) {
034            SubGraph sd = Select.getSelection(blackboard);
035    //        for (Vertex v:sd.vertices)
036    //        for (Vertex v : sd.vertices)
037    //            System.out.print(v.getID());
038    //        System.out.println("");
039            if (sd == null)
040                sd = new SubGraph();
041            for (VertexModel v : sd.vertices)
042                if (!last.vertices.contains(v)) {
043                    select(v);
044                    last.vertices.add(v);
045                }
046            Vector<VertexModel> rm = new Vector<VertexModel>();
047            for (VertexModel v : last.vertices)
048                if (!sd.vertices.contains(v)) {
049                    deselect(v);
050                    rm.add(v);
051    //                last.vertices.remove(v);
052                }
053            last.vertices.removeAll(rm);
054            for (EdgeModel e : sd.edges)
055                if (!last.edges.contains(e)) {
056                    select(e);
057                    last.edges.add(e);
058                }
059            Vector<EdgeModel> rme = new Vector<EdgeModel>();
060            for (EdgeModel e : last.edges)
061                if (!sd.edges.contains(e)) {
062                    deselect(e);
063                    rme.add(e);
064                }
065            last.edges.removeAll(rme);
066            //last=sd;
067            //last.vertices=(HashSet<Vertex>) sd.vertices.clone();
068            //last.edges=(HashSet<Edge>) sd.edges.clone();
069        }
070    
071        private void deselect(EdgeModel e) {
072            e.setSelected(false);
073        }
074    
075        private void select(EdgeModel e) {
076            e.setSelected(true);
077        }
078    
079        private void deselect(VertexModel v) {
080            v.setSelected(false);
081        }
082    
083        private void select(VertexModel v) {
084            v.setSelected(true);
085        }
086    }