1 4 package org.webdocwf.util.loader.wizard; 5 6 import java.awt.Toolkit ; 7 import java.awt.event.ActionEvent ; 8 import java.io.File ; 9 import java.io.FileOutputStream ; 10 import java.io.PrintStream ; 11 12 import javax.swing.AbstractAction ; 13 import javax.swing.ImageIcon ; 14 import javax.swing.JFileChooser ; 15 import javax.swing.JOptionPane ; 16 import javax.swing.KeyStroke ; 17 18 import org.webdocwf.util.loader.generator.Utils; 19 20 25 public class SaveProjectAction extends AbstractAction { 26 27 OctopusProjectFrame octopusProjectFrame; 28 29 public SaveProjectAction(OctopusProjectFrame octopusProjectFrame, String label) { 30 this.octopusProjectFrame = octopusProjectFrame; 31 putValue(NAME, label); 32 putValue( 33 SMALL_ICON, 34 new ImageIcon ( 35 getClass().getClassLoader().getResource( 36 "org/webdocwf/util/loader/" + "wizard/images/Save.gif"))); 37 putValue(SHORT_DESCRIPTION, "Save TDT project"); 38 putValue(LONG_DESCRIPTION, "Save Together Data Transformer project"); 39 putValue( 40 ACCELERATOR_KEY, 41 KeyStroke.getKeyStroke('S', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); 42 putValue(MNEMONIC_KEY, new Integer ('S')); 43 } 44 45 49 public void actionPerformed(ActionEvent e) { 50 try { 51 OctopusLoaderData loaderData = 52 octopusProjectFrame.getLoaderPanel().getOctopusLoaderInit(); 53 OctopusGeneratorData generatorData = 54 octopusProjectFrame.getGeneratorPanel().getOctopusGeneratorInit(); 55 56 String confString = "# Octopus project file. \n\n"; 57 confString += "ProjectName="+octopusProjectFrame.getProjectName(); 58 confString += "\n\n"; 59 confString += "# Loader section \n"; 60 confString += loaderData.toConfString(); 61 confString += "\n\n# Generator section \n"; 62 confString += generatorData.toConfString(); 63 64 confString = Utils.replaceAll(confString,"\\","\\\\"); 65 66 if( !this.octopusProjectFrame.getPathToOPF().equals("") ) { 67 this.saveFile( this.octopusProjectFrame.getPathToOPF(), confString); 68 return; 69 } 70 71 File current = new File (System.getProperty("user.dir")); 72 JFileChooser chooser = new JFileChooser (current); 73 chooser.setDialogTitle("Save Together Data Transformer project ..."); 74 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 75 chooser.setFileFilter( 76 (javax.swing.filechooser.FileFilter ) new OctopusFileFilter(OctopusFileFilter.OPF)); 77 chooser.setAcceptAllFileFilterUsed(false); 78 79 chooser.setSelectedFile(new File ("new_project.opf")); 80 chooser.setApproveButtonText("Save"); 81 int v = chooser.showOpenDialog(octopusProjectFrame); 82 83 switch (v) { 84 case JFileChooser.APPROVE_OPTION : 85 if (chooser.getSelectedFile() != null) { 86 this.octopusProjectFrame.setPathToOPF(chooser.getSelectedFile().getAbsolutePath()); 87 this.saveFile(chooser.getSelectedFile().getAbsolutePath(), confString); 88 } 89 break; 90 case JFileChooser.CANCEL_OPTION : 91 case JFileChooser.ERROR_OPTION : 92 chooser.removeAll(); 93 chooser = null; 94 current = null; 95 96 } 97 98 } catch (Exception ex) { 99 String message = "Error while save Together Data Transformer project. Message :" + ex.getMessage(); 100 JOptionPane.showMessageDialog(octopusProjectFrame, message + "\n", "Error", 0); 101 } 102 } 103 104 private boolean saveFile(String fileName, String text) throws Exception { 105 boolean success = true; 106 try { 107 File saveAsFile = new File (fileName); 108 FileOutputStream fos = new FileOutputStream (saveAsFile); 109 PrintStream printStream = new PrintStream (fos, true); 110 printStream.print(text); 111 fos.close(); 112 printStream.close(); 113 } catch (Exception e) { 114 success = false; 115 throw e; 116 } 117 return success; 118 } 119 } 120 | Popular Tags |