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.graph.EdgeModel;
007    import graphlab.graph.graph.SubGraph;
008    import graphlab.graph.graph.VertexModel;
009    import graphlab.platform.core.AbstractAction;
010    import graphlab.platform.core.BlackBoard;
011    import graphlab.plugins.main.core.actions.edge.EdgeSelectData;
012    import graphlab.plugins.main.core.actions.vertex.VertexSelectData;
013    
014    import java.awt.*;
015    import java.awt.event.KeyEvent;
016    
017    /**
018     * Author: Azin Azadi
019     */
020    public class Select extends AbstractAction {
021        private boolean deSelectOlderSelections = true;
022        private boolean invertOlderSelections;
023        public static final String EVENT_KEY = "graph selected V and E";
024    
025        /**
026         * constructor
027         *
028         * @param bb the blackboard of the action
029         */
030        public Select(BlackBoard bb) {
031            super(bb);
032            listen4Event(VertexSelectData.EVENT_KEY);
033            listen4Event(EdgeSelectData.EVENT_KEY);
034            blackboard.setData(EVENT_KEY, new SubGraph());
035            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(new KeyEventPostProcessor() {
036                public boolean postProcessKeyEvent(KeyEvent e) {
037                    deSelectOlderSelections = true;
038                    invertOlderSelections = false;
039                    if (e.isControlDown())
040                        deSelectOlderSelections = false;
041                    if (e.isShiftDown())
042                        invertOlderSelections = true; //not yet implemented //todo: implement
043                    return false;
044                }
045            });
046        }
047    
048        public void performAction(String eventName, Object value) {
049            if (eventName == VertexSelectData.EVENT_KEY) {
050                selectVertex();
051    
052            }
053            if (eventName == EdgeSelectData.EVENT_KEY)
054                selectEdge();
055        }
056    
057        public static SubGraph getSelection(BlackBoard blackboard) {
058            return blackboard.getData(EVENT_KEY);
059        }
060    
061        public static void setSelection(BlackBoard blackboard, SubGraph sg) {
062            blackboard.setData(EVENT_KEY, sg);
063        }
064    
065        private void selectEdge() {
066            EdgeSelectData esd = blackboard.getData(EdgeSelectData.EVENT_KEY);
067            SubGraph sd = getSelection(blackboard);
068            EdgeModel e = esd.edge;
069            if (deSelectOlderSelections) {
070                sd.edges.clear();
071                sd.vertices.clear();
072                sd.edges.add(e);
073            } else {
074                if (sd.edges.contains(e)) {
075                    sd.edges.remove(e);
076                } else
077                    sd.edges.add(e);
078            }
079            blackboard.setData(EVENT_KEY, sd);
080        }
081    
082        private void selectVertex() {
083            VertexSelectData vsd = blackboard.getData(VertexSelectData.EVENT_KEY);
084            SubGraph sd = getSelection(blackboard);
085            VertexModel v = vsd.v;
086            if (deSelectOlderSelections) {
087                sd.edges.clear();
088                sd.vertices.clear();
089                sd.vertices.add(v);
090            } else {
091                if (sd.vertices.contains(v)) {
092                    sd.vertices.remove(v);
093                } else
094                    sd.vertices.add(v);
095            }
096    
097            blackboard.setData(EVENT_KEY, sd);
098        }
099    }