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.event.GraphEvent;
008    import graphlab.graph.graph.GraphModel;
009    import graphlab.graph.graph.GraphPoint;
010    import graphlab.graph.graph.SubGraph;
011    import graphlab.graph.graph.VertexModel;
012    import graphlab.platform.core.AbstractAction;
013    import graphlab.platform.core.BlackBoard;
014    import graphlab.platform.core.Listener;
015    import graphlab.plugins.commonplugin.undo.Undoable;
016    import graphlab.plugins.commonplugin.undo.UndoableActionOccuredData;
017    import graphlab.plugins.main.select.ClearSelection;
018    import graphlab.plugins.main.select.Select;
019    
020    import java.awt.*;
021    import java.awt.geom.Rectangle2D;
022    
023    /**
024     * Adds a vertex to the graph: listens for "graph select point" & ...
025     *
026     * @author azin azadi
027     */
028    
029    public class AddVertex extends AbstractAction implements Undoable {
030        public final static String DISABLE = "AddVertex.Disable";
031    
032        public AddVertex(BlackBoard bb) {
033            super(bb);
034            listen4Event(GraphEvent.EVENT_KEY);
035            blackboard.addListener(DISABLE, new Listener() {
036                public void performJob(String name) {
037                    disable = (Boolean) blackboard.getData(DISABLE);
038                    if (disable)
039                        disable();
040                    else
041                        enable();
042                }
043    
044                public void keyChanged(String key, Object value) {
045                    disable = (Boolean) blackboard.getData(DISABLE);
046                    if (disable)
047                        disable();
048                    else
049                        enable();
050                }
051    
052                public boolean isEnable() {
053                    return true;
054                }
055            });
056        }
057    
058        protected SubGraph sd;
059        boolean disable = false;
060    
061        public void performAction(String key, Object value) {
062            sd = Select.getSelection(blackboard);
063            GraphModel g = blackboard.getData(GraphAttrSet.name);
064            Boolean aBoolean = blackboard.getData(ClearSelection.lastTimeGraphWasClear);
065            if (aBoolean == null)
066                return;
067            boolean b = (Boolean) aBoolean;
068            if (b && (sd == null || (sd.vertices.size() == 0 && sd.edges.size() == 0))) {
069                GraphEvent gpd = blackboard.getData(GraphEvent.EVENT_KEY);
070                if (gpd.eventType != GraphEvent.CLICKED) {
071                    return;
072                }
073                GraphModel graph = gpd.graph;
074    
075                VertexModel v = doJob(graph, gpd.mousePos);
076    
077                UndoableActionOccuredData uaod = new UndoableActionOccuredData(this);
078                uaod.properties.put("AddedVertex", v);
079                uaod.properties.put("Graph", g);
080                blackboard.setData(UndoableActionOccuredData.EVENT_KEY, uaod);
081            }
082            blackboard.setData(ClearSelection.lastTimeGraphWasClear, true);
083    
084        }
085    
086        /**
087         * adds a vertex to the given location of graph
088         *
089         * @return the added vertex
090         */
091        public static VertexModel doJob(GraphModel g, int x, int y) {
092            VertexModel v = new VertexModel();
093            Point p = v.getCenter();
094            v.setLocation(new GraphPoint(x - p.x, y - p.y));
095            g.insertVertex(v);
096    
097            return v;
098        }
099    
100        /**
101         * adds a vertex to the given location of graph
102         *
103         * @return the added vertex
104         */
105        public static VertexModel doJob(GraphModel g, GraphPoint position) {
106            VertexModel v = new VertexModel();
107            v.setLocation(position);
108            g.insertVertex(v);
109            return v;
110        }
111    
112        /**
113         * adds a vertex to a random position of the graph
114         * return the added vertex
115         */
116        public static VertexModel addVertexToRandomPosition(GraphModel g) {
117            Rectangle2D.Double b = g.getZoomedBounds();
118            return doJob(g, (int) (b.getWidth() * Math.random()), (int) (b.getHeight() * Math.random()));
119        }
120    
121        public void undo(UndoableActionOccuredData uaod) {
122            VertexModel v = (VertexModel) uaod.properties.get("AddedVertex");
123            GraphModel g = (GraphModel) uaod.properties.get("Graph");
124            g.removeVertex(v);
125    
126        }
127    
128        public void redo(UndoableActionOccuredData uaod) {
129            VertexModel v = (VertexModel) uaod.properties.get("AddedVertex");
130            GraphModel g = (GraphModel) uaod.properties.get("Graph");
131            g.insertVertex(v);
132    
133        }
134    }