class Shortcut::Window

Public Class Methods

action(name, &block) click to toggle source
# File lib/shortcut/window.rb, line 162
def action(name, &block)
  send :define_method, "action_#{name}_handler".to_sym do
    self.instance_eval(&block)
  end
end
always_on_top(bool_value) click to toggle source
# File lib/shortcut/window.rb, line 132
def always_on_top(bool_value)
  send :define_method, :always_on_top do
    setAlwaysOnTop(bool_value)
  end
end
get_image(path) click to toggle source
# File lib/shortcut/window.rb, line 168
def get_image(path)
  ImageIcon.new(path).getImage
end
key_pressed(code, action = nil, &block) click to toggle source
# File lib/shortcut/window.rb, line 150
def key_pressed(code, action = nil, &block)
  send :define_method, "key_#{code}_pressed".to_sym do
    if action
      method_name = "action_#{action}_handler".to_sym
      raise UndefinedActionError.new("undefined action `#{action}'") unless respond_to?(method_name)
      self.send(method_name)
    else
      self.instance_eval(&block)
    end
  end
end
layout(type_class) click to toggle source
# File lib/shortcut/window.rb, line 144
def layout(type_class)
  send :define_method, :shortcut_window_layout do
    setLayout(type_class.new)
  end
end
size(width, height) click to toggle source
# File lib/shortcut/window.rb, line 138
def size(width, height)
  send :define_method, :shortcut_window_size do
    setSize(width, height)
  end
end
title(text) click to toggle source
# File lib/shortcut/window.rb, line 126
def title(text)
  send :define_method, :default_title do
    text
  end
end

Public Instance Methods

actionPerformed(event) click to toggle source
# File lib/shortcut/window.rb, line 200
def actionPerformed(event)
  method_name = "action_#{event.getActionCommand}_handler".to_sym
  send(method_name) if respond_to?(method_name)
end
click(x, y) click to toggle source
# File lib/shortcut/window.rb, line 51
def click(x, y)
  robot.mouseMove(x, y)
  robot.mousePress(InputEvent::BUTTON1_MASK)
  robot.mouseRelease(InputEvent::BUTTON1_MASK)
end
create_button(text, action, options = {}) click to toggle source
# File lib/shortcut/window.rb, line 38
def create_button(text, action, options = {})
  options = {enabled: true}.merge(options)
  JButton.new(text).tap do |btn|
    btn.setActionCommand(action)
    btn.addActionListener(self)
    btn.setEnabled(options[:enabled])
  end
end
display_info(text) click to toggle source
# File lib/shortcut/window.rb, line 72
def display_info(text)
  feedback_text_area.append("\n#{text}")
  feedback_text_area.setCaretPosition(feedback_text_area.getLineStartOffset(feedback_text_area.getLineCount() - 1))
end
feedback_scroll_panel(text_area, dimension) click to toggle source
# File lib/shortcut/window.rb, line 66
def feedback_scroll_panel(text_area, dimension)
  JScrollPane.new(text_area).tap do |scroll_panel|
    scroll_panel.setPreferredSize(dimension)
  end
end
feedback_text_area(start_text = '') click to toggle source
# File lib/shortcut/window.rb, line 57
def feedback_text_area(start_text = '')
  @feedback_text_area ||= JTextArea.new.tap do |text_area|
    text_area.setEditable(false)
    text_area.setText(start_text)
    text_area.setBackground(Color::WHITE)
    text_area.setForeground(Color::BLACK)
  end
end
load_image(path) click to toggle source
# File lib/shortcut/window.rb, line 77
def load_image(path)
  buffer_array = ByteArrayInputStream.new(File.read(path).to_java_bytes)
  image_input_stream = ImageIO.createImageInputStream(buffer_array)
  ImageIO.read(image_input_stream)
end
nativeKeyPressed(event) click to toggle source
# File lib/shortcut/window.rb, line 195
def nativeKeyPressed(event)
  method_name = "key_#{event.getKeyCode}_pressed".to_sym
  send(method_name) if respond_to?(method_name)
end
operative_system() click to toggle source
# File lib/shortcut/window.rb, line 83
def operative_system
  java.lang.System.getProperty("os.name").downcase
end
robot() click to toggle source
# File lib/shortcut/window.rb, line 47
def robot
  @robot ||= Robot.new
end
run() click to toggle source
# File lib/shortcut/window.rb, line 96
def run
  setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)

  setTitle(respond_to?(:default_title) ? default_title : 'Shortcut App')

  always_on_top if respond_to?(:always_on_top)

  set_window_icons

  if respond_to?(:shortcut_window_size)
    shortcut_window_size
  else
    setSize(440, 250)
  end

  if respond_to?(:shortcut_window_layout)
    shortcut_window_layout
  else
    setLayout(BorderLayout.new)
  end

  GlobalScreen.getInstance.addNativeKeyListener(self)
  addWindowListener(self)

  create_components if respond_to?(:create_components)

  setVisible true
end
set_window_icons() click to toggle source
# File lib/shortcut/window.rb, line 87
def set_window_icons
  # http://www.iconarchive.com/show/oxygen-icons-by-oxygen-icons.org/Apps-accessories-calculator-icon.html
  icons = ArrayList.new
  [16, 32, 64, 128].each do |size|
    icons.add(self.class.get_image("images/icons/#{size}.png"))
  end
  setIconImages(icons)
end
windowClosed(e) click to toggle source
# File lib/shortcut/window.rb, line 185
def windowClosed(e)
  GlobalScreen.unregisterNativeHook
  java.lang.System.runFinalization
  java.lang.System.exit(0)
end
windowOpened(e) click to toggle source
# File lib/shortcut/window.rb, line 178
def windowOpened(e)
  requestFocusInWindow
  GlobalScreen.registerNativeHook
rescue NativeHookException => e
  display_info(e.toString)
end