Skip to content

Commit

Permalink
huge rename <sniff>
Browse files Browse the repository at this point in the history
  • Loading branch information
rdp committed Jun 19, 2012
1 parent 72d4f2b commit 9cc23bc
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 23 deletions.
14 changes: 7 additions & 7 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Here's how:
You can program behavior, like this:

>> frame.elements['button1'].on_clicked {
SwingHelpers.show_blocking_message_dialog "you clicked button1!"
SimpleGui.show_blocking_message_dialog "you clicked button1!"
}

This has the effect of separating your view from your controller, in this case, because you can store the layouts in
Expand All @@ -25,14 +25,14 @@ and the separation allows for easy peace of mind.

It also has helper methods for common GUI tasks, like the above show_blocking_message_dialog:

SwingHelpers.new_nonexisting_filechooser_and_go # select file or filename for a "new file" (not yet existing)
SwingHelpers.new_existing_dir_chooser_and_go # select pre-existing directory
SwingHelpers.show_in_explorer(filename) # reveals file in Explorer for windows, Finder for OS X
text_from_user = SwingHelpers.get_user_input "Input your name:" # these raise an exception if the user cancels the dialog,
SimpleGui.new_nonexisting_filechooser_and_go # select file or filename for a "new file" (not yet existing)
SimpleGui.new_existing_dir_chooser_and_go # select pre-existing directory
SimpleGui.show_in_explorer(filename) # reveals file in Explorer for windows, Finder for OS X
text_from_user = SimpleGui.get_user_input "Input your name:" # these raise an exception if the user cancels the dialog,

A select-button prompt dialog:

if(SwingHelpers.show_select_buttons_prompt("message title", :yes => 'text for the yes button', :no => 'text for the no button', :cancel => 'text for the cancel button') == :yes)
if(SimpleGui.show_select_buttons_prompt("message title", :yes => 'text for the yes button', :no => 'text for the no button', :cancel => 'text for the cancel button') == :yes)
# they chose the "yes" equivalent button...
end

Expand Down Expand Up @@ -61,7 +61,7 @@ To install/use:

$ gem install simple-ruby-gui-creator

>> require 'simple-ruby-gui-creator'
>> require 'simple_gui'
>> ... [see above]

== Known problems ==
Expand Down
47 changes: 47 additions & 0 deletions bin/simple-ruby-gui-creator
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class TestWindow < ParseTemplate::JFramer
| [ ] |
| [ ] |
| |
| [Show code snippet :create_snippet] |
--------------------------------------------------------------------------
EOL
Kernel.print string
Expand All @@ -28,6 +29,52 @@ class TestWindow < ParseTemplate::JFramer
elements[:create_button].on_clicked {
ParseTemplate::JFramer.new.parse_setup_string elements[:text_area_to_use].text
}
elements[:create_snippet].on_clicked {
frame = ParseTemplate::JFramer.new.parse_setup_string elements[:text_area_to_use].text
frame.close

element_code = ""
frame.elements.each{|e|
if e[1].is_a? Java::JavaxSwing::JButton
element_code += " elements[:#{e[0]}].on_clicked { puts 'clicked #{e[0]}' }\n"
end
}

code=<<-EOL

require 'simple-ruby-gui-creator.rb'

class MyWindow < ParseTemplate::JFramer
def initialize
super
parse_setup_filename 'my_window.template' # create this file first

#{element_code}
end

end
MyWindow.new
EOL
frame2 = ParseTemplate::JFramer.new.parse_setup_string <<-EOL
"Here's your snippet!"
[:code,width=500,height=400]
[ ]
[ ]
[Save:save1]
"Here's your template:"
[:template_text,width=500,height=400,font=fixed_width]
[ ]
[Save:save2]
EOL
frame2.elements[:code].text = code
frame2.elements[:save1].on_clicked { }
frame2.elements[:save2].on_clicked { }
frame2.elements[:template_text].text = elements[:text_area_to_use].text
}
end

def save_file default_filename
SimpleGui.new_nonexisting_or_existing_filechooser_and_go "Choose where to save:", nil, default_filename
end

end
Expand Down
17 changes: 11 additions & 6 deletions lib/simple-ruby-gui-creator/parse_template.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'java'

require File.dirname(__FILE__) + '/swing_helpers.rb' # for #close, etc., basically required as of today..
require File.dirname(__FILE__) + '/simple_gui.rb' # for JFrame#close, etc., basically required as of today..

# for docs, see the README
module ParseTemplate
Expand Down Expand Up @@ -30,7 +30,7 @@ def initialize
attr_accessor :frame

def parse_setup_filename filename
parse_string File.read(filename)
parse_setup_string File.read(filename)
self
end

