KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > notepad > AppFrame


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 demo.notepad;
24
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.io.*;
28 import java.net.*;
29 import javax.swing.*;
30 import apollo.*;
31 import houston.*;
32
33 public class AppFrame extends JFrame
34 {
35
36    private final static String JavaDoc APP_TITLE = "Secure Notepad";
37    SecureFileChooser _chooser;
38    FileContents _contents;
39    JTextArea _editor;
40
41    public AppFrame()
42    {
43       super( APP_TITLE );
44
45       JButton newBt = new JButton( "New" );
46       JButton openBt = new JButton( "Open" );
47       JButton saveBt = new JButton( "Save" );
48       JButton saveAsBt = new JButton( "Save As" );
49       JButton copyBt = new JButton( "Copy" );
50       JButton pasteBt = new JButton( "Paste" );
51
52       newBt.addActionListener(
53          new ActionListener()
54          {
55             public void actionPerformed( ActionEvent ev )
56             {
57                onNew();
58             }
59          } );
60
61       openBt.addActionListener(
62          new ActionListener()
63          {
64             public void actionPerformed( ActionEvent ev )
65             {
66                onOpen();
67             }
68          } );
69
70       saveBt.addActionListener(
71          new ActionListener()
72          {
73             public void actionPerformed( ActionEvent ev )
74             {
75                onSave();
76             }
77          } );
78
79       saveAsBt.addActionListener(
80          new ActionListener()
81          {
82             public void actionPerformed( ActionEvent ev )
83             {
84                onSaveAs();
85             }
86          } );
87
88       copyBt.addActionListener(
89          new ActionListener()
90          {
91             public void actionPerformed( ActionEvent ev )
92             {
93                onCopy();
94             }
95          } );
96
97       pasteBt.addActionListener(
98          new ActionListener()
99          {
100             public void actionPerformed( ActionEvent ev )
101             {
102                onPaste();
103             }
104          } );
105
106       JPanel buttonPanel = new JPanel( new FlowLayout() );
107       buttonPanel.add( newBt );
108       buttonPanel.add( openBt );
109       buttonPanel.add( saveBt );
110       buttonPanel.add( saveAsBt );
111       buttonPanel.add( new JSeparator() );
112       buttonPanel.add( copyBt );
113       buttonPanel.add( pasteBt );
114
115       _chooser = new SecureFileChooser();
116
117       _editor = new JTextArea();
118       _editor.setBackground( Color.yellow );
119       _editor.setFont( new Font( "sansserif", Font.PLAIN, 22 ) );
120
121       getContentPane().add( buttonPanel, BorderLayout.NORTH );
122       getContentPane().add( new JScrollPane( _editor ), BorderLayout.CENTER );
123
124       setSize( 400, 400 );
125
126       onNew();
127
128       addWindowListener(
129          new WindowAdapter()
130          {
131             public void windowClosing( WindowEvent e )
132             {
133                onExit();
134             }
135          } );
136    }
137
138    public void onCopy()
139    {
140       String JavaDoc text = _editor.getSelectedText();
141       SecureClipboard.copyText( text );
142    }
143
144    public void onExit()
145    {
146       System.exit( 0 );
147    }
148
149    public void onNew()
150    {
151       _contents = null;
152       _editor.setText( "" );
153       updateTitle();
154    }
155
156    public void onOpen()
157    {
158       try
159       {
160          if( _chooser.showOpenDialog() == SecureFileChooser.CANCEL )
161             return;
162
163          _contents = _chooser.getFileContents();
164          _editor.setText( loadText( _contents ) );
165          updateTitle();
166       }
167       catch( IOException ioex )
168       {
169          Status.error( "*** failed to open document: " + ioex.toString() );
170       }
171    }
172
173    public void onPaste()
174    {
175       String JavaDoc text = SecureClipboard.pasteText();
176       if( !text.equals( "" ) )
177          _editor.replaceSelection( text );
178    }
179
180    public void onSave()
181    {
182       try
183       {
184          if( _contents == null )
185          {
186             // pop up a dialog
187

188             InputStream in = new StringBufferInputStream( _editor.getText() );
189
190             if( _chooser.showSaveDialog( in ) == SecureFileChooser.CANCEL )
191                return;
192
193             _contents = _chooser.getFileContents();
194             updateTitle();
195          }
196          else
197             saveText( _contents, _editor.getText() );
198       }
199       catch( IOException ioex )
200       {
201          Status.error( "*** failed to save document: " + ioex.toString() );
202       }
203    }
204
205    public void onSaveAs()
206    {
207       if( _contents == null )
208          onSave();
209       else
210       {
211          try
212          {
213             if( _chooser.showSaveAsDialog( _contents ) == SecureFileChooser.CANCEL )
214                return;
215
216             _contents = _chooser.getFileContents();
217             updateTitle();
218          }
219          catch( IOException ioex )
220          {
221             Status.error( "*** failed to save document: " + ioex.toString() );
222          }
223       }
224    }
225
226    public void updateTitle()
227    {
228       if( _contents == null )
229          setTitle( "Untitled - " + APP_TITLE );
230       else
231       {
232          try
233          {
234             setTitle( _contents.getName() + " (" + _contents.getLength() + "/"
235                    + _contents.getMaxLength() + ") - " + APP_TITLE );
236          }
237          catch( IOException ioex )
238          {
239             Status.error( "*** failed to retrieve document name: " + ioex.toString() );
240          }
241       }
242    }
243
244    private static String JavaDoc loadText( FileContents contents ) throws IOException
245    {
246       if( contents == null )
247          return "";
248
249       BufferedReader in = new BufferedReader(
250             new InputStreamReader( contents.getInputStream() ) );
251       StringBuffer JavaDoc buf = new StringBuffer JavaDoc( ( int ) contents.getLength() );
252
253       String JavaDoc line;
254       while( ( line = in.readLine() ) != null )
255          buf.append( line + "\n" );
256
257       in.close();
258       return buf.toString();
259    }
260
261    private static void saveText( FileContents contents, String JavaDoc text ) throws IOException
262    {
263       int bytesRequired = text.length() * 2;
264       if( bytesRequired > contents.getMaxLength() )
265          contents.setMaxLength( bytesRequired );
266
267       BufferedWriter out = new BufferedWriter(
268             new OutputStreamWriter( contents.getOutputStream( true ) ) );
269       /*
270        * overwrite:=true
271        */

272       out.write( text );
273       out.close();
274    }
275 }
276
Popular Tags