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.event.GraphEvent;
008    import graphlab.graph.graph.EdgeModel;
009    import graphlab.graph.graph.GraphModel;
010    import graphlab.graph.graph.SubGraph;
011    import graphlab.graph.graph.VertexModel;
012    import graphlab.graph.ui.GTabbedGraphPane;
013    import graphlab.graph.ui.GraphRectRegionSelect;
014    import graphlab.platform.core.AbstractAction;
015    import graphlab.platform.core.BlackBoard;
016    import graphlab.plugins.main.saveload.xmlparser.GraphmlHandlerImpl;
017    import graphlab.plugins.main.saveload.xmlparser.GraphmlParser;
018    import graphlab.plugins.main.select.Select;
019    import graphlab.ui.UIUtils;
020    import org.xml.sax.InputSource;
021    import org.xml.sax.SAXParseException;
022    
023    import java.awt.*;
024    import java.awt.datatransfer.*;
025    import java.io.ByteArrayInputStream;
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.util.HashSet;
029    import java.util.Iterator;
030    
031    /**
032     * @author rouzbeh
033     */
034    public class Paste extends AbstractAction {
035        public static final String event = UIUtils.getUIEventKey("Paste");
036        public static String status = "";
037        GraphRectRegionSelect graphRectRegionSelector = new GraphRectRegionSelect(blackboard) {
038    
039            public void onMouseMoved(GraphEvent data) {
040    //            _onMouseMoved(data);
041            }
042    
043            public void onDrop(GraphEvent data) {
044                _onDrop(data);
045            }
046        };
047    
048        public Paste(BlackBoard bb) {
049            super(bb);
050            this.listen4Event(event);
051        }
052    
053        public void performAction(String eventName, Object value) {
054            GTabbedGraphPane.showNotificationMessage("Select The Paste Region", blackboard, true);
055            graphRectRegionSelector.startSelectingRegion();
056        }
057    
058        void _onDrop(GraphEvent data) {
059            GTabbedGraphPane.showNotificationMessage("", blackboard, true);
060            GraphModel gg = new GraphModel();
061            GraphModel g = blackboard.getData(GraphAttrSet.name);
062            Toolkit tk = Toolkit.getDefaultToolkit();
063            Clipboard cb = tk.getSystemClipboard();
064            Transferable content = cb.getContents(this);
065    
066            if (content == null) {
067                //          nothing to paste
068                tk.beep();
069                return;
070            }
071    
072    //      we only accept string or plain text data
073            if (content.isDataFlavorSupported(DataFlavor.stringFlavor) ||
074                    content.isDataFlavorSupported(DataFlavor.getTextPlainUnicodeFlavor())) {
075                String strData = null;
076                InputStream stream = null;
077                try {
078                    //              representation class is a String, so use that
079                    strData = (String) content.getTransferData(DataFlavor.stringFlavor);
080                } catch (Exception e1) {
081                    try {
082                        //                  representation class is an input stream, so
083                        //                  leave the strData variable as null to be checked
084                        //                  by the code below
085                        stream = (InputStream) content.getTransferData(DataFlavor.getTextPlainUnicodeFlavor());
086                    } catch (Exception e2) {
087                        //                  it was something we could handle but it didn't
088                        //                   want to be retrieved, too bad
089                        tk.beep();
090    //                    return;
091                    }
092                }
093    
094    
095                if (strData != null) {
096                    //              data was a string, create a byte array and a
097                    //              byte array input stream and read it
098                    byte[] bytes = strData.getBytes();
099                    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
100                    try {
101                        GraphmlHandlerImpl gi = new GraphmlHandlerImpl(gg);
102                        GraphmlParser.parse(new InputSource(bais), gi);
103                        //                    PasteHandlerImpl ghi = new PasteHandlerImpl(gg);
104                        //                    PasteParser.parse(new InputSource(bais), ghi);
105                        bais.close();
106    
107                    } catch (Exception e) {
108                        if (e.getClass().equals(SAXParseException.class)) {
109                            tk.beep();
110                            e.printStackTrace();
111                        } else {
112                            e.printStackTrace();
113                        }
114                    }
115                } else if (stream != null) {
116                    //                  data was an input stream, read that directly
117                    try {
118                        GraphmlHandlerImpl phi = new GraphmlHandlerImpl(gg);
119                        GraphmlParser.parse(new InputSource(stream), phi);
120                        stream.close();
121                    } catch (Exception e) {
122    
123                    }
124                }
125    
126    
127            } else {
128                tk.beep();
129            }
130    //        Point p;
131    //        //adds the pasted graph from temp graph to main graph
132    //        if(blackboard.get(GraphClickData.name) != null){
133    //            GraphClickData gcd = blackboard.get(GraphClickData.name);
134    //            p = gcd.me.getPoint();
135    //
136    //        }
137    //        else{
138    //            p=new Point(200,200);
139    //        }
140            HashSet<VertexModel> toBeSelectedVertices = new HashSet<VertexModel>();
141            HashSet<EdgeModel> toBeSelectedEdges = new HashSet<EdgeModel>();
142    
143            for (VertexModel vm : gg) {
144                toBeSelectedVertices.add(vm);
145            }
146            for (Iterator<EdgeModel> em = gg.edgeIterator(); em.hasNext();) {
147                toBeSelectedEdges.add(em.next());
148            }
149            g.addSubGraph(gg, graphRectRegionSelector.getCurrentRect().getBounds());
150    //        ClearSelection.clearSelected(gg.blackboard);
151            //        ClearSelection.clearSelected(g.blackboard);
152            //selects the inserted edges & vertices
153            SubGraph sd = new SubGraph();
154            for (VertexModel v : toBeSelectedVertices) {
155                sd.vertices.add(v);
156            }
157            for (EdgeModel e : toBeSelectedEdges) {
158                sd.edges.add(e);
159            }
160            Select.setSelection(blackboard, sd);
161    
162    //        if the prev. operation was cut, the clipboard should be cleand
163            if (status.equalsIgnoreCase("cut")) {
164                String _data = "";
165                StringSelection string = new StringSelection(_data);
166                cb.setContents(string, string);
167                try {
168                    cb.getContents(this).getTransferData(DataFlavor.stringFlavor).toString();
169                } catch (UnsupportedFlavorException e) {
170                    e.printStackTrace();
171                } catch (IOException e) {
172                    e.printStackTrace();
173                }
174            }
175        }
176    }