Webber: A Website Construction Application


PolyMenuWindow.java


// A multi-window program frame with a window-selection menu

package library;

import java.awt.*;
import java.awt.event.*;

public class PolyMenuWindow extends PolyWindow implements ActionListener {

	// Single transferable Window menu
	private static Menu mWindow = null;

	// Constructor - Add an item for this window to the Window menu
	public PolyMenuWindow(String title) {
		super(title);
		if (mWindow==null) mWindow = new Menu(getMenuName());
		mWindow.add(Lib.newMenuItem(title, getName(), null));
	}

	// Return the name to use for the window menu
	public String getMenuName() { return "Window"; }

	// Get the menu bar for this window (create one if necessary)
	public MenuBar getMenuBar() {
		MenuBar bar = super.getMenuBar();	// Get menu bar (if any)
		if (bar==null) setMenuBar(bar = new MenuBar());
		return bar;
	}

	// Add a menu to this window's menu bar
	public void addMenu(Menu menu) { getMenuBar().add(menu); }

	// Return the item for this window from the Window menu
	private MenuItem getMenuItem() {
		return Lib.getMenuItem(mWindow, getName());
	}

	// Override Frame to update the Window menu
	public void setTitle(String title) {
		super.setTitle(title);			// Set the window's title
		getMenuItem().setLabel(title);		// Set the menu item label
	}

	// Update and add the Window menu to to an activated window
	public void windowActivated(WindowEvent e) {
		super.windowActivated(e);		// Call super-class
		Lib.addMenuListener(mWindow, this);	// Register ActionListeners
		getMenuItem().setEnabled(false);	// Disable this menu item
		addMenu(mWindow);			// Add menu to this window
	}

	// Update the Window menu for a deactivated window
	public void windowDeactivated(WindowEvent e) {
		super.windowDeactivated(e);		// Call super-class
		Lib.removeMenuListener(mWindow, this);	// Unregister ActionListeners
		getMenuItem().setEnabled(true);		// Enable this menu item
	}

	// Update the Window menu for a closed window
	public void windowClosed(WindowEvent e) {
		super.windowClosed(e);			// Call super-class
		mWindow.remove(getMenuItem());		// Remove this menu item
	}

	// Activate the window named by an item selected from the Window menu
	public void actionPerformed(ActionEvent e) {
		PolyWindow w = getWindow(e.getActionCommand());
		if (w!=null) w.toFront();
	}
}

Go To: Source Code