﻿B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=8.1
@EndOfDesignText@
'Based on CLVSelections V1.00 - https://www.b4x.com/android/forum/threads/b4x-clvselections-extended-selection-modes-for-xcustomlistview.114364/
'Amended and simplified to better suit my need, allowing multi select with ctrl and shift keys. 2021-08-27
Sub Class_Globals
	Private xui As XUI
	Public SelectedItems As B4XSet
	Public SelectedColor As Int
	Public SelectedTextColor As Int 
	Private mCLV As CustomListView
	Public UnselectedColor As Int
	Public UnselectedTextColor As Int 
	Public mMultiSelect As Boolean
	Private ctrlPressed=False, shiftPressed=False As Boolean
	Private lastSelected As Int
End Sub

Public Sub Initialize (CLV As CustomListView, MultiSelect As Boolean)
	mCLV = CLV
	mMultiSelect=MultiSelect
	SelectedColor=CLV.PressedColor
	SelectedTextColor=xui.Color_White
	UnselectedColor=xui.Color_Transparent
	UnselectedTextColor=CLV.DefaultTextColor
	SelectedItems=B4XCollections.CreateSet
	AddKeyPressedListener
End Sub

Public Sub ItemClicked (Index As Int)
	If mMultiSelect Then
		If ctrlPressed=False And shiftPressed=False Then
			Clear
			SelectItem(Index)
		Else If ctrlPressed=True And shiftPressed=False Then
			If SelectedItems.Contains(Index) Then
				DeSelectItem(Index)
			Else
				SelectItem(Index)
			End If
		Else If ctrlPressed=False And shiftPressed=True Then
			If SelectedItems.Size=0 Or IsNumber(lastSelected)=False Then
				SelectItem(Index)
			Else
				If lastSelected>Index Then
					For i=Index To lastSelected
						SelectItem(i)
					Next
				Else
					For i=lastSelected To Index
						SelectItem(i)
					Next
				End If
			End If
		End If
	Else
		Clear
		SelectItem(Index)
	End If
	lastSelected=Index
End Sub

Private Sub SelectItem(index As Int)
	Dim pnl As B4XView = mCLV.GetPanel(index)
	pnl.Color = SelectedColor
	For Each v As B4XView In pnl.GetAllViewsRecursive
		v.TextColor=SelectedTextColor
	Next
	SelectedItems.Add(index)
End Sub

Private Sub DeSelectItem(index As Int)
	Dim pnl As B4XView = mCLV.GetPanel(index)
	pnl.Color = UnselectedColor
	For Each v As B4XView In pnl.GetAllViewsRecursive
		v.TextColor=UnselectedTextColor
	Next
	SelectedItems.Remove(index)
End Sub

Public Sub Clear
	Log("selections clear")
	Log(mCLV.AsView.Tag)
	If SelectedItems.Size = 0 Then Return
	For Each i As Int In SelectedItems.AsList
		Dim pnl As B4XView = mCLV.GetPanel(i)
		pnl.Color = UnselectedColor
		For Each v As B4XView In pnl.GetAllViewsRecursive
			v.TextColor=UnselectedTextColor
		Next
	Next
	SelectedItems.Clear
End Sub

Public Sub SelectAndMakeVisible (Index As Int)
	Dim Target As Int = Max(0, Index - (mCLV.LastVisibleIndex - mCLV.FirstVisibleIndex - 1) / 2)	
		If SelectedItems.Contains(Index) = False Then
			ItemClicked(Index)		
		End If
		If mCLV.FirstVisibleIndex > Index Or mCLV.LastVisibleIndex < Index Then
			mCLV.ScrollToItem(Target)
	End If
End Sub

Private Sub AddKeyPressedListener
	Dim r As Reflector
	r.Target = mCLV.AsView 
	r.AddEventFilter("keypressed", "javafx.scene.input.KeyEvent.ANY")
End Sub

Private Sub KeyPressed_Filter (e As Event)
	Dim jo As JavaObject = e
	ctrlPressed=jo.RunMethod("isControlDown", Null)
	shiftPressed=jo.RunMethod("isShiftDown", Null)
	e.Consume
End Sub