KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > apollo > dev > DevFileSaveService


1 /*
2 ** Apollo - Test Skeleton Toolkit for Web Start/JNLP
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package apollo.dev;
24
25 import java.io.*;
26 import javax.swing.JFileChooser JavaDoc;
27 import apollo.*;
28
29 public class DevFileSaveService implements FileSaveService
30 {
31    JFileChooser JavaDoc _chooser;
32
33    public DevFileSaveService()
34    {
35       _chooser = new JFileChooser JavaDoc();
36    }
37
38    public FileContents saveAsFileDialog( String JavaDoc pathHint, String JavaDoc 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 JavaDoc pathHint, String JavaDoc exts[], InputStream in, String JavaDoc 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 JavaDoc ex )
81          {
82          }
83       }
84
85       return new DevFileContents( file );
86    }
87 }
88
Popular Tags