Webber: A Website Construction Application


HtmlWindow.java


// Generic superclass for HtmlViewer and HtmlEditor windows

package library.html;

import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;

import library.*;

public abstract class HtmlWindow extends PolyMenuWindow implements ActionListener {

	private static String modFlag = " *";	// Modified flag for window title
	private static String href = "Href";	// New file/URL command
	protected HtmlFile htmlFile;		// Parent HtmlFile object
	protected HtmlPage reader;		// Viewer or Editor component
	private TextField filename;		// Filename/URL display area
	private Label message;			// Message output area

	// Constructor
	protected HtmlWindow(String title, HtmlFile htmlFile) {
		super(title);					// Call super-class
		this.htmlFile = htmlFile;			// Set HtmlFile parent
		setTitle(title);				// Title modifications
	}

	// Set the filename for this window
	public void setFilename(String fname) { filename.setText(fname); }

	// Get and set the text in the HTML reader component
	public char[] getText() { return reader.getText(); }
	public void setText(char[] text) { reader.setText(text, htmlFile.isPlain); }

	// Set the text for the message area
	public void setMessage(String text) { message.setText(text); }

	// Create a new menu using specified items linked to an ActionListener
	// items[0] is the name of the menu
	// items[1, 3, 5, etc] are the item labels
	// items[2, 4, 6, etc] are the item action command strings
	static Menu newMenu(String[] items, ActionListener a) {
		Menu menu = new Menu(items[0]);
		for (int i=1, num=items.length; i+1 < num; i += 2)
			menu.add(Lib.newMenuItem(items[i], items[i+1], a));
		return menu;
	}

	// Get the index of an action command in a menu items array (or -1)
	static int getMenuActionIndex(String[] items, String command) {
		for (int i=2, num=items.length; i<num; i += 2)
			if (items[i].equals(command)) return (i >> 1) - 1;
		return -1;					// Command not found
	}

	// Initialize and open a viewer or editor window
	void initWindow(String[] fileMenu, Panel buttons, String fname, char[] text) {
		addMenu(newMenu(fileMenu, this));		// Create File menu
		add(reader);					// Add viewer to window
		Panel p = new Panel(new BorderLayout());	// Create top panel
		p.add(buttons, "North");			// Add button panel
		filename = new TextField();			// Create filename area
		setFilename(fname);				// Set filename
		filename.addActionListener(new ActionListener() {// Add action listener
			public void actionPerformed(ActionEvent e) {
				htmlFile.openUrl(e.getActionCommand()); // Open file/URL
			}
		});
		p.add(filename, "South");			// Add filename label
		add(p, "North");				// Add top components
		message = new Label() {				// Create message area
			public void paint(Graphics g) {		// Override paint
				super.paint(g);			// Call super-class
				Dimension d = getSize();	// Get label size
				g.setColor(SystemColor.control);// Draw 3D rectangle
				g.draw3DRect(2, 2, d.width-3, d.height-3, false);
			}					// Modify text update
			public void setText(String text) { super.setText("  "+text); }
		};
		message.setBackground(SystemColor.control);	// Set area background
		add(message, "South");				// Add message area
		setSize(600, 400);				// Set window size
		setVisible(true);				// Display the window
	}	

	// Close the specified window
	boolean closeWindow(HtmlWindow win) {
		if (!win.htmlFile.checkSave(win, true)) return false;
		win.dispose();					// Destroy window
		return true;
	}		

	// Close all viewer and editor windows (exit program)
	void closeAllWindows() {
		Enumeration e = elements();			// Get active windows
		while (e.hasMoreElements())			// Close one by one
			if (!closeWindow((HtmlWindow)e.nextElement())) break;
	}

	// Process window activated event to update the HtmlFile active window
	public void windowActivated(WindowEvent e) {
		htmlFile.active = this;				// Set active window
		super.windowActivated(e);			// Call super-class
		reader.repaint();				// Refresh display
	}

	// Process window closing event to save any changes to text
	public void windowClosing(WindowEvent e) {
		if (htmlFile.checkSave(this, true)) super.windowClosing(e);
	}

	// Override PolyMenuWindow to append the modified flag to the title (if any)
	public void setTitle(String title) {
		if (htmlFile.isModified) title += modFlag;	// Append modified flag
		super.setTitle(title);				// Set window title etc
	}

	// Remove the modified flag from the title (if any) and call setTitle
	void resetModified() {
		String s = getTitle();
		if (!htmlFile.isModified)			// Remove flag?
			s = s.substring(0, s.length() - modFlag.length());
		setTitle(s);
	}
}

Go To: Source Code