######################################################################
##
## 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 Calc 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 generates forms to execute Calc operations.
######################################################################

proc view(calc, ac)
{
  # generates <h1>...</h1>
  ac.h1("Basic Actions on this Calc object")

  # generates a HTML form referring the do_add action.
  ac.action_form(calc,"do_add")
    # generates the submit button.
    ac.submit_button("Add")
    ac.print(" n1: ")
    # generates the text field input for the variable "a_n1".
    ac.text_input("a_n1", 5)
    ac.print(" n2: ")
    # generates the text field input for the variable "a_n2".
    ac.text_input("a_n2", 5)
  ac.end_form()

  # generates a HTML form referring the do_sub action.
  ac.action_form(calc,"do_sub")
    # generates the submit button.
    ac.submit_button("Sub")
    ac.print(" n1: ")
    # generates the text field input for the variable "a_n1".
    ac.text_input("a_n1", 5)
    ac.print(" n2: ")
    # generates the text field input for the variable "a_n2".
    ac.text_input("a_n2", 5)
  ac.end_form()

  # generates a HTML form referring the do_mult action.
  ac.action_form(calc,"do_mult")
    # generates the submit button.
    ac.submit_button("Mult")
    ac.print(" n1: ")
    # generates the text field input for the variable "a_n1".
    ac.text_input("a_n1", 5)
    ac.print(" n2: ")
    # generates the text field input for the variable "a_n2".
    ac.text_input("a_n2", 5)
  ac.end_form()

  # generates a HTML form referring the do_div action.
  ac.action_form(calc,"do_div")
    # generates the submit button.
    ac.submit_button("Div")
    ac.print(" n1: ")
    # generates the text field input for the variable "a_n1".
    ac.text_input("a_n1", 5)
    ac.print(" n2: ")
    # generates the text field input for the variable "a_n2".
    ac.text_input("a_n2", 5)
  ac.end_form()
}

######################################################################
## This action executes the add operation on a Calc.
######################################################################

proc do_add(calc, ac)
{
  # gets the form variables "a_n1" and "a_n2".
  a_n1 = ac.getLong("a_n1")
  a_n2 = ac.getLong("a_n2")

  # executes the remote add operation.
  result = calc.add(a_n1, a_n2)

  # display the result.
  ac.print (a_n1)
  ac.print (" + ")
  ac.print (a_n2)
  ac.print (" = ")
  ac.println(result)

  # generates the forms.
  view(calc, ac)
}

######################################################################
## This action executes the sub operation on a Calc.
######################################################################

proc do_sub(calc, ac)
{
  # gets the form variables "a_n1" and "a_n2".
  a_n1 = ac.getLong("a_n1")
  a_n2 = ac.getLong("a_n2")

  # executes the remote sub operation.
  result = calc.sub(a_n1, a_n2)

  # display the result.
  ac.print (a_n1)
  ac.print (" - ")
  ac.print (a_n2)
  ac.print (" = ")
  ac.println(result)

  # generates the forms.
  view(calc, ac)
}

######################################################################
## This action executes the mult operation on a Calc.
######################################################################

proc do_mult(calc, ac)
{
  # gets the form variables "a_n1" and "a_n2".
  a_n1 = ac.getLong("a_n1")
  a_n2 = ac.getLong("a_n2")

  # executes the remote mult operation.
  result = calc.mult(a_n1, a_n2)

  # display the result.
  ac.print (a_n1)
  ac.print (" * ")
  ac.print (a_n2)
  ac.print (" = ")
  ac.println(result)

  # generates the forms.
  view(calc, ac)
}

######################################################################
## This action executes the div operation on a Calc.
######################################################################

proc do_div(calc, ac)
{
  # gets the form variables "a_n1" and "a_n2".
  a_n1 = ac.getLong("a_n1")
  a_n2 = ac.getLong("a_n2")

  # executes the remote div operation.
  result = calc.div(a_n1, a_n2)

  # display the result.
  ac.print (a_n1)
  ac.print (" / ")
  ac.print (a_n2)
  ac.print (" = ")
  ac.println(result)

  # generates the forms.
  view(calc, ac)
}

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

declareAction("view", view, "Views the Calc")
declareAction("do_add", do_add, "Executes the add operation")
declareAction("do_sub", do_sub, "Executes the sub operation")
declareAction("do_mult", do_mult, "Executes the mult operation")
declareAction("do_div", do_div, "Executes the div operation")

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