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.core.actions.vertex;
005    
006    import graphlab.graph.atributeset.GraphAttrSet;
007    import graphlab.graph.graph.EdgeModel;
008    import graphlab.graph.graph.GraphModel;
009    import graphlab.graph.graph.VertexModel;
010    import graphlab.platform.core.AbstractAction;
011    import graphlab.platform.core.BlackBoard;
012    import graphlab.plugins.commonplugin.undo.Undoable;
013    import graphlab.plugins.commonplugin.undo.UndoableActionOccuredData;
014    
015    import java.util.Iterator;
016    
017    /**
018     * Author: Ruzbeh Ebrahimi
019     */
020    public class DeleteVertex extends AbstractAction implements Undoable {
021        public DeleteVertex(BlackBoard bb) {
022            super(bb);
023            this.listen4Event(VertexSelectData.EVENT_KEY);
024        }
025    
026        public void performAction(String eventName, Object value) {
027            VertexSelectData vsd = blackboard.getData(VertexSelectData.EVENT_KEY);
028            GraphModel g = blackboard.getData(GraphAttrSet.name);
029            VertexModel v = vsd.v;
030            UndoableActionOccuredData uaod = new UndoableActionOccuredData(this);
031            uaod.properties.put("DeletedVertex", v);
032            uaod.properties.put("RelatedEdges", g.edgeIterator(v));
033            uaod.properties.put("Graph", g);
034            blackboard.setData(UndoableActionOccuredData.EVENT_KEY, uaod);
035            doJob(g, v);
036        }
037    
038        public static void doJob(GraphModel g, VertexModel v) {
039    
040            g.removeVertex(v);
041        }
042    
043        public void undo(UndoableActionOccuredData uaod) {
044            VertexModel v = (VertexModel) uaod.properties.get("DeletedVertex");
045            GraphModel g = (GraphModel) uaod.properties.get("Graph");
046            g.insertVertex(v);
047            Iterator<EdgeModel> iter = (Iterator<EdgeModel>) uaod.properties.get("RelatedEdges");
048            for (Iterator iter2 = iter; iter2.hasNext();) {
049                g.insertEdge(((EdgeModel) iter2.next()));
050            }
051    //        v.view.repaint();
052    
053        }
054    
055        public void redo(UndoableActionOccuredData uaod) {
056            VertexModel v = (VertexModel) uaod.properties.get("DeletedVertex");
057            GraphModel g = (GraphModel) uaod.properties.get("Graph");
058            DeleteVertex.doJob(g, v);
059    
060        }
061    }