view src/luan/modules/swing/Component.luan @ 1943:763e8e77c468 default tip

swing
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 19 May 2025 17:19:23 -0600
parents a25b7963a792
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local type = Luan.type or error()
local set_metatable = Luan.set_metatable or error()
local ipairs = Luan.ipairs or error()
local Utils = require "luan:swing/Utils.luan"
local fail = Utils.fail or error()
local make_metatable = Utils.make_metatable or error()
local delete = Utils.delete or error()
local check_empty = Utils.check_empty or error()
local get_font = require("luan:swing/Font.luan").get or error()
local Awt_container = require "luan:swing/Awt_container.luan"
local super__index = Awt_container.__index or error()
local super__new_index = Awt_container.__new_index or error()
local super_construct = Awt_container.construct or error()
require "java"
local JComponent = require "java:javax.swing.JComponent"
local JPanel = require "java:javax.swing.JPanel"
local SwingLuan = require "java:luan.modules.swing.SwingLuan"
local scrollIntoViewVertically = SwingLuan.scrollIntoViewVertically
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "swing/Component"


local Component = {}

local alignments = {
	left = JComponent.LEFT_ALIGNMENT
}

function Component.__index(component,key)
	local rtn = super__index(component,key)
	if rtn ~= fail then return rtn end
	if key == "border" then
		return component.java.getBorder()
	end
	if key == "constraints" then
		return nil
	end
	return fail
end

function Component.__new_index(component,key,value)
	local rtn = super__new_index(component,key,value)
	if rtn ~= fail then return end
	if key == "foreground_color" then
		component.java.setForeground(value)
		return
	end
	if key == "border" then
		component.java.setBorder(value)
		return
	end
	if key == "visible" then
		component.java.setVisible(value)
		return
	end
	return fail
end

local mt = make_metatable(Component)
Component.mt = mt

local function construct(component,props)
	super_construct(component,props)
	local jcomponent = component.java
	jcomponent.putClientProperty("luan",component)
	local layout = delete(props,"layout")
	if layout~=nil then
		if type(layout) == "function" then
			layout = layout(component)
		end
		jcomponent.setLayout(layout)
	end
	local function set_font(font)
		if type(font) == "table" then
			font = get_font(font)
		end
		jcomponent.setFont(font)
	end
	local font = delete(props,"font")
	if font~=nil then set_font(font) end
	local border = delete(props,"border")
	if border~=nil then jcomponent.setBorder(border) end
	local foreground_color = delete(props,"foreground_color")
	if foreground_color~=nil then jcomponent.setForeground(foreground_color) end
	local background_color = delete(props,"background_color")
	if background_color~=nil then jcomponent.setBackground(background_color) end
	local visible = delete(props,"visible")
	if visible~=nil then jcomponent.setVisible(visible) end
	local alignment_x = delete(props,"alignment_x")
	if alignment_x~=nil then
		alignment_x = alignments[alignment_x] or error "invalid alignment_x"
		jcomponent.setAlignmentX(alignment_x)
	end
	local tool_tip_text = delete(props,"tool_tip_text")
	if tool_tip_text~=nil then jcomponent.setToolTipText(tool_tip_text) end
	component.constraints = delete(props,"constraints")
	local children = delete(props,"children")
	if children~=nil then
		for _, child in ipairs(children) do
			jcomponent.add(child.java,child.constraints)
		end
	end
	component.set_font = set_font
	component._dont_gc = {}
	function component.dont_gc(obj)
		component._dont_gc[obj] = true
	end
--[[
	function component.add(child)
		jcomponent.add(child.java,child.constraints)
	end
]]
	function component.scroll_into_view_vertically()
		scrollIntoViewVertically(jcomponent)
	end
	component.set_layout = jcomponent.setLayout
	local function changed()
		jcomponent.revalidate()
		jcomponent.repaint()
	end
	function component.add(el)
		jcomponent.add(el.java,el.constraints)
		changed()
	end
	function component.remove(el)
		jcomponent.remove(el.java)
		changed()
	end
	function component.add_all(list)
		for _, child in ipairs(list) do
			jcomponent.add(child.java)
		end
		changed()
	end
end
Component.construct = construct

function Component.new_component(jcomponent)
	local component = { java = jcomponent }
	construct(component,{})
	set_metatable(component,mt)
	return component
end

function Component.new_panel(props)
	local panel = { java = JPanel.new() }
	construct(panel,props)
	check_empty(props)
	set_metatable(panel,mt)
	return panel
end

return Component