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.core.extension; 005 006 import graphlab.graph.atributeset.GraphAttrSet; 007 import graphlab.graph.graph.GraphModel; 008 import graphlab.graph.ui.GTabbedGraphPane; 009 import graphlab.platform.core.BlackBoard; 010 import graphlab.plugins.main.core.actions.StatusBarMessage; 011 import graphlab.plugins.main.saveload.ExampleFileFilter; 012 import graphlab.plugins.main.saveload.SaveLoadPluginMethods; 013 import graphlab.plugins.main.saveload.core.GraphIOException; 014 import graphlab.ui.AbstractExtensionAction; 015 016 import javax.swing.*; 017 import java.io.File; 018 import java.io.IOException; 019 020 /** 021 * @author azin azadi 022 023 */ 024 public class GraphWriterExtensionAction extends AbstractExtensionAction { 025 026 private GraphWriterExtension ge; 027 private String prefix; 028 029 public GraphWriterExtensionAction(BlackBoard bb, GraphWriterExtension ge) { 030 super(bb, ge); 031 this.ge = ge; 032 } 033 034 public String getParentMenuName() { 035 return "File.Save Graph To"; 036 } 037 038 public String getMenuNamePrefix() { 039 return ""; 040 } 041 042 public void performExtension() { 043 try { 044 if (ge != null) 045 exportGraph(); 046 } 047 catch (Exception e) { 048 e.printStackTrace(); 049 } 050 } 051 052 String ss; 053 GraphModel g; 054 IOException ee; 055 GraphIOException gioe; 056 057 private void exportGraph() throws IOException, GraphIOException { 058 g = ((GraphModel) (blackboard.getData(GraphAttrSet.name))); 059 JFileChooser fileChooser = new JFileChooser(); 060 ExampleFileFilter fileFilter = new ExampleFileFilter(ge.getExtension(), ge.getName()); 061 fileFilter.setDescription(ge.getDescription()); 062 fileChooser.setFileFilter(fileFilter); 063 if (GraphIOExtensionHandler.defaultFile != null) 064 fileChooser.setCurrentDirectory(new File(GraphIOExtensionHandler.defaultFile)); 065 066 int l = fileChooser.showSaveDialog(null); 067 if (l == JFileChooser.APPROVE_OPTION) { 068 ss = fileChooser.getSelectedFile().getAbsolutePath(); 069 if (!ge.getExtension().equalsIgnoreCase(SaveLoadPluginMethods.getExtension(fileChooser.getSelectedFile()))) 070 ss += "." + ge.getExtension(); 071 GraphIOExtensionHandler.defaultFile = ss; 072 if (!((new File(ss)).isFile()) 073 || JOptionPane.showConfirmDialog( 074 null, 075 "A file with name " 076 + ss 077 + " exists! Do you want to rewrite it?", 078 "GraphLab Saving ...", 079 JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { 080 081 GTabbedGraphPane.showNotificationMessage("Saving in the file ...", blackboard, true); 082 new Thread() { 083 public void run() { 084 try { 085 ge.write(new File(ss), g); 086 } catch (GraphIOException e) { 087 //todo: handle exceptions 088 gioe = e; 089 } 090 StatusBarMessage.setMessage(blackboard, ""); 091 GTabbedGraphPane.showTimeNotificationMessage("File Saved!", blackboard, 5000, true); 092 } 093 }.start(); 094 095 } 096 } 097 } 098 }