changeset 1884:55ad3e7cd01a

swing
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 08 Apr 2025 19:43:00 -0600
parents 3c30ae764004
children d1708f8d4923
files src/luan/modules/swing/Check_box_menu_item.luan src/luan/modules/swing/Menu.luan src/luan/modules/swing/Menu_bar.luan src/luan/modules/swing/Scroll_pane.luan src/luan/modules/swing/Swing.luan src/luan/modules/swing/SwingLuan.java src/luan/modules/swing/Utils.luan
diffstat 7 files changed, 82 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/swing/Check_box_menu_item.luan	Tue Apr 08 13:40:37 2025 -0600
+++ b/src/luan/modules/swing/Check_box_menu_item.luan	Tue Apr 08 19:43:00 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 check_empty = Utils.check_empty or error()
+local delete = Utils.delete or error()
 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()
@@ -35,10 +37,14 @@
 
 local mt = make_metatable(Check_box_menu_item)
 
-function Check_box_menu_item.new(cbmi,props)
+function Check_box_menu_item.new(props)
 	props = props or {}
-	local cbmi = { java = JCheckBoxMenuItem.new() }
+	local jcbmi = JCheckBoxMenuItem.new()
+	local cbmi = { java = jcbmi }
+	local state = delete(props,"state")
+	if state~=nil then jcbmi.setState(state) end
 	super_construct(cbmi,props)
+	check_empty(props)
 	set_metatable(cbmi,mt)
 	return cbmi
 end
--- a/src/luan/modules/swing/Menu.luan	Tue Apr 08 13:40:37 2025 -0600
+++ b/src/luan/modules/swing/Menu.luan	Tue Apr 08 19:43:00 2025 -0600
@@ -1,8 +1,11 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local set_metatable = Luan.set_metatable or error()
+local ipairs = Luan.ipairs or error()
 local Utils = require "luan:swing/Utils.luan"
 local make_metatable = Utils.make_metatable or error()
+local check_empty = Utils.check_empty or error()
+local delete = Utils.delete or error()
 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()
@@ -17,11 +20,25 @@
 Menu.__new_index = super__new_index
 local mt = make_metatable(Menu)
 
+local separator = {}
+Menu.separator = separator
+
 function Menu.new(props)
 	props = props or {}
 	local jmenu = JMenu.new()
 	local menu = { java = jmenu }
 	super_construct(menu,props)
+	local menu_items = delete(props,"menu_items")
+	if menu_items~=nil then
+		for _, menu_item in ipairs(menu_items) do
+			if menu_item == separator then
+				jmenu.addSeparator()
+			else
+				jmenu.add(menu_item.java)
+			end
+		end
+	end
+	check_empty(props)
 	menu.add_separator = jmenu.addSeparator
 	function menu.add(menu_item)
 		jmenu.add(menu_item.java)
--- a/src/luan/modules/swing/Menu_bar.luan	Tue Apr 08 13:40:37 2025 -0600
+++ b/src/luan/modules/swing/Menu_bar.luan	Tue Apr 08 19:43:00 2025 -0600
@@ -1,17 +1,38 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
+local set_metatable = Luan.set_metatable or error()
+local ipairs = Luan.ipairs or error()
+local Utils = require "luan:swing/Utils.luan"
+local make_metatable = Utils.make_metatable or error()
+local check_empty = Utils.check_empty or error()
+local check_not_nil = Utils.check_not_nil or error()
+local delete = Utils.delete or error()
+local Component = require "luan:swing/Component.luan"
+local super_construct = Component.construct or error()
 require "java"
 local JMenuBar = require "java:javax.swing.JMenuBar"
 
 
 local Menu_bar = {}
 
-function Menu_bar.new()
+local mt = make_metatable(Component)
+
+function Menu_bar.new(props)
+	check_not_nil(props)
 	local jmenu_bar = JMenuBar.new()
 	local menu_bar = { java = jmenu_bar }
+	super_construct(menu_bar)
+	local menus = delete(props,"menus")
+	if menus~=nil then
+		for _, menu in ipairs(menus) do
+			jmenu_bar.add(menu.java)
+		end
+	end
+	check_empty(props)
 	function menu_bar.add(menu)
 		jmenu_bar.add(menu.java)
 	end
+	set_metatable(menu_bar,mt)
 	return menu_bar
 end
 
