﻿B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=8.9
@EndOfDesignText@
'Version 1.00
#Event: GetUpdatedValue (FC As FocusedCell2) As Object
#Event: EnterEditMode (FC As FocusedCell2) 
Sub Class_Globals
	Type FocusedCell2 (ColumnId As String, RowId As Long, View As B4XView, VisibleIndex As Int, PrevValue As Object, Tag As Object, ClickType As Int)
	Public CurrentlyFocusedCell2 As FocusedCell2
	Private mTable As B4XTable
	Private mCallback As Object
	Private mEventName As String
End Sub

Public Sub Initialize (Table As B4XTable, Callback As Object, EventName As String)
	mTable = Table
	mCallback = Callback
	mEventName = EventName
End Sub

'Returns true if the table is in editing state.
Public Sub getIsInEditingState As Boolean
	Return CurrentlyFocusedCell2.RowId > 0
End Sub

Private Sub IsEditingViewInCorrectPosition As Boolean
	Return CurrentlyFocusedCell2.VisibleIndex = mTable.VisibleRowIds.IndexOf(CurrentlyFocusedCell2.RowId)
End Sub

'Should be called from DataUpdated event.
Public Sub DataUpdated
	If getIsInEditingState And IsEditingViewInCorrectPosition = False Then
		ExitEditMode
	End If
End Sub

'Should be called when the table is resized.
Public Sub TableResized
	If getIsInEditingState Then SetEditableControlLayout(CurrentlyFocusedCell2)
End Sub

'Should be called when an editable cell is clicked.
Public Sub CellClicked (ColumnId As String, RowId As Long)
	MakeCellEditable(ColumnId, RowId)
	CurrentlyFocusedCell2.ClickType = 1
End Sub

' ****************************************
Public Sub CellLongClicked (ColumnId As String, RowId As Long)
	MakeCellEditable(ColumnId, RowId)
	CurrentlyFocusedCell2.ClickType = 2
End Sub
' ****************************************

Private Sub MakeCellEditable (ColumnId As String, RowId As Long)
	If getIsInEditingState And CurrentlyFocusedCell2.ColumnId = ColumnId And CurrentlyFocusedCell2.RowId = RowId Then Return
	If getIsInEditingState Then
		ExitEditMode
	End If
	CurrentlyFocusedCell2 = CreateFocusedCell2(ColumnId, RowId, Null)
	CurrentlyFocusedCell2.PrevValue = mTable.GetRow(RowId).Get(ColumnId)
	CallSub2(mCallback, mEventName & "_EnterEditMode", CurrentlyFocusedCell2)
	SetEditableControlLayout(CurrentlyFocusedCell2)
End Sub

Private Sub SetEditableControlLayout (SelectedControl As FocusedCell2)
	Dim c As B4XTableColumn = mTable.GetColumn(SelectedControl.ColumnId)
	CurrentlyFocusedCell2.VisibleIndex = mTable.VisibleRowIds.IndexOf(SelectedControl.RowId)
	Dim p As B4XView = c.CellsLayouts.Get(CurrentlyFocusedCell2.VisibleIndex + 1) '+1 because of the header
	If SelectedControl.View.Parent <> p Then
		If p.GetView(p.NumberOfViews - 1).Tag = "TextFlow" Then
			p.GetView(p.NumberOfViews - 1).RemoveViewFromParent 'remove the special TextFlow control that is added to highlight search result
		End If
		p.AddView(SelectedControl.View, 0, 0, p.Width, p.Height)
		p.GetView(c.LabelIndex).Visible = False
		SelectedControl.View.BringToFront
		CurrentlyFocusedCell2.View.RequestFocus
	Else
		SelectedControl.View.SetLayoutAnimated(0, 0, 0, p.Width, p.Height)
	End If
End Sub

'Exits edit mode
Public Sub ExitEditMode
	If getIsInEditingState = False Then Return
	Dim c As B4XTableColumn = mTable.GetColumn(CurrentlyFocusedCell2.ColumnId)
	Dim Value As Object = CallSub2(mCallback, mEventName & "_GetUpdatedValue", CurrentlyFocusedCell2)
	Dim parent As B4XView = CurrentlyFocusedCell2.View.Parent
	mTable.sql1.ExecNonQuery2($"UPDATE data SET ${c.SQLID} = ? WHERE rowid = ?"$, Array(Value, CurrentlyFocusedCell2.RowId))
	CurrentlyFocusedCell2.RowId = 0
	mTable.RefreshNow
	parent.GetView(c.LabelIndex).Visible = True
	CurrentlyFocusedCell2.View.RemoveViewFromParent
End Sub

Private Sub CreateFocusedCell2 (ColumnId As String, RowId As Long, View As B4XView) As FocusedCell2
	Dim t1 As FocusedCell2
	t1.Initialize
	t1.ColumnId = ColumnId
	t1.RowId = RowId
	t1.View = View
	Return t1
End Sub
