Class: Glimmer::SWT::MenuProxy

Inherits:
WidgetProxy show all
Defined in:
lib/glimmer/swt/menu_proxy.rb

Overview

Proxy for org.eclipse.swt.widgets.Menu

Functions differently from other widget proxies.

Glimmer automatically detects if this is a drop down menu or pop up menu from its parent if no SWT style is passed in.

There are 3 possibilities:

  • SWT :bar style is passed in: Menu Bar

  • Parent is ShellProxy: Pop Up Menu (having style :pop_up)

  • Parent is another Menu: Drop Down Menu (having style :drop_down)

In order to get the SWT Menu object, one must call ‘#swt_widget`.

In the case of a Drop Down menu, this automatically creates an SWT MenuItem object with style :cascade

In order to retrieve the menu item widget proxy, one must call ‘#menu_item_proxy`

Follows the Proxy Design Pattern

Constant Summary

Constants inherited from WidgetProxy

WidgetProxy::DEFAULT_INITIALIZERS, WidgetProxy::DEFAULT_STYLES, WidgetProxy::KEYWORD_ALIASES

Instance Attribute Summary collapse

Attributes inherited from WidgetProxy

#drag_source_proxy, #drag_source_style, #drag_source_transfer, #drop_target_proxy, #drop_target_transfer, #finished_add_content, #parent_proxy, #swt_widget

Attributes included from Custom::Drawable

#image_double_buffered, #requires_shape_disposal

Instance Method Summary collapse

Methods inherited from WidgetProxy

#add_observer, #async_exec, #auto_exec, #can_add_observer?, #can_handle_drag_observation_request?, #can_handle_drop_observation_request?, #content, create, #dispose, #disposed?, #ensure_drag_source_proxy, #ensure_drop_target_proxy, #extract_args, #finish_add_content!, flyweight_swt_widget_classes, #has_style?, #height, #method_missing, #pack_same_size, #post_add_content, #post_initialize_child, #proxy_source_object, #remove_observer, #respond_to?, swt_widget_class_for, swt_widget_class_manual_entries, #sync_exec, #timer_exec, underscored_widget_name, widget_exists?, #widget_property_listener_installers, widget_proxy_class, #width, #x, #y

Methods included from Custom::Drawable

#add_shape, #clear_shapes, #deregister_shape_painting, #expanded_shapes, #image_buffered_shapes, #paint_pixel_by_pixel, #setup_shape_painting, #shape_at_location, #shapes, #swt_drawable

Methods included from ProxyProperties

#has_attribute_getter?, #has_attribute_setter?, #method_missing, #proxy_source_object, #respond_to?

Methods included from Properties

attribute_getter, #attribute_getter, attribute_setter, #attribute_setter, normalized_attribute, #normalized_attribute, ruby_attribute_getter, #ruby_attribute_setter, ruby_attribute_setter

Methods included from Packages

included

Constructor Details

#initialize(parent, args) ⇒ MenuProxy

Returns a new instance of MenuProxy.



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/glimmer/swt/menu_proxy.rb', line 51

def initialize(parent, args)
  index = args.delete(args.last) if args.last.is_a?(Numeric)
  styles = args.map(&:to_sym)
  if !styles.include?(:bar) && !parent.swt_widget.is_a?(Menu)
    styles = styles.unshift(:pop_up)
  end

  swt_widget_class = self.class.swt_widget_class_for('menu')
  if parent.swt_widget.is_a?(Menu)
    @menu_item_proxy = SWT::WidgetProxy.new('menu_item', parent, [:cascade] + [index].compact)
    @swt_menu_item = @menu_item_proxy.swt_widget
    @swt_widget = swt_widget_class.new(@menu_item_proxy.swt_widget)
    @swt_menu_item.setMenu(swt_widget)
  elsif parent.swt_widget.is_a?(Shell)
    @swt_widget = swt_widget_class.new(parent.swt_widget, style('menu', styles))
  else
    @swt_widget = swt_widget_class.new(parent.swt_widget)
  end

  if styles.include?(:bar)
    DisplayProxy.instance.auto_exec { parent.swt_widget.setMenuBar(swt_widget) }
  elsif styles.include?(:pop_up)
    DisplayProxy.instance.auto_exec { parent.swt_widget.setMenu(swt_widget) }
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Glimmer::SWT::WidgetProxy

Instance Attribute Details

Returns the value of attribute menu_item_proxy.



49
50
51
# File 'lib/glimmer/swt/menu_proxy.rb', line 49

def menu_item_proxy
  @menu_item_proxy
end

Returns the value of attribute menu_parent.



49
50
51
# File 'lib/glimmer/swt/menu_proxy.rb', line 49

def menu_parent
  @menu_parent
end

#swt_menu_itemObject (readonly)

Returns the value of attribute swt_menu_item.



49
50
51
# File 'lib/glimmer/swt/menu_proxy.rb', line 49

def swt_menu_item
  @swt_menu_item
end

Instance Method Details

#can_handle_observation_request?(observation_request, super_only: false) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
# File 'lib/glimmer/swt/menu_proxy.rb', line 107

def can_handle_observation_request?(observation_request, super_only: false)
  observation_request = observation_request.to_s
  super_result = super(observation_request)
  if observation_request.start_with?('on_') && !super_result && !super_only
    return menu_item_proxy.can_handle_observation_request?(observation_request)
  else
    super_result
  end
end

#get_attribute(attribute_name) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/glimmer/swt/menu_proxy.rb', line 97

def get_attribute(attribute_name)
  if normalized_attribute(attribute_name) == 'text'
    @swt_menu_item.getText
  elsif normalized_attribute(attribute_name) == 'enabled'
    @swt_menu_item.getEnabled
  else
    super(attribute_name)
  end
end

#handle_observation_request(observation_request, &block) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/glimmer/swt/menu_proxy.rb', line 117

def handle_observation_request(observation_request, &block)
  if can_handle_observation_request?(observation_request, super_only: true)
    super
  else
    menu_item_proxy.handle_observation_request(observation_request, &block)
  end
end

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_attribute?(attribute_name, *args)
  if ['text', 'enabled'].include?(attribute_name.to_s)
    true
  else
    super(attribute_name, *args)
  end
end

#set_attribute(attribute_name, *args) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/glimmer/swt/menu_proxy.rb', line 85

def set_attribute(attribute_name, *args)
  if normalized_attribute(attribute_name) == 'text'
    text_value = args[0]
    @swt_menu_item.setText text_value
  elsif normalized_attribute(attribute_name) == 'enabled'
    value = args[0]
    @swt_menu_item.setEnabled value
  else
    super(attribute_name, *args)
  end
end