Module: SimpleGuiCreator

Defined in:
lib/simple_gui_creator/parse_template.rb,
lib/simple_gui_creator.rb,
lib/simple_gui_creator.rb,
lib/simple_gui_creator/swing_helpers.rb,
lib/simple_gui_creator/simple_gui_creator.rb

Overview

for documentation, see the README file

Defined Under Namespace

Classes: DropDownSelector, FileDialog, JButton, JCheckBox, JComboBox, JFileChooser, JOptionPane, NonBlockingDialog, ParseTemplate

Constant Summary collapse

JFile =

no import for this one, so we don’t lose access to Ruby’s File class

java.io.File

Class Method Summary collapse

Class Method Details

.get_password_input(text) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 182

def self.get_password_input text
  p 'please enter password at prompt'
  pwd = JPasswordField.new(10)
  got = JOptionPane.showConfirmDialog(nil, pwd, text, JOptionPane::OK_CANCEL_OPTION) < 0
  if got
    raise 'cancelled ' + text
  else
    # convert to ruby string [?]
    out = ''
    pwd.password.each{|b|
      out << b
    }
    out
  end
end

.get_user_input(message, default = '', cancel_or_blank_ok = false) ⇒ Object

prompts for user input, raises if they cancel the prompt or if they enter nothing



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 122

def self.get_user_input(message, default = '', cancel_or_blank_ok = false)
  p 'please enter the information in the prompt:' + message[0..50] + '...'
  received = javax.swing.JOptionPane.showInputDialog(nil, message, default)
  if !cancel_or_blank_ok
    raise 'user cancelled input prompt ' + message unless received
	  raise 'did not enter anything?' + message unless received.present?
 received = nil # always return nil
  end
  p 'received answer:' + received
  received
end

.hard_exit!Object



198
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 198

def self.hard_exit!; java::lang::System.exit 0; end

.invoke_in_gui_threadObject

sometimes I think I need this, but it never seems to help



200
201
202
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 200

def self.invoke_in_gui_thread # sometimes I think I need this, but it never seems to help
  SwingUtilities.invoke_later { yield }
end