Expand All @@ -41,6 +41,7 @@ def parse_setup_string string
@window_max_x = 100
all_lines = string.lines.to_a
all_lines.each_with_index{|line, idx|
begin
@current_x = 10
if line =~ /\t/
raise "sorry, tabs arent allowed, but you can request it #{line.inspect} line #{idx}"
Expand Down Expand Up @@ -68,7 +69,7 @@ def parse_setup_string string
end_spot = cur_spot + name.length
count_lines_below = 0
matching_blank_text_area_string = '[' + ' '*(end_spot-cur_spot) + ']'
empty_it_out = matching_blank_text_area_string.gsub(/./, ' ')
empty_it_out = matching_blank_text_area_string.gsub(/[\[\]]/, '_') # can't actually blank it out...
for line2 in all_lines[idx+1..-1]
if line2[cur_spot..(end_spot+1)] == matching_blank_text_area_string
line2[cur_spot, end_spot-cur_spot+2] = empty_it_out # :)
Expand Down Expand Up @@ -99,7 +100,11 @@ def parse_setup_string string
@current_y += @current_line_height
end
# build in realtime LOL
@frame.set_size @window_max_x + 25, @current_y + 40
@frame.set_size @window_max_x + 25, @current_y + 40
rescue
puts "failed on line #{line.strip} number: #{idx}"
raise
end
}
self
end
Expand All @@ -122,7 +127,7 @@ def setup_element element, name, height=nil, set_text_on_this = element
abs_y = nil
#height = nil
width = nil
if name.include? ':' # like "Start:start_button" ... disallows using colon at all, but hey...
if name.include?(':') && !name.end_with?(':') # like "Start:start_button" or "start:button:code_name,attribs" but not "Hello:" let that through
text = name.split(':')[0..-2].join(':') # only accept last colon, so they can have text with colons in it
code_name_with_attrs = name.split(':')[-1]
if code_name_with_attrs.split(',')[0] !~ /=/
Expand Down Expand Up @@ -168,7 +173,7 @@ def setup_element element, name, height=nil, set_text_on_this = element
end
if !width
if text.blank?
raise 'cannot have blank original text without some size specifier' + name
raise 'cannot have blank original text without some size specifier:' + name
end
if text.strip != text
# let blank space count as "space" for now, but don't actually set it LOL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
require 'java'
require 'sane' # gem dependency

module SwingHelpers
module SimpleGui

include_package 'javax.swing'
# will use these constants (http://jira.codehaus.org/browse/JRUBY-5107)
Expand Down Expand Up @@ -125,7 +125,7 @@ def disable

end

ToolTipManager.sharedInstance().setDismissDelay(10000)
ToolTipManager.sharedInstance().setDismissDelay(10000) # these are way too fast normally

class JFrame

Expand Down Expand Up @@ -278,14 +278,28 @@ def set_file f
alias setFile set_file

# choose a file that may or may not exist yet...
def self.new_nonexisting_filechooser_and_go title = nil, default_dir = nil, default_file = nil # within JFileChooser class for now...
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_file
out.set_file default_file
if default_filename
out.set_file default_filename
end
got = out.go true
got
end

# choose a file that may or may not exist yet...
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)
Expand Down
6 changes: 3 additions & 3 deletions lib/simple-ruby-gui-creator.rb → lib/simple_gui.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# just autoload everything always :)

module SimpleRubyGuiCreator
module SimpleGui
def self.snake_case string
string = string.to_s.dup
string.gsub!(/::/, '/')
Expand All @@ -12,7 +12,7 @@ def self.snake_case string
end
end

for clazz in [:DriveInfo, :MouseControl, :ParseTemplate, :PlayAudio, :PlayMp3Audio, :RubyClip, :SwingHelpers]
new_path = File.dirname(__FILE__) + '/simple-ruby-gui-creator/' + SimpleRubyGuiCreator.snake_case(clazz) + '.rb'
for clazz in [:DriveInfo, :MouseControl, :ParseTemplate, :PlayAudio, :PlayMp3Audio, :RubyClip, :SimpleGui]
new_path = File.dirname(__FILE__) + '/simple-ruby-gui-creator/' + SimpleGui.snake_case(clazz) + '.rb'
autoload clazz, new_path
end
2 changes: 1 addition & 1 deletion spec/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
require 'rspec/autorun'
require 'sane'
#$: << File.dirname(__FILE__) + '/../lib'
require_relative '../lib/simple-ruby-gui-creator'
require_relative '../lib/simple_gui'
8 changes: 7 additions & 1 deletion spec/parse_template.spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def parse_string string
# buttons should require a code name :P
# allow prepropagation of textareas, for easier width detection...and/or separating out concerns...hmm...
# parse_setup_string string, :text_area_to_use_text => string

# parse_setup_string :default_font =>

it "should accept height, width, abs_x, abs_y" do
frame = parse_string ' [a:my_name,abs_x=1,abs_y=2,width=100,height=101] '
get_dimentia(frame.elements[:my_name]).should == [1,2,101,100]
Expand Down Expand Up @@ -154,6 +155,11 @@ def get_dimentia(element)
get_dimentia(frame.elements[:text_area]).should == [0, 0, 32, 319]# it's "sub-contained" in a jscrollpane <sigh>
end

it "should let you use ending colons" do
frame = parse_string "\"Here's your template:\""
frame.elements.length.should == 0 # and hope it includes the colon in there :)
end


it "should allow for spaced out attributes" do
frame = parse_string "| [ :text, width = 200 ] "
Expand Down

0 comments on commit 9cc23bc

Please sign in to comment.