Mercurial Hosting > luan
changeset 1916:3fc4b465e9b0 default tip
File_chooser
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 17 Apr 2025 23:27:42 -0600 |
parents | 15dda75a887f |
children | |
files | src/luan/modules/editor/window.luan src/luan/modules/swing/File_chooser.luan src/luan/modules/swing/Frame.luan |
diffstat | 3 files changed, 73 insertions(+), 71 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/modules/editor/window.luan Thu Apr 17 19:02:49 2025 -0600 +++ b/src/luan/modules/editor/window.luan Thu Apr 17 23:27:42 2025 -0600 @@ -22,6 +22,8 @@ local make_find_panel = require "luan:editor/find.luan" local add_menu_bar = require "luan:editor/menu.luan" local Swing = require "luan:swing/Swing.luan" +local File_chooser = require "luan:swing/File_chooser.luan" +local choose_file = File_chooser.awt_choose_file or error() local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "editor/window" @@ -135,21 +137,21 @@ text_area.document.add_undo_listener(set_title) window.new = new_window function window.open() - local file_chooser = frame.file_chooser_load() - if file ~= nil then - file_chooser.directory = file.parent() - end - file_chooser.visible = true - local new_file = file_chooser.file + local new_file = choose_file{ + action = "load" + parent = frame + directory = file and file.parent() + } if new_file ~= nil then new_window(new_file) end end function window.save() if file == nil then - local file_chooser = frame.file_chooser_save() - file_chooser.visible = true - file = file_chooser.file + file = choose_file{ + action = "save" + parent = frame + } if file == nil then return false end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/swing/File_chooser.luan Thu Apr 17 23:27:42 2025 -0600 @@ -0,0 +1,62 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local Io = require "luan:Io.luan" +local new_file = Io.schemes.file or error() +local Utils = require "luan:swing/Utils.luan" +local delete = Utils.delete or error() +local check_empty = Utils.check_empty or error() +require "java" +local FileDialog = require "java:java.awt.FileDialog" +local JFileChooser = require "java:javax.swing.JFileChooser" +--local JFileChooser = require "java:com.formdev.flatlaf.util.SystemFileChooser" + + +local File_chooser = {} + +local awt_actions = { + load = FileDialog.LOAD + save = FileDialog.SAVE +} + +function File_chooser.awt_choose_file(props) + local parent = delete(props,"parent") + parent = parent and parent.java + local file_dialog = FileDialog.new(parent) + local directory = delete(props,"directory") + if directory~=nil then file_dialog.setDirectory(directory.to_string()) end + local action = delete(props,"action") or error[["action" property required]] + action = awt_actions[action] or error[["action" property must be "load" or "save"]] + check_empty(props) + file_dialog.setMode(action) + file_dialog.setVisible(true) + local file = file_dialog.getFile() + if file == nil then + return nil + end + local dir = file_dialog.getDirectory() + return new_file(dir..file) +end + +function File_chooser.swing_choose_file(props) + local file_chooser = JFileChooser.new() + local directory = delete(props,"directory") + if directory~=nil then file_chooser.setCurrentDirectory(directory.java.file) end + local parent = delete(props,"parent") + parent = parent and parent.java + local action = delete(props,"action") or error[["action" property required]] + check_empty(props) + local state + if action == "load" then + state = file_chooser.showOpenDialog(parent) + elseif action == "save" then + state = file_chooser.showSaveDialog(parent) + else + error[["action" property must be "load" or "save"]] + end + if state ~= JFileChooser.APPROVE_OPTION then + return nil + end + return new_file( file_chooser.getSelectedFile() ) +end + +return File_chooser
--- a/src/luan/modules/swing/Frame.luan Thu Apr 17 19:02:49 2025 -0600 +++ b/src/luan/modules/swing/Frame.luan Thu Apr 17 23:27:42 2025 -0600 @@ -1,8 +1,6 @@ local Luan = require "luan:Luan.luan" local error = Luan.error local set_metatable = Luan.set_metatable or error() -local Io = require "luan:Io.luan" -local new_file = Io.schemes.file or error() local Utils = require "luan:swing/Utils.luan" local fail = Utils.fail or error() local make_metatable = Utils.make_metatable or error() @@ -14,7 +12,6 @@ local super_construct = Awt_container.construct or error() require "java" local JFrame = require "java:javax.swing.JFrame" -local FileDialog = require "java:java.awt.FileDialog" local SwingLuan = require "java:luan.modules.swing.SwingLuan" local newCloseListener = SwingLuan.newCloseListener local Logging = require "luan:logging/Logging.luan" @@ -49,53 +46,6 @@ local mt = make_metatable(Frame) -local File_chooser = {} - -function File_chooser.__index(file_chooser,key) - if key == "file" then - local file_dialog = file_chooser.java - local file = file_dialog.getFile() - if file == nil then - return nil - end - local dir = file_dialog.getDirectory() - return file and new_file(dir..file) - end - if key == "directory" then - local dir = file_chooser.java.getDirectory() - return new_file(dir) - end - if key == "visible" then - return file_chooser.java.isVisible() - end - return fail -end - -function File_chooser.__new_index(file_chooser,key,value) - if key == "file" then - file_chooser.java.setFile(value.to_string()) - return - end - if key == "directory" then - file_chooser.java.setDirectory(value.to_string()) - return - end - if key == "visible" then - file_chooser.java.setVisible(value) - return - end - return fail -end - -local mt_fc = make_metatable(File_chooser) - -local function new_file_chooser(jframe) - local file_dialog = FileDialog.new(jframe) - local file_chooser = { java = file_dialog } - set_metatable(file_chooser,mt_fc) - return file_chooser -end - function Frame.new(props) local jframe = JFrame.new() jframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); @@ -110,18 +60,6 @@ function frame.set_menu_bar(menu_bar) jframe.setJMenuBar(menu_bar.java) end - local file_chooser - local function get_file_chooser(mode) - file_chooser = file_chooser or new_file_chooser(jframe) - file_chooser.java.setMode(mode) - return file_chooser - end - function frame.file_chooser_load() - return get_file_chooser(FileDialog.LOAD) - end - function frame.file_chooser_save() - return get_file_chooser(FileDialog.SAVE) - end frame.pack = jframe.pack set_metatable(frame,mt) return frame