Webber: A Website Construction Application


CommonDialog.java


// General purpose common dialog window

package library;

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

public class CommonDialog extends Dialog {

	// Public constants
	public static final int OK=0, YES_NO=1, YES_NO_CANCEL=2;// Dialog types
	public static final int YES=0, NO=1, CANCEL=2;		// Dialog responses

	// Local variables
	private Frame parent = null;				// Parent window
	private int type = YES_NO_CANCEL;			// This dialog type
	private String text;					// Message label text
	private int response;					// Dialog response
	private boolean isRelative = false;			// Window start location

	// Constructor
	public CommonDialog(Frame parent, String title, boolean modal) {
		super(parent, title, modal);
		this.parent = parent;				// Save window parent
	}

	// Set the type of this dialog
	public void setType(int type) { this.type = type; }

	// Set the text for the dialog message label
	public void setText(String text) { this.text = text; }

	// Override Dialog to build the dialog window
	public void show() { if (!isVisible()) openDialog(); super.show(); }

	// Return the response from the dialog
	public int getResponse() { return response; }

	// Return the dimensions to use for the dialog window
	public Dimension getStartSize() { return new Dimension(200, 150); }

	// Select the method to return the initial location of the dialog window
	public void setRelative(boolean isRelative) { this.isRelative = isRelative; }

	// Return the initial location of the dialog window (parent centre)
	private Point getLocationRelative() {
		Rectangle r = parent.getBounds();		// Get parent bounds
		Dimension d = getStartSize();			// Get window dimensions
		return new Point(r.x + r.width/2 - d.width/2,	// Calculate top-left
				r.y + r.height/2 - d.height/2);
	}

	// Return the initial location of the dialog window (screen centre)
	private Point getLocationAbsolute() {
		Dimension s = getToolkit().getScreenSize(), w = getStartSize();
		return new Point(s.width/2 - w.width/2, s.height/2 - w.height/2);
	}

	// Return the initial location for the top-left of the dialog window
	private Point getStartLocation() {
		return isRelative ? getLocationRelative() : getLocationAbsolute();
	}

	// Set the initial dialog window bounds
	private void setStartBounds() {
		Dimension d = getStartSize();			// Get size
		Point p = getStartLocation();			// Get location
		setBounds(p.x, p.y, d.width, d.height);		// Set window bounds
	}

	// Return the button-spacing gaps
	public Dimension getButtonGaps() { return new Dimension(5, 5); }

	// Return the button-panel insets
	public Insets getButtonInsets() { return new Insets(5, 5, 5, 5); }

	// Return the button labels for a dialog of the specified type
	public String[] getButtonLabels(int type) {
		String[][] labels = {
			{ "OK" },				// 1-button message
			{ "Yes", "No" },			// Choice x 2
			{ "Yes", "No", "Cancel" } };		// Choice x 2 + Cancel
		return labels[type];
	}

	// Add buttons for a dialog of the specified type to Panel p
	private void addButtons(Panel p, int type, ActionListener a, KeyListener k) {
		String[] labels = getButtonLabels(type);
		Button b;
		for (int i=0, num=labels.length; i < num; ++i) {
			p.add(b = Lib.newButton(labels[i], ""+(char)i, a));
			b.addKeyListener(k);
		}
	}

	// Set the dialog response and close the dialog window
	private void setResponse(int response) { this.response = response; dispose(); }

	// Return an ActionListener for processing buttons
	private ActionListener getActionListener() {
	    return new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			setResponse((int)(e.getActionCommand().charAt(0)));
		}
	    };
	}

	// Return a KeyListener for processing buttons
	private KeyListener getKeyListener() {
	    return new KeyAdapter() {
		public void keyPressed(KeyEvent e) {
			switch (e.getKeyCode()) {
			    case e.VK_ESCAPE:				// Escape -> Cancel
				setResponse(CANCEL);
				break;
			}
		}
		public void keyTyped(KeyEvent e) {			// Button hotkeys
			String[] labels = getButtonLabels(type);	// Get label array
			char c = Character.toUpperCase(e.getKeyChar());	// Ignore case
			for (int i=0, num=labels.length; i < num; ++i)	// Check each label
				if (c==Character.toUpperCase(labels[i].charAt(0))) {
					setResponse(i);			// Set response
					break;
				}
		}
	    };
	}

	// Return a WindowListener to trap window events
	private WindowListener getWindowListener() {
	    return new WindowAdapter() {
		public void windowClosing(WindowEvent e) { setResponse(CANCEL); }
	    };
	}

	// Construct the dialog window
	private void openDialog() {
		setStartBounds();				// Set initial bounds
		Panel p;
		if (text!=null) {				// Create a label?
			p = new Panel(new BorderLayout());
			p.add(new AreaLabel(text));		// Multi-line label
			add(p, "Center");
		}
		p = new Panel(new GridLayout()) {		// Override p.getInsets
			public Insets getInsets() { return getButtonInsets(); }
		};
		((GridLayout)p.getLayout()).setHgap(getButtonGaps().width);
		addButtons(p, type, getActionListener(), getKeyListener());
		add(p, "South");				// Add panel to window
		addWindowListener(getWindowListener());		// Close -> Cancel
	}

	// Open a dialog with the specified settings and return the response
	public static int getDialog(Frame parent, String title,
				    String text, int type, boolean modal) {
		CommonDialog d = new CommonDialog(parent, title, modal);
		d.setText(text);
		d.setType(type);
		d.show();
		return d.getResponse();
	}

	// Open a modal Yes/No/Cancel dialog and return the response
	public static int getDialog(Frame parent, String title, String text) {
		return getDialog(parent, title, text, YES_NO_CANCEL, true);
	}
}

Go To: Source Code