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.event.GraphEvent;
007    import graphlab.graph.graph.SubGraph;
008    import graphlab.platform.core.AbstractAction;
009    import graphlab.platform.core.BlackBoard;
010    import graphlab.plugins.main.core.actions.VertexTransformer;
011    
012    /**
013     * @author Azin Azadi
014     */
015    public class ClearSelection extends AbstractAction {
016        /**
017         * indicates that wheter the last time GraphSelectPointData fired, it was originally cleared or not (it was cleared by this object)
018         * the initially use was for AddVertex, because it only adds vertices on graphselectpoints and if the selection was empty,
019         * so AddVertex and ClearSelection are both listening to GraphSelectPoint, so if
020         * ClearSelection fired before AddVertex it clears the selection and then AddVertex adds the Vertex
021         * but it shouldn't do that, because at the click time the selection wasn'nt clear
022         * and it is a mistake (bug) from users point of view,....
023         */
024        public static final String lastTimeGraphWasClear = "last time graph was cleared";
025    
026        /**
027         * constructor
028         *
029         * @param bb the blackboard of the action
030         */
031        public ClearSelection(BlackBoard bb) {
032            super(bb);
033    //        listen4Event(UI.getUIEvent("clear selection"));
034            listen4Event(GraphEvent.EVENT_KEY);
035            blackboard.setData(lastTimeGraphWasClear, false);
036        }
037    
038        SubGraph sd;
039    
040        public void performAction(String eventName, Object value) {
041            GraphEvent gpd = (GraphEvent) value;
042            if (gpd.eventType != GraphEvent.CLICKED) {
043                return;
044            }
045    
046            if (VertexTransformer.isPositionOnResizeBoxes(gpd.mousePos, blackboard))
047                return;
048    
049            sd = Select.getSelection(blackboard);
050            if (sd.vertices.size() != 0 || sd.edges.size() != 0) {
051                blackboard.setData(lastTimeGraphWasClear, false);
052                clearSelected(blackboard);
053            } else
054                blackboard.setData(lastTimeGraphWasClear, true);
055    
056        }
057    
058        public static void clearSelected(BlackBoard bb) {
059            bb.setData(Select.EVENT_KEY, new SubGraph());
060        }
061    }