Class: Watobo::Gui::ViewerHandlerCtrl

Inherits:
FXHorizontalFrame
  • Object
show all
Defined in:
lib/watobo/gui/custom_viewer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, opts = { :opts => LAYOUT_FILL_X|LAYOUT_SIDE_BOTTOM, :padding => 0 }) ⇒ ViewerHandlerCtrl

Returns a new instance of ViewerHandlerCtrl.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/watobo/gui/custom_viewer.rb', line 12

def initialize(parent, opts = { :opts => LAYOUT_FILL_X|LAYOUT_SIDE_BOTTOM, :padding => 0 })
  super parent, opts
  @parent = parent
  #handler_ctrl_frame = FXHorizontalFrame.new(self, :opts => LAYOUT_FILL_X, :padding => 0)
  FXLabel.new(self, "View Handler:")
  @handler_status_lbl = FXLabel.new(self, "None")
  @handler_status_lbl.backColor = "red"
  add_handler_btn = FXButton.new(self, "add", nil, nil, 0, FRAME_RAISED|LAYOUT_FILL_Y|LAYOUT_RIGHT)
  add_handler_btn.connect(SEL_COMMAND){ add_handler }
  reload_handler_btn = FXButton.new(self, "reload", nil, nil, 0, FRAME_RAISED|LAYOUT_FILL_Y|LAYOUT_RIGHT)
  reload_handler_btn.connect(SEL_COMMAND){ load_handler(@handler_file) }
  reset_handler_btn = FXButton.new(self, "reset", nil, nil, 0, FRAME_RAISED|LAYOUT_FILL_Y|LAYOUT_RIGHT)
  
  reset_handler_btn.connect(SEL_COMMAND){
    @handler = nil
    @handler_file = nil
    @handler_status_lbl.text =  "None"
    @handler_status_lbl.backColor = "red"
  }
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



6
7
8
# File 'lib/watobo/gui/custom_viewer.rb', line 6

def handler
  @handler
end

Instance Method Details

#add_handlerObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/watobo/gui/custom_viewer.rb', line 34

def add_handler        
   handler_filename = FXFileDialog.getOpenFilename(self, "Select handler file", @handler_path, "*.rb\n*")
    if handler_filename != "" then
      if File.exists?(handler_filename) then
        @handler_file = handler_filename
        @handler_path = File.dirname(handler_filename) + "/"
        load_handler(handler_filename)
      end
    end
  
end

#call_handler(object) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/watobo/gui/custom_viewer.rb', line 77

def call_handler(object)
  return object if @handler.nil?
  begin
    result = @handler.call(object)
    return result       
  rescue => bang
    result = bang.to_s
    result << bang.backtrace.join("\n")
    return result
  end
  
end

#has_handler?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/watobo/gui/custom_viewer.rb', line 8

def has_handler?
  !@handler.nil?
end

#load_handler(file) ⇒ Object



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
# File 'lib/watobo/gui/custom_viewer.rb', line 46

def load_handler(file)
  @handler = nil
  @handler_status_lbl.text = "None"
  @handler_status_lbl.backColor = "red"
  
  return false if file.nil?
  return false unless File.exist? file
  begin
    source = File.read(file)
    #puts source
    result = eval(source)
    if result.respond_to? :call
      @handler = result
      @handler_status_lbl.text = "#{File.basename(file).gsub(/\.rb$/,'')}"
      @handler_status_lbl.textColor = 'black'
      @handler_status_lbl.backColor = 'green'
      @parent.setText
    end
    return true
               
   rescue SyntaxError, LocalJumpError, NameError => e
     out = e.to_s
     out << e.backtrace.join("\n")
   rescue => bang
     out = bang
     out << bang.backtrace.join("\n")
   end
   puts out
   return false
end