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.ClearSelection; 015 import graphlab.plugins.main.select.Select; 016 import graphlab.ui.UIUtils; 017 018 import java.awt.*; 019 import java.awt.datatransfer.Clipboard; 020 import java.awt.datatransfer.StringSelection; 021 import java.util.Collection; 022 import java.util.HashSet; 023 import java.util.Iterator; 024 025 /** 026 * @author Ruzbeh Ebrahimi 027 */ 028 public class Cut extends AbstractAction { 029 public static final String event = UIUtils.getUIEventKey("Cut"); 030 031 public Cut(BlackBoard bb) { 032 super(bb); 033 this.listen4Event(event); 034 } 035 036 public void performAction(String eventName, Object value) { 037 SubGraph sd = Select.getSelection(blackboard); 038 GraphModel gg = blackboard.getData(GraphAttrSet.name); 039 cut(sd, gg, blackboard); 040 Paste.status = "Cut"; 041 042 043 } 044 045 public static void cut(SubGraph sd, GraphModel gg, BlackBoard bb) { 046 GraphModel g = new GraphModel(gg.isDirected()); 047 moveToGraph(g, sd.edges, sd.vertices, gg); 048 049 050 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); 051 String data = ("" 052 + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 053 + "<!DOCTYPE graphml SYSTEM \"graphml.dtd\">\n" 054 + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n" 055 + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" 056 + " xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\n" 057 + " http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n" 058 + GraphML.graph2GraphML(g) 059 + "</graphml>"); 060 061 062 StringSelection string = new StringSelection(data); 063 cb.setContents(string, string); 064 ClearSelection.clearSelected(bb); 065 } 066 067 public static void moveToGraph(GraphModel g, Collection<EdgeModel> edges, Collection<VertexModel> vertices, GraphModel previousGraph) { 068 HashSet<EdgeModel> rightEdges = new HashSet<EdgeModel>(); 069 HashSet<EdgeModel> wrongEdges = new HashSet<EdgeModel>(); 070 Iterator<EdgeModel> iter = previousGraph.edgeIterator(); 071 removeInCompleteEdgesFromGraph(g, iter, vertices, wrongEdges); 072 removeInCompleteEdgesFromSelection(g, edges, vertices, rightEdges); 073 for (VertexModel v : vertices) 074 previousGraph.removeVertex(v); 075 g.insertVertices(vertices); 076 077 for (EdgeModel e : rightEdges) 078 g.insertEdge(e); 079 080 // previousGraph.view.repaint(); 081 082 } 083 084 private static void removeInCompleteEdgesFromSelection(GraphModel g, Collection<EdgeModel> edges, Collection<VertexModel> vertices, HashSet<EdgeModel> rightEdges) { 085 for (EdgeModel e : edges) { 086 087 if (vertices.contains(e.source) && vertices.contains(e.target)) { 088 rightEdges.add(e); 089 } else { 090 g.removeEdge(e); 091 } 092 } 093 } 094 095 private static void removeInCompleteEdgesFromGraph(GraphModel g, Iterator<EdgeModel> iter, Collection<VertexModel> vertices, HashSet<EdgeModel> wrongEdges) { 096 for (; iter.hasNext();) { 097 EdgeModel em = iter.next(); 098 099 if ((vertices.contains(em.source) && !vertices.contains(em.target)) || (vertices.contains(em.target) && !vertices.contains(em.source))) { 100 // wrongEdges.add(em); 101 //the power full edge iterator 102 g.removeEdge(em); 103 104 } 105 106 } 107 108 } 109 110 111 }