/*
 * @(#)TextAreaView.java    1.0  18 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 edu.neu.ccs.*;
import edu.neu.ccs.util.*;
import javax.swing.*;

/**
 * <P>A <CODE>{@link TypedView TypedView}</CODE> 
 * for input of <CODE>String</CODE>s that may represent
 * multiple lines of text.</P>
 *
 * @author  Jeff Raab
 * @version 2.2
 * @since   1.0
 */
public class TextAreaView 
    extends JTextArea 
    implements TypedView, JPTConstants 
{
    /** Default content for this view. */
    protected String defaultValue = "";

    /** Property list for this view object. */
    protected InputProperties properties = new InputProperties();
    
    //////////////////
    // Constructors //
    //////////////////
    
    /**
     * Constructs an empty text area view.
     *
     * @see #TextAreaView(String)
     */
    public TextAreaView() {
        this("");
    }
    
    /**
     * Constructs a text area view with the given default content.
     *
     * @param text the default content for this view
     * @see #TextAreaView()
     */
    public TextAreaView(String text) {
        super(text = ((text == null) ? "" : text));

        setDefaultViewState(text);
    }

    ///////////////
    // TypedView //
    ///////////////

    /**
     * Returns an <CODE>XString</CODE> object 
     * whose state is set to the <CODE>String</CODE> content
     * for this view.
     *
     * @see #requestObject()
     * @see TypedView
     */
    public Stringable demandObject() {
        return new XString(getViewState());
    }

    /**
     * Returns an <CODE>XString</CODE> object 
     * whose state is set to the <CODE>String</CODE> content
     * for this view.
     *
     * @see #demandObject()
     * @see TypedView
     */
    public Stringable requestObject() {
        return demandObject();
    }

    public void setInputProperties(InputProperties p) {
        if (p == null)
            p = InputProperties.BASE_PROPERTIES;
    
        InputProperties oldProperties = getInputProperties();
    
        properties = p;
        
        // if the input properties has changed
        if ((getInputProperties() != null) &&
            !getInputProperties().equals(oldProperties)) {
        
            // notify listeners of property change
            firePropertyChange(
                INPUT_PROPERTIES,
                oldProperties,
                getInputProperties());
        }
    }
    
    public InputProperties getInputProperties() {
        return properties;
    }

    /**
     * Returns the <CODE>XString</CODE> class object.
     *
     * @see TypedView
     */ 
    public Class getDataType() {
        return XString.class;
    }
    
    /////////////////
    // Displayable //
    /////////////////

    /**
     * Sets the content for this text area view
     * to the given <CODE>String</CODE>.  
     *
     * If the provided data is <CODE>null</CODE>,
     * the content for this text area 
     * is set to the empty string.
     *
     * @param data the new content for this text area view
     * @see #getViewState()
     * @see Displayable
     */
    public void setViewState(String data) {
        if (data == null)
            data = "";

        setText(data);

        // notify listeners of property change
        firePropertyChange(
            VIEW_STATE, 
            null, 
            data);
    }
    
    /**
     * Returns the content for this text area view.
     *
     * @see #setViewState(String)
     * @see Displayable
     */
    public String getViewState() {
        return getText();
    }

    /**
     * Sets the default content for this text area view
     * to the given <CODE>String</CODE>.  
     *
     * If the provided data is <CODE>null</CODE>,
     * the default content for this text area 
     * is set to the empty string.
     *
     * @param data the new default content for this text area view
     * @see #reset()
     * @see #getDefaultViewState()
     * @see Displayable
     */
    public void setDefaultViewState(String data) {
        data = (data == null) ? "" : data;
        defaultValue = data;

        // notify listeners of property change
        firePropertyChange(DEFAULT_VIEW_STATE, null, data);
    }

    /**
     * Returns the default content for this text area view.
     *
     * @see #setDefaultViewState(String)
     * @see #reset()
     */
    public String getDefaultViewState() {
        return defaultValue;
    }
    
    /**
     * Sets the content for this text area view 
     * to the default content for this text area view.
     * 
     * @see #setDefaultViewState(String)
     * @see Displayable
     */
    public void reset() {
        setViewState(getDefaultViewState());
    }
}
