import java.awt.*;
import java.util.StringTokenizer;
 
/**
 * CSC 442F:    Software Engineering
 * Software:    Calendar Client
 * Component:   DescriptionPanel class
 * Description: The DescriptionPanel class produces a panel for describing
 *              an appointment. This panel accepts date input from the user.
 *
 * @author David Tam
 * @version 1.0
 */
public class DescriptionPanel extends Panel
{ 
  // Text input areas
  public Label description;

  // Text input areas
  public TextField descriptionTxt;

  // GridBagLayout constraints
  protected GridBagConstraints constraints;

 /** 
  * Constructor
  * ===========
  *
  */
  public DescriptionPanel()
  {
    description  = new Label("Description");
    descriptionTxt  = new TextField(55);

    // Constraints instantiation
    constraints = new GridBagConstraints();
    
    // Panel layout
    setLayout(new GridBagLayout());
    constraints.anchor=constraints.WEST;
    constraints.gridx=0;
    constraints.gridy=0;
    add(description, constraints);
    constraints.gridy=1;
    add(descriptionTxt, constraints);
  }
    
  
 /**
  * Method to send build response
  * =============================
  * 
  * @return Returns a String in the form of the protocol
  * Returns "none" if text is empty.
  */
  public String buildResponse()
  {
  
    String descriptionTxt_out = descriptionTxt.getText();
    StringBuffer descFormat = new StringBuffer("");
    StringTokenizer token = new StringTokenizer(descriptionTxt_out, ",");
    boolean found=false;

    // Escape code for commas in description
    while(token.hasMoreTokens())
    {
      descFormat.append(token.nextToken());
      descFormat.append("^^");
      found=true;
    }
    if (descFormat.length()==0)
      descFormat.append("none");
    
    // Get rid of last pair of ^^
    if (found==true)
      descFormat = new StringBuffer(descFormat.toString().substring(0,
                                                        descFormat.length()-2));                                                       
    return descFormat.toString();
  }

 /**
  * Method to toggle input fields between editable and noneditable
  * ==============================================================
  *
  * @param value   A boolean value that set field to editable or noneditable
  */
  public void setEditable(boolean value)
  {
    descriptionTxt.setEditable(value);
  }

}
