changeset 1870:e3a8568a11be default tip

swing
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 02 Apr 2025 21:18:25 -0600 (8 hours ago)
parents e561174a8f69
children
files src/goodjava/util/GoodUtils.java src/luan/modules/swing/Abstract_button.luan src/luan/modules/swing/Check_box_menu_item.luan src/luan/modules/swing/Component.luan src/luan/modules/swing/Document.luan src/luan/modules/swing/JUndo.java src/luan/modules/swing/LuanUndoManager.java src/luan/modules/swing/Menu.luan
diffstat 8 files changed, 94 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/util/GoodUtils.java	Wed Apr 02 17:41:21 2025 -0600
+++ b/src/goodjava/util/GoodUtils.java	Wed Apr 02 21:18:25 2025 -0600
@@ -87,4 +87,8 @@
 		return sb.toString();
 	}
 
+	public static boolean equal(Object o1,Object o2) {
+		return o1==null ? o2==null : o1.equals(o2);
+	}
+
 }
--- a/src/luan/modules/swing/Abstract_button.luan	Wed Apr 02 17:41:21 2025 -0600
+++ b/src/luan/modules/swing/Abstract_button.luan	Wed Apr 02 21:18:25 2025 -0600
@@ -2,6 +2,8 @@
 local error = Luan.error
 local Utils = require "luan:swing/Utils.luan"
 local fail = Utils.fail or error()
+local Component = require "luan:swing/Component.luan"
+local super = Component.new or error()
 require "java"
 
 
@@ -23,6 +25,7 @@
 end
 
 function Abstract_button.new(button)
+	super(button)
 	local jbutton = button.java
 	button.set_enabled = jbutton.setEnabled
 	return button
--- a/src/luan/modules/swing/Check_box_menu_item.luan	Wed Apr 02 17:41:21 2025 -0600
+++ b/src/luan/modules/swing/Check_box_menu_item.luan	Wed Apr 02 21:18:25 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 new_menu_item = Menu_item.new or error()
+local super = Menu_item.new or error()
 require "java"
 local JCheckBoxMenuItem = require "java:javax.swing.JCheckBoxMenuItem"
 
@@ -37,7 +37,7 @@
 
 function Check_box_menu_item.new(cbmi)
 	local cbmi = { java = JCheckBoxMenuItem.new() }
-	new_menu_item(cbmi)
+	super(cbmi)
 	set_metatable(cbmi,mt)
 	return cbmi
 end
--- a/src/luan/modules/swing/Component.luan	Wed Apr 02 17:41:21 2025 -0600
+++ b/src/luan/modules/swing/Component.luan	Wed Apr 02 21:18:25 2025 -0600
@@ -9,6 +9,7 @@
 
 function Component.new(component)
 	local jcomponent = component.java
+	jcomponent.putClientProperty("luan",component)  -- don't gc
 	component.request_focus_in_window = jcomponent.requestFocusInWindow
 	function component.set_font(font)
 		if type(font) == "table" then
--- a/src/luan/modules/swing/Document.luan	Wed Apr 02 17:41:21 2025 -0600
+++ b/src/luan/modules/swing/Document.luan	Wed Apr 02 21:18:25 2025 -0600
@@ -1,7 +1,7 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 require "java"
-local JUndo = require "java:luan.modules.swing.JUndo"
+local LuanUndoManager = require "java:luan.modules.swing.LuanUndoManager"
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "swing/Document"
 
@@ -10,14 +10,16 @@
 
 function Document.new(jdocument)
 	local document = { java = jdocument }
-	local undo = JUndo.new()
+	local undo = LuanUndoManager.new()
 	jdocument.addUndoableEditListener(undo)
-	jdocument.putProperty("undo",undo.manager);
-	document.undo = undo.manager.undo
-	document.redo = undo.manager.redo
-	document.can_undo = undo.manager.canUndo
-	document.can_redo = undo.manager.canRedo
-	document.add_undoable_edit_listener = undo.addListener
+	jdocument.putProperty("undo",undo);
+	document.undo = undo.undo
+	document.redo = undo.redo
+	document.can_undo = undo.canUndo
+	document.can_redo = undo.canRedo
+	document.add_undo_listener = undo.addListener
+	document.is_unedited = undo.isUnedited
+	document.set_unedited = undo.setUnedited
 	return document
 end
 
