Mercurial Hosting > luan
changeset 1616:915cb538e2a3
start swing
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 05 Jun 2021 21:42:29 -0600 |
parents | 38894708bade |
children | d88fb2eb15aa |
files | src/luan/modules/Thread.luan src/luan/modules/swing/Font.luan src/luan/modules/swing/Frame.luan src/luan/modules/swing/Label.luan src/luan/modules/swing/List.luan src/luan/modules/swing/Scroll_pane.luan src/luan/modules/swing/Swing.luan src/luan/modules/swing/SwingLuan.java src/luan/modules/swing/Text_area.luan |
diffstat | 9 files changed, 263 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/modules/Thread.luan Thu Jun 03 18:00:49 2021 -0600 +++ b/src/luan/modules/Thread.luan Sat Jun 05 21:42:29 2021 -0600 @@ -22,6 +22,7 @@ end end end +Thread.safe = safe function Thread.fork(fn) fn = safe(fn)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/swing/Font.luan Sat Jun 05 21:42:29 2021 -0600 @@ -0,0 +1,25 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local pairs = Luan.pairs or error() +require "java" +local JFont = require "java:java.awt.Font" +local TextAttribute = require "java:java.awt.font.TextAttribute" + + +local Font = {} + +local keys = { + family = TextAttribute.FAMILY + size = TextAttribute.SIZE +} + +function Font.get(attrs) + local jattrs = {} + for key, val in pairs(attrs) do + key = keys[key] or error("invalid font key: "..key) + jattrs[key] = val + end + return JFont.getFont(jattrs) +end + +return Font
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/swing/Frame.luan Sat Jun 05 21:42:29 2021 -0600 @@ -0,0 +1,41 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local set_metatable = Luan.set_metatable or error() +require "java" +local JFrame = require "java:javax.swing.JFrame" + + +local Frame = {} + +local mt = {} + +function mt.__index(frame,key) + if key == "visible" then + return frame.java.isVisible() + end + error("'"..key.."' not defined") +end + +function mt.__new_index(frame,key,value) + if key == "visible" then + frame.java.setVisible(value) + return + end + error("'"..key.."' not defined") +end + +function Frame.new(title) + local jframe = JFrame.new(title) + local frame = { java = jframe } + function frame.exit_on_close() + jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + end + function frame.add(component) + jframe.add(component.java) + end + frame.pack = jframe.pack + set_metatable(frame,mt) + return frame +end + +return Frame
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/swing/Label.luan Sat Jun 05 21:42:29 2021 -0600 @@ -0,0 +1,27 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local set_metatable = Luan.set_metatable or error() +require "java" +local JLabel = require "java:javax.swing.JLabel" + + +local Label = {} + +local mt = {} + +function mt.__index(label,key) + error("'"..key.."' not defined") +end + +function mt.__new_index(label,key,value) + error("'"..key.."' not defined") +end + +function Label.new(text) + local jlabel = JLabel.new(text) + local label = { java = jlabel } + set_metatable(label,mt) + return label +end + +return Label
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/swing/List.luan Sat Jun 05 21:42:29 2021 -0600 @@ -0,0 +1,37 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local set_metatable = Luan.set_metatable or error() +require "java" +local JList = require "java:javax.swing.JList" +local DefaultListModel = require "java:javax.swing.DefaultListModel" + + +local List = {} + +local mt = {} + +function mt.__index(list,key) + if key == "size" then + return list.model.getSize() + end + error("'"..key.."' not defined") +end + +function mt.__new_index(list,key,value) + if key == "size" then + list.model.setSize(value) + return + end + error("'"..key.."' not defined") +end + +function List.new() + local model = DefaultListModel.new() + local jlist = JList.new(model) + local list = { java=jlist, model=model } + list.add_element = model.addElement + set_metatable(list,mt) + return list +end + +return List
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/swing/Scroll_pane.luan Sat Jun 05 21:42:29 2021 -0600 @@ -0,0 +1,30 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local set_metatable = Luan.set_metatable or error() +require "java" +local JScrollPane = require "java:javax.swing.JScrollPane" + + +local Scroll_pane = {} + +local mt = {} + +function mt.__index(scroll_pane,key) + error("'"..key.."' not defined") +end + +function mt.__new_index(scroll_pane,key,value) + error("'"..key.."' not defined") +end + +function Scroll_pane.new(view) + local jscroll_pane = JScrollPane.new(view.java) + local scroll_pane = { java = jscroll_pane } + function scroll_pane.set_row_header_view(view) + jscroll_pane.setRowHeaderView(view.java) + end + set_metatable(scroll_pane,mt) + return scroll_pane +end + +return Scroll_pane
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/swing/Swing.luan Sat Jun 05 21:42:29 2021 -0600 @@ -0,0 +1,16 @@ +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 + +return Swing
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/swing/SwingLuan.java Sat Jun 05 21:42:29 2021 -0600 @@ -0,0 +1,29 @@ +package luan.modules.swing; + +import javax.swing.SwingUtilities; +import luan.Luan; +import luan.LuanFunction; +import luan.LuanMutable; +import luan.LuanException; + + +public final class SwingLuan { + + private static Runnable runnable(final Luan luan,final LuanFunction fn) { + return new Runnable() { + public void run() { + try { + fn.call(luan); + } catch(LuanException e) { + e.printStackTrace(); + } + } + }; + } + + public static void run(Luan luan,LuanFunction fn) /*throws LuanException*/ { + luan = new Luan(luan); + LuanMutable.makeImmutable(fn); + SwingUtilities.invokeLater(runnable(luan,fn)); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/swing/Text_area.luan Sat Jun 05 21:42:29 2021 -0600 @@ -0,0 +1,57 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local set_metatable = Luan.set_metatable or error() +local type = Luan.type or error() +local Font = require "luan:swing/Font.luan" +require "java" +local JTextArea = require "java:javax.swing.JTextArea" + + +local Text_area = {} + +local mt = {} + +function mt.__index(text_area,key) + if key == "line_wrap" then + return text_area.java.getLineWrap() + end + if key == "wrap_style_word" then + return text_area.java.getWrapStyleWord() + end + if key == "tab_size" then + return text_area.java.getTabSize() + end + error("'"..key.."' not defined") +end + +function mt.__new_index(text_area,key,value) + if key == "line_wrap" then + text_area.java.setLineWrap(value) + return + end + if key == "wrap_style_word" then + text_area.java.setWrapStyleWord(value) + return + end + if key == "tab_size" then + text_area.java.setTabSize(value) + return + end + if key == "font" then + if type(value) == "table" then + value = Font.get(value) + end + text_area.java.setFont(value) + return + end + error("'"..key.."' not defined") +end + +function Text_area.new(rows,columns) + local jtext_area = JTextArea.new(rows,columns) + local text_area = { java = jtext_area } + set_metatable(text_area,mt) + return text_area +end + +return Text_area