Class: SimpleGuiCreator::JOptionPane

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_gui_creator/swing_helpers.rb

Constant Summary collapse

JOptionReturnValuesTranslator =
{0 => :yes, 1 => :no, 2 => :cancel, -1 => :exited}

Class Method Summary collapse

Class Method Details

.show_select_buttons_prompt(message, names_hash = {}) ⇒ Object

accepts :yes => “yes text”, :no => “no text” returns :yes :no :cancel or :exited you must specify text for valid options. example, if you specify :cancel => ‘cancel’ then it won’t raise if they cancel raises if they select cancel or exit, unless you pass in :exited => true as an option



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/simple_gui_creator/swing_helpers.rb', line 21

def self.show_select_buttons_prompt message, names_hash = {}
  names_hash[:yes] ||= 'Yes'
  names_hash[:no] ||= 'No'
  # ok ?
  old = ['no', 'yes', 'ok'].map{|name| 'OptionPane.' + name + 'ButtonText'}.map{|name| [name, UIManager.get(name)]}
  if names_hash[:yes]
    UIManager.put("OptionPane.yesButtonText", names_hash[:yes])
  end
  if names_hash[:no]
    UIManager.put("OptionPane.noButtonText", names_hash[:no])
  end
  # if names_hash[:ok] # ???
  #   UIManager.put("OptionPane.okButtonText", names_hash[:ok])
  # end
  if names_hash[:cancel]
    UIManager.put("OptionPane.noButtonText", names_hash[:cancel])
  end
  title = message.split(' ')[0..5].join(' ')
  returned = JOptionPane.showConfirmDialog nil, message, title, JOptionPane::YES_NO_CANCEL_OPTION
  old.each{|name, old_setting| UIManager.put(name, old_setting)}
  out = JOptionReturnValuesTranslator[returned]
  if !out || !names_hash.key?(out)
    raise 'canceled or exited an option prompt:' + out.to_s + ' ' + message
  end
  out
end