Skip to content

Commit

Permalink
Finish fixing up app to use JFileChooser. [#7]
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Dec 4, 2009
1 parent 172098d commit d56cdea
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 33 deletions.
14 changes: 7 additions & 7 deletions features/step_definitions/file_chooser_steps.rb
@@ -1,30 +1,30 @@
Given t(/^the file chooser "([^\"]*)" is visible$/) do |name|
container.set dialog(name, nil)
container.set file_chooser
end

Given t(/^the file chooser "([^\"]*)" is the container$/) do |name|
container.set dialog(name, nil)
container.set file_chooser
end

When t(/^I select the (temp(?:orary)? )?file "([^\"]*)"$/) do |temp, name|
prefix = temp.to_s.empty? ? nil : $TEMP_DIR
When %Q(I fill the text field "#1" with "#{File.join [prefix, name].compact}")
file_chooser.selected_file = java.io.File.new File.join([prefix, name].compact)
end

When t(/^I approve the file chooser$/) do
When %Q(I click the button "OK")
file_chooser.approve_selection
end

When t(/^I close the file chooser "([^\"]*)"$/) do |name|
dialog(name, nil).close
file_chooser.close
end

Then t(/^I should (not )*see the file chooser "([^\"]*)"$/) do |negation, name|
if negation
expect_timeout(:id => "ComponentOperator.WaitComponentTimeout") do
dialog(name, nil)
file_chooser
end
else
dialog(name, nil).visible?.should be_true
file_chooser.visible?.should be_true
end
end
1 change: 0 additions & 1 deletion features/support/env.rb
Expand Up @@ -7,7 +7,6 @@
puts "It takes a while for the scenarios to begin executing, so please be patient..."

$NO_MAC_MENUBAR = true # disable Mac-style menu bars so Swinger can drive the menus
$MOCK_FILE_CHOOSERS = true # don't bring up file chooser dialogs
# TODO: put those global variables in a config object or something
require path + '/../../src/main'
sleep(1)
Expand Down
10 changes: 10 additions & 0 deletions features/support/file_chooser_helper.rb
@@ -0,0 +1,10 @@
module Swinger
module FileChooser
java_import org.netbeans.jemmy.operators.JFileChooserOperator

def file_chooser
JFileChooserOperator.new
end
end
end
World(Swinger::FileChooser)
34 changes: 13 additions & 21 deletions src/application_controller.rb
Expand Up @@ -25,35 +25,27 @@ def startup
end

# Displays a file chooser dialog with the supplied title and mode.
# Mode can be :load or :save.
# Mode can be :open (with synonym :load) or :save.
# Returns the full pathname of the selected file, or nil if the dialog was cancelled.
# TODO: Should this be in its own class? Should it return the File object itself?
def choose_file(title, mode = :save)
if $MOCK_FILE_CHOOSERS # Fake the dialog since Swinger can't drive AWT widgets
choose_file_mock title, mode
else
choose_file_awt title, mode
end
choose_file_swing title, mode
end

protected

def choose_file_awt(title, mode)
java_mode = java.awt.FileDialog.const_get mode.to_s.upcase!
dialog = java.awt.FileDialog.new(nil, title, java_mode)
dialog.show
if dialog.file.nil? # User cancelled without selecting a file
return nil
end
return dialog.directory + dialog.file
end

def choose_file_mock(title, mode)
filename = javax.swing.JOptionPane.show_input_dialog(nil, _('Enter file path:'), title, javax.swing.JOptionPane::QUESTION_MESSAGE)
if filename.nil? # User cancelled
return nil
def choose_file_swing(title, mode)
mode = :open if mode == :load # support for AWT-style mode
dialog_type = javax.swing.JFileChooser.const_get(mode.to_s.upcase + '_DIALOG')
dialog = javax.swing.JFileChooser.new
dialog.dialog_title = title
dialog.dialog_type = dialog_type
dialog.file_selection_mode = javax.swing.JFileChooser::DIRECTORIES_ONLY
value = dialog.send "show_#{mode}_dialog", nil
if value == javax.swing.JFileChooser::APPROVE_OPTION
dialog.selected_file.absolute_path
else
return File.expand_path filename
nil
end
end
end
Expand Down
11 changes: 8 additions & 3 deletions src/document/document_controller.rb
Expand Up @@ -22,8 +22,13 @@ def self.create_instance(filename = nil, new_file = true)
dialog_title = new_file ? _('New') : _('Open')
filename ||= choose_file(dialog_title, new_file ? :save : :load)

@new_file = new_file
@filename = filename
super()
if filename.nil?
# User cancelled file chooser.
return nil
else
@new_file = new_file
@filename = filename
super()
end
end
end
2 changes: 1 addition & 1 deletion src/document/document_model.rb
Expand Up @@ -25,7 +25,7 @@ def filename=(string)
@filename = string
create_file @filename if @new_file
db = File.join File.expand_path(@filename), 'data'
@data = DataTableModel.new(db) if (@new_file or File.exists?(db + '.h2'))
@data = DataTableModel.new(db) # if (@new_file or File.exists?(db + '.h2'))
end
return @filename
else
Expand Down

0 comments on commit d56cdea

Please sign in to comment.