//--------------------------------------------------------------
//
//  DataAction.Cc
//
//  Rowset data entry method and toolbar classes
//
//  Dependencies: VdBase.H
//
//  Example of use:
//
//    oForm = new form()
//    set procedure to DataAction.Cc additive
//    oForm.DataAction = new DataAction( oForm )
//    new DataBar().attach((oForm)
//
//
//  dBASE Samples Group
//
//  Copyright (c) 2000, dBASE, Inc.
//  All rights reserved.
//
//---------------------------------------------------------------
#include VdBase.h
class DataAction( oForm )

   this.Form = oForm

   function appendRow
      local bAppend
      bAppend = false
      if ( class::rowActive() )
         try
            bAppend := this.Form.rowset.beginAppend()
         catch ( Exception e )
            AlertMessage("Unable to add a new row.","Alert")
            bAppend := false
            this.Form.rowset.abandon()
         endtry
      endif
      this.refreshMenu()
      return ( bAppend )

   function deleteRow
      local bDelete 
      bDelete = false
      if ( class::rowActive() )
         if ConfirmationMessage("Delete the current row?", ;
                  "Confirmation") == BUTTON_YES
            try 
               bDelete := this.Form.rowset.delete()
            catch ( Exception e )
               AlertMessage("Unable to delete the current row."+;
                             chr(13)+e.message,"Alert")
               bDelete := false
            endtry
            if bDelete and not this.Form.rowset.next( -1 )
               this.Form.rowset.next() 
            endif
         endif
      endif
      this.refreshMenu()
      return ( bDelete )

   function saveRow
      local bSave
      bSave = false
      if ( class::rowActive() )
         try
            bSave := this.Form.rowset.save()
         catch ( Exception e )
            AlertMessage("Unable to save the row."+;
                          chr(13)+e.message,"Alert")
            bSave := false
         endtry
      endif
      return ( bSave )

   function refreshRow
      return class::rowActive() and this.Form.rowset.refreshControls()

   function abandonRow
      return class::rowActive() and this.Form.rowset.abandon()

   function rowActive
      return not ( this.Form.rowset = null or ;
                   ( not this.Form.rowset.parent.active ) )

   function refreshMenu
       local bAtFirst, bAtLast
       bAtFirst = this.Form.rowset.atFirst()
       bAtLast  = this.Form.rowset.atLast()
       if type("this.Form.root.NavMenu") == "O"
          with ( this.Form.root.NavMenu )
             menuFirst.enabled := ( not bAtFirst )
             menuPrev.enabled  := ( not bAtFirst )
             menuNext.enabled  := ( not bAtLast )
             menuLast.enabled  := ( not bAtLast )
          endwith
       endif
      return ( ( not bAtFirst ) and ( not bAtLast ) )

endclass

class Databar of toolbar

   this.text     := "Data"

   this.tAppend = new ToolButton( this )
   with ( this.tAppend )
      onClick  := { ;this.parent.form.dataAction.appendRow() }
      bitmap   := "filename :Images:new.bmp"
      speedTip := "Append Row"
   endwith

   this.tDelete = new ToolButton( this )
   with ( this.tDelete )
      onClick  := { ;this.parent.form.dataAction.deleteRow() }
      bitmap   := "filename :Images:delete.bmp"
      speedTip := "Delete Row..."
   endwith

   this.tSave = new ToolButton( this )
   with ( this.tSave )
      onClick  := { ;this.parent.form.dataAction.saveRow() }
      bitmap   := "filename :Images:save.bmp"
      speedTip := "Save Row"
   endwith

   this.tAbandon = new ToolButton( this )
   with ( this.tAbandon )
      onClick  := { ;this.parent.form.dataAction.abandonRow() }
      bitmap   := "filename :Images:abandon.bmp"
      speedTip := "Abandon Row"
   endwith


   function onUpdate

      if type("this.form.rowset.parent.active") == "L" and ;
         this.form.rowset.parent.active and ;
         this.form.rowset.state # 5

         store true to this.tAppend.enabled,;
                       this.tDelete.enabled,;
                       this.tSave.enabled,;
                       this.tAbandon.enabled

      else

         store false to this.tAppend.enabled,;
                        this.tDelete.enabled,;
                        this.tSave.enabled,;
                        this.tAbandon.enabled

      endif

endclass
