######################################################################
##
## 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 GridService::Grid
## 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 GridService::Grid operations.
######################################################################

proc view(grid, ac)
{
  # generates <h1>...</h1>
  ac.h1("Basic Actions on this Grid")

  # generates anchors to other viewing actions.
  ac.println("<UL>")
  ac.print("<LI>") ac.anchorToAction(grid, "view1", "Textual View")
  ac.print("<LI>") ac.anchorToAction(grid, "view2", "Textual View + Update")
  ac.print("<LI>") ac.anchorToAction(grid, "view3", "Tabular View")
  ac.print("<LI>") ac.anchorToAction(grid, "view4", "Form View")
  ac.println("</UL>")

  # generates a HTML form referring the bind_into_NS action.
  ac.action_form(grid,"bind_into_NS")
    # generates the submit button.
    ac.submit_button("Bind it into the Name Service")
    ac.print(" a path ")
    # generates the text field input for the variable "a_path".
    ac.text_input("a_path", 20)
  ac.end_form()

  ac.hr()
}

######################################################################
## This action binds the grid into the Name Service.
######################################################################

proc bind_into_NS(grid, ac)
{
  # gets the form variable "a_path".
  a_path = ac.getString("a_path")

  # creates a path for the Name Service.
  import NSTools
  name = NSTools.StringToName(a_path)

  # rebinds the grid into the Name Service.
  ac.getNS().rebind(name, grid)

  # generates the anchors.
  view(grid, ac)
}

######################################################################
## This action generates a textual view of a GridService::Grid.
######################################################################

proc view1(grid, ac)
{
  # generates the anchors.
  view(grid, ac)

  # generates a textual view.

  h = grid.height
  w = grid.width

  ac.println("<PRE>")
  ac.print ("Y/X")
  for j in range (0,w-1) {
    ac.print('\t')
    ac.print(j)
  }
  ac.println("")

  for i in range (0,h-1) {
    ac.print(i)
    for j in range (0,w-1) {
      ac.print('\t')
      ac.print(grid.get(i,j))
    }
    ac.println("")
  }
  ac.println("</PRE>")
}

######################################################################
## This action generates a textual view of a GridService::Grid and a
## form to update the grid.
######################################################################

proc view2(grid, ac)
{
  # generates the textual view.
  view1(grid, ac)

  # generates a HTML form referring the set_item2 action.  
  ac.action_form(grid,"set_item2")
    # generates the submit button.
    ac.submit_button("Update")
    ac.print(" X ")
    # generates the text field input for the variable "a_x".
    ac.text_input("a_x", 4)
    ac.print(" Y ")
    # generates the text field input for the variable "a_y".
    ac.text_input("a_y", 4)
    ac.print(" Value ")
    # generates the text field input for the variable "a_value".
    ac.text_input("a_value", 4)
  ac.end_form()

  # The same thing but automatically generated!
  ac.invoke_form(grid, "set")
}

######################################################################
## This action updates an item of the grid.
######################################################################

proc set_item2(grid, ac)
{
  # gets the form variables "a_y", "a_x" and "a_value".
  a_y = ac.getLong("a_y")
  a_x = ac.getLong("a_x")
  a_value = ac.getDouble("a_value")

  # executes the remote set operation.
  grid.set(a_y,a_x,a_value)

  # generates the textual view.
  ac.apply("view2", grid)
}

######################################################################
## This action generates a tabular view of a GridService::Grid and a
## form to update the grid.
######################################################################

proc view3(grid, ac)
{
  # generates the anchors.
  view(grid, ac)

  # generates a tabular view.

  h = grid.height
  w = grid.width

  ac.println("<TABLE>")

  ac.println("<TR>")
  ac.print ("<TH>Y/X</TH>")
  for j in range (0,w-1) {
    ac.print("<TH>")
    ac.print(j)
    ac.print("</TH>")
  }
  ac.println("")
  ac.println("</TR>")

  for i in range(0,h-1) {
    ac.print("<TR>")
    ac.print("<TD>")
    ac.print(i)
    ac.print("</TD>")
    for j in range (0,w-1) {
      ac.print("<TD>")
      ac.print(grid.get(i,j))
      ac.print("</TD>")
    }
    ac.print("</TR>")
  }

  ac.println("</TABLE>")

  # generates a HTML form referring the set_item3 action.
  ac.action_form(grid,"set_item3")
    # generates the submit button.
    ac.submit_button("Update")
    ac.print(" X ")
    # generates the text field input for the variable "a_x".
    ac.text_input("a_x", 4)
    ac.print(" Y ")
    # generates the text field input for the variable "a_y".
    ac.text_input("a_y", 4)
    ac.print(" Value ")
    # generates the text field input for the variable "a_value".
    ac.text_input("a_value", 4)
  ac.end_form()

  # the same thing but automatically generated!
  ac.invoke_form(grid, "set")
}

######################################################################
## This action updates an item of the grid.
######################################################################

proc set_item3(grid, ac)
{
  # gets the form variables "a_y", "a_x" and "a_value".
  a_y = ac.getLong("a_y")
  a_x = ac.getLong("a_x")
  a_value = ac.getDouble("a_value")

  # executes the remote set operation.
  grid.set(a_y,a_x,a_value)

  # generates the tabular view.
  ac.apply("view3",grid)
}

######################################################################
## This action generates a form view of a GridService::Grid.
######################################################################

proc view4(grid, ac)
{
  # generates the anchors.
  view(grid, ac)

  # generates a form view.

  h = grid.height
  w = grid.width

  ac.println("<TABLE>")
  for i in range(0,h-1) {
    ac.println("<TR>")
    for j in range(0, w-1) {
      ac.println("<TD>")
      # generates a HTML form referring the set_item4 action.
      ac.action_form(grid,"set_item4")
        ac.text_input("a_value", 3, grid.get(i,j))
        ac.hidden_input("a_x", j)
        ac.hidden_input("a_y", i)
      ac.end_form()
      ac.println("</TD>")
    }
    ac.println("</TR>")
  }
  ac.println("</TABLE>")
}

######################################################################
## This action updates an item of the grid.
######################################################################

proc set_item4(grid, ac)
{
  # gets the form variables "a_y", "a_x" and "a_value".
  a_y = ac.getLong("a_y")
  a_x = ac.getLong("a_x")
  a_value = ac.getDouble("a_value")

  # executes the remote set operation.
  grid.set(a_y,a_x,a_value)

  # generates the form view.
  ac.apply("view4",grid)
}

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

declareAction("view", view, "Views the GridService::Grid")
declareAction("bind_into_NS", bind_into_NS, "Binds into the Name Service")
declareAction("view1", view1, "Views the GridService::Grid")
declareAction("view2", view2, "Views the GridService::Grid")
declareAction("set_item2", set_item2, "Updates the GridService::Grid")
declareAction("view3", view3, "Views the GridService::Grid")
declareAction("set_item3", set_item3, "Updates the GridService::Grid")
declareAction("view4", view4, "Views the GridService::Grid")
declareAction("set_item4", set_item4, "Updates the GridService::Grid")

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