docwhat's avatardocwhat's blog

JRuby FileChooser example

I’m working on the next version of It’s All Text! and it needs a stand-alone editor server. I’ve had a couple false starts other languages but I’m thinking it should be Java or JRuby so that it is fairly portable without me having too much grief. In addition, installing Java is a little more acceptable to a random group of people than installing Ruby (at the moment, it may change in the future).

Actually, I found two projects that make it much more likely I’ll use JRuby instead of Java:

  1. rawr - a gem to bundle your JRuby project into an app, .exe or .jar. I won’t have to force the user to install Java.
  2. monkeybars - a gem to providing an MVC model for swing development. You use netbeans, whatever, to build the V, and ruby to do the M and C.

The snippet below allows the user to pick an editor application from their system. It should be able to be ported to pure Java without too much grief if I want to.

The interesting bits are the FileFilter subclass and the OS detection (JRuby specific).

require 'rbconfig'
require 'pathname'

class AppFilter < Java::javax::swing::filechooser::FileFilter
  def accept fobj
    return true if fobj.canExecute
    return fobj.isDirectory
  end

  def getDescription
    "Applications"
  end
end


# Detect OS
OS = case Config::CONFIG['host_os']
  when /darwin/      then :mac
  when /mswin|mingw/ then :windows
else
  :unix
end

# Asks the user to choose an editor.
# Returns either a Pathname object or nil (if canceled)
def pickEditor
  # Create a FileChooser
  fc = Java::javax::swing::JFileChooser.new("JRuby panel")
  fc.set_dialog_title("Please select your editor")
  fc.setFileFilter AppFilter.new

  if :mac == OS
    fc.setCurrentDirectory(java.io.File.new("/Applications"))
  elsif :windows == OS
    fc.setCurrentDirectory(java.io.File.new("C:\"))
  else
    fc.setCurrentDirectory(java.io.File.new("/usr/bin"))
  end

  success = fc.show_open_dialog(nil)
  if success == Java::javax::swing::JFileChooser::APPROVE_OPTION
    return Pathname.new(fc.get_selected_file.get_absolute_path)
  else
    nil
  end
end

puts "The user picked: #{pickEditor}"

# EOF

Comments

Gravatar for colin dean
Colin Dean

For lines 31-37, could you do this instead?

initiallocation = “/usr/bin” initiallocation = “C:\” if :windows == OS initiallocation = “/Applications” if :mac == OS fc.setCurrentDirectory(java.io.File.new(initiallocation))

From 7 lines to 4, and arguably more readable. Just flexing my early morning refactoring brain.

Gravatar for john
John

I would like to try your sample, whats in the rbconfig file ?

Thanks, John

Gravatar for docwhat
docwhat

rbconfig comes with ruby. You can check it out via irb. It’s not very well documented, but it basically includes information on how that ruby was compiled or installed on the system.

Gravatar for docwhat
docwhat

Yup. That’d work. A case statement would work too.

Gravatar for john
John

The snippet works great, thanks.

Do you use JTextArea at all ? I am having some issues with it.

Thanks, JT

Gravatar for docwhat
docwhat

Nope, not yet. I’m a complete n00b to this stuff.

Gravatar for john
John

Your code has helped figure some things out, I will try to return the favor with mine own, maybe it might help you or spark an idea.

Thanks, John

The Code: http://pastie.org/1362778 Its not as nice looking as yours, but I have only been coding for about 90 days on and off.

Good luck.

Gravatar for docwhat
docwhat

Cool!

I’ve actually worked hard at keeping my code readable. I was “insulted” by another student in college when she looked over my shoulder and said something to the effect that a child could read it. She meant to be mean, since I wasn’t use cool language tricks to make it hard to read…but I figured out that what I was doing was making the code easy to read, which made my life easier.

Ciao!

Submit a Comment

docwhat

The personal blog of Christian Höltje.
docwhat docwhat contact