<commit_before>require 'jruby'
require 'java'
require 'irb'
require 'irb/completion'

#java_require 'ruby_console'  # Require this file, instead of pasting code into .java
java_package 'com.moneydance.modules.features.ruby.rb'

import java.awt.Color
import java.awt.Font
import java.awt.EventQueue
import javax.swing.JFrame
import javax.swing.JFileChooser
java_import 'java.awt.event.*' # Compiler needs this directive

# Moneydance IRB Console
class RubyConsole
  java_implements ActionListener

  # Try to find preferred font family, use otherwise -- err -- otherwise
  def self.find_font otherwise, style, size, *families
    avail_families = java.awt.GraphicsEnvironment.local_graphics_environment.available_font_family_names
    fontname = families.find(proc { otherwise }) { |name| avail_families.include? name }
    Font.new(fontname, style, size)
  end

  # Starts new RubyConsole
  def initialize ruby_main, context
    @ruby_main = ruby_main
    @context = context
    @root = context.get_root_account
    Object.const_set :MD, @context
    Object.const_set :ROOT, @root

    if EventQueue.dispatch_thread?
      # Called from Moneydance GUI, use thread other than EventQueue.dispatch_thread
      Thread.new do
        run_irb
      end
    else
      # Called from command line (rake ant:load), we're not inside Moneydance GUI
      run_irb JFrame::EXIT_ON_CLOSE
    end
  end

  # Runs Swing wrapper for IRB - call from thread other than EventQueue.dispatch_thread
  #
  def run_irb close_operation = JFrame::HIDE_ON_CLOSE # DISPOSE_ON_CLOSE ?
    text = javax.swing.JTextPane.new
    text.font = RubyConsole.find_font 'Monospaced', Font::PLAIN, 14, 'Monaco', 'Andale Mono'
    text.margin = java.awt.Insets.new(8, 8, 8, 8)
    text.caret_color = Color.new(0xa4, 0x00, 0x00)
    text.background = Color.new(0xf2, 0xf2, 0xf2)
    text.foreground = Color.new(0xa4, 0x00, 0x00)

    pane = javax.swing.JScrollPane.new
    pane.viewport_view = text

    @frame = JFrame.new "Moneydance Interactive JRuby #{JRUBY_VERSION} Console " +
                            "(tab will autocomplete)"
    @frame.default_close_operation = close_operation
    @frame.set_size 700, 600
    @frame.content_pane.add pane

    header = " MD - Moneydance context: ComMoneydanceAppsMdController::Main \n" +
        " ROOT - Moneydance root account: ComMoneydanceAppsMdModel::RootAccount \n\n"
    readline = org.jruby.demo.TextAreaReadline.new text, header
    JRuby.objectspace = true # useful for code completion
    readline.hook_into_runtime_with_streams(JRuby.runtime)

    EventQueue.invoke_later proc { show }

    ARGV << '--readline' << '--prompt' << 'inf-ruby'
    IRB.start(__FILE__)
    @ruby_main.cleanup
  end

  java_signature 'public void actionPerformed(ActionEvent e)'

  def action_performed event

  end

  def load_file
    fc = JFileChooser.new
    fc.setDialogTitle("Choose Ruby File")
    if fc.showOpenDialog(self) == JFileChooser::APPROVE_OPTION<commit_msg>RubyConsole ActionListener interface documented<commit_after>require 'jruby'
require 'java'
require 'irb'
require 'irb/completion'

#java_require 'ruby_console'  # Require this file, instead of pasting code into .java
java_package 'com.moneydance.modules.features.ruby.rb'

import java.awt.Color
import java.awt.Font
import java.awt.EventQueue
import javax.swing.JFrame
import javax.swing.JFileChooser
java_import 'java.awt.event.*' # Compiler needs this directive to implement ActionListener

# Moneydance IRB Console
# Implements ActionListener: The listener interface for receiving action events.
# Instance of class that implements this interface can registered with any component
# using the component's #addActionListener method. When the action event occurs,
# registered listener's #actionPerformed method is invoked.
class RubyConsole
  java_implements ActionListener

  # Try to find preferred font family, use otherwise -- err -- otherwise
  def self.find_font otherwise, style, size, *families
    families = java.awt.GraphicsEnvironment.local_graphics_environment.available_font_family_names
    fontname = families.find(proc { otherwise }) { |name| families.include? name }
    Font.new(fontname, style, size)
  end

  # Starts new RubyConsole
  def initialize ruby_main, context
    @ruby_main = ruby_main
    @context = context
    @root = context.get_root_account
    Object.const_set :MD, @context
    Object.const_set :ROOT, @root

    if EventQueue.dispatch_thread?
      # Called from Moneydance GUI, use thread other than EventQueue.dispatch_thread
      Thread.new do
        run_irb
      end
    else
      # Called from command line (rake ant:load), we're not inside Moneydance GUI
      run_irb JFrame::EXIT_ON_CLOSE
    end
  end

  # Runs Swing wrapper for IRB - call from thread other than EventQueue.dispatch_thread
  #
  def run_irb close_operation = JFrame::HIDE_ON_CLOSE # DISPOSE_ON_CLOSE ?
    text = javax.swing.JTextPane.new
    text.font = RubyConsole.find_font 'Monospaced', Font::PLAIN, 14, 'Monaco', 'Andale Mono'
    text.margin = java.awt.Insets.new(8, 8, 8, 8)
    text.caret_color = Color.new(0xa4, 0x00, 0x00)
    text.background = Color.new(0xf2, 0xf2, 0xf2)
    text.foreground = Color.new(0xa4, 0x00, 0x00)

    pane = javax.swing.JScrollPane.new
    pane.viewport_view = text

    @frame = JFrame.new "Moneydance Interactive JRuby #{JRUBY_VERSION} Console " +
                            "(tab will autocomplete)"
    @frame.default_close_operation = close_operation
    @frame.set_size 700, 600
    @frame.content_pane.add pane

    header = " MD - Moneydance context: ComMoneydanceAppsMdController::Main \n" +
        " ROOT - Moneydance root account: ComMoneydanceAppsMdModel::RootAccount \n\n"
    readline = org.jruby.demo.TextAreaReadline.new text, header
    JRuby.objectspace = true # useful for code completion
    readline.hook_into_runtime_with_streams(JRuby.runtime)

    EventQueue.invoke_later proc { show }

    ARGV << '--readline' << '--prompt' << 'inf-ruby'
    IRB.start(__FILE__)
    @ruby_main.cleanup
  end

  java_signature 'public void actionPerformed(ActionEvent event)'
  # from ActionListener interface: Invoked when an action event occurs.
  def action_performed event

  end

  def load_file
    fc = JFileChooser.new
    fc.setDialogTitle("Choose Ruby File")
    if fc.showOpenDialog(self) == JFileChooser::APPROVE_OPTION
{'fraction_non_alphanumeric': 0.08631512868801004, 'fraction_numerical': 0.010671688637790333, 'mean_word_length': 3.7953348382242287, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, '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': '5588535', 'n_tokens_mistral': 2141, 'n_tokens_neox': 1985, 'n_words': 527}