Having convinced myself that the vanilla processing selectInput is a piece of crap (yet more reflection nonsense) I though I had better come up with an alternative for ruby processing, here is such an example (yielded by my good friend google and a bit of imagination, works for me):-
require 'rbconfig'
require 'pathname'
System = Java::JavaLang::System
JFile = Java::JavaIo::File
JXChooser = Java::javax::swing::JFileChooser
class ImageFilter < Java::javax::swing::filechooser::FileFilter
def accept fobj
return [".png",".jpg"].include? File.extname(fobj.to_s).downcase
return fobj.isDirectory
end
def getDescription
"Image Files"
end
end
OS = case RbConfig::CONFIG['host_os']
when /darwin/ then :mac
when /mswin|mingw/ then :windows
else
:unix
end
def pickImage
fc = JXChooser.new("Image Chooser")
fc.set_dialog_title("Please select an image")
fc.setFileFilter ImageFilter.new
if :mac == OS
fc.setCurrentDirectory(JFile.new(System.getProperty("user.home") << "/Pictures"))
elsif :windows == OS
fc.setCurrentDirectory(JFile.new(System.getProperty("user.dir")))
else
fc.setCurrentDirectory(JFile.new(System.getProperty("user.home")))
end
success = fc.show_open_dialog(nil)
if success == JXChooser::APPROVE_OPTION
return Pathname.new(fc.get_selected_file.get_absolute_path)
else
nil
end
end
puts "The user picked: #{pickImage}"
This would be the wrapper for ruby-processing?
def select_input(*args, &block)
if block_given?
else
raise ArgumentError, "select_input must be called with a block" , caller
end
end
No comments:
Post a Comment