package awtExtExamples ; /* * * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.awt.BorderLayout; import java.awt.Label; import java.awt.Panel; import java.awt.Event; import java.awt.Font; import sysExt.* ; import awtExt.*; /** * A test of menus invoking user defined methods. Also demonstrates * how to control event propagation so that an event could be handled * anywhere in the container hierarchy. All menuItems 'consume' the * ACTION_EVENT, with the exception of the 'vier' menu item, which * allows the ACTION_EVENT to propagate upward. User defined methods * are registered with MenuItems, Menus, and the MenuBar. In the java * console you will see that all menu selections, with the exception * of 'vier', fire only one user registered method. The 'vier' * selection, fires registered methods on the MenuItem, Menu and the * MenuBar level. */ public class MenuTest extends Frame { Label lbl; public MenuTest() { super("MenuTest"); methodRef CheckboxMenuItemCallback = null, MenuItemCallback = null, MenuCallback = null; try { /* ** Create callbacks for menu items and menu. ** Shows how menus can be handled at different levels ** of containment */ CheckboxMenuItemCallback = Callback.newRef( this, "CheckboxMenuItemMethod" ) ; MenuItemCallback = Callback.newRef( this, "menuItemMethod" ) ; MenuCallback = Callback.newRef( this, "menuMethod" ) ; /* ** Create menu bar and attach callback method 'menuBarMethod'. ** This callback will be invoked ONLY if the event was NOT ** consumed by a lower level menu component... */ MenuBar mb = new MenuBar( this, "menuBarMethod" ); /* ** Create a menu with a callback method. This method ** will NOT be called back since the event is consumed ** by the menu's MenuItems callbacks */ Menu m = new Menu("English", MenuCallback ); m.add(new MenuItem("one", MenuItemCallback)); m.add(new MenuItem("two", MenuItemCallback)); m.add(new MenuItem("three",MenuItemCallback)); m.add(new MenuItem("four", MenuItemCallback)); Menu submenu = new Menu("more", MenuCallback ); submenu.add(new MenuItem("five", MenuItemCallback)); submenu.add(new MenuItem("six", MenuItemCallback)); m.add(submenu); Menu submenu2 = new Menu("more", MenuCallback ); submenu2.add(new MenuItem("seven", MenuItemCallback)); submenu2.add(new MenuItem("eight", MenuItemCallback)); submenu.add(submenu2); mb.add(m); /* ** Create a menu with a callback method. This method ** will be called back only when "vier" is pressed. ** All other choices are consumed by the individual MenuItem's ** callbacks. */ m = new Menu("Nederlands", MenuCallback ); m.add(new MenuItem("een", MenuItemCallback)); m.add(new MenuItem("twee" , MenuItemCallback)); m.add(new MenuItem("drie", MenuItemCallback)); m.add(new MenuItem("vier", MenuItemCallback)); mb.add(m); /* ** Create another menu with a callback method. This method ** will be called back only when "vier" is pressed. ** All other choices are consumed by the individual MenuItem's ** callbacks. */ m = new Menu("Deutsch", MenuCallback ); m.add(new CheckboxMenuItem("eins", CheckboxMenuItemCallback)); m.add(new CheckboxMenuItem("twei", CheckboxMenuItemCallback)); m.add(new CheckboxMenuItem("drie", CheckboxMenuItemCallback)); m.add(new CheckboxMenuItem("vier", CheckboxMenuItemCallback)); mb.add(m); setMenuBar(mb); setLayout(new BorderLayout()); add( "Center", lbl = new Label(" The last menu selection will be displayed here. ", Label.CENTER)) ; setFont(new Font("Helvetica", Font.BOLD, 18)); move(300, 200); resize( 640, 200 ); show(); } catch( Exception e ) { System.out.println( "OOPS!! EXCEPTION!!!" ) ; e.printStackTrace() ; } ; } /* ** This method is invoked from a MenuItem. If the item is 'vier' ** It allows the action event to propagate up the containment ** hierarchy. */ public void menuItemMethod( CallbackInfo cb) { Event evt = cb.evt ; System.out.println("> In menuItemCallback..." + evt.target.toString()); ; lbl.setText(evt.target.toString()); if( ((MenuItem)(evt.target)).getLabel().equals( "vier" ) ) cb.processed = false ; else cb.processed = true ; } /* ** This method is invoked from a Menu */ public void menuMethod( CallbackInfo cb ) { Event evt = cb.evt ; System.out.println(">> In menuCallback..." + evt.target.toString()); ; lbl.setText(evt.target.toString()); } /* ** This method is invoked from the MenuBar */ public void menuBarMethod( CallbackInfo cb ) { Event evt = cb.evt ; System.out.println(">>> In menubar callback..." + evt.target.toString()); ; lbl.setText(evt.target.toString()); } /* ** This method is invoked from a MenuItem. If the item is 'vier' ** It allows the action event to propagate up the containment ** hierarchy. */ public void CheckboxMenuItemMethod( CallbackInfo cb) { Event evt = cb.evt ; System.out.println("> In CheckboxMenuItemCallback..." + evt.target.toString()); ; if( ((CheckboxMenuItem)(evt.target)).getLabel().equals( "vier" ) ) cb.processed = false ; else cb.processed = true ; lbl.setText(evt.target.toString()); } public static void main(String args[]) { new MenuTest(); } }