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.saveload.matrix;
005    
006    import graphlab.graph.atributeset.GraphAttrSet;
007    import graphlab.graph.graph.GraphModel;
008    import graphlab.graph.graph.SubGraph;
009    import graphlab.platform.core.AbstractAction;
010    import graphlab.platform.core.BlackBoard;
011    import graphlab.plugins.main.select.Select;
012    import graphlab.ui.UIUtils;
013    
014    import java.awt.*;
015    import java.awt.datatransfer.Clipboard;
016    import java.awt.datatransfer.StringSelection;
017    
018    /**
019     * @author Azin Azadi
020     */
021    public class CopyAsMatrix extends AbstractAction {
022        String event = UIUtils.getUIEventKey("copy as matrix");
023    
024        /**
025         * constructor
026         *
027         * @param bb the blackboard of the action
028         */
029        public CopyAsMatrix(BlackBoard bb) {
030            super(bb);
031            listen4Event(event);
032        }
033    
034        public void performAction(String eventName, Object value) {
035            SubGraph sd = Select.getSelection(blackboard);
036            copyAsMatrix(((GraphModel) blackboard.getData(GraphAttrSet.name)), sd);
037        }
038    
039        /**
040         * sd should be a subset of g
041         */
042        public static void copyAsMatrix(GraphModel gg, SubGraph sd) {
043            GraphModel g = new GraphModel(gg.isDirected());
044            moveToGraph(sd, g);
045            boolean[][] m = Matrix.graph2Matrix(g);
046            Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
047            String data = Matrix.Matrix2String(m);
048            StringSelection string = new StringSelection(data);
049            cb.setContents(string, string);
050    //        moveToGraph(sd, gg);
051        }
052    
053        private static void moveToGraph(SubGraph sd, GraphModel g) {
054            g.insertVertices(sd.vertices);
055            g.insertEdges(sd.edges);
056        }
057    
058    
059    }