KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > draw > 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.draw;
24
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.awt.geom.*;
28 import java.io.*;
29 import java.util.*;
30 import javax.swing.*;
31 import apollo.*;
32 import houston.*;
33
34 public class AppFrame extends JFrame
35 {
36    public final static String JavaDoc APP_TITLE = "Secure Draw";
37    private Canvas _canvas;
38    private SecureFileChooser _chooser;
39    private FileContents _contents;
40
41    public AppFrame()
42    {
43       super( APP_TITLE );
44
45       JMenuBar menuBar = new JMenuBar();
46
47       JMenu fileMenu = new JMenu( "File" );
48
49       JMenuItem newMi = new JMenuItem( "New" );
50       newMi.addActionListener(
51          new ActionListener()
52          {
53             public void actionPerformed( ActionEvent ev )
54             {
55                onNew();
56             }
57          } );
58       fileMenu.add( newMi );
59
60       JMenuItem openMi = new JMenuItem( "Open" );
61       openMi.addActionListener(
62          new ActionListener()
63          {
64             public void actionPerformed( ActionEvent ev )
65             {
66                onOpen();
67             }
68          } );
69       fileMenu.add( openMi );
70
71       JMenuItem saveMi = new JMenuItem( "Save" );
72       saveMi.addActionListener(
73          new ActionListener()
74          {
75             public void actionPerformed( ActionEvent ev )
76             {
77                onSave();
78             }
79          } );
80       fileMenu.add( saveMi );
81
82       JMenuItem printMi = new JMenuItem( "Print" );
83       printMi.addActionListener(
84          new ActionListener()
85          {
86             public void actionPerformed( ActionEvent ev )
87             {
88                onPrint();
89             }
90          } );
91       fileMenu.add( printMi );
92
93       JMenuItem exitMi = new JMenuItem( "Exit" );
94       exitMi.addActionListener(
95          new ActionListener()
96          {
97             public void actionPerformed( ActionEvent ev )
98             {
99                onExit();
100             }
101          } );
102       fileMenu.addSeparator();
103       fileMenu.add( exitMi );
104
105       JMenu editMenu = new JMenu( "Edit" );
106       JMenuItem copyMi = new JMenuItem( "Copy" );
107       copyMi.addActionListener(
108          new ActionListener()
109          {
110             public void actionPerformed( ActionEvent ev )
111             {
112                onCopy();
113             }
114          } );
115       editMenu.add( copyMi );
116
117       menuBar.add( fileMenu );
118       menuBar.add( editMenu );
119
120       setJMenuBar( menuBar );
121
122       _canvas = new Canvas();
123
124       getContentPane().setLayout( new BorderLayout() );
125       getContentPane().add( _canvas, BorderLayout.CENTER );
126
127       _chooser = new SecureFileChooser();
128
129       setSize( 400, 400 );
130
131       updateTitle();
132
133       addWindowListener(
134          new WindowAdapter()
135          {
136             public void windowClosing( WindowEvent e )
137             {
138                onExit();
139             }
140          } );
141    }
142
143    public void onCopy()
144    {
145       Image image = _canvas.getImage();
146       TransferableImage trans = new TransferableImage( image );
147
148       SecureClipboard.setContents( trans );
149    }
150
151    public void onExit()
152    {
153       setVisible( false );
154       dispose();
155       System.exit( 0 );
156    }
157
158    public void onNew()
159    {
160       _contents = null;
161       _canvas.clear();
162       updateTitle();
163    }
164
165    public void onOpen()
166    {
167       try
168       {
169          if( _chooser.showOpenDialog() == SecureFileChooser.CANCEL )
170             return;
171
172          _contents = _chooser.getFileContents();
173          _canvas.clear();
174          open( _contents.getInputStream() );
175          _canvas.repaint();
176          updateTitle();
177       }
178       catch( IOException ioex )
179       {
180          Status.error( "*** failed to open drawing: " + ioex.toString() );
181       }
182
183    }
184
185
186    public void onPrint()
187    {
188       PrintService ps = ServiceManager.lookupPrintService();
189       boolean ok = ps.print( _canvas );
190       if( !ok )
191          Status.error( "*** failed to print drawing" );
192    }
193
194    public void onSave()
195    {
196       try
197       {
198          if( _contents == null )
199          {
200             // pop up a dialog
201

202             PipedInputStream in = new PipedInputStream();
203             final PipedOutputStream out = new PipedOutputStream( in );
204
205             Thread JavaDoc writeWorker = new Thread JavaDoc(
206                new Runnable JavaDoc()
207                {
208                   public void run()
209                   {
210                      try
211                      {
212                         save( out );
213                      }
214                      catch( IOException ioex )
215                      {
216                         Status.error( "*** failed to save drawing: " + ioex.toString() );
217                      }
218                      finally
219                      {
220                         try
221                         {
222                            out.close();
223                         }
224                         catch( IOException ioex )
225                         {}
226                      }
227                   }
228                } );
229             writeWorker.start();
230
231             if( _chooser.showSaveDialog( in ) == SecureFileChooser.CANCEL )
232                return;
233
234             _contents = _chooser.getFileContents();
235             updateTitle();
236          }
237          else
238          {
239             OutputStream out = _contents.getOutputStream( true );
240             /*
241              * overwrite:=true
242              */

243             save( out );
244             out.flush();
245             out.close();
246          }
247       }
248       catch( IOException ioex )
249       {
250          Status.error( "*** failed to save drawing: " + ioex.toString() );
251       }
252    }
253
254    private void open( InputStream in ) throws IOException
255    {
256       open( new DataInputStream( in ) );
257    }
258
259    private void open( DataInputStream in ) throws IOException
260    {
261       java.util.List JavaDoc lines = _canvas.getLines();
262
263       int lineCount = in.readInt();
264
265       for( int i = 0; i < lineCount; i++ )
266       {
267          double x1 = in.readDouble();
268          double y1 = in.readDouble();
269          double x2 = in.readDouble();
270          double y2 = in.readDouble();
271
272          Line2D.Double line = new Line2D.Double( x1, y1, x2, y2 );
273          lines.add( line );
274       }
275    }
276
277    private void save( OutputStream out ) throws IOException
278    {
279       save( new DataOutputStream( out ) );
280    }
281
282    private void save( DataOutputStream out ) throws IOException
283    {
284       java.util.List JavaDoc lines = _canvas.getLines();
285
286       int lineCount = lines.size();
287       out.writeInt( lineCount );
288
289       for( int i = 0; i < lineCount; i++ )
290       {
291          Line2D line = ( Line2D ) lines.get( i );
292          out.writeDouble( line.getX1() );
293          out.writeDouble( line.getY1() );
294          out.writeDouble( line.getX2() );
295          out.writeDouble( line.getY2() );
296       }
297    }
298
299    private void updateTitle()
300    {
301       if( _contents == null )
302          setTitle( "Untitled - " + APP_TITLE );
303       else
304       {
305          try
306          {
307             setTitle( _contents.getName() + " (" + _contents.getLength() + "/"
308                    + _contents.getMaxLength() + ") - " + APP_TITLE );
309          }
310          catch( IOException ioex )
311          {
312             Status.error( "*** failed to retrieve document name: " + ioex.toString() );
313          }
314       }
315    }
316 }
317
Popular Tags