view src/luan/modules/swing/List.luan @ 1956:19de10be4c37

swing and rpc
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 28 May 2025 15:58:56 -0600
parents 92fe3927a090
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local set_metatable = Luan.set_metatable or error()
local Utils = require "luan:swing/Utils.luan"
local check_empty = Utils.check_empty or error()
local fail = Utils.fail or error()
local make_metatable = Utils.make_metatable or error()
local remove = Utils.remove or error()
local Component = require "luan:swing/Component.luan"
local super__index = Component.__index or error()
local super__new_index = Component.__new_index or error()
local super_construct = Component.construct or error()
local Swing = require "luan:swing/Swing.luan"
local alignments = Swing.alignments or error()
require "java"
local ListLuan = require "java:luan.modules.swing.ListLuan"
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "swing/List"


local List = {}

function List.__index(list,key)
	local rtn = super__index(list,key)
	if rtn ~= fail then return rtn end
	local jlist = list.java
	if key == "size" then
		return jlist.model.getSize()
	end
	if key == "selected_value" then
		return jlist.getSelectedValue()
	end
	return fail
end

function List.__new_index(list,key,value)
	local rtn = super__new_index(list,key,value)
	if rtn ~= fail then return end
	local jlist = list.java
	if key == "selected_value" then
		jlist.setSelectedValue(value,true)
		return
	end
	return fail
end

local mt = make_metatable(List)

function List.new(props)
	local jlist = ListLuan.new()
	local list = { java = jlist }
	super_construct(list,props)
	local horizontal_alignment = remove(props,"horizontal_alignment")
	if horizontal_alignment~=nil then
		local align = alignments[horizontal_alignment] or error "invalid horizontal_alignment"
		jlist.setHorizontalAlignment(align)
	end
	local hover_background = remove(props,"hover_background")
	if hover_background~=nil then
		jlist.setHoverBackground(hover_background)
	end
	check_empty(props)
	list.add_element = jlist.model.addElement
	list.remove_element = jlist.model.removeElement
	function list.repaint(value)
		jlist.repaint()
	end
	function list.add_list_selection_listener(fn)
		jlist.addListSelectionListener(fn)
	end
	set_metatable(list,mt)
	return list
end

return List