.new_existing_dir_chooser_and_go(title = nil, default_dir = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 78

def self.new_existing_dir_chooser_and_go title=nil, default_dir = nil
  chooser = JFileChooser.new;
  chooser.setCurrentDirectory(JFile.new default_dir) if default_dir
  chooser.set_title title
  chooser.setFileSelectionMode(JFileChooser::DIRECTORIES_ONLY)
  chooser.setAcceptAllFileFilterUsed(false)
  chooser.set_approve_button_text "Select This Directory"

  if (chooser.showOpenDialog(nil) == JFileChooser::APPROVE_OPTION)
    return chooser.getSelectedFile().get_absolute_path
  else
   raise "No dir selected " + title.to_s
  end
end

.new_nonexisting_filechooser_and_go(title = nil, default_dir = nil, default_filename = nil) ⇒ Object

choose a file that may or may not exist yet…



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 57

def self.new_nonexisting_filechooser_and_go title = nil, default_dir = nil, default_filename = nil # within JFileChooser class for now...
  out = JFileChooser.new
  out.set_title title
  if default_dir
    out.set_current_directory JFile.new(default_dir)
  end
  if default_filename
    out.set_file default_filename
  end
	  found_non_exist = false
	  while(!found_non_exist) 
    got = out.go true
		if(File.exist? got) 
SimpleGuiCreator.show_blocking_message_dialog 'this file already exists, choose a new filename ' + got		  
		else
		 found_non_exist = true
		end
	  end
	  got
end

.new_nonexisting_or_existing_filechooser_and_go(title = nil, default_dir = nil, default_filename = nil) ⇒ Object

choose a file that may or may not exist yet…



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 40

def self.new_nonexisting_or_existing_filechooser_and_go title = nil, default_dir = nil, default_filename = nil # within JFileChooser class for now...
  out = JFileChooser.new
  out.set_title title
  if default_dir
    out.set_current_directory JFile.new(default_dir)
  end
  if default_filename
    out.set_file default_filename
  end
  got = out.go true
	  if !got
 raise "did not select anything #{title} #{default_dir} #{default_filename}"
	  end
	  got
end

.new_previously_existing_file_selector_and_go(title, use_this_dir = nil) ⇒ Object

this doesn’t have an “awesome” way to force existence, just loops



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 95

def self.new_previously_existing_file_selector_and_go title, use_this_dir = nil

  out = FileDialog.new(nil, title, FileDialog::LOAD) # LODO no self in here... ?
  out.set_title title
	out.set_filename_filter {|file, name|
        true # works os x, not doze?
	}
	
  if use_this_dir
    # FileDialog only accepts paths a certain way...
    dir = File.expand_path(use_this_dir)
    dir = dir.gsub(File::Separator, File::ALT_SEPARATOR) if File::ALT_SEPARATOR
    out.setDirectory(dir) 
  end
	got = nil
  while(!got)
 got = out.go
    raise 'cancelled choosing existing file ' + title unless got # I think we always want to raise...
 unless File.exist? got
   show_blocking_message_dialog "please select a file that already exists, or cancel" 
got = nil
 end
	end
  got
end

.open_url_to_view_it_non_blocking(url) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 29

def self.open_url_to_view_it_non_blocking url
  raise 'non http url?' unless url =~ /^http/i
  if OS.windows?
    system("start #{url.gsub('&', '^&')}") # LODO would launchy help/work here with the full url?
  else
    system "#{OS.open_file_command} \"#{url}\""
    sleep 2 # disallow any program exiting immediately...which can make the window *not* popup in certain circumstances...LODO fix [is it windows only?]
  end
end

.show_blocking_message_dialog(message, title = message.split("\n")[0], style = JOptionPane::INFORMATION_MESSAGE) ⇒ Object Also known as: show_message



163
164
165
166
167
168
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 163

def self.show_blocking_message_dialog message, title = message.split("\n")[0], style= JOptionPane::INFORMATION_MESSAGE
  puts "please use GUI window popup... #{message} ..." if $simple_creator_show_console_prompts
  JOptionPane.showMessageDialog(nil, message, title, style)
  # the above has no return value <sigh> so just return true
  true
end

.show_in_explorer(filename_or_path) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 142

def self.show_in_explorer filename_or_path
  raise 'nonexistent file cannot reveal in explorer?' + filename_or_path unless File.exist?(filename_or_path)
  filename_or_path = '"' + to_filename(filename_or_path.gsub('"', '\\"')) + '"' # quotify, escape it, FWIW untested
  if OS.doze?
    begin
      c = "explorer /e,/select,#{filename_or_path}" 
      system c # command returns immediately...so calling system on it is ok
    rescue => why_does_this_happen_ignore_this_exception_it_probably_actually_succeeded
   3 # for debugging so it can break here
    end
  elsif OS.mac?
    c = "open -R " + "\"" + filename_or_path + "\""
    system c # returns immediately
  elsif OS.linux?
    c = "nohup nautilus --no-desktop #{filename_or_path}" 
    system c
  else
    raise 'os reveal unsupported?'
  end
end

.show_non_blocking_message_dialog(message, close_button_text = 'Close') ⇒ Object



174
175
176
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 174

def self.show_non_blocking_message_dialog message, close_button_text = 'Close'
  NonBlockingDialog.new(message, close_button_text) # we don't care if they close this one via the x
end

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



178
179
180
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 178

def self.show_select_buttons_prompt message, names_hash = {}
  JOptionPane.show_select_buttons_prompt message, names_hash
end

.snake_case(string) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/simple_gui_creator.rb', line 3

def self.snake_case string
  string = string.to_s.dup
  string.gsub!(/::/, '/')
  string.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  string.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  string.tr!("-", "_")
  string.downcase!
	string
end

.to_filename(string) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/simple_gui_creator/simple_gui_creator.rb', line 134

def self.to_filename string
 if File::ALT_SEPARATOR
   File.expand_path(string).gsub('/', File::ALT_SEPARATOR)
 else
   File.expand_path(string)
 end
end