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.



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

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)
  @swt_layout.marginWidth = 15 if @swt_layout.respond_to?(:marginWidth)
  @swt_layout.marginHeight = 15 if @swt_layout.respond_to?(:marginHeight)
  @widget_proxy.swt_widget.setLayout(@swt_layout)
end

Instance Attribute Details

#swt_layoutObject (readonly)

Returns the value of attribute swt_layout.



34
35
36
# File 'lib/glimmer/swt/layout_proxy.rb', line 34

def swt_layout
  @swt_layout
end

#widget_proxyObject (readonly)

Returns the value of attribute widget_proxy.



34
35
36
# File 'lib/glimmer/swt/layout_proxy.rb', line 34

def widget_proxy
  @widget_proxy
end

Class Method Details

.layout_exists?(underscored_layout_name) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# File 'lib/glimmer/swt/layout_proxy.rb', line 40

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



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/glimmer/swt/layout_proxy.rb', line 50

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



90
91
92
93
94
# File 'lib/glimmer/swt/layout_proxy.rb', line 90

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



100
101
102
# File 'lib/glimmer/swt/layout_proxy.rb', line 100

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

#attribute_setter(attribute_name) ⇒ Object



96
97
98
# File 'lib/glimmer/swt/layout_proxy.rb', line 96

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

#get_attribute(attribute_name) ⇒ Object



86
87
88
# File 'lib/glimmer/swt/layout_proxy.rb', line 86

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

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#set_attribute(attribute_name, *args) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/glimmer/swt/layout_proxy.rb', line 78

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