<commit_before>require 'java'
require File.dirname(__FILE__) + '/swing_helpers.rb'

module ParseTemplate

  include_package 'javax.swing'; [JFrame, JPanel, JButton, JLabel]
  
  class JFramer < JFrame
    
    def initialize 
      super()
      @panel = JPanel.new
      @elements = {}
      @panel.set_layout nil
      add @panel # why can't I just slap these down? panel? huh?
	  #show
	end 
	attr_accessor :elements
	attr_accessor :panel
  end
  
  def self.parse_file filename
    parse_string File.read(filename)
  end
  
  def self.get_text_width text
	font = UIManager.getFont("Label.font")
    frc = java.awt.font.FontRenderContext.new(font.transform, true, true)
    textLayout = java.awt.font.TextLayout.new(text, font, frc)
    textLayout.bounds.width
  end
  
  def self.setup_element element, name, width=200
  		  # name is now like ["Setup Preferences:preferences"]
		  name = name[0]
		  if name.include? ':' # like "Start:start_button" ... disallows using colon at all, but hey...
		    text = name.split(':')[0..-2].join(':') # only accept last colon, so they can have text with colons in it
			name = name.split(':')[-1]
		  else
		    text = name
		  end
         p 'text width', name, get_text_width(text)
		  element.text=text
          #button.set_location(current_x, current_y)
		  element.set_bounds(@current_x, @current_y, width, 20)
		  @current_x += width + 5 # doesn't have a 'real' size yet...I guess...yikes
          @frame.panel.add element
          @frame.elements[name] = element
	      @max_x = [@max_x, @current_x].max
  end
  
  # returns a jframe that "matches" whatever the template says
  def self.parse_string string
    got = string
    # >>"[ a ] [ b ]".split(/(\[.*?\])/)
    # => ["", "[ a ]", " ", "[ b ]"]	
    @frame = JFramer.new
	@current_y = 10
	@max_x = 100
    got.each_line{|l|
	  @current_x = 10
	  button_line_regex = /\[(.*?)\]/
	  # >> "|  [Setup Preferences:preferences] [Start:start] [Stop:stop] |" .scan  /\[(.*?)\]/
	  # => [["Setup Preferences:preferences"], ["Start:start"], ["Stop:stop"]]
	  
	  text_regex = /<<(.*?)>>/ # 
	  
	  title_regex = /\s*[-]+([\w ]+)[-]+\s*$/  # ----(a Title)---
	  if l =~ title_regex
	    @frame.set_title $1 # done :)
	  elsif l =~ button_line_regex
        l.scan(button_line_regex).each{|name|
		  button = JButton.new
		  setup_element(button, name)
		}
 		@current_y += 25
	  elsif l =~ text_regex
	    for name in l.scan(text_regex)
	      label = JLabel.new
		  setup_element(label, name, 350)
        end
	    @current_y += 25
	  end
	}
	@frame.set_size @max_x+25, @current_y+45
	@frame
  end

end<commit_msg>smaller<commit_after>require 'java'
require File.dirname(__FILE__) + '/swing_helpers.rb'

module ParseTemplate

  include_package 'javax.swing'; [JFrame, JPanel, JButton, JLabel]
  
  class JFramer < JFrame
    
    def initialize 
      super()
      @panel = JPanel.new
      @elements = {}
      @panel.set_layout nil
      add @panel # why can't I just slap these down? panel? huh?
	  #show
	end 
	attr_accessor :elements
	attr_accessor :panel
  end
  
  def self.parse_file filename
    parse_string File.read(filename)
  end
  
  def self.get_text_width text
	font = UIManager.getFont("Label.font")
    frc = java.awt.font.FontRenderContext.new(font.transform, true, true)
    textLayout = java.awt.font.TextLayout.new(text, font, frc)
    textLayout.bounds.width
  end
  
  def self.setup_element element, name, width=nil
  		  # name is now like ["Setup Preferences:preferences"]
		  name = name[0]
		  if name.include? ':' # like "Start:start_button" ... disallows using colon at all, but hey...
		    text = name.split(':')[0..-2].join(':') # only accept last colon, so they can have text with colons in it
			name = name.split(':')[-1]
		  else
		    text = name
		  end
		  element.text=text
		  p 'width of ' + text, get_text_width(text)
          width ||= get_text_width(text) + 50
		  element.set_bounds(@current_x, @current_y, width, 20)
		  @current_x += width + 5 # doesn't have a 'real' size yet...I guess...yikes
          @frame.panel.add element
          @frame.elements[name] = element
	      @max_x = [@max_x, @current_x].max
  end
  
  # returns a jframe that "matches" whatever the template says
  def self.parse_string string
    got = string
    # >>"[ a ] [ b ]".split(/(\[.*?\])/)
    # => ["", "[ a ]", " ", "[ b ]"]	
    @frame = JFramer.new
	@current_y = 10
	@max_x = 100
    got.each_line{|l|
	  @current_x = 10
	  button_line_regex = /\[(.*?)\]/
	  # >> "|  [Setup Preferences:preferences] [Start:start] [Stop:stop] |" .scan  /\[(.*?)\]/
	  # => [["Setup Preferences:preferences"], ["Start:start"], ["Stop:stop"]]
	  
	  text_regex = /<<(.*?)>>/ # 
	  
	  title_regex = /\s*[-]+([\w ]+)[-]+\s*$/  # ----(a Title)---
	  if l =~ title_regex
	    @frame.set_title $1 # done :)
	  elsif l =~ button_line_regex
        l.scan(button_line_regex).each{|name|
		  button = JButton.new
		  setup_element(button, name)
		}
 		@current_y += 25
	  elsif l =~ text_regex
	    for name in l.scan(text_regex)
	      label = JLabel.new
		  setup_element(label, name, 350)
        end
	    @current_y += 25
	  end
	}
	@frame.set_size @max_x+25, @current_y+45
	@frame
  end

end
{'fraction_non_alphanumeric': 0.15014847809948031, 'fraction_numerical': 0.010579064587973273, 'mean_word_length': 2.4021464646464645, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1256651', 'n_tokens_mistral': 2154, 'n_tokens_neox': 1864, 'n_words': 440}