/*
 * @(#)JPTDialog.java    1.0  16 February 2001
 *
 * Copyright 2004
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.gui;

import java.awt.*;
import javax.swing.*;

/**
 * <P>Dialog box containing 
 * a <CODE>{@link TypedView TypedView}</CODE> 
 * and an <CODE>{@link ActionsPanel ActionsPanel}</CODE> 
 * that contains actions that interact with the view.</P>
 *
 * @author  Jeff Raab
 * @version 2.2
 * @since   1.0
 */
public class JPTDialog extends JDialog {

    /** The input view contained in this dialog. */
    protected TypedView view = null;
    
    /** 
     * The actions panel containing actions 
     * that interact with the view in this dialog. 
     */
    protected ActionsPanel actions = new ActionsPanel();
    
    //////////////////
    // Constructors //
    //////////////////

    /**
     * Constructs an input dialog 
     * containing the given view
     * but with no title or parent window.
     *
     * @param viewObject the input component to be used
     * @see #JPTDialog(TypedView, String)
     * @see #JPTDialog(TypedView, String, Dialog, boolean)
     * @see #JPTDialog(TypedView, String, Frame, boolean)
     * @throws NullPointerException if the given input component
     *      is <CODE>null</CODE>
     */
    public JPTDialog(TypedView viewObject) {
        this(viewObject, null, (Frame)null, false);
    }
    
    /**
     * Constructor for an input dialog 
     * containing the given view and the given title, 
     * but with no parent window.
     *
     * @param viewObject the input component to be used
     * @param title the title text for this dialog
     * @see #JPTDialog(TypedView)
     * @see #JPTDialog(TypedView, String, Dialog, boolean)
     * @see #JPTDialog(TypedView, String, Frame, boolean)
     * @throws NullPointerException if the given input component
     *      is <CODE>null</CODE>
     */
    public JPTDialog(TypedView viewObject, String title) {
        this(viewObject, title, (Frame)null, false);
    }

    /**
     * Constructor for an input dialog 
     * containing the given view,
     * with the given title, parent dialog, and modality.
     *
     * @param viewObject the input component to be used
     * @param title the title text for this dialog
     * @param owner the parent dialog for this dialog
     * @param modal whether or not this dialog is modal
     * @see #JPTDialog(TypedView)
     * @see #JPTDialog(TypedView, String)
     * @see #JPTDialog(TypedView, String, Frame, boolean)
     * @throws NullPointerException if the given input component
     *      is <CODE>null</CODE>
     */
    public JPTDialog(
        TypedView viewObject, 
        String title, 
        Dialog owner, 
        boolean modal) 
    {
        super(owner, (title == null ? "" : title), modal);
        initialize(viewObject);
    }
    
    /**
     * Constructor for an input dialog 
     * containing the given view,
     * with the provided title, parent frame, and modality.
     *
     * @param viewObject the input component to be used
     * @param title the title text for this dialog
     * @param owner the parent frame for this dialog
     * @param modal whether or not this dialog is modal
     * @see #JPTDialog(TypedView)
     * @see #JPTDialog(TypedView, String)
     * @see #JPTDialog(TypedView, String, Dialog, boolean)
     * @throws NullPointerException if the given input component
     *      is <CODE>null</CODE>
     */
    public JPTDialog(
        TypedView viewObject, 
        String title, 
        Frame owner, 
        boolean modal) 
    {
        super(owner, (title == null ? "" : title), modal);
        initialize(viewObject);
    }
    
    ////////////////
    // Public API //
    ////////////////
    
    /**
     * Returns the input component contained by this dialog.
     */
    public TypedView getTypedView() {
        return view;
    }

    /**
     * Adds the given action to the actions panel 
     * contained by this dialog.
     *
     * If the given action is <CODE>null</CODE>,
     * no action is added to the actions panel,
     * and the method returns <CODE>null</CODE>.
     *
     * @param a the action to be added
     * @return the button created to initiate the action
     * @see #addDefaultAction(Action)
     */
    public JButton addAction(Action a) {
        if (a == null)
            return null;
    
        return actions.addAction(a);
    }

    /**
     * Adds the given action to the actions panel 
     * contained by this dialog,
     * as the default action for the dialog.
     *
     * If the given action is <CODE>null</CODE>,
     * no action is added to the actions panel,
     * and the method returns <CODE>null</CODE>.
     *
     * The given action replaces the current default action
     * for the dialog, if one exists.
     *
     * @param action the action to be added
     * @return the button created to initiate the action
     * @see #addAction(Action)
     */
    public JButton addDefaultAction(Action a) {
        JButton defaultButton = actions.addAction(a);
        getRootPane().setDefaultButton(defaultButton);
        return defaultButton;
    }

    /**
     * Sets the actions panel for this dialog
     * to the provided actions panel.
     *
     * If the given actions panel is <CODE>null</CODE>,
     * the current actions panel is not changed.
     *
     * @param p the new actions panel
     * @see #getActionsPanel()
     */
    public void setActionsPanel(ActionsPanel p) {
        if (p == null)
            return;
    
        Container pane = (Container)getContentPane();
    
        pane.remove(actions);
        actions = p;
        pane.add(actions, BorderLayout.SOUTH);
    }
    
    /**
     * Returns the actions panel for this dialog.
     *
     * @see #setActionsPanel(ActionsPanel)
     */
    public ActionsPanel getActionsPanel() {
        return actions;
    }

    /**
     * Centers this dialog on the user screen.
     *
     * @see JDialog#pack()
     */
    public void center() {
        Dimension screen = 
            Toolkit.getDefaultToolkit().getScreenSize();

        setLocation((screen.width  - getWidth())  / 2,
                    (screen.height - getHeight()) / 2);
    }

    /////////////////////
    // Private methods //
    /////////////////////

    /**
     * Performs the common tasks 
     * required for constructors of this class.
     *
     * @param viewObject the desired input view
     * @throws NullPointerException if the given input component
     *      is <CODE>null</CODE>
     */
    private void initialize(TypedView viewObject) {
        if (viewObject == null) {
            throw new NullPointerException(
                "Input component is null.");
        }
    
        view = viewObject;

        JPanel pane = new JPanel(new BorderLayout());
        pane.add((Component)view, BorderLayout.CENTER);
        pane.add(actions, BorderLayout.SOUTH);
        
        setContentPane(pane);
    }
}
