changeset 1882:f8ca4a147ac9 default tip

swing
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 07 Apr 2025 22:34:12 -0600
parents c7c7d62f8c62
children
files src/luan/modules/BasicLuan.java src/luan/modules/Utils.java src/luan/modules/swing/Abstract_button.luan src/luan/modules/swing/Button.luan src/luan/modules/swing/Check_box_menu_item.luan src/luan/modules/swing/Component.luan src/luan/modules/swing/Dialog.luan src/luan/modules/swing/Menu.luan src/luan/modules/swing/Menu_item.luan src/luan/modules/swing/Utils.luan website/src/manual.html.luan
diffstat 11 files changed, 71 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/BasicLuan.java	Mon Apr 07 21:14:53 2025 -0600
+++ b/src/luan/modules/BasicLuan.java	Mon Apr 07 22:34:12 2025 -0600
@@ -110,8 +110,10 @@
 		return table.rawGet(index);
 	}
 
-	public static void raw_set(LuanTable table,Object index,Object value) throws LuanException {
-		table.rawPut(index,value);
+	public static Object raw_set(LuanTable table,Object index,Object value) throws LuanException {
+		Utils.checkNotNull(table);
+		Utils.checkNotNull(index,2);
+		return table.rawPut(index,value);
 	}
 
 	public static int raw_len(Object v) throws LuanException {
--- a/src/luan/modules/Utils.java	Mon Apr 07 21:14:53 2025 -0600
+++ b/src/luan/modules/Utils.java	Mon Apr 07 22:34:12 2025 -0600
@@ -67,6 +67,10 @@
 		checkNotNull(fn,1);
 	}
 
+	public static void checkNotNull(Object obj,int pos) throws LuanException {
+		checkNotNull(obj,"value",pos);
+	}
+
 	public static byte[] readAll(InputStream in)
 		throws IOException
 	{
--- a/src/luan/modules/swing/Abstract_button.luan	Mon Apr 07 21:14:53 2025 -0600
+++ b/src/luan/modules/swing/Abstract_button.luan	Mon Apr 07 22:34:12 2025 -0600
@@ -1,13 +1,13 @@
 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 fail = Utils.fail or error()
 local make_metatable = Utils.make_metatable or error()
+local delete = Utils.delete 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 = Component.construct or error()
+local super_construct = Component.construct or error()
 require "java"
 
 
@@ -32,20 +32,18 @@
 	return fail
 end
 
-local mt = make_metatable(Abstract_button)
+Abstract_button.mt = make_metatable(Abstract_button)
 
-local function construct(button)
-	super(button)
+local function construct(button,props)
+	super_construct(button)
 	local jbutton = button.java
+	local text = delete(props,"text")
+	if text~=nil then jbutton.setText(text) end
+	local enabled = delete(props,"enabled")
+	if enabled~=nil then jbutton.setEnabled(enabled) end
 	button.set_enabled = jbutton.setEnabled
 	return button
 end
 Abstract_button.construct = construct
 
-function Abstract_button.new(button)
-	button = construct(button)
-	set_metatable(button,mt)
-	return button
-end
-
 return Abstract_button
--- a/src/luan/modules/swing/Button.luan	Mon Apr 07 21:14:53 2025 -0600
+++ b/src/luan/modules/swing/Button.luan	Mon Apr 07 22:34:12 2025 -0600
@@ -1,18 +1,22 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
+local set_metatable = Luan.set_metatable or error()
 local Abstract_button = require "luan:swing/Abstract_button.luan"
 local super__index = Abstract_button.__index or error()
 local super__new_index = Abstract_button.__new_index or error()
-local super_new = Abstract_button.new or error()
+local super_construct = Abstract_button.construct or error()
+local super_mt = Abstract_button.mt or error()
 require "java"
 local JButton = require "java:javax.swing.JButton"
 
 
 local Button = {}
 
-function Button.new(button)
+function Button.new(props)
+	props = props or {}
 	local button = { java = JButton.new() }
-	super_new(button)
+	super_construct(button,props)
+	set_metatable(button,super_mt)
 	return button
 end
 
--- a/src/luan/modules/swing/Check_box_menu_item.luan	Mon Apr 07 21:14:53 2025 -0600
+++ b/src/luan/modules/swing/Check_box_menu_item.luan	Mon Apr 07 22:34:12 2025 -0600
@@ -7,7 +7,7 @@
 local Menu_item = require "luan:swing/Menu_item.luan"
 local super__index = Menu_item.__index or error()
 local super__new_index = Menu_item.__new_index or error()
-local super = Menu_item.new or error()
+local super_construct = Menu_item.construct or error()
 require "java"
 local JCheckBoxMenuItem = require "java:javax.swing.JCheckBoxMenuItem"
 
@@ -35,9 +35,10 @@
 
 local mt = make_metatable(Check_box_menu_item)
 
-function Check_box_menu_item.new(cbmi)
+function Check_box_menu_item.new(cbmi,props)
+	props = props or {}
 	local cbmi = { java = JCheckBoxMenuItem.new() }
-	super(cbmi)
+	super_construct(cbmi,props)
 	set_metatable(cbmi,mt)
 	return cbmi
 end
--- a/src/luan/modules/swing/Component.luan	Mon Apr 07 21:14:53 2025 -0600
+++ b/src/luan/modules/swing/Component.luan	Mon Apr 07 22:34:12 2025 -0600
@@ -5,6 +5,8 @@
 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()
 require "java"
 local JPanel = require "java:javax.swing.JPanel"
@@ -38,7 +40,7 @@
 
 local mt = make_metatable(Component)
 
-local function construct(component)
+local function construct(component,props)
 	local jcomponent = component.java
 	jcomponent.putClientProperty("luan",component)  -- don't gc
 	component.request_focus_in_window = jcomponent.requestFocusInWindow
@@ -60,15 +62,18 @@
 end
 Component.construct = construct
 
-local function new(component)
-	component = construct(component)
+function Component.new_component(jcomponent)
+	local component = { java = jcomponent }
+	construct(component,{})
 	set_metatable(component,mt)
 	return component
 end
-Component.new = new
 
-function Component.new_panel()
-	return new{ java = JPanel.new() }
+function Component.new_panel(props)
+	local panel = { java = JPanel.new() }
+	construct(panel,props)
+	set_metatable(panel,mt)
+	return panel
 end
 
 return Component
--- a/src/luan/modules/swing/Dialog.luan	Mon Apr 07 21:14:53 2025 -0600
+++ b/src/luan/modules/swing/Dialog.luan	Mon Apr 07 22:34:12 2025 -0600
@@ -4,7 +4,7 @@
 local Utils = require "luan:swing/Utils.luan"
 local fail = Utils.fail or error()
 local make_metatable = Utils.make_metatable or error()
-local new_component = require("luan:swing/Component.luan").new or error()
+local new_component = require("luan:swing/Component.luan").new_component or error()
 require "java"
 local JDialog = require "java:javax.swing.JDialog"
 local Logging = require "luan:logging/Logging.luan"
@@ -22,7 +22,7 @@
 		local jcomponent = jdialog.getContentPane()
 		local component = jcomponent.getClientProperty("luan")
 		if component == nil then
-			component = new_component{java=jcomponent}
+			component = new_component(jcomponent)
 		end
 		return component
 	end
--- a/src/luan/modules/swing/Menu.luan	Mon Apr 07 21:14:53 2025 -0600
+++ b/src/luan/modules/swing/Menu.luan	Mon Apr 07 22:34:12 2025 -0600
@@ -6,7 +6,7 @@
 local Menu_item = require "luan:swing/Menu_item.luan"
 local super__index = Menu_item.__index or error()
 local super__new_index = Menu_item.__new_index or error()
-local super = Menu_item.construct or error()
+local super_construct = Menu_item.construct or error()
 require "java"
 local JMenu = require "java:javax.swing.JMenu"
 
@@ -17,10 +17,11 @@
 Menu.__new_index = super__new_index
 local mt = make_metatable(Menu)
 
-function Menu.new()
+function Menu.new(props)
+	props = props or {}
 	local jmenu = JMenu.new()
 	local menu = { java = jmenu }
-	super(menu)
+	super_construct(menu,props)
 	menu.add_separator = jmenu.addSeparator
 	function menu.add(menu_item)
 		jmenu.add(menu_item.java)
--- a/src/luan/modules/swing/Menu_item.luan	Mon Apr 07 21:14:53 2025 -0600
+++ b/src/luan/modules/swing/Menu_item.luan	Mon Apr 07 22:34:12 2025 -0600
@@ -4,6 +4,8 @@
 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 Abstract_button = require "luan:swing/Abstract_button.luan"
 local super__index = Abstract_button.__index or error()
 local super__new_index = Abstract_button.__new_index or error()
@@ -41,10 +43,13 @@
 
 local mt = make_metatable(Menu_item)
 
-local function construct(menu_item)
-	local menu_item = menu_item or { java = JMenuItem.new() }
-	super_construct(menu_item)
+local function construct(menu_item,props)
+	super_construct(menu_item,props)
 	local jmenu_item = menu_item.java
+	local accelerator = delete(props,"accelerator")
+	if accelerator~=nil then jmenu_item.setAccelerator(KeyStroke.getKeyStroke(accelerator)) end
+	local action_listener = delete(props,"action_listener")
+	if action_listener~=nil then jmenu_item.addActionListener(newActionListener(action_listener)) end
 	function menu_item.add_action_listener(action_listener)
 		jmenu_item.addActionListener(newActionListener(action_listener))
 	end
@@ -52,8 +57,12 @@
 end
 Menu_item.construct = construct
 
-function Menu_item.new(menu_item)
-	menu_item = construct(menu_item)
+function Menu_item.new(props)
+	props = props or {}
+	local jmenu_item = JMenuItem.new()
+	local menu_item = { java = jmenu_item }
+	menu_item = construct(menu_item,props)
+	check_empty(props)
 	set_metatable(menu_item,mt)
 	return menu_item
 end
--- a/src/luan/modules/swing/Utils.luan	Mon Apr 07 21:14:53 2025 -0600
+++ b/src/luan/modules/swing/Utils.luan	Mon Apr 07 22:34:12 2025 -0600
@@ -1,6 +1,9 @@
 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 Table = require "luan:Table.luan"
+local is_empty = Table.is_empty or error()
 
 
 local Utils = {}
@@ -28,4 +31,12 @@
 	return mt
 end
 
+function Utils.delete(t,key)
+	return raw_set(t,key,nil)
+end
+
+function Utils.check_empty(props)
+	is_empty(props) or error("unrecognized keys "..stringify(props))
+end
+
 return Utils
--- a/website/src/manual.html.luan	Mon Apr 07 21:14:53 2025 -0600
+++ b/website/src/manual.html.luan	Mon Apr 07 22:34:12 2025 -0600
@@ -2325,6 +2325,7 @@
 <code>table</code> must be a table,
 <code>index</code> any value different from <b>nil</b>,
 and <code>value</code> any Luan value.
+Returns the old value of <code>table[index]</code>.
 </p>
 <%
 						end