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.ccp;
005    
006    import graphlab.graph.atributeset.GraphAttrSet;
007    import graphlab.graph.graph.EdgeModel;
008    import graphlab.graph.graph.GraphModel;
009    import graphlab.graph.graph.SubGraph;
010    import graphlab.graph.graph.VertexModel;
011    import graphlab.graph.io.GraphML;
012    import graphlab.platform.core.AbstractAction;
013    import graphlab.platform.core.BlackBoard;
014    import graphlab.plugins.main.select.Select;
015    import graphlab.ui.UIUtils;
016    
017    import java.awt.*;
018    import java.awt.datatransfer.Clipboard;
019    import java.awt.datatransfer.StringSelection;
020    import java.util.HashMap;
021    
022    /**
023     * @author roozbeh
024     */
025    public class Copy extends AbstractAction {
026        public static final String event = UIUtils.getUIEventKey("Copy");
027    
028        public Copy(BlackBoard bb) {
029            super(bb);
030            this.listen4Event(event);
031        }
032    
033        public void performAction(String eventName, Object value) {
034            SubGraph sd = Select.getSelection(blackboard);
035            GraphModel gg = blackboard.getData(GraphAttrSet.name);
036            copy(sd);
037    //        sd.moveToGraph(gg);
038    
039            Paste.status = "Copy";
040        }
041    
042        /**
043         * copies the given subgraph as a (GraphML) String to clipboard
044         */
045        public static void copy(SubGraph subGraph) {
046            GraphModel g = new GraphModel();
047            copyGraph(subGraph, g);
048    //        gg.view.repaint();
049            Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
050            String data = (""
051                    + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
052                    + "<!DOCTYPE graphml SYSTEM \"graphml.dtd\">\n"
053                    + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n"
054                    + "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
055                    + "    xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\n"
056                    + "     http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n"
057                    + GraphML.graph2GraphML(g)
058                    + "</graphml>");
059    
060    
061            StringSelection string = new StringSelection(data);
062            cb.setContents(string, string);
063        }
064    
065        /**
066         * copies the sd to g
067         */
068        public static void copyGraph(SubGraph sd, GraphModel g) {
069            HashMap<VertexModel, VertexModel> map = new HashMap<VertexModel, VertexModel>();
070            for (VertexModel v1 : sd.vertices) {
071                VertexModel v = new VertexModel(v1);
072                map.put(v1, v);
073                g.insertVertex(v);
074    
075            }
076    
077            for (EdgeModel e1 : sd.edges) {
078                EdgeModel e = new EdgeModel(e1, map.get(e1.source), map.get(e1.target));
079    
080                g.insertEdge(e);
081            }
082    
083        }
084    
085    }