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; 005 006 import graphlab.graph.atributeset.GraphAttrSet; 007 import graphlab.graph.graph.AbstractGraphRenderer; 008 import graphlab.graph.graph.GraphModel; 009 import graphlab.graph.graph.SubGraph; 010 import graphlab.platform.core.BlackBoard; 011 import graphlab.platform.plugin.PluginMethods; 012 import graphlab.plugins.main.saveload.image.Image; 013 import graphlab.plugins.main.saveload.matrix.CopyAsMatrix; 014 import graphlab.plugins.main.saveload.matrix.LoadMatrix; 015 import graphlab.plugins.main.saveload.matrix.Matrix; 016 import graphlab.plugins.main.saveload.matrix.SaveMatrix; 017 import org.xml.sax.SAXException; 018 019 import javax.xml.parsers.ParserConfigurationException; 020 import java.io.File; 021 import java.io.IOException; 022 023 /** 024 * @author azin azadi 025 026 */ 027 public class SaveLoadPluginMethods implements PluginMethods { 028 private BlackBoard blackboard; 029 030 public SaveLoadPluginMethods(BlackBoard blackboard) { 031 this.blackboard = blackboard; 032 } 033 034 //************************ G E N E R A L *********************************** 035 036 //************************ S A V E *********************************** 037 038 /** 039 * saves the current graph as matrix 040 * 041 * @see graphlab.plugins.main.saveload.matrix.SaveMatrix#saveMatrix(graphlab.graph.graph.GraphModel,java.io.File) 042 */ 043 public void saveAsMatrix(File file) throws IOException { 044 SaveMatrix.saveMatrix(getGraph(), file); 045 } 046 047 /** 048 * @see graphlab.plugins.main.saveload.matrix.SaveMatrix#saveMatrix(graphlab.graph.graph.GraphModel,java.io.File) 049 */ 050 public void saveAsMatrix(GraphModel g, File file) throws IOException { 051 SaveMatrix.saveMatrix(g, file); 052 } 053 054 public String matrix2String(GraphModel g) { 055 return (Matrix.Matrix2String(Matrix.graph2Matrix(g))); 056 } 057 058 059 /** 060 * saves the current graph as a (format) image. format e.g. jpeg, png, ... 061 */ 062 public void saveAsImage(File file, String format) { 063 format = format.replaceAll(".", ""); 064 Image.save(Image.Graph2Image((AbstractGraphRenderer) blackboard.getData(AbstractGraphRenderer.EVENT_KEY), getGraph()), file, format); 065 066 } 067 068 /** 069 * saves g in file as a (jpeg) image 070 */ 071 public void saveAsImage(GraphModel g, File file, String extension) { 072 graphlab.plugins.main.saveload.image.SaveImage.saveImage(g, file, extension); 073 } 074 075 076 /** 077 * @see graphlab.plugins.main.saveload.Save#saveGraphML(graphlab.graph.graph.GraphModel,java.io.File) 078 */ 079 public void saveAsGraphML(File file) throws IOException { 080 graphlab.plugins.main.saveload.Save.saveGraphML(getGraph(), file); 081 } 082 083 /** 084 * @see graphlab.plugins.main.saveload.Save#saveGraphML(graphlab.graph.graph.GraphModel,java.io.File) 085 */ 086 public void saveAsGraphML(GraphModel g, File f) throws IOException { 087 graphlab.plugins.main.saveload.Save.saveGraphML(g, f); 088 } 089 090 //************************ L O A D *********************************** 091 092 /** 093 * loads the matrix saved in file to the current graph 094 * 095 * @param file 096 */ 097 public GraphModel loadMatrix(File file) throws IOException { 098 return LoadMatrix.loadMatrix(file); 099 } 100 101 /** 102 * clears the current graph and load a graphml file saved in file to the current graph 103 */ 104 public GraphModel loadGraphML(File f) throws IOException, ParserConfigurationException, SAXException { 105 return graphlab.plugins.main.saveload.Load.loadGraphFromFile(f); 106 } 107 108 /** 109 * copies the Vertices and Edges as a graph to clipboard 110 * 111 * @param sd 112 */ 113 //todo: move this to ccp (Rousbeh) 114 public void copySelectedAsMatrix(SubGraph sd) { 115 CopyAsMatrix.copyAsMatrix(getGraph(), sd); 116 } 117 118 private GraphModel getGraph() { 119 GraphModel g = blackboard.getData(GraphAttrSet.name); 120 return g; 121 } 122 123 /** 124 * Return the extension portion of the file's name . 125 * 126 * @see #getExtension 127 * @see javax.swing.filechooser.FileFilter#accept 128 */ 129 public static String getExtension(File f) { 130 if (f != null) { 131 String filename = f.getName(); 132 int i = filename.lastIndexOf('.'); 133 if (i > 0 && i < filename.length() - 1) { 134 return filename.substring(i + 1).toLowerCase(); 135 } 136 ; 137 } 138 return null; 139 } 140 }