Class: Glimmer::SWT::LayoutProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/glimmer/swt/layout_proxy.rb

Overview

Proxy for org.eclipse.swt.widgets.Layout

This is meant to be used with a WidgetProxy where it will set the layout in the SWT widget upon instantiation.

Follows the Proxy Design Pattern

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(underscored_layout_name, widget_proxy, args) ⇒ LayoutProxy

Returns a new instance of LayoutProxy.



43
44
45
46
47
48
49
# File 'lib/glimmer/swt/layout_proxy.rb', line 43

def initialize(underscored_layout_name, widget_proxy, args)
  @underscored_layout_name = underscored_layout_name
  @widget_proxy = widget_proxy
  args = SWTProxy.constantify_args(args)
  @swt_layout = self.class.swt_layout_class_for(underscored_layout_name).new(*args)
  @widget_proxy.swt_widget.setLayout(@swt_layout)
end

Instance Attribute Details

#swt_layoutObject (readonly)

Returns the value of attribute swt_layout.



13
14
15
# File 'lib/glimmer/swt/layout_proxy.rb', line 13

def swt_layout
  @swt_layout
end

#widget_proxyObject (readonly)

Returns the value of attribute widget_proxy.



13
14
15
# File 'lib/glimmer/swt/layout_proxy.rb', line 13

def widget_proxy
  @widget_proxy
end

Class Method Details

.layout_exists?(underscored_layout_name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
# File 'lib/glimmer/swt/layout_proxy.rb', line 19

def layout_exists?(underscored_layout_name)
  begin
    swt_layout_class_for(underscored_layout_name)
    true
  rescue NameError => e
    false
  end
end

.swt_layout_class_for(underscored_layout_name) ⇒ Object

This supports layouts in and out of basic SWT library



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/glimmer/swt/layout_proxy.rb', line 29

def swt_layout_class_for(underscored_layout_name)
  swt_layout_name = underscored_layout_name.camelcase(:upper)
  swt_layout_class = eval(swt_layout_name)
  unless swt_layout_class.ancestors.include?(Layout)
    raise NameError, "Class #{swt_layout_class} matching #{underscored_layout_name} is not a subclass of org.eclipse.swt.widgets.Layout"
  end
  swt_layout_class
rescue => e
  Glimmer::Config.logger&.debug e.message
  # Glimmer::Config.logger&.debug "#{e.message}\n#{e.backtrace.join("\n")}"
  raise e
end

Instance Method Details

#apply_property_type_converters(attribute_name, args) ⇒ Object



67
68
69
70
71
# File 'lib/glimmer/swt/layout_proxy.rb', line 67

def apply_property_type_converters(attribute_name, args)
  if args.count == 1 && SWTProxy.has_constant?(args.first)
    args[0] = SWTProxy.constant(args.first)
  end
end

#attribute_getter(attribute_name) ⇒ Object



77
78
79
# File 'lib/glimmer/swt/layout_proxy.rb', line 77

def attribute_getter(attribute_name)
  "#{attribute_name.to_s.camelcase(:lower)}"
end

#attribute_setter(attribute_name) ⇒ Object



73
74
75
# File 'lib/glimmer/swt/layout_proxy.rb', line 73

def attribute_setter(attribute_name)
  "#{attribute_name.to_s.camelcase(:lower)}="
end

#get_attribute(attribute_name) ⇒ Object



63
64
65
# File 'lib/glimmer/swt/layout_proxy.rb', line 63

def get_attribute(attribute_name)
  @swt_layout.send(attribute_getter(attribute_name))
end

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/glimmer/swt/layout_proxy.rb', line 51

def has_attribute?(attribute_name, *args)
  @swt_layout.respond_to?(attribute_setter(attribute_name), args)
end

#set_attribute(attribute_name, *args) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/glimmer/swt/layout_proxy.rb', line 55

def set_attribute(attribute_name, *args)
  apply_property_type_converters(attribute_name, args)
  if args.first != @swt_layout.send(attribute_getter(attribute_name))
    @swt_layout.send(attribute_setter(attribute_name), *args)
    @widget_proxy.swt_widget.getShell.pack
  end
end