Class: SimpleGuiCreator::ParseTemplate

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParseTemplate

Returns a new instance of ParseTemplate.



15
16
17
18
19
20
21
22
# File 'lib/simple_gui_creator/parse_template.rb', line 15

def initialize 
  super()
  @panel = JPanel.new
  @elements = {}
  @panel.set_layout nil
  add @panel # why can't I just slap these down? panel? huh?
   show # this always bites me...I new it up an it just doesn't appear...
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



25
26
27
# File 'lib/simple_gui_creator/parse_template.rb', line 25

def elements
  @elements
end

#frameObject

Returns the value of attribute frame.



27
28
29
# File 'lib/simple_gui_creator/parse_template.rb', line 27

def frame
  @frame
end

#original_titleObject

Returns the value of attribute original_title.



26
27
28
# File 'lib/simple_gui_creator/parse_template.rb', line 26

def original_title
  @original_title
end

#panelObject (readonly)

Returns the value of attribute panel.



24
25
26
# File 'lib/simple_gui_creator/parse_template.rb', line 24

def panel
  @panel
end

Instance Method Details

#parse_setup_filename(filename) ⇒ Object



29
30
31
32
# File 'lib/simple_gui_creator/parse_template.rb', line 29

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

#parse_setup_string(string) ⇒ Object

“matches” whatever the template string looks like…



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/simple_gui_creator/parse_template.rb', line 35

def parse_setup_string string
  @frame = self # LODO refactor
  @current_y = 10
  @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"
    end
    button_line_regex = /\[(.*?)\]/
    #>> "|  [Setup Preferences:preferences] [Start:start] [Stop:stop] |" .scan button_line_regex
    #=> [["Setup Preferences:preferences"], ["Start:start"], ["Stop:stop"]]
    
    text_regex = /"([^"]+)"/ # "some text:name"
    blank_line_regex = /^\s*(|\|)\s+(|\|)\s*$/ # matches " | | " or just empty...
    title_regex = /\s*[-]+([\w ]+)[-]+\s*$/  # ----(a Title)---
    @current_line_height = 25
    
    if line =~ title_regex
      @frame.set_title $1 # done :)
      @frame.original_title = $1.dup.freeze # freeze...LOL        
    elsif line =~ blank_line_regex
      @current_y += @current_line_height
    else
      # attempt an element line
      cur_x = 0        
      while next_match = closest_next_regex_match([button_line_regex, text_regex], line[cur_x..-1])
        cur_spot = line[cur_x..-1] =~ next_match
        cur_spot += cur_x # we had only acted on a partial line, above, so add in the part we didn't do, to get the right offset number
        captured = $1
        end_spot = cur_spot + captured.length
        if next_match == button_line_regex
          handle_button_at_current captured, cur_spot, end_spot, all_lines, idx
        elsif next_match == text_regex
          label = JLabel.new
          setup_element(label, captured)            
        end
        cur_x = end_spot
      end        
      @current_y += @current_line_height
    end
    
    # build in realtime LOL
    @frame.set_size @window_max_x + 25, @current_y + 40      
    rescue
      puts "Parsing failed on line #{line.inspect} number: #{idx+1}!"
      raise
    end
  }
  self
end