001: package org.sape.carbon.services.console;
002:
003: import java.awt.event.ActionEvent;
004: import java.awt.event.ActionListener;
005: import java.beans.PropertyVetoException;
006:
007: import javax.swing.JCheckBoxMenuItem;
008: import javax.swing.JInternalFrame;
009: import javax.swing.JMenu;
010: import javax.swing.JMenuItem;
011: import javax.swing.event.MenuEvent;
012: import javax.swing.event.MenuListener;
013:
014: /**
015: * <p>This is the template for Classes.</p>
016: *
017: * Copyright 2002 Sapient
018: * @since carbon 1.0
019: * @author Greg Hinkle, January 2002
020: * @version $Revision: 1.3 $($Author: ghinkl $ / $Date: 2003/04/04 01:11:49 $)
021: */
022: public class WindowMenu extends JMenu {
023: private MDIDesktopPane desktop;
024:
025: private JMenuItem cascade = new JMenuItem("Cascade");
026: private JMenuItem tile = new JMenuItem("Tile");
027:
028: public WindowMenu(MDIDesktopPane desktop) {
029: this .desktop = desktop;
030: setText("Window");
031: cascade.addActionListener(new ActionListener() {
032: public void actionPerformed(ActionEvent ae) {
033: WindowMenu.this .desktop.cascadeFrames();
034: }
035: });
036: tile.addActionListener(new ActionListener() {
037: public void actionPerformed(ActionEvent ae) {
038: WindowMenu.this .desktop.tileFrames();
039: }
040: });
041: addMenuListener(new MenuListener() {
042: public void menuCanceled(MenuEvent e) {
043: }
044:
045: public void menuDeselected(MenuEvent e) {
046: removeAll();
047: }
048:
049: public void menuSelected(MenuEvent e) {
050: buildChildMenus();
051: }
052: });
053: }
054:
055: /* Sets up the children menus depending on the current desktop state */
056: private void buildChildMenus() {
057: int i;
058: ChildMenuItem menu;
059: JInternalFrame[] array = desktop.getAllFrames();
060:
061: add(cascade);
062: add(tile);
063: if (array.length > 0)
064: addSeparator();
065: cascade.setEnabled(array.length > 0);
066: tile.setEnabled(array.length > 0);
067:
068: for (i = 0; i < array.length; i++) {
069: menu = new ChildMenuItem(array[i]);
070: menu.setState(i == 0);
071: menu.addActionListener(new ActionListener() {
072: public void actionPerformed(ActionEvent ae) {
073: JInternalFrame frame = ((ChildMenuItem) ae
074: .getSource()).getFrame();
075: frame.moveToFront();
076: try {
077: frame.setSelected(true);
078: } catch (PropertyVetoException e) {
079: e.printStackTrace();
080: }
081: }
082: });
083: menu.setIcon(array[i].getFrameIcon());
084: add(menu);
085: }
086: }
087:
088: /* This JCheckBoxMenuItem descendant is used to track the child frame that corresponds
089: to a give menu. */
090: class ChildMenuItem extends JCheckBoxMenuItem {
091: private JInternalFrame frame;
092:
093: public ChildMenuItem(JInternalFrame frame) {
094: super (frame.getTitle());
095: this .frame = frame;
096: }
097:
098: public JInternalFrame getFrame() {
099: return frame;
100: }
101: }
102: }
|