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.visualization.localsfvis;
005    
006    import graphlab.graph.atributeset.GraphAttrSet;
007    import graphlab.graph.graph.GraphModel;
008    import graphlab.graph.graph.GraphPoint;
009    import graphlab.graph.graph.VertexModel;
010    import graphlab.platform.core.AbstractAction;
011    import graphlab.platform.core.BlackBoard;
012    import graphlab.ui.UIUtils;
013    
014    import java.awt.geom.Rectangle2D;
015    
016    
017    /**
018     * @author houshmand hasannia
019     */
020    public class Random extends AbstractAction {
021        String event = UIUtils.getUIEventKey("Random");
022        GraphModel g;
023    
024        /**
025         * constructor
026         *
027         * @param bb the blackboard of the action
028         */
029        public Random(BlackBoard bb) {
030            super(bb);
031            listen4Event(event);
032        }
033    
034        /**
035         * like Action
036         *
037         * @param eventName
038         * @param value
039         */
040        public void performAction(String eventName, Object value) {
041            g = blackboard.getData(GraphAttrSet.name);
042            n = g.getVerticesCount();
043            VertexModel[] v = getVertices();
044            Rectangle2D.Double b = g.getZoomedBounds();
045            int w = (int) b.width;
046            int h = (int) b.height;
047            if (w < 5)
048                w = 150;
049            if (h < 5)
050                h = 150;
051            for (int i = 0; i < n; i++) {
052                double x = Math.random() * w;
053                double y = Math.random() * h;
054                v[i].setLocation(new GraphPoint(x, y));
055            }
056        }
057    
058        private VertexModel[] getVertices() {
059            VertexModel[] ret = new VertexModel[n];
060            int i = 0;
061            for (VertexModel vm : g) {
062                ret[i++] = vm;
063            }
064            return ret;
065        }
066    
067        int n;
068    }
069