1 22 23 package apollo.dev; 24 25 import java.io.*; 26 import javax.swing.JFileChooser ; 27 import apollo.*; 28 29 public class DevFileSaveService implements FileSaveService 30 { 31 JFileChooser _chooser; 32 33 public DevFileSaveService() 34 { 35 _chooser = new JFileChooser (); 36 } 37 38 public FileContents saveAsFileDialog( String pathHint, String exts[], FileContents contents ) 39 throws IOException 40 { 41 if( pathHint != null ) 42 _chooser.setCurrentDirectory( new File( pathHint ) ); 43 44 if( _chooser.showSaveDialog( null ) == JFileChooser.CANCEL_OPTION ) 45 return null; 46 else 47 return save( _chooser.getSelectedFile(), contents.getInputStream() ); 48 } 49 50 public FileContents saveFileDialog( String pathHint, String exts[], InputStream in, String name ) 51 throws IOException 52 { 53 if( pathHint != null ) 54 _chooser.setCurrentDirectory( new File( pathHint ) ); 55 56 if( _chooser.showSaveDialog( null ) == JFileChooser.CANCEL_OPTION ) 57 return null; 58 else 59 return save( _chooser.getSelectedFile(), in ); 60 } 61 62 private FileContents save( File file, InputStream in ) throws IOException 63 { 64 byte buffer[] = new byte[1024]; 65 try 66 { 67 FileOutputStream out = new FileOutputStream( file ); 68 int bytes_read; 69 while( ( bytes_read = in.read( buffer ) ) != -1 ) 70 out.write( buffer, 0, bytes_read ); 71 72 } 73 finally 74 { 75 try 76 { 77 if( in != null ) 78 in.close(); 79 } 80 catch( Exception ex ) 81 { 82 } 83 } 84 85 return new DevFileContents( file ); 86 } 87 } 88 | Popular Tags |