﻿B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=10.7
@EndOfDesignText@
#Event: DragDropped
Sub Class_Globals
	Private DragAndDrop1 As DragAndDrop
	Public txtField As B4XFloatTextField
	Private xui As XUI
	Private mCallback As Object
	Private mEventName As String
	Public Enabled As Boolean = True
	Private mExtensions As List
End Sub

'Enables drag and drop on B4XFloatTextField. A valid drop puts the full path in the field.
'Extensions - Filter files based on case insensitive extensions. Pass Null (default) to accept all files and folders.
'MakeViewReadonly defaults to True and prevents manual editing.
'After a successful drop, raises EventName_DragDropped on Callback if the handler exists.
Public Sub Initialize (TextField As B4XFloatTextField, Extensions As List = Null, EventName As String = "", Callback As Object = Me, MakeViewReadonly As Boolean = True)
	mCallback = Callback
	mEventName = EventName
	If Initialized(Extensions) Then
		mExtensions.Initialize
		For Each ext As String In Extensions
			If ext.StartsWith(".") = False Then ext = "." & ext
			mExtensions.Add(ext.ToLowerCase)
		Next
	End If
	DragAndDrop1.Initialize(Me)
	DragAndDrop1.MakeDragTarget(TextField.mBase, "DropTarget")
	txtField = TextField
	If MakeViewReadonly Then
		If txtField.TextField Is TextArea Then
			txtField.TextField.As(TextArea).Editable = False
		Else If txtField.TextField Is TextField Then
			txtField.TextField.As(TextField).Editable = False
		End If
	End If
End Sub


Private Sub DropTarget_DragEntered(e As DragEvent)
	Dim clr As Int
	If IsValidDropEvent(e) Then
		clr = xui.Color_Green
	Else
		clr = xui.Color_Red
	End If
	txtField.TextField.SetColorAndBorder(xui.Color_White, 4dip, clr, 0)
End Sub

Private Sub DropTarget_DragExited(e As DragEvent)
	txtField.TextField.SetColorAndBorder(xui.Color_White, 1dip, xui.Color_LightGray, 2dip)
End Sub

Private Sub DropTarget_DragOver(e As DragEvent)
	If IsValidDropEvent(e) Then e.AcceptTransferModes(TransferMode.COPY)
End Sub

Private Sub IsValidDropEvent(e As DragEvent) As Boolean
	If Enabled = False Then Return False
	If e.GetDragboard.HasFiles Then
		Dim files As List = e.GetDragboard.GetFiles
		Try
			If files.Size = 1 And File.Exists(files.get(0), "") Then
				If Initialized(mExtensions) And mExtensions.Size > 0 Then
					Dim Path As String = files.Get(0)
					If File.IsDirectory(Path, "") Then Return False
					Path = Path.ToLowerCase
					For Each ext As String In mExtensions
						If Path.EndsWith(ext) Then Return True
					Next
				Else
					Return True
				End If
			End If
			Return False
		Catch
			Log(LastException)
		End Try
	End If
	Return False
End Sub

Private Sub DropTarget_DragDropped(e As DragEvent)
	If IsValidDropEvent(e) = False Then
		e.SetDropCompleted(False)
		Return
	End If
	Dim filename As String = e.GetDragboard.GetFiles.Get(0)
	txtField.Text = filename
	If xui.SubExists(mCallback, mEventName & "_DragDropped", 0) Then
		CallSubDelayed(mCallback, mEventName & "_DragDropped")
	End If
	e.SetDropCompleted(True)
End Sub