1 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 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 202 PipedInputStream in = new PipedInputStream(); 203 final PipedOutputStream out = new PipedOutputStream( in ); 204 205 Thread writeWorker = new Thread ( 206 new Runnable () 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 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 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 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 |