--- a/src/luan/modules/swing/JUndo.java	Wed Apr 02 17:41:21 2025 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,36 +0,0 @@
-package luan.modules.swing;
-
-import java.util.WeakHashMap;
-import javax.swing.event.UndoableEditListener;
-import javax.swing.event.UndoableEditEvent;
-import javax.swing.undo.UndoManager;
-import luan.Luan;
-import luan.LuanFunction;
-import luan.LuanException;
-import luan.LuanRuntimeException;
-
-
-public final class JUndo implements UndoableEditListener {
-	public final UndoManager manager = new UndoManager();
-	private final WeakHashMap<LuanFunction,Boolean> map = new WeakHashMap<LuanFunction,Boolean>();
-	private final Luan luan;
-
-	public JUndo(Luan luan) {
-		this.luan = luan;
-	}
-
-	@Override public void undoableEditHappened(UndoableEditEvent event) {
-		manager.addEdit(event.getEdit());
-		for( LuanFunction fn : map.keySet() ) {
-			try {
-				fn.call(luan);
-			} catch(LuanException e) {
-				throw new LuanRuntimeException(e);
-			}
-		}
-	}
-
-	public void addListener(LuanFunction fn) {
-		map.put(fn,Boolean.TRUE);
-	}
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/swing/LuanUndoManager.java	Wed Apr 02 21:18:25 2025 -0600
@@ -0,0 +1,67 @@
+package luan.modules.swing;
+
+import java.util.WeakHashMap;
+import javax.swing.event.UndoableEditListener;
+import javax.swing.event.UndoableEditEvent;
+import javax.swing.undo.UndoManager;
+import javax.swing.undo.UndoableEdit;
+import javax.swing.undo.CannotUndoException;
+import javax.swing.undo.CannotRedoException;
+import goodjava.util.GoodUtils;
+import luan.Luan;
+import luan.LuanFunction;
+import luan.LuanException;
+import luan.LuanRuntimeException;
+import goodjava.logging.Logger;
+import goodjava.logging.LoggerFactory;
+
+
+public final class LuanUndoManager extends UndoManager {
+	private static final Logger logger = LoggerFactory.getLogger(LuanUndoManager.class);
+
+	private final WeakHashMap<LuanFunction,Boolean> map = new WeakHashMap<LuanFunction,Boolean>();
+	private final Luan luan;
+	private UndoableEdit unedited = null;
+
+	public LuanUndoManager(Luan luan) {
+		this.luan = luan;
+	}
+
+	private void notifyListeners() {
+		for( LuanFunction fn : map.keySet() ) {
+			try {
+				fn.call(luan);
+			} catch(LuanException e) {
+				throw new LuanRuntimeException(e);
+			}
+		}
+	}
+
+	@Override public void undoableEditHappened(UndoableEditEvent event) {
+		super.undoableEditHappened(event);
+		notifyListeners();
+	}
+
+	@Override public void undo() throws CannotUndoException {
+		super.undo();
+		notifyListeners();
+	}
+
+	@Override public void redo() throws CannotRedoException {
+		super.redo();
+		notifyListeners();
+	}
+
+	public void addListener(LuanFunction fn) {
+		map.put(fn,Boolean.TRUE);
+	}
+
+	public boolean isUnedited() {
+		return GoodUtils.equal(unedited,editToBeUndone());
+	}
+
+	public void setUnedited() {
+		unedited = editToBeUndone();
+		notifyListeners();
+	}
+}
--- a/src/luan/modules/swing/Menu.luan	Wed Apr 02 17:41:21 2025 -0600
+++ b/src/luan/modules/swing/Menu.luan	Wed Apr 02 21:18:25 2025 -0600
@@ -3,20 +3,24 @@
 local set_metatable = Luan.set_metatable or error()
 local Utils = require "luan:swing/Utils.luan"
 local make_metatable = Utils.make_metatable or error()
-local Abstract_button = require "luan:swing/Abstract_button.luan"
+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()
 require "java"
 local JMenu = require "java:javax.swing.JMenu"
 
 
 local Menu = {}
 
-Menu.__index = Abstract_button.__index or error()
-Menu.__new_index = Abstract_button.__new_index or error()
+Menu.__index = super__index
+Menu.__new_index = super__new_index
 local mt = make_metatable(Menu)
 
 function Menu.new()
 	local jmenu = JMenu.new()
 	local menu = { java = jmenu }
+	super(menu)
 	menu.add_separator = jmenu.addSeparator
 	function menu.add(menu_item)
 		jmenu.add(menu_item.java)