view src/luan/modules/swing/Utils.luan @ 1990:f34f0a2447a5 default tip

add change_password.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 30 Jun 2025 22:52:19 -0600
parents bb12d502b72e
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local raw_set = Luan.raw_set or error()
local stringify = Luan.stringify or error()
local pairs = Luan.pairs or error()
local Table = require "luan:Table.luan"
local is_empty = Table.is_empty or error()
require "java"
local Dimension = require "java:java.awt.Dimension"
local Point = require "java:java.awt.Point"


local Utils = {}

local fail = {}
Utils.fail = fail

function Utils.make_metatable(class)
	local __index = class.__index or error()
	local __new_index = class.__new_index or error()

	local mt = {}

	function mt.__index(t,key)
		local rtn = __index(t,key)
		rtn ~= fail or error("'"..key.."' not defined")
		return rtn
	end

	function mt.__new_index(t,key,value)
		local rtn = __new_index(t,key,value)
		rtn ~= fail or error("'"..key.."' not defined")
	end

	return mt
end

function Utils.remove(t,key)
	return raw_set(t,key,nil)
end

function Utils.check_empty(props)
	if not is_empty(props) then
		local keys = {}
		for key in pairs(props) do
			keys[#keys+1] = key
		end
		error("unrecognized keys "..stringify(keys))
	end
end

function Utils.check_not_nil(props)
	props or error "missing required properties table"
end

function Utils.to_dimension(tbl)
	local width = tbl.width or error "missing width"
	local height = tbl.height or error "missing height"
	return Dimension.new(width,height)
end

function Utils.from_dimension(dim)
	return { width=dim.width, height=dim.height }
end

function Utils.to_point(tbl)
	local x = tbl.x or error "missing x"
	local y = tbl.y or error "missing y"
	return Point.new(x,y)
end

function Utils.from_point(point)
	return { x=point.x, y=point.y }
end

return Utils