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
73
74
75
76
77
78
# 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)
  @swt_layout.marginTop = 0 if @swt_layout.respond_to?(:marginTop)
  @swt_layout.marginRight = 0 if @swt_layout.respond_to?(:marginRight)
  @swt_layout.marginBottom = 0 if @swt_layout.respond_to?(:marginBottom)
  @swt_layout.marginLeft = 0 if @swt_layout.respond_to?(:marginLeft)
  old_layout = @widget_proxy.swt_widget.getLayout
  @widget_proxy.swt_widget.setLayout(@swt_layout)
  @widget_proxy.swt_widget.layout if old_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



97
98
99
100
101
# File 'lib/glimmer/swt/layout_proxy.rb', line 97

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



107
108
109
# File 'lib/glimmer/swt/layout_proxy.rb', line 107

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

#attribute_setter(attribute_name) ⇒ Object



103
104
105
# File 'lib/glimmer/swt/layout_proxy.rb', line 103

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

#get_attribute(attribute_name) ⇒ Object



93
94
95
# File 'lib/glimmer/swt/layout_proxy.rb', line 93

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

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/glimmer/swt/layout_proxy.rb', line 80

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

#set_attribute(attribute_name, *args) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/glimmer/swt/layout_proxy.rb', line 84

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.layout
    @widget_proxy.swt_widget.getShell.layout
  end
end