1 /* 2 * File: Netscape.java 3 * 4 * This applet/application demonstrates the following classes: 5 * TextArea, Frame, MenuBar, Menu, MenuItem, CheckboxMenuItem, 6 * MenuShortcut, FileDialog, BufferedReader, and FileReader. 7 * 8 * Note that MenuItems generate ActionEvents, while CheckboxMenuItems 9 * generate ItemEvents. 10 * 11 * Copyright: Northeast Parallel Architectures Center 12 * 13 */ 14 15 import java.applet.Applet; 16 import java.awt.*; 17 import java.awt.event.*; 18 import java.io.*; 19 20 public class Netscape extends Applet implements ActionListener { 21 22 private Button button; 23 24 public void init() { 25 button = new Button( "New Web Browser" ); 26 add( button ); 27 button.addActionListener( this ); 28 } 29 30 public void actionPerformed( ActionEvent e ) { 31 new NewWebBrowser(); 32 } 33 34 public static void main( String[] args ) { 35 new NewWebBrowser(); 36 } 37 38 } 39 40 // Simulate a Netscape web browser window: 41 class NewWebBrowser extends Frame { 42 43 protected static int num_windows = 0; // number of open windows 44 45 String currentDir = "."; // current dir for the open file dialog 46 47 String ellipsis = "..."; // ellipsis for menu items 48 // char ellipsis = '\u2026'; // Why doesn't this work? 49 50 // GUI components: 51 MenuBar menubar; 52 TextArea textarea; 53 54 // Constructor: 55 public NewWebBrowser() { 56 57 // Invoke the constructor of the superclass: 58 super( "Web Browser" ); 59 60 num_windows++; // update open window counter 61 62 // Set frame parameters: 63 setSize( 700, 800 ); 64 setFont( new Font( "SansSerif", Font.BOLD, 12 ) ); 65 66 textarea = new TextArea(); 67 add( textarea, BorderLayout.CENTER ); 68 textarea.setFont( new Font( "Monospaced", Font.BOLD, 10 ) ); 69 70 menubar = new MenuBar(); 71 menubar.add( new FileMenu() ); 72 menubar.add( new EditMenu() ); 73 menubar.add( new ViewMenu() ); 74 menubar.add( new GoMenu() ); 75 menubar.add( new BookmarksMenu() ); 76 menubar.add( new OptionsMenu() ); 77 menubar.add( new DirectoryMenu() ); 78 menubar.add( new WindowMenu() ); 79 menubar.setHelpMenu( new HelpMenu() ); // the Help menu is special 80 81 setMenuBar( menubar ); 82 setVisible( true ); 83 } 84 85 class FileMenu extends Menu implements ActionListener { 86 87 // Menu item names: 88 MenuItem newWebBrowserItem, 89 newMailMessageItem, 90 mailDocumentItem, 91 openLocationItem, 92 openFileItem, 93 saveAsItem, 94 uploadFileItem, 95 printItem, 96 closeItem, 97 exitItem; 98 99 // Menu constructor: 100 public FileMenu() { 101 102 // Invoke the constructor of the superclass: 103 super( "File" ); 104 105 // Menu items with shortcuts: 106 newWebBrowserItem 107 = new MenuItem( "New Web Browser", 108 new MenuShortcut( KeyEvent.VK_N ) ); 109 newMailMessageItem 110 = new MenuItem( "New Mail Message", 111 new MenuShortcut( KeyEvent.VK_M ) ); 112 mailDocumentItem 113 = new MenuItem( "Mail Document" ); 114 115 openLocationItem 116 = new MenuItem( "Open Location" + ellipsis, 117 new MenuShortcut( KeyEvent.VK_L ) ); 118 openFileItem 119 = new MenuItem( "Open File" + ellipsis, 120 new MenuShortcut( KeyEvent.VK_O ) ); 121 saveAsItem 122 = new MenuItem( "Save As" + ellipsis, 123 new MenuShortcut( KeyEvent.VK_S ) ); 124 uploadFileItem 125 = new MenuItem( "Upload File" + ellipsis ); 126 127 printItem 128 = new MenuItem( "Print" + ellipsis, 129 new MenuShortcut( KeyEvent.VK_P ) ); 130 closeItem 131 = new MenuItem( "Close", 132 new MenuShortcut( KeyEvent.VK_W ) ); 133 exitItem 134 = new MenuItem( "Exit", 135 new MenuShortcut( KeyEvent.VK_Q ) ); 136 137 // Build the menu: 138 add( newWebBrowserItem ); 139 add( newMailMessageItem ); 140 add( mailDocumentItem ); 141 addSeparator(); 142 add( openLocationItem ); 143 add( openFileItem ); 144 add( saveAsItem ); 145 add( uploadFileItem ); 146 addSeparator(); 147 add( printItem ); 148 addSeparator(); 149 add( closeItem ); 150 add( exitItem ); 151 152 // Disable menu item(s): 153 uploadFileItem.setEnabled( false ); 154 155 // Listen for ActionEvents generated by menu items: 156 newWebBrowserItem.addActionListener( this ); 157 openFileItem.addActionListener( this ); 158 exitItem.addActionListener( this ); 159 } 160 161 // ActionEvent handler: 162 public void actionPerformed( ActionEvent e ) { 163 164 // File:New 165 if ( e.getSource() == newWebBrowserItem ) { 166 new NewWebBrowser(); 167 168 // File:Open 169 } else if ( e.getSource() == openFileItem ) { 170 FileDialog d = 171 new FileDialog( NewWebBrowser.this, "Open File", FileDialog.LOAD ); 172 d.setFont( new Font( "Monospaced", Font.PLAIN, 12 ) ); 173 d.setDirectory( currentDir ); 174 d.setVisible( true ); 175 String filename = d.getFile(); 176 if ( filename != null ) { 177 currentDir = d.getDirectory(); 178 try { 179 BufferedReader in = 180 new BufferedReader( 181 new FileReader( currentDir + filename ) ); 182 StringBuffer buf = new StringBuffer(); 183 String text; 184 while ( ( text = in.readLine() ) != null ) { 185 buf.append( text + "\n" ); 186 } 187 in.close(); 188 textarea.setText( buf.toString() ); 189 } catch ( IOException e2 ) { 190 System.err.println( e2.toString() ); 191 } 192 } 193 194 // File:Exit 195 } else if ( e.getSource() == exitItem ) { 196 setVisible( false ); 197 dispose(); 198 if ( --num_windows == 0 ) { 199 try { 200 System.exit(0); 201 } catch ( SecurityException e3 ) { } 202 } 203 } 204 } 205 206 } 207 208 class EditMenu extends Menu implements ActionListener { 209 210 // Menu item names: 211 MenuItem undoItem, 212 cutItem, 213 copyItem, 214 pasteItem, 215 findItem, 216 findAgainItem; 217 218 // Menu constructor: 219 public EditMenu() { 220 221 // Invoke the constructor of the superclass: 222 super( "Edit" ); 223 224 // Menu items with shortcuts: 225 undoItem 226 = new MenuItem( "Undo", 227 new MenuShortcut( KeyEvent.VK_Z ) ); 228 cutItem 229 = new MenuItem( "Cut", 230 new MenuShortcut( KeyEvent.VK_X ) ); 231 copyItem 232 = new MenuItem( "Copy", 233 new MenuShortcut( KeyEvent.VK_C ) ); 234 pasteItem 235 = new MenuItem( "Paste", 236 new MenuShortcut( KeyEvent.VK_V ) ); 237 findItem 238 = new MenuItem( "Find" + ellipsis, 239 new MenuShortcut( KeyEvent.VK_F ) ); 240 findAgainItem 241 = new MenuItem( "Find Again", 242 new MenuShortcut( KeyEvent.VK_G ) ); 243 244 // Build the menu: 245 add( undoItem ); 246 addSeparator(); 247 add( cutItem ); 248 add( copyItem ); 249 add( pasteItem ); 250 addSeparator(); 251 add( findItem ); 252 add( findAgainItem ); 253 254 } 255 256 // ActionEvent handler: 257 public void actionPerformed( ActionEvent e ) { 258 } 259 260 } 261 262 class ViewMenu extends Menu implements ActionListener { 263 264 // Menu constructor: 265 public ViewMenu() { 266 267 // Invoke the constructor of the superclass: 268 super( "View" ); 269 270 } 271 272 // ActionEvent handler: 273 public void actionPerformed( ActionEvent e ) { 274 } 275 276 } 277 278 class GoMenu extends Menu implements ActionListener { 279 280 // Menu constructor: 281 public GoMenu() { 282 283 // Invoke the constructor of the superclass: 284 super( "Go" ); 285 286 } 287 288 // ActionEvent handler: 289 public void actionPerformed( ActionEvent e ) { 290 } 291 292 } 293 294 class BookmarksMenu extends Menu implements ActionListener { 295 296 // Menu constructor: 297 public BookmarksMenu() { 298 299 // Invoke the constructor of the superclass: 300 super( "Bookmarks" ); 301 302 } 303 304 // ActionEvent handler: 305 public void actionPerformed( ActionEvent e ) { 306 } 307 308 } 309 310 class OptionsMenu extends Menu implements ActionListener { 311 312 // Menu item names: 313 MenuItem generalPrefs, 314 mailAndNewsPrefs, 315 networkPrefs, 316 securityPrefs; 317 318 // Checkable menu item names: 319 CheckboxMenuItem showMenubarItem, 320 showToolbarItem, 321 showLocationItem, 322 showDirectoryButtonsItem, 323 showJavaConsoleItem; 324 325 // Menu constructor: 326 public OptionsMenu() { 327 328 // Invoke the constructor of the superclass: 329 super( "Options" ); 330 331 // Menu items: 332 generalPrefs = new MenuItem( "General Preferences" + ellipsis ); 333 mailAndNewsPrefs = new MenuItem( "Mail and News Preferences" + ellipsis ); 334 networkPrefs = new MenuItem( "Network Preferences" + ellipsis ); 335 securityPrefs = new MenuItem( "Security Preferences" + ellipsis ); 336 337 // Checkable menu items: 338 showMenubarItem = new CheckboxMenuItem( "Show Menubar" ); 339 showMenubarItem.setState( true ); // checked by default 340 showToolbarItem = new CheckboxMenuItem( "Show Toolbar" ); 341 showLocationItem = new CheckboxMenuItem( "Show Location" ); 342 showDirectoryButtonsItem = new CheckboxMenuItem( "Show Directory Buttons" ); 343 showJavaConsoleItem = new CheckboxMenuItem( "Show Java Console" ); 344 345 // Build the menu: 346 add( generalPrefs ); 347 add( mailAndNewsPrefs ); 348 add( networkPrefs ); 349 add( securityPrefs ); 350 addSeparator(); 351 add( showMenubarItem ); 352 add( showToolbarItem ); 353 add( showLocationItem ); 354 add( showDirectoryButtonsItem ); 355 add( showJavaConsoleItem ); 356 addSeparator(); 357 add( new DocEncodingSubmenu() ); 358 359 } 360 361 // ActionEvent handler: 362 public void actionPerformed( ActionEvent e ) { 363 } 364 365 class DocEncodingSubmenu extends Menu implements ActionListener { 366 367 // Menu constructor: 368 public DocEncodingSubmenu() { 369 370 // Invoke the constructor of the superclass: 371 super( "Document Encoding" ); 372 373 // Build the menu: 374 add( new CheckboxMenuItem( "Western (Latin-1)", true ) ); 375 addSeparator(); 376 add( new CheckboxMenuItem( "Central European (Latin-2)" ) ); 377 addSeparator(); 378 add( new CheckboxMenuItem( "Japanese (Auto-Detect)" ) ); 379 add( new CheckboxMenuItem( "Japanese (Shift-JIS)" ) ); 380 add( new CheckboxMenuItem( "Japanese (EUC-JP)" ) ); 381 // (many more such items omitted) 382 383 } 384 385 // ActionEvent handler: 386 public void actionPerformed( ActionEvent e ) { 387 } 388 389 } 390 391 } 392 393 class DirectoryMenu extends Menu implements ActionListener { 394 395 // Menu constructor: 396 public DirectoryMenu() { 397 398 // Invoke the constructor of the superclass: 399 super( "Directory" ); 400 401 } 402 403 // ActionEvent handler: 404 public void actionPerformed( ActionEvent e ) { 405 } 406 407 } 408 409 class WindowMenu extends Menu implements ActionListener { 410 411 // Menu constructor: 412 public WindowMenu() { 413 414 // Invoke the constructor of the superclass: 415 super( "Window" ); 416 417 } 418 419 // ActionEvent handler: 420 public void actionPerformed( ActionEvent e ) { 421 } 422 423 } 424 425 class HelpMenu extends Menu implements ActionListener { 426 427 // Menu constructor: 428 public HelpMenu() { 429 430 // Invoke the constructor of the superclass: 431 super( "Help" ); 432 433 // Build the menu: 434 add( new MenuItem( "About Netscape" ) ); 435 add( new MenuItem( "About Plug-ins" ) ); 436 add( new MenuItem( "Registration Information" ) ); 437 add( new MenuItem( "Software" ) ); 438 addSeparator(); 439 add( new MenuItem( "Handbook" ) ); 440 441 } 442 443 // ActionEvent handler: 444 public void actionPerformed( ActionEvent e ) { 445 } 446 447 } 448 449 } 450