The Java Program: SimpleMenu.java

  1 // This example is from the book "Java in a Nutshell, Second Edition".
  2 // Written by David Flanagan.  Copyright (c) 1997 O'Reilly & Associates.
  3 // You may distribute this source code for non-commercial purposes only.
  4 // You may study, modify, and use this example for any purpose, as long as
  5 // this notice is retained.  Note that this example is provided "as is",
  6 // WITHOUT WARRANTY of any kind either expressed or implied.
  7 
  8 import java.awt.*;
  9 import java.awt.event.*;
 10 import java.util.Locale;
 11 import java.util.ResourceBundle;
 12 import java.util.MissingResourceException;
 13 
 14 /** A convenience class to automatically create localized menu panes */
 15 public class SimpleMenu {
 16 
 17    /** The convenience method that creates menu panes */
 18    public static Menu create (String bundlename,
 19                              final String menuname, String[] itemnames) {
 20      // Get the resource bundle used for this menu.
 21      final ResourceBundle b = ResourceBundle.getBundle(bundlename);
 22 
 23      class CommandListener implements ActionListener {
 24         public void actionPerformed (ActionEvent e) {
 25            String cmd = e.getActionCommand();
 26            System.out.println (cmd);
 27            System.out.println (b.getString (menuname + "." + cmd + ".label"));
 28         }
 29      }
 30 
 31      CommandListener theListener = new CommandListener ();
 32 
 33      // Get the menu title from the bundle.  Use name as default label.
 34      String menulabel;
 35      try { menulabel = b.getString(menuname + ".label"); }
 36      catch(MissingResourceException e) { menulabel = menuname; }
 37 
 38      Menu m = new Menu(menulabel); // Create the menu pane.
 39 
 40     // For each named item in the menu.
 41     for(int i = 0; i < itemnames.length; i++) {
 42       // Look up the label for the item, using name as default.
 43       String itemlabel;
 44       try { itemlabel=b.getString(menuname + "." + itemnames[i] + ".label"); }
 45       catch (MissingResourceException e) { itemlabel = itemnames[i]; }
 46 
 47       // Look up a shortcut for the item, and create the menu shortcut, if any.
 48       String shortcut;
 49       try{shortcut = b.getString(menuname + "." + itemnames[i]+".shortcut"); }
 50       catch (MissingResourceException e) { shortcut = null; }
 51       MenuShortcut ms = null;
 52       if (shortcut != null) ms = new MenuShortcut(shortcut.charAt(0));
 53 
 54       // Create the menu item.
 55       MenuItem mi;
 56       if (ms != null) mi = new MenuItem(itemlabel, ms);
 57       else mi = new MenuItem(itemlabel);
 58 
 59       // Register an action listener and command for the item.
 60       mi.addActionListener (theListener);
 61       mi.setActionCommand (itemnames[i]);  // identifying string
 62 
 63       // Add the item to the menu.
 64       m.add(mi);
 65     }
 66 
 67     // Return the automatically created localized menu.
 68     return m;
 69   }
 70 
 71   /** A simple test program for the above code */
 72   public static void main(String[] args) {
 73     // Set the default locale based on the command-line args.
 74     if (args.length == 2) Locale.setDefault(new Locale(args[0], args[1]));
 75 
 76     Frame f = new Frame("SimpleMenu Test");  // Create a window.
 77     MenuBar menubar = new MenuBar();         // Create a menubar.
 78     f.setMenuBar(menubar);                   // Add menubar to window.
 79 
 80     // Create a menu using our convenience routine (and the default locale).
 81     Menu colors = SimpleMenu.create ("Menus", "colors",
 82                                     new String[] { "red", "green", "blue" });
 83 
 84     menubar.add(colors);                     // Add the menu to the menubar.
 85     f.setSize(300, 150);                     // Set the window size.
 86     f.show();                                // Pop the window up.
 87   }
 88 }