Skip to content

Commit

Permalink
add some specs for specifying text area height, width, how were these…
Browse files Browse the repository at this point in the history
… absent?
  • Loading branch information
rdp committed Oct 23, 2013
1 parent a87ef70 commit ec3f3d1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 17 deletions.
31 changes: 24 additions & 7 deletions README
Expand Up @@ -42,7 +42,6 @@ Set behaviors like this:
SimpleGuiCreator.show_blocking_message_dialog "you clicked button1!"
}


Having the layout as ASCII text separates views from controllers, in this case, because you can store the layouts in
an entirely separate file (or embedded in the code). "Normal humans" can then edit the design layout files, for instance.
It has proven quite nice at separating concerns.
Expand All @@ -52,16 +51,16 @@ More complicated example:
---------- Window Title ----------------------------------
| [a button:button1] [button text:button2] |
| "some text2:text1" |
| [An Editable Text Area with 4 rows:text_area] |
| [ ] |
| [ ] |
| [ ] |
| [Editable Text Area with 4 rows :text_area_name] |
| [ ] |
| [ ] |
| [ ] |
| [Dropdown Default \/ :dropdown_name] |
| [✓:checkbox_name] "Some text" |
----------------------------------------------------------

See the file spec/parse_template.spec.rb for examples of how to use each element.
The window borders are also optional.
See the file spec/parse_template.spec.rb for examples of how to use the various elements.
The window borders along the edge are optional.

The library also has helper methods for common GUI tasks, like:

Expand Down Expand Up @@ -94,6 +93,24 @@ It has helpers to set/get system clipboard contents.
It has helpers to control/query the system mouse.
It has helpers to query the current system for its DVD drives, be notified when disks are inserted/changed, etc.


Even more complicated example:

---------- Window Title ----------------------------------
| [a button:button1] [button text:button2] |
| "some text2:text1" |
| [Editable Text Area with 4 rows :text_area] |
| [ ] |
| [ ] |
| [ ] |
| [Editable Text Field with 1 row and fixed spacing chars :width=700, height=1char, font=fixed_width] |
| [ ] |
| [Dropdown Default \/ :dropdown_name] |
| [✓:checkbox_name] "Some text" |
----------------------------------------------------------



Feedback/feature requests welcome.
http://groups.google.com/group/roger-projects, roger-projects@googlegroups.com
I'd even be happy to wrap other gui frameworks (currently jruby/swing only) with the same functionality,
Expand Down
19 changes: 15 additions & 4 deletions lib/simple_gui_creator/parse_template.rb
Expand Up @@ -8,7 +8,7 @@ module SimpleGuiCreator

include_package 'javax.swing';

[JFrame, JPanel, JButton, JTextArea, JLabel, UIManager, JScrollPane, JCheckBox, JComboBox, JComponent].each{|kls|
[JFrame, JPanel, JButton, JTextArea, JLabel, UIManager, JScrollPane, JCheckBox, JComboBox, JComponent, JTextField].each{|kls|
kls.__persistent__=true # lazy me--also pull in the constant :)
}

Expand Down Expand Up @@ -161,6 +161,11 @@ def handle_button_at_current captured, cur_spot, end_spot, all_lines, idx
def get_text_width text
get_text_dimentia(text).width
end

def get_text_height text
get_text_dimentia(text).height
end


def get_text_dimentia text
font = UIManager.getFont("Label.font")
Expand Down Expand Up @@ -206,11 +211,17 @@ def setup_element element, name_and_code, height=nil, set_text_on_this = element
end

for name2 in ['abs_x', 'abs_y', 'width', 'height']
var = attributes_hashed.delete(name2)
var = attributes_hashed.delete(name2) # remove them
if var
if var =~ /chars$/
if var =~ /char(s|)$/
count = var[0..-5].to_i
var = get_text_width('m'*count) # TODO fails for height 30chars
if name2 == 'width'
var = get_text_width('m'*count)
elsif name2 == 'height'
var = get_text_height("lg")*count
else
raise "huh #{name2} #{var} has chars?"
end
elsif var =~ /px$/
var = var[0..-3].to_i
else
Expand Down
37 changes: 31 additions & 6 deletions spec/parse_template.spec.rb
Expand Up @@ -87,7 +87,14 @@ def parse_string string

it "should accept zero length strings if they have a width spec" do
frame = parse_string "| \":my_name,width=250\""
frame.elements[:my_name].text.should == ''
frame.elements[:my_name].text.should == ''
end

it "should accept char as length" do
frame = parse_string "| \":my_name,width=255char\""
frame.elements[:my_name].size.width.should == 2719
frame = parse_string "| \":my_name,width=255chars\""
frame.elements[:my_name].size.width.should == 2719
end

it "should not add codeless items to elements" do
Expand Down Expand Up @@ -166,18 +173,38 @@ def get_dimentia(element)
button.size.width.should==129 # bigger than 35, basically
end

# not yet anyway :)
# it "should parse text fields [single line text areas]" do
# string = "[ :text_area,type=editable] |"
# frame = parse_string string
# frame.elements[:text_area].class.should == Java::JavaxSwing::JTextField
# end

it "should parse text areas" do
string = <<-EOL
| [ :text_area] |
| [ ] |
| [ :text_area2] |
| [ ] |
EOL
frame = parse_string string
frame.elements[:text_area].class.should == Java::JavaxSwing::JTextArea
frame.elements.length.should == 1 # not create fake empty buttons underneath :)
frame.elements.length.should == 2 # not create fake empty buttons underneath :)
text_area_dimentia(frame.elements[:text_area]).should == [0, 0, 48, 319] # it's "sub-contained" in a jscrollpane so these numbers are relative to that <sigh>
text_area_dimentia(frame.elements[:text_area2]).should == [0, 0, 48, 319] # ?? 308
end

it "should allow text area sizing" do
string = <<-EOL
[ :text_area,width=155char,height=255chars]
[ ]
EOL
frame = parse_string string
text_area_dimentia(frame.elements[:text_area]).should == [0, 0, 2833, 1649] # XXX is this a "perfect" height?
end

def text_area_dimentia(element)
element.class.should == Java::JavaxSwing::JTextArea
# weird swing bug? it doesn't propagate size for awhile or something?
while(get_dimentia(element) == [0,0,0,0])
puts 'weird TextArea 0,0,0,0'
sleep 0.2
Expand All @@ -190,7 +217,6 @@ def text_area_dimentia(element)
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 ] "
get_dimentia(frame.elements[:text])[3].should == 200
Expand All @@ -206,8 +232,7 @@ def text_area_dimentia(element)
[button : button][textare : textarea]
[ ]
EOL
text_area_dimentia(frame.elements[:textarea]).should == [0, 0, 32, 88]

text_area_dimentia(frame.elements[:textarea]).should == [0, 0, 32, 88]
end

it "should allow for blank lines to mean spacing" do
Expand Down

0 comments on commit ec3f3d1

Please sign in to comment.