--- a/src/luan/modules/swing/Scroll_pane.luan	Tue Apr 08 13:40:37 2025 -0600
+++ b/src/luan/modules/swing/Scroll_pane.luan	Tue Apr 08 19:43:00 2025 -0600
@@ -4,7 +4,7 @@
 local Utils = require "luan:swing/Utils.luan"
 local make_metatable = Utils.make_metatable or error()
 local Component = require "luan:swing/Component.luan"
-local super = Component.construct or error()
+local super_construct = Component.construct or error()
 require "java"
 local JScrollPane = require "java:javax.swing.JScrollPane"
 
@@ -16,7 +16,7 @@
 function Scroll_pane.new(view)
 	local jscroll_pane = JScrollPane.new(view.java)
 	local scroll_pane = { java = jscroll_pane }
-	super(scroll_pane)
+	super_construct(scroll_pane)
 	function scroll_pane.set_row_header_view(view)
 		jscroll_pane.setRowHeaderView(view.java)
 	end
--- a/src/luan/modules/swing/Swing.luan	Tue Apr 08 13:40:37 2025 -0600
+++ b/src/luan/modules/swing/Swing.luan	Tue Apr 08 19:43:00 2025 -0600
@@ -1,16 +1,11 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
-local Thread = require "luan:Thread.luan"
-local safe = Thread.safe or error()
 require "java"
 local SwingLuan = require "java:luan.modules.swing.SwingLuan"
 
 
 local Swing = {}
 
-function Swing.run(fn)
-	fn = safe(fn)
-	SwingLuan.run(fn)
-end
+Swing.run = SwingLuan.run
 
 return Swing
--- a/src/luan/modules/swing/SwingLuan.java	Tue Apr 08 13:40:37 2025 -0600
+++ b/src/luan/modules/swing/SwingLuan.java	Tue Apr 08 19:43:00 2025 -0600
@@ -1,5 +1,6 @@
 package luan.modules.swing;
 
+import java.lang.reflect.InvocationTargetException;
 import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 import javax.swing.KeyStroke;
@@ -15,6 +16,8 @@
 import java.awt.event.WindowListener;
 import java.awt.event.WindowEvent;
 import java.awt.event.WindowAdapter;
+import goodjava.logging.Logger;
+import goodjava.logging.LoggerFactory;
 import luan.Luan;
 import luan.LuanFunction;
 import luan.LuanTable;
@@ -23,6 +26,7 @@
 
 
 public class SwingLuan {
+	private static final Logger logger = LoggerFactory.getLogger(SwingLuan.class);
 
 	static {
 		//System.setProperty("apple.awt.application.name", "MyApplication");
@@ -43,24 +47,6 @@
 */
 	}
 
-	private static Runnable runnable(final Luan luan,final LuanFunction fn) {
-		return new Runnable() {
-			public void run() {
-				try {
-					fn.call(luan);
-				} catch(LuanException e) {
-					throw new LuanRuntimeException(e);
-				}
-			}
-		};
-	}
-
-	public static void run(Luan luan,LuanFunction fn) /*throws LuanException*/ {
-		//luan = new Luan(luan);
-		//LuanMutable.makeImmutable(fn);
-		SwingUtilities.invokeLater(runnable(luan,fn));
-	}
-
 	private static void exception(LuanException e) {
 		System.err.println(e.getLuanStackTraceString());
 		System.exit(1);
@@ -70,6 +56,22 @@
 		exception( new LuanException(msg) );
 	}
 
+	private static Runnable runnable(final Luan luan,final LuanFunction fn) {
+		return new Runnable() {
+			public void run() {
+				try {
+					fn.call(luan);
+				} catch(LuanException e) {
+					exception(e);
+				}
+			}
+		};
+	}
+
+	public static void run(Luan luan,LuanFunction fn) throws InterruptedException, InvocationTargetException {
+		SwingUtilities.invokeAndWait(runnable(luan,fn));
+	}
+
 	public static ActionListener newActionListener(final Luan luan,final LuanFunction fn) throws LuanException {
 		if( fn == null )
 			exception("function is null");
--- a/src/luan/modules/swing/Utils.luan	Tue Apr 08 13:40:37 2025 -0600
+++ b/src/luan/modules/swing/Utils.luan	Tue Apr 08 19:43:00 2025 -0600
@@ -2,6 +2,7 @@
 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()
 
@@ -36,7 +37,17 @@
 end
 
 function Utils.check_empty(props)
-	is_empty(props) or error("unrecognized keys "..stringify(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
 
 return Utils