/Users/lyon/j4p/src/j2d/gui/WindowMenu.java
|
1 package j2d.gui;
2
3 import javax.swing.*;
4 import javax.swing.event.MenuEvent;
5 import javax.swing.event.MenuListener;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.beans.PropertyVetoException;
9
10 /**
11 * Menu component that handles the functionality expected of a standard
12 * "Windows" menu for MDI applications.
13 * <p/>
14 * Modified from
15 * <a href = http://www.javaworld.com/javaworld/jw-05-2001/jw-0525-mdi.html>
16 * Javaworld web site</a>
17 */
18 public class WindowMenu extends JMenu {
19 private MDIDesktopPane desktop;
20 private JMenuItem cascade = new JMenuItem("Cascade");
21 // private JMenuItem tile=new JMenuItem("Tile");
22
23 /**
24 * Adds a window menu to the specified desktop pane.
25 */
26
27 public WindowMenu(MDIDesktopPane desktop) {
28 this.desktop = desktop;
29 setText("Window");
30 cascade.addActionListener(new ActionListener() {
31 public void actionPerformed(ActionEvent ae) {
32 WindowMenu.this.desktop.cascadeFrames();
33 }
34 });
35 /*
36 tile.addActionListener(new ActionListener() {
37 public void actionPerformed(ActionEvent ae) {
38 WindowMenu.this.desktop.tileFrames();
39 }
40 });
41 */
42 addMenuListener(new MenuListener() {
43 public void menuCanceled(MenuEvent e) {
44 }
45
46 public void menuDeselected(MenuEvent e) {
47 removeAll();
48 }
49
50 public void menuSelected(MenuEvent e) {
51 buildChildMenus();
52 }
53 });
54 }
55
56 /* Sets up the children menus depending on the current desktop state */
57 private void buildChildMenus() {
58 int i;
59 ChildMenuItem menu;
60 JInternalFrame[] array = desktop.getAllFrames();
61
62 add(cascade);
63 // add(tile);
64 if (array.length > 0) addSeparator();
65 cascade.setEnabled(array.length > 0);
66 // tile.setEnabled(array.length > 0);
67
68 for (i = 0; i < array.length; i++) {
69 menu = new ChildMenuItem(array[i]);
70 menu.setState(i == 0);
71 menu.addActionListener(new ActionListener() {
72 public void actionPerformed(ActionEvent ae) {
73 JInternalFrame frame = ((ChildMenuItem) ae.getSource()).getFrame();
74 frame.moveToFront();
75 try {
76 frame.setSelected(true);
77 } catch (PropertyVetoException e) {
78 e.printStackTrace();
79 }
80 }
81 });
82 menu.setIcon(array[i].getFrameIcon());
83 add(menu);
84 }
85 }
86
87 /**
88 * This JCheckBoxMenuItem descendant is used to track the child frame that corresponds
89 * to a given menu.
90 */
91 class ChildMenuItem extends JCheckBoxMenuItem {
92 private JInternalFrame frame;
93
94 public ChildMenuItem(JInternalFrame frame) {
95 super(frame.getTitle());
96 this.frame = frame;
97 }
98
99 public JInternalFrame getFrame() {
100 return frame;
101 }
102 }
103 }
104