/*
 * @(#)DhcpmgrDialog.java	1.3	99/08/05 SMI
 *
 * Copyright (c) 1999 by Sun Microsystems, Inc.
 * All rights reserved.
 */
package com.sun.dhcpmgr.client;

import javax.swing.JDialog;
import javax.swing.BoxLayout;
import javax.swing.JSeparator;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Frame;
import java.util.Vector;
import java.util.Enumeration;

import com.sun.dhcpmgr.ui.ButtonPanel;
import com.sun.dhcpmgr.ui.ButtonPanelListener;

/**
 * This abstract class provides a framework for building all of the dialogs
 * used by DHCP Mgr.  Subclasses must implement the abstract methods defined
 * here as well as override any other appropriate methods.  Most of these
 * methods are declared protected because they are implementation details
 * which need not be visible outside the dialog we're actually implementing.
 */
public abstract class DhcpmgrDialog extends JDialog
	implements ButtonPanelListener {
    // Listeners receive action events when user presses OK
    private Vector listeners;
    // ButtonPanel is protected so subclasses can manipulate directly
    protected ButtonPanel buttonPanel;

    public DhcpmgrDialog(Frame f, boolean allowsReset) {
        super(f);
        listeners = new Vector();
	// Layout is subclass main panel, then a separator, then buttons
	getContentPane().setLayout(new BoxLayout(getContentPane(),
	    BoxLayout.Y_AXIS));
	// Create buttonPanel first so subclasses can modify it if need be
	buttonPanel = new ButtonPanel(allowsReset);
	buttonPanel.addButtonPanelListener(this);
	getContentPane().add(getMainPanel());
	getContentPane().add(new JSeparator());
	getContentPane().add(buttonPanel);
	// Position relative to our owning frame
	setLocationRelativeTo(f);
    }

    /**
     * Return the main display panel
     */
    protected abstract JPanel getMainPanel();

    public void addActionListener(ActionListener l) {
	listeners.addElement(l);
    }
    
    public void removeActionListener(ActionListener l) {
	listeners.removeElement(l);
    }
    
    /**
     * fire the action event
     */
    protected abstract void fireActionPerformed();

    protected void fireActionPerformed(Object source, String command) {
	ActionEvent e = new ActionEvent(source, ActionEvent.ACTION_PERFORMED,
	    command);
	Enumeration en = listeners.elements();
	while (en.hasMoreElements()) {
	    ActionListener l = (ActionListener)en.nextElement();
	    l.actionPerformed(e);
	}
    }

    /**
     * Handle user clicks on the dialog buttons
     */
    public void buttonPressed(int buttonId) {
        switch (buttonId) {
	case OK:
	    doOk();
	    break;
	case CANCEL:
	    doCancel();
	    break;
	case HELP:
	    doHelp();
	    break;
	case RESET:
	    doReset();
	    break;
	default:
	    break;
	 }
    }

    /**
     * Handle user pressing OK button
     */
    protected abstract void doOk();

    /**
     * Handle user pressing Cancel button, default is to disappear
     */
    protected void doCancel() {
    	setVisible(false);
	dispose();
    }

    /**
     * Handle user pressing Reset; this default implementation does
     * nothing, subclasses should override.
     */
    protected void doReset() {
    	// Do nothing
    }

    /**
     * Handle user pressing Help
     */
    protected void doHelp() {
        DhcpmgrApplet.showHelp(getHelpKey());
    }

    /**
     * Return the help key
     */
    protected abstract String getHelpKey();
}
