1 32 33 package com.jeantessier.dependencyfinder.gui; 34 35 import java.awt.event.*; 36 import java.io.*; 37 38 import javax.swing.*; 39 40 public class SaveFileAction extends AbstractAction implements Runnable { 41 private DependencyFinder model; 42 private String encoding; 43 private String dtdPrefix; 44 45 private String indentText; 46 private File file; 47 48 public SaveFileAction(DependencyFinder model, String encoding, String dtdPrefix) { 49 this.model = model; 50 this.encoding = encoding; 51 this.dtdPrefix = dtdPrefix; 52 53 putValue(Action.LONG_DESCRIPTION, "Save current graph to XML file"); 54 putValue(Action.NAME, "Save"); 55 putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("icons/save.gif"))); 56 } 57 58 public String getIndentText() { 59 return indentText; 60 } 61 62 public void setIndentText(String indentText) { 63 this.indentText = indentText; 64 } 65 66 public void actionPerformed(ActionEvent e) { 67 JFileChooser chooser = new JFileChooser(); 68 chooser.addChoosableFileFilter(new XMLFileFilter()); 69 int returnValue = chooser.showSaveDialog(model); 70 if (returnValue == JFileChooser.APPROVE_OPTION) { 71 file = chooser.getSelectedFile(); 72 new Thread (this).start(); 73 } 74 } 75 76 public void run() { 77 try { 78 model.getStatusLine().showInfo("Saving " + file.getName() + " ..."); 79 80 PrintWriter out = new PrintWriter(new FileWriter(file)); 81 com.jeantessier.dependency.Printer printer = new com.jeantessier.dependency.XMLPrinter(out, encoding, dtdPrefix); 82 if (indentText != null) { 83 printer.setIndentText(indentText); 84 } 85 86 printer.traverseNodes(model.getPackages()); 87 88 out.close(); 89 90 model.getStatusLine().showInfo("Saved " + file.getName()); 91 } catch (IOException ex) { 92 model.getStatusLine().showError("Cannot save: " + ex.getClass().getName() + ": " + ex.getMessage()); 93 } 94 } 95 } 96 | Popular Tags |