1 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 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 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 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 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 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 buf = new StringBuffer ( ( int ) contents.getLength() ); 252 253 String 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 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 272 out.write( text ); 273 out.close(); 274 } 275 } 276 | Popular Tags |