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.atributeset.GraphAttrSet;
007    import graphlab.graph.graph.EdgeModel;
008    import graphlab.graph.graph.GraphModel;
009    import graphlab.graph.graph.SubGraph;
010    import graphlab.graph.graph.VertexModel;
011    import graphlab.platform.core.AbstractAction;
012    import graphlab.platform.core.BlackBoard;
013    import graphlab.ui.UIUtils;
014    
015    import java.util.Iterator;
016    
017    /**
018     * @author Rouzbeh Ebrahimi
019     */
020    public class InvertSelection extends AbstractAction {
021        public static final String event = UIUtils.getUIEventKey("InvertSelection");
022    
023        public InvertSelection(BlackBoard bb) {
024            super(bb);
025            listen4Event(event);
026            blackboard.setData(Select.EVENT_KEY, new SubGraph());
027    
028        }
029    
030        public void performAction(String eventName, Object value) {
031            GraphModel g = ((GraphModel) (blackboard.getData(GraphAttrSet.name)));
032            SubGraph sd = getSelection();
033            Iterator<VertexModel> vertices = g.iterator();
034            Iterator<EdgeModel> edges = g.lightEdgeIterator();
035            for (; vertices.hasNext();) {
036    
037                VertexModel vertex = vertices.next();
038                if (sd.vertices.contains(vertex)) {
039                    sd.vertices.remove(vertex);
040                } else
041                    sd.vertices.add(vertex);
042    
043            }
044            for (; edges.hasNext();) {
045    
046                EdgeModel edge = edges.next();
047                if (sd.edges.contains(edge)) {
048                    sd.edges.remove(edge);
049                } else {
050                    sd.edges.add(edge);
051                }
052    
053            }
054            Select.setSelection(blackboard, sd);
055        }
056    
057        public SubGraph getSelection() {
058            return Select.getSelection(blackboard);
059        }
060    }