changeset 1942:a25b7963a792 default tip

swing
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 19 May 2025 14:53:04 -0600
parents fb9563144b34
children
files src/luan/modules/swing/Awt_container.luan src/luan/modules/swing/Awt_window.luan src/luan/modules/swing/Color.luan src/luan/modules/swing/Component.luan src/luan/modules/swing/Dialog.luan src/luan/modules/swing/Frame.luan src/luan/modules/swing/Scroll_pane.luan src/luan/modules/swing/SwingLuan.java
diffstat 8 files changed, 46 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/swing/Awt_container.luan	Sun May 18 18:47:06 2025 -0600
+++ b/src/luan/modules/swing/Awt_container.luan	Mon May 19 14:53:04 2025 -0600
@@ -42,6 +42,10 @@
 		jcomponent.setLocation(to_point(value))
 		return
 	end
+	if key == "maximum_size" then
+		jcomponent.setMaximumSize(to_dimension(value))
+		return
+	end
 	return fail
 end
 
@@ -56,7 +60,10 @@
 	if location~=nil then jcomponent.setLocation(location.x,location.y) end
 	local preferred_size = delete(props,"preferred_size")
 	if preferred_size~=nil then jcomponent.setPreferredSize(to_dimension(preferred_size)) end
+	local maximum_size = delete(props,"maximum_size")
+	if maximum_size~=nil then jcomponent.setMaximumSize(to_dimension(maximum_size)) end
 	component.repaint = jcomponent.repaint
+	component.validate = jcomponent.validate
 	function component.add(el)
 		jcomponent.add(el.java)
 	end
--- a/src/luan/modules/swing/Awt_window.luan	Sun May 18 18:47:06 2025 -0600
+++ b/src/luan/modules/swing/Awt_window.luan	Mon May 19 14:53:04 2025 -0600
@@ -43,6 +43,7 @@
 	local jwindow = window.java
 	local focusable_window_state = delete(props,"focusable_window_state")
 	if focusable_window_state~=nil then jwindow.setFocusableWindowState(focusable_window_state) end
+	window.pack = jwindow.pack
 	function window.add_close_listener(close_listener)
 		jwindow.addWindowListener(newCloseListener(close_listener))
 	end
--- a/src/luan/modules/swing/Color.luan	Sun May 18 18:47:06 2025 -0600
+++ b/src/luan/modules/swing/Color.luan	Mon May 19 14:53:04 2025 -0600
@@ -4,6 +4,8 @@
 local integer = Number.integer or error()
 require "java"
 local JColor = require "java:java.awt.Color"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "swing/Color"
 
 
 local Color = {}
--- a/src/luan/modules/swing/Component.luan	Sun May 18 18:47:06 2025 -0600
+++ b/src/luan/modules/swing/Component.luan	Mon May 19 14:53:04 2025 -0600
@@ -16,6 +16,8 @@
 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"
 
@@ -108,6 +110,9 @@
 	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
 end
 Component.construct = construct
--- a/src/luan/modules/swing/Dialog.luan	Sun May 18 18:47:06 2025 -0600
+++ b/src/luan/modules/swing/Dialog.luan	Mon May 19 14:53:04 2025 -0600
@@ -55,7 +55,6 @@
 	local content_pane = delete(props,"content_pane")
 	if content_pane~=nil then jdialog.setContentPane(content_pane.java) end
 	check_empty(props)
-	dialog.pack = jdialog.pack
 	function dialog.move_into_owner()
 		jdialog.setLocationRelativeTo(jdialog.getOwner())
 	end
--- a/src/luan/modules/swing/Frame.luan	Sun May 18 18:47:06 2025 -0600
+++ b/src/luan/modules/swing/Frame.luan	Mon May 19 14:53:04 2025 -0600
@@ -52,7 +52,6 @@
 	function frame.set_menu_bar(menu_bar)
 		jframe.setJMenuBar(menu_bar.java)
 	end
-	frame.pack = jframe.pack
 	set_metatable(frame,mt)
 	return frame
 end
--- a/src/luan/modules/swing/Scroll_pane.luan	Sun May 18 18:47:06 2025 -0600
+++ b/src/luan/modules/swing/Scroll_pane.luan	Mon May 19 14:53:04 2025 -0600
@@ -12,6 +12,8 @@
 local run_later = Swing.run_later or error()
 require "java"
 local JScrollPane = require "java:javax.swing.JScrollPane"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "swing/Scroll_pane"
 
 
 local Scroll_pane = {}
--- a/src/luan/modules/swing/SwingLuan.java	Sun May 18 18:47:06 2025 -0600
+++ b/src/luan/modules/swing/SwingLuan.java	Mon May 19 14:53:04 2025 -0600
@@ -6,6 +6,7 @@
 import javax.swing.Action;
 import javax.swing.AbstractAction;
 import javax.swing.JComponent;
+import javax.swing.JViewport;
 import javax.swing.Timer;
 import javax.swing.event.ChangeListener;
 import javax.swing.event.ChangeEvent;
@@ -13,6 +14,8 @@
 import javax.swing.text.Document;
 import javax.swing.undo.UndoManager;
 import java.awt.Component;
+import java.awt.Rectangle;
+import java.awt.Point;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 import java.awt.event.WindowListener;
@@ -211,4 +214,30 @@
 		};
 		tc.getActionMap().put("redo",redoAction);
 	}
+
+	public static void scrollIntoViewVertically(JComponent component) throws LuanException {
+		Rectangle bounds = component.getBounds();
+		int y1 = bounds.y;
+		int dy = bounds.height;
+		Component c; 
+		for( c = component.getParent(); !(c instanceof JViewport); c = c.getParent() ) {
+			if( c == null )
+				throw new LuanException("Not inside a viewport");
+			bounds = c.getBounds();
+			y1 += bounds.y;
+		}
+		JViewport viewport = (JViewport)c;
+		y1 -= viewport.getView().getBounds().y;
+		int y2 = y1 + dy;
+		Point p = viewport.getViewPosition();
+		int y1View = p.y;
+		int y2View = y1View + viewport.getBounds().height;
+		if( y1View > y1 ) {
+			p.y = y1;
+			viewport.setViewPosition(p);
+		} else if ( y2View < y2 ) {
+			p.y += y2 - y2View;
+			viewport.setViewPosition(p);
+		}
+	}
 }