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    
005    package graphlab.plugins.main.select;
006    
007    import graphlab.graph.graph.GraphPoint;
008    import graphlab.graph.graph.VertexModel;
009    import graphlab.plugins.commonplugin.undo.Undoable;
010    import graphlab.plugins.commonplugin.undo.UndoableActionOccuredData;
011    import graphlab.plugins.main.GraphData;
012    import graphlab.plugins.main.core.AlgorithmUtils;
013    import graphlab.plugins.main.extension.GraphActionExtension;
014    
015    import java.util.HashMap;
016    import java.util.HashSet;
017    import java.util.Map;
018    
019    /**
020     * @author Azin Azadi
021     */
022    public class ScaleOutSelection implements GraphActionExtension, Undoable {
023        public String getName() {
024            return "Scale Out Selection";
025        }
026    
027        public String getDescription() {
028            return "Shrinks the selection";
029        }
030    
031        public void action(GraphData gd) {
032            if (gd.select.isSelectionEmpty())
033                return;
034            HashSet<VertexModel> V = gd.select.getSelectedVertices();
035            //add undo data
036            UndoableActionOccuredData uaod = new UndoableActionOccuredData(this);
037            fillUndoPos(uaod.properties, gd, "old pos");
038    
039            GraphPoint center = AlgorithmUtils.getCenter(V);
040            for (VertexModel v : V) {
041                GraphPoint loc = v.getLocation();
042                double x = loc.x - center.x;
043                double y = loc.y - center.y;
044                setNewLocation(v, loc, x, y);
045            }
046            fillUndoPos(uaod.properties, gd, "new pos");
047    
048            gd.core.addUndoData(uaod);
049        }
050    
051        protected void setNewLocation(VertexModel v, GraphPoint loc, double x, double y) {
052            v.setLocation(new GraphPoint(loc.x - x / 1.25, loc.y - y / 1.25));
053        }
054    
055    
056        //---------------           U N D O           ----------------------
057        public void undo(UndoableActionOccuredData uaod) {
058            doUndoPos(uaod.properties, "new pos");
059        }
060    
061        public void redo(UndoableActionOccuredData uaod) {
062            doUndoPos(uaod.properties, "old pos");
063        }
064    
065        public static void fillUndoPos(HashMap<String, Object> properties, GraphData gd, String lbl) {
066            HashMap<VertexModel, GraphPoint> pos = new HashMap<VertexModel, GraphPoint>();
067            for (VertexModel v : gd.select.getSelectedVertices()) {
068                pos.put(v, v.getLocation());
069            }
070            properties.put("graph", gd.getGraph());
071            properties.put(lbl, pos);
072        }
073    
074        public static void doUndoPos(HashMap<String, Object> uaod, String lbl) {
075            HashMap<VertexModel, GraphPoint> pos = (HashMap<VertexModel, GraphPoint>) uaod.get(lbl);
076            for (Map.Entry<VertexModel, GraphPoint> x : pos.entrySet()) {
077                x.getKey().setLocation(x.getValue());
078            }
079        }
080    }