Mercurial Hosting > luan
changeset 1945:e2d69cfe1b3c default tip
swing
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 20 May 2025 18:32:45 -0600 |
parents | bb12d502b72e |
children | |
files | src/luan/modules/swing/Abstract_button.luan src/luan/modules/swing/List.luan src/luan/modules/swing/ListLuan.java src/luan/modules/swing/Swing.luan |
diffstat | 4 files changed, 121 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/modules/swing/Abstract_button.luan Tue May 20 11:18:26 2025 -0600 +++ b/src/luan/modules/swing/Abstract_button.luan Tue May 20 18:32:45 2025 -0600 @@ -10,24 +10,19 @@ local super__index = Component.__index or error() local super__new_index = Component.__new_index or error() local super_construct = Component.construct or error() +local Swing = require "luan:swing/Swing.luan" +local alignments = Swing.alignments or error() require "java" local KeyEvent = require "java:java.awt.event.KeyEvent" local SwingLuan = require "java:luan.modules.swing.SwingLuan" local newActionListener = SwingLuan.newActionListener local newChangeListener = SwingLuan.newChangeListener -local SwingConstants = require "java:javax.swing.SwingConstants" local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "swing/Abstract_button" local Abstract_button = {} -local alignments = { - left = SwingConstants.LEFT - center = SwingConstants.CENTER - right = SwingConstants.RIGHT -} - function Abstract_button.__index(abstract_button,key) local rtn = super__index(abstract_button,key) if rtn ~= fail then return rtn end
--- a/src/luan/modules/swing/List.luan Tue May 20 11:18:26 2025 -0600 +++ b/src/luan/modules/swing/List.luan Tue May 20 18:32:45 2025 -0600 @@ -2,15 +2,18 @@ local error = Luan.error local set_metatable = Luan.set_metatable or error() local Utils = require "luan:swing/Utils.luan" +local check_empty = Utils.check_empty or error() local fail = Utils.fail or error() local make_metatable = Utils.make_metatable or error() +local remove = Utils.remove 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() +local Swing = require "luan:swing/Swing.luan" +local alignments = Swing.alignments or error() require "java" -local JList = require "java:javax.swing.JList" -local DefaultListModel = require "java:javax.swing.DefaultListModel" +local ListLuan = require "java:luan.modules.swing.ListLuan" local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "swing/List" @@ -20,8 +23,9 @@ function List.__index(list,key) local rtn = super__index(list,key) if rtn ~= fail then return rtn end + local jlist = list.java if key == "size" then - return list.model.getSize() + return jlist.model.getSize() end return fail end @@ -29,31 +33,35 @@ function List.__new_index(list,key,value) local rtn = super__new_index(list,key,value) if rtn ~= fail then return end - local model = list.model - if model.instanceof(DefaultListModel) then - if key == "size" then - list.model.setSize(value) - return - end - end return fail end local mt = make_metatable(List) -local function new(model) - local jlist = JList.new(model) - local list = { java = jlist, model = model } - super(list) - if model.instanceof(DefaultListModel) then - list.add_element = model.addElement +function List.new(props) + local jlist = ListLuan.new() + local list = { java = jlist } + super_construct(list,props) + local horizontal_alignment = remove(props,"horizontal_alignment") + if horizontal_alignment~=nil then + local align = alignments[horizontal_alignment] or error "invalid horizontal_alignment" + jlist.setHorizontalAlignment(align) + end + local hover_background = remove(props,"hover_background") + if hover_background~=nil then + jlist.setHoverBackground(hover_background) + end + check_empty(props) + list.add_element = jlist.model.addElement + list.remove_element = jlist.model.removeElement + function list.set_selected_value(value) + jlist.setSelectedValue(value,true) + end + function list.repaint(value) + jlist.repaint() end set_metatable(list,mt) return list end -function List.new_default_list() - return new(DefaultListModel.new()) -end - return List
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/swing/ListLuan.java Tue May 20 18:32:45 2025 -0600 @@ -0,0 +1,83 @@ +package luan.modules.swing; + +import javax.swing.JList; +import javax.swing.DefaultListModel; +import javax.swing.ListCellRenderer; +import javax.swing.DefaultListCellRenderer; +import javax.swing.JLabel; +import java.awt.Component; +import java.awt.Color; +import java.awt.event.MouseMotionAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseAdapter; +import goodjava.logging.Logger; +import goodjava.logging.LoggerFactory; +import luan.LuanTable; + + +public class ListLuan extends JList<Object> { + private static final Logger logger = LoggerFactory.getLogger(ListLuan.class); + + private static final ListCellRenderer<Object> cellRenderer = new DefaultListCellRenderer() { + private final JLabel label = new JLabel(); + + @Override public Component getListCellRendererComponent(JList<?> jlist,Object value,int index,boolean isSelected,boolean cellHasFocus) { + super.getListCellRendererComponent(jlist,value,index,isSelected,cellHasFocus); + ListLuan list = (ListLuan)jlist; + if( list.horizontalAlignment != -1 ) + setHorizontalAlignment(list.horizontalAlignment); + if( !isSelected && index == list.hoveredIndex ) + setBackground(list.hoverBackground); + if( value instanceof LuanTable ) { + LuanTable tbl = (LuanTable)value; + String text = (String)tbl.rawGet("text"); + if( text != null ) + setText(text); + Color foregroundColor = (Color)tbl.rawGet("foreground_color"); + if( foregroundColor != null ) + setForeground(foregroundColor); + } + return this; + } + }; + + public final DefaultListModel<Object> model; + private int horizontalAlignment = -1; + private Color hoverBackground = null; + private int hoveredIndex = -1; + + public ListLuan() { + super(new DefaultListModel<Object>()); + this.model = (DefaultListModel)getModel(); + //logger.info(getSelectionBackground().toString()); + setCellRenderer(cellRenderer); + addMouseMotionListener(new MouseMotionAdapter() { + @Override public void mouseMoved(MouseEvent e) { + if( hoverBackground == null ) return; + int index = locationToIndex(e.getPoint()); + if (index != hoveredIndex) { + hoveredIndex = index; + repaint(); + } + } + }); + addMouseListener(new MouseAdapter() { + @Override public void mouseExited(MouseEvent e) { + if( hoverBackground == null ) return; + hoveredIndex = -1; + repaint(); + } + }); + } + + public void setHorizontalAlignment(int alignment) { + this.horizontalAlignment = alignment; + repaint(); + } + + public void setHoverBackground(Color hoverBackground) { + this.hoverBackground = hoverBackground; + this.hoveredIndex = -1; + repaint(); + } +}
--- a/src/luan/modules/swing/Swing.luan Tue May 20 11:18:26 2025 -0600 +++ b/src/luan/modules/swing/Swing.luan Tue May 20 18:32:45 2025 -0600 @@ -8,6 +8,7 @@ local Desktop = require "java:java.awt.Desktop" local Window = require "java:java.awt.Window" local UIManager = require "java:javax.swing.UIManager" +local SwingConstants = require "java:javax.swing.SwingConstants" local FlatLightLaf = require "java:com.formdev.flatlaf.FlatLightLaf" local SwingLuan = require "java:luan.modules.swing.SwingLuan" local Logging = require "luan:logging/Logging.luan" @@ -55,4 +56,10 @@ end end +Swing.alignments = { + left = SwingConstants.LEFT + center = SwingConstants.CENTER + right = SwingConstants.RIGHT +} + return Swing