view src/luan/modules/swing/Awt_container.luan @ 1944:bb12d502b72e default tip

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 20 May 2025 11:18:26 -0600
parents 763e8e77c468
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()
local Utils = require "luan:swing/Utils.luan"
local fail = Utils.fail or error()
local remove = Utils.remove or error()
local check_not_nil = Utils.check_not_nil or error()
local to_dimension = Utils.to_dimension or error()
local from_dimension = Utils.from_dimension or error()
local to_point = Utils.to_point or error()
local from_point = Utils.from_point or error()
require "java"
local SwingLuan = require "java:luan.modules.swing.SwingLuan"
local newResizeListener = SwingLuan.newResizeListener
local notifyAfterResizeStops = SwingLuan.notifyAfterResizeStops
local newMoveListener = SwingLuan.newMoveListener
local notifyAfterMoveStops = SwingLuan.notifyAfterMoveStops


local Awt_container = {}

function Awt_container.__index(component,key)
	local jcomponent = component.java
	if key == "foreground_color" then
		return jcomponent.getForeground()
	end
	if key == "visible" then
		return jcomponent.isVisible()
	end
	if key == "size" then
		return from_dimension(jcomponent.getSize())
	end
	if key == "location" then
		return from_point(jcomponent.getLocation())
	end
	return fail
end

function Awt_container.__new_index(component,key,value)
	local jcomponent = component.java
	if key == "location" then
		jcomponent.setLocation(to_point(value))
		return
	end
	if key == "maximum_size" then
		jcomponent.setMaximumSize(to_dimension(value))
		return
	end
	return fail
end

--Awt_container.mt = make_metatable(Awt_container)

function Awt_container.construct(component,props)
	check_not_nil(props)
	local jcomponent = component.java
	local size = remove(props,"size")
	if size~=nil then jcomponent.setSize(size.width,size.height) end
	local location = remove(props,"location")
	if location~=nil then jcomponent.setLocation(location.x,location.y) end
	local preferred_size = remove(props,"preferred_size")
	if preferred_size~=nil then jcomponent.setPreferredSize(to_dimension(preferred_size)) end
	local maximum_size = remove(props,"maximum_size")
	if maximum_size~=nil then jcomponent.setMaximumSize(to_dimension(maximum_size)) end
--[[
	component.repaint = jcomponent.repaint
	component.validate = jcomponent.validate
	function component.add(el)
		jcomponent.add(el.java)
	end
	function component.remove(el)
		jcomponent.remove(el.java)
	end
	function component.add_all(list)
		for _, child in ipairs(list) do
			jcomponent.add(child.java)
		end
	end
]]
	function component.add_resize_listener(fn)
		jcomponent.addComponentListener(newResizeListener(fn))
	end
	function component.add_resize_stopped_listener( time_after_stopped, fn )
		notifyAfterResizeStops( jcomponent, newResizeListener(fn), time_after_stopped )
	end
	function component.add_move_stopped_listener( time_after_stopped, fn )
		notifyAfterMoveStops( jcomponent, newMoveListener(fn), time_after_stopped )
	end
	component.request_focus_in_window = jcomponent.requestFocusInWindow
	component.request_focus = jcomponent.requestFocus
	return component
end

return Awt_container