/*****************************************************************************
 *                                                                           *
 *         Licensed Materials - Property of IBM                              *
 *                                                                           *
 *         "Restricted Materials of IBM"                                     *
 *                                                                           *
 *         5799-QRY                                                          *
 *                                                                           *
 *         (C) Copyright IBM Corp. 1998                                      *
 *                                                                           *
 *****************************************************************************/
package com.ibm.devtools.SIPNoTE;

import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.text.*;

/**
 * A class for creating the Jephyr window for incoming messages.
 * This class utilizes Swing version 1.0 for the GUI interface.
 * The user can open a window to respond to the received message
 * by pressing ctrl-s.  Pressing escape or clicking on the window
 * will make it disappear.
 *
 * @author Bill Nagy
 *
 * @see JSendWnd
 */
public class JephyrWnd
{
  JFrame myFrame;
  JTextArea messagearea;
  JephyrWndKeyListener windowklistener;
  JephyrWndMouseListener windowmlistener;

  /**
   * Create a new Jephyr Window.
   * 
   * @param message The Jephyr message object to be displayed.
   */
  public JephyrWnd(JMessage message)
  {
    myFrame= new JFrame("Jephyr");
    /*Create a text area for displaying the jephyr*/
    messagearea = new JTextArea();
    messagearea.setEditable(false);
    myFrame.getContentPane().add(messagearea, "Center");
    windowklistener = new JephyrWndKeyListener(message, this);
    windowmlistener = new JephyrWndMouseListener(message, this);

    int cnt;
    String msgtext = new String("To:");
    for (cnt = 0; cnt<message.getRecipientList().length; cnt++)
      {
	if (cnt == 0)
	 msgtext = msgtext.concat(" "+message.getRecipientList()[cnt]);
	else
	  msgtext = msgtext.concat(", "+message.getRecipientList()[cnt]);
      }
    msgtext = msgtext.concat("\nFrom: "+message.getSender()+"\n\n");
    
    for (cnt = 0; cnt <message.getMessage().length; cnt++)
      {
	msgtext = msgtext.concat((message.getMessage()[cnt])+"\n");
      }

    messagearea.setText(msgtext);
    myFrame.pack();
    
    /*We want to provide a minimum size so that the window
      looks nice, but we want to allow it to also grow if necessary.*/
    Dimension d = myFrame.getSize();
    if (d.height < 100)
      d.height = 100;

    if (d.width < 400)
      d.width = 400;

    myFrame.setSize(d);

    myFrame.show();
    myFrame.toFront();
  }

  public static void main(String[] argv)
  {
    JMessage testmsg =
      new JMessage((new String[] {"This is a", "test."}), "TestSender",
      new String[] {"Recipient1", "Recipient2"});
    
    JephyrWnd jwnd = new JephyrWnd(testmsg);

    try
      {
	while(true)
	  {
	    Thread.sleep(10000);
	  }
      }
    catch (InterruptedException e){}
    
  }
}


/* The classes below process the events which are received
   by the Jephyr window.*/

class JephyrWndKeyListener extends KeyAdapter
{
  private JephyrWnd window;
  private JMessage msg;
  
  JephyrWndKeyListener(JMessage msg, JephyrWnd window)
  {
    this.window = window;
    this.msg = msg;
    window.messagearea.addKeyListener(this);
  }
  
  public void keyPressed(KeyEvent e)
  {
    
    /*If the user hit ctrl-s (or just s) pull up a new send window
      with the recipients already filled in.*/
    if (e.getKeyCode() == KeyEvent.VK_S)
      {
	int cnt;
	String reciplist = new String();
	
	for (cnt = 0; cnt<msg.getRecipientList().length; cnt++)
	  {
	    if (!msg.getRecipientList()[cnt].equalsIgnoreCase(System.getProperty("user.name")))
	      reciplist = reciplist+msg.getRecipientList()[cnt]+" ";
	  }
	reciplist = reciplist+msg.getSender();
	
	JSendWnd rspwnd = new JSendWnd(reciplist);
      }
    
    /*If the user hits escape, make the window go away.*/
    if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
      {
	window.myFrame.dispose();
      }

  }

}

class JephyrWndMouseListener extends MouseAdapter
{
  private JephyrWnd window;
  private JMessage msg;
  
  JephyrWndMouseListener(JMessage msg, JephyrWnd window)
  {
    this.window = window;
    this.msg = msg;
    window.messagearea.addMouseListener(this);
  }

  /*If the user clicks, make the window go away.*/
  public void mouseClicked(MouseEvent e)
  {
    window.myFrame.dispose();
  }
}
