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

  1. //: Menus.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. // A menu-building system; also demonstrates
  50. // icons in labels and menu items.
  51. package c13.swing;
  52. import java.awt.*;
  53. import java.awt.event.*;
  54. import java.awt.swing.*;
  55.  
  56. public class Menus extends JPanel {
  57.   static final Boolean
  58.     bT = new Boolean(true), 
  59.     bF = new Boolean(false);
  60.   // Dummy class to create type identifiers:
  61.   static class MType { MType(int i) {} };
  62.   static final MType
  63.     mi = new MType(1), // Normal menu item
  64.     cb = new MType(2), // Checkbox menu item
  65.     rb = new MType(3); // Radio button menu item
  66.   JTextField t = new JTextField(10);
  67.   JLabel l = new JLabel("Icon Selected", 
  68.     Faces.faces[0], JLabel.CENTER);
  69.   ActionListener a1 = new ActionListener() {
  70.     public void actionPerformed(ActionEvent e) {
  71.       t.setText(
  72.         ((JMenuItem)e.getSource()).getText());
  73.     }
  74.   };
  75.   ActionListener a2 = new ActionListener() {
  76.     public void actionPerformed(ActionEvent e) {
  77.       JMenuItem mi = (JMenuItem)e.getSource();
  78.       l.setText(mi.getText());
  79.       l.setIcon(mi.getIcon());
  80.     }
  81.   };
  82.   // Store menu data as "resources":
  83.   public Object[][] fileMenu = {
  84.     // Menu name and accelerator:
  85.     { "File", new Character('F') },
  86.     // Name type accel listener enabled
  87.     { "New", mi, new Character('N'), a1, bT },
  88.     { "Open", mi, new Character('O'), a1, bT },
  89.     { "Save", mi, new Character('S'), a1, bF },
  90.     { "Save As", mi, new Character('A'), a1, bF},
  91.     { null }, // Separator
  92.     { "Exit", mi, new Character('x'), a1, bT },
  93.   };
  94.   public Object[][] editMenu = {
  95.     // Menu name:
  96.     { "Edit", new Character('E') },
  97.     // Name type accel listener enabled
  98.     { "Cut", mi, new Character('t'), a1, bT },
  99.     { "Copy", mi, new Character('C'), a1, bT },
  100.     { "Paste", mi, new Character('P'), a1, bT },
  101.     { null }, // Separator
  102.     { "Select All", mi,new Character('l'),a1,bT},
  103.   };
  104.   public Object[][] helpMenu = {
  105.     // Menu name:
  106.     { "Help", new Character('H') },
  107.     // Name type accel listener enabled
  108.     { "Index", mi, new Character('I'), a1, bT },
  109.     { "Using help", mi,new Character('U'),a1,bT},
  110.     { null }, // Separator
  111.     { "About", mi, new Character('t'), a1, bT },
  112.   };
  113.   public Object[][] optionMenu = {
  114.     // Menu name:
  115.     { "Options", new Character('O') },
  116.     // Name type accel listener enabled
  117.     { "Option 1", cb, new Character('1'), a1,bT},
  118.     { "Option 2", cb, new Character('2'), a1,bT},
  119.   };
  120.   public Object[][] faceMenu = {
  121.     // Menu name:
  122.     { "Faces", new Character('a') },
  123.     // Optinal last element is icon
  124.     { "Face 0", rb, new Character('0'), a2, bT, 
  125.       Faces.faces[0] },
  126.     { "Face 1", rb, new Character('1'), a2, bT, 
  127.       Faces.faces[1] },
  128.     { "Face 2", rb, new Character('2'), a2, bT, 
  129.       Faces.faces[2] },
  130.     { "Face 3", rb, new Character('3'), a2, bT, 
  131.       Faces.faces[3] },
  132.     { "Face 4", rb, new Character('4'), a2, bT, 
  133.       Faces.faces[4] },
  134.   };
  135.   public Object[] menuBar = {
  136.     fileMenu, editMenu, faceMenu, 
  137.     optionMenu, helpMenu,
  138.   };
  139.   static public JMenuBar
  140.   createMenuBar(Object[] menuBarData) {
  141.     JMenuBar menuBar = new JMenuBar();
  142.     for(int i = 0; i < menuBarData.length; i++)
  143.       menuBar.add(
  144.         createMenu((Object[][])menuBarData[i]));
  145.     return menuBar;
  146.   }
  147.   static ButtonGroup bgroup;
  148.   static public JMenu 
  149.   createMenu(Object[][] menuData) {
  150.     JMenu menu = new JMenu();
  151.     menu.setText((String)menuData[0][0]);
  152.     menu.setMnemonic(
  153.       ((Character)menuData[0][1]).charValue());
  154.     // Create redundantly, in case there are
  155.     // any radio buttons:
  156.     bgroup = new ButtonGroup();
  157.     for(int i = 1; i < menuData.length; i++) {
  158.       if(menuData[i][0] == null)
  159.         menu.add(new JSeparator());
  160.       else
  161.         menu.add(createMenuItem(menuData[i]));
  162.     }
  163.     return menu;
  164.   }
  165.   static public JMenuItem 
  166.   createMenuItem(Object[] data) {
  167.     JMenuItem m = null;
  168.     MType type = (MType)data[1];
  169.     if(type == mi)
  170.       m = new JMenuItem();
  171.     else if(type == cb)
  172.       m = new JCheckBoxMenuItem();
  173.     else if(type == rb) {
  174.       m = new JRadioButtonMenuItem();
  175.       bgroup.add(m);
  176.     }
  177.     m.setText((String)data[0]);
  178.     m.setMnemonic(
  179.       ((Character)data[2]).charValue());
  180.     m.addActionListener(
  181.       (ActionListener)data[3]);
  182.     m.setEnabled(
  183.       ((Boolean)data[4]).booleanValue());
  184.     if(data.length == 6)
  185.       m.setIcon((Icon)data[5]);
  186.     return m;
  187.   }
  188.   Menus() {
  189.     setLayout(new BorderLayout());
  190.     add(createMenuBar(menuBar), 
  191.       BorderLayout.NORTH);
  192.     JPanel p = new JPanel();
  193.     p.setLayout(new BorderLayout());
  194.     p.add(t, BorderLayout.NORTH);
  195.     p.add(l, BorderLayout.CENTER);
  196.     add(p, BorderLayout.CENTER);
  197.   }
  198.   public static void main(String args[]) {
  199.     Show.inFrame(new Menus(), 300, 200);
  200.   }
  201. } ///:~