home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c13 / MenuNew.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  7.0 KB  |  204 lines

  1. //: MenuNew.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Menus in Java 1.1
  50. import java.awt.*;
  51. import java.awt.event.*;
  52.  
  53. public class MenuNew extends Frame {
  54.   String[] flavors = { "Chocolate", "Strawberry",
  55.     "Vanilla Fudge Swirl", "Mint Chip", 
  56.     "Mocha Almond Fudge", "Rum Raisin", 
  57.     "Praline Cream", "Mud Pie" };
  58.   TextField t = new TextField("No flavor", 30);
  59.   MenuBar mb1 = new MenuBar();
  60.   Menu f = new Menu("File");
  61.   Menu m = new Menu("Flavors");
  62.   Menu s = new Menu("Safety");
  63.   // Alternative approach:
  64.   CheckboxMenuItem[] safety = {
  65.     new CheckboxMenuItem("Guard"),
  66.     new CheckboxMenuItem("Hide")
  67.   };
  68.   MenuItem[] file = {
  69.     // No menu shortcut:
  70.     new MenuItem("Open"),
  71.     // Adding a menu shortcut is very simple:
  72.     new MenuItem("Exit", 
  73.       new MenuShortcut(KeyEvent.VK_E))
  74.   };
  75.   // A second menu bar to swap to:
  76.   MenuBar mb2 = new MenuBar();
  77.   Menu fooBar = new Menu("fooBar");
  78.   MenuItem[] other = {
  79.     new MenuItem("Foo"),
  80.     new MenuItem("Bar"),
  81.     new MenuItem("Baz"),
  82.   };
  83.   // Initialization code:
  84.   {
  85.     ML ml = new ML();
  86.     CMIL cmil = new CMIL();
  87.     safety[0].setActionCommand("Guard");
  88.     safety[0].addItemListener(cmil);
  89.     safety[1].setActionCommand("Hide");
  90.     safety[1].addItemListener(cmil);
  91.     file[0].setActionCommand("Open");
  92.     file[0].addActionListener(ml);
  93.     file[1].setActionCommand("Exit");
  94.     file[1].addActionListener(ml);
  95.     other[0].addActionListener(new FooL());
  96.     other[1].addActionListener(new BarL());
  97.     other[2].addActionListener(new BazL());
  98.   }
  99.   Button b = new Button("Swap Menus");
  100.   public MenuNew() {
  101.     FL fl = new FL();
  102.     for(int i = 0; i < flavors.length; i++) {
  103.       MenuItem mi = new MenuItem(flavors[i]);
  104.       mi.addActionListener(fl);
  105.       m.add(mi);
  106.       // Add separators at intervals:
  107.       if((i+1) % 3 == 0) 
  108.         m.addSeparator();
  109.     }
  110.     for(int i = 0; i < safety.length; i++)
  111.       s.add(safety[i]);
  112.     f.add(s);
  113.     for(int i = 0; i < file.length; i++)
  114.       f.add(file[i]);
  115.     mb1.add(f);
  116.     mb1.add(m);
  117.     setMenuBar(mb1);
  118.     t.setEditable(false);
  119.     add(t, BorderLayout.CENTER);
  120.     // Set up the system for swapping menus:
  121.     b.addActionListener(new BL());
  122.     add(b, BorderLayout.NORTH);
  123.     for(int i = 0; i < other.length; i++)
  124.       fooBar.add(other[i]);
  125.     mb2.add(fooBar);
  126.   }
  127.   class BL implements ActionListener {
  128.     public void actionPerformed(ActionEvent e) {
  129.       MenuBar m = getMenuBar();
  130.       if(m == mb1) setMenuBar(mb2);
  131.       else if (m == mb2) setMenuBar(mb1);
  132.     }
  133.   }
  134.   class ML implements ActionListener {
  135.     public void actionPerformed(ActionEvent e) {
  136.       MenuItem target = (MenuItem)e.getSource();
  137.       String actionCommand = 
  138.         target.getActionCommand();
  139.       if(actionCommand.equals("Open")) {
  140.         String s = t.getText();
  141.         boolean chosen = false;
  142.         for(int i = 0; i < flavors.length; i++)
  143.           if(s.equals(flavors[i])) chosen = true;
  144.         if(!chosen)
  145.           t.setText("Choose a flavor first!");
  146.         else
  147.           t.setText("Opening "+ s +". Mmm, mm!");
  148.       } else if(actionCommand.equals("Exit")) {
  149.         dispatchEvent(
  150.           new WindowEvent(MenuNew.this, 
  151.             WindowEvent.WINDOW_CLOSING));
  152.       }
  153.     }
  154.   }
  155.   class FL implements ActionListener {
  156.     public void actionPerformed(ActionEvent e) {
  157.       MenuItem target = (MenuItem)e.getSource();
  158.       t.setText(target.getLabel());
  159.     }
  160.   }
  161.   // Alternatively, you can create a different
  162.   // class for each different MenuItem. Then you
  163.   // Don't have to figure out which one it is:
  164.   class FooL implements ActionListener {
  165.     public void actionPerformed(ActionEvent e) {
  166.       t.setText("Foo selected");
  167.     }
  168.   }
  169.   class BarL implements ActionListener {
  170.     public void actionPerformed(ActionEvent e) {
  171.       t.setText("Bar selected");
  172.     }
  173.   }
  174.   class BazL implements ActionListener {
  175.     public void actionPerformed(ActionEvent e) {
  176.       t.setText("Baz selected");
  177.     }
  178.   }
  179.   class CMIL implements ItemListener {
  180.     public void itemStateChanged(ItemEvent e) {
  181.       CheckboxMenuItem target = 
  182.         (CheckboxMenuItem)e.getSource();
  183.       String actionCommand = 
  184.         target.getActionCommand();
  185.       if(actionCommand.equals("Guard"))
  186.         t.setText("Guard the Ice Cream! " +
  187.           "Guarding is " + target.getState());
  188.       else if(actionCommand.equals("Hide"))
  189.         t.setText("Hide the Ice Cream! " +
  190.           "Is it cold? " + target.getState());
  191.     }
  192.   }
  193.   public static void main(String[] args) {
  194.     MenuNew f = new MenuNew();
  195.     f.addWindowListener(
  196.       new WindowAdapter() {
  197.         public void windowClosing(WindowEvent e) {
  198.           System.exit(0);
  199.         }
  200.       });
  201.     f.setSize(300,200);
  202.     f.setVisible(true);
  203.   }
  204. } ///:~