######################################################################
##
## Copyright (c) LIFL 1999
## The GOODE team of the LIFL
## University of Lille, France
## All rights reserved
##
## The CorbaWeb script associated to the OMG IDL ChatService::Proxy
## interface.
##
######################################################################

# Each action must have two arguments: the first one refers to the
# current CORBA object and the second one refers to a CorbaWeb
# ActionContext instance.

######################################################################
## This action manages the output frame.
######################################################################

import CorbaWeb

# Inheritance to be a CORBA callback object which pushes Web documents.
class UserImpl(CorbaWeb.PushCallbackObject)
{
  # The constructor.

  proc __UserImpl__(self, proxy, ac)
  {
    # Initializes the PushCallbackObject part.
    self.__PushCallbackObject__(ac, ChatService.User)

    # Changes the User associated to the proxy.
    proxy.change_user(self)

    # Stores the Proxy.
    self._proxy = proxy
  }

  # When the navigator quits this page, then stops the Proxy.

  proc stop_callback(self)
  {
    println("UserImpl::stop_callback")
    self._proxy.quit()
  }

  # OMG IDL: oneway void receive(in Message m);
  #
  proc receive(self, message)
  {
    # Begins the pushed document.
    self.begin_push("text/plain")

    self._ac.println(message.n + ": " + message.t)

    # End of the pushed document.
    self.end_push()
  }
    
  # OMG IDL: oneway void chat_stopped();
  #
  proc chat_stopped(self)
  {
    # Begins the pushed document.
    self.begin_push("text/plain")

    self._ac.println("The chat is stopped!")

    # End of the pushed document.
    self.end_push()

    # Disconnects the object and closes the Web link.
    self.stop_push()
  }
}

proc output_frame(proxy, ac)
{
  # Creates a User for this Proxy.
  user = UserImpl(proxy, ac)

  # Pushes the MIME header.
  user.send_push_header()
}

######################################################################
## This action manages the input frame.
######################################################################

proc input_frame(proxy, ac)
{
  # generates a HTML form referring the do_send action.
  ac.action_form(proxy,"do_send")
    # generates the submit button.
    ac.submit_button("Send")
    ac.println(" the following text ")
    # generates the text field input for the variable "a_text".
    ac.text_input("a_text", 40)
  ac.end_form()
}

######################################################################
## This action executes the send operation on a ChatService::Proxy.
######################################################################

proc do_send(proxy, ac)
{
  # gets the form variable "a_text".
  a_text = ac.getString("a_text")

  # executes the remote send operation.
  proxy.send(a_text)

  # generates the form.
  input_frame(proxy, ac)
}


######################################################################
## Declaration of these actions into the CorbaWeb ActionRepository.
######################################################################

declareAction("output_frame", output_frame,
                      "Frame for outputs")
declareAction("input_frame", input_frame,
                      "Frame for inputs")
declareAction("do_send", do_send, "Do a send")

######################################################################
# end of file 'CorbaWeb/actions/ChatService_Proxy'
######################################################################
