Class: Glimmer::SWT::Custom::Shape

Inherits:
WidgetProxy show all
Includes:
PropertyOwner
Defined in:
lib/glimmer/swt/custom/shape.rb,
lib/glimmer/swt/custom/shape/arc.rb,
lib/glimmer/swt/custom/shape/line.rb,
lib/glimmer/swt/custom/shape/oval.rb,
lib/glimmer/swt/custom/shape/text.rb,
lib/glimmer/swt/custom/shape/image.rb,
lib/glimmer/swt/custom/shape/point.rb,
lib/glimmer/swt/custom/shape/polygon.rb,
lib/glimmer/swt/custom/shape/polyline.rb,
lib/glimmer/swt/custom/shape/rectangle.rb

Overview

Represents a shape (graphics) to be drawn on a control/widget/canvas/display That is because Shape is drawn on a parent as graphics and doesn’t have an SWT widget for itself

Direct Known Subclasses

Arc, Image, Line, Oval, Point, Polygon, Polyline, Rectangle, Text

Defined Under Namespace

Classes: Arc, Image, Line, Oval, Point, Polygon, Polyline, Rectangle, Text

Constant Summary collapse

String =
Text

Constants inherited from WidgetProxy

WidgetProxy::DEFAULT_INITIALIZERS, WidgetProxy::JS_KEY_CODE_TO_SWT_KEY_CODE_MAP, WidgetProxy::JS_LOCATION_TO_SWT_KEY_LOCATION_MAP, WidgetProxy::SWT_CURSOR_TO_CSS_CURSOR_MAP

Instance Attribute Summary

Attributes inherited from WidgetProxy

#args, #background, #children, #cursor, #disposed?, #enabled, #focus, #font, #foreground, #menu, #menu_requested, #menu_x, #menu_y, #parent, #path, #rendered

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PropertyOwner

#attribute_getter, #attribute_setter

Methods inherited from WidgetProxy

#add_content_on_render, #add_css_class, #add_css_classes, #add_observer, #apply_property_type_converters, #build_dom, #can_handle_observation_request?, #clear_css_classes, #content, #content_on_render_blocks, #css_classes, #default_observation_request_to_event_mapping, #dialog_ancestor, #dispose, #dom_element, #effective_observation_request_to_event_mapping, #event_handling_suspended?, #event_listener_proxies, for, #get_data, #handle_javascript_observation_request, #handle_observation_request, #has_style?, #id, #id=, #initialize, #listener_dom_element, #listener_path, #listeners, #listeners_for, max_id_number_for, max_id_numbers, #name, next_id_number_for, #observation_request_to_event_mapping, #observation_requests, #parent_dom_element, #parent_path, #parents, #post_dispose_child, #post_initialize_child, #print, #property_type_converters, #reattach, #remove_all_listeners, #remove_css_class, #remove_css_classes, #remove_event_listener_proxies, reset_max_id_numbers!, #resume_event_handling, #selector, #set_data, #set_focus, #shell, #skip_content_on_render_blocks?, #style_element, #suspend_event_handling, #swt_data, #swt_widget, underscored_widget_name, widget_class, widget_exists?, widget_handling_listener, #widget_property_listener_installers

Constructor Details

This class inherits a constructor from Glimmer::SWT::WidgetProxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

TODO look why image is not working with the method_missing and respond_to? on shape



150
151
152
153
154
155
156
157
158
# File 'lib/glimmer/swt/custom/shape.rb', line 150

def method_missing(method_name, *args, &block)
  if method_name.to_s.end_with?('=')
    set_attribute(method_name, *args)
  elsif has_attribute?(method_name) && args.empty?
    get_attribute(method_name)
  else # TODO support proxying calls to handle_observation_request for listeners just like WidgetProxy
    super(method_name, *args, &block)
  end
end

Class Method Details

.create(parent, keyword, args, &property_block) ⇒ Object



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

def create(parent, keyword, args, &property_block)
  potential_shape_class_name = keyword.to_s.camelcase(:upper).to_sym
  if constants.include?(potential_shape_class_name)
    const_get(potential_shape_class_name).new(parent, args, property_block)
  else
    new(parent, args, property_block)
  end
end

.keywordsObject



54
55
56
57
58
59
# File 'lib/glimmer/swt/custom/shape.rb', line 54

def keywords
  constants.select do |constant|
    constant_value = const_get(constant)
    constant_value.respond_to?(:ancestors) && constant_value.ancestors.include?(Glimmer::SWT::Custom::Shape)
  end.map {|constant| constant.to_s.underscore}
end

.valid?(parent, keyword, args, &block) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/glimmer/swt/custom/shape.rb', line 49

def valid?(parent, keyword, args, &block)
  return true if keyword.to_s == 'shape'
  keywords.include?(keyword.to_s)
end

Instance Method Details

#attach(the_parent_dom_element) ⇒ Object



170
171
172
# File 'lib/glimmer/swt/custom/shape.rb', line 170

def attach(the_parent_dom_element)
  the_parent_dom_element.html("#{the_parent_dom_element.html()}\n#{@dom}")
end

#background=(value) ⇒ Object



63
64
65
66
67
# File 'lib/glimmer/swt/custom/shape.rb', line 63

def background=(value)
  value = ColorProxy.new(value) if value.is_a?(String) || value.is_a?(Symbol)
  @background = value
  dom_element.css('fill', background.to_css) unless background.nil?
end

#current_parameter_name?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def current_parameter_name?(attribute_name)
  parameter_names.include?(attribute_name.to_s.to_sym)
end

#domObject



178
179
180
181
182
183
184
185
# File 'lib/glimmer/swt/custom/shape.rb', line 178

def dom
  shape_id = id
  shape_class = name
  @dom ||= xml {
    g(id: shape_id, class: shape_class) {
    }
  }.to_s
end

#elementObject



174
175
176
# File 'lib/glimmer/swt/custom/shape.rb', line 174

def element
  'g'
end

#foreground=(value) ⇒ Object



69
70
71
72
73
74
# File 'lib/glimmer/swt/custom/shape.rb', line 69

def foreground=(value)
  value = ColorProxy.new(value) if value.is_a?(String) || value.is_a?(Symbol)
  @foreground = value
  dom_element.css('stroke', foreground.to_css) unless foreground.nil?
  dom_element.css('fill', 'transparent') if background.nil?
end

#get_attribute(attribute_name) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/glimmer/swt/custom/shape.rb', line 140

def get_attribute(attribute_name)
  if parameter_name?(attribute_name)
    arg_index = parameter_index(attribute_name)
    @args[arg_index] if arg_index
  elsif (respond_to?(attribute_name, super: true) and respond_to?(attribute_setter(attribute_name), super: true))
    self.send(attribute_name)
  end
end

#get_parameter_attribute(attribute_name) ⇒ Object



115
116
117
# File 'lib/glimmer/swt/custom/shape.rb', line 115

def get_parameter_attribute(attribute_name)
  @args[parameter_index(attribute_getter(attribute_name))]
end

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
# File 'lib/glimmer/swt/custom/shape.rb', line 123

def has_attribute?(attribute_name, *args)
  parameter_name?(attribute_name) or
    (respond_to?(attribute_name, super: true) and respond_to?(attribute_setter(attribute_name), super: true))
end

#location_parameter_namesObject

subclasses may override to specify location parameter names if different from x and y (e.g. all polygon points are location parameters) used in calculating movement changes



95
96
97
# File 'lib/glimmer/swt/custom/shape.rb', line 95

def location_parameter_names
  [:x, :y]
end

#parameter_index(attribute_name) ⇒ Object



111
112
113
# File 'lib/glimmer/swt/custom/shape.rb', line 111

def parameter_index(attribute_name)
  parameter_names.index(attribute_name.to_s.to_sym)
end

#parameter_name?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def parameter_name?(attribute_name)
  possible_parameter_names.map(&:to_s).include?(attribute_getter(attribute_name))
end

#parameter_namesObject

parameter names for arguments to pass to SWT GC.xyz method for rendering shape (e.g. draw_image(image, x, y) yields :image, :x, :y parameter names)



89
90
91
# File 'lib/glimmer/swt/custom/shape.rb', line 89

def parameter_names
  [:x, :y, :width, :height]
end

#possible_parameter_namesObject



99
100
101
# File 'lib/glimmer/swt/custom/shape.rb', line 99

def possible_parameter_names
  parameter_names
end

#post_add_contentObject



76
77
78
79
# File 'lib/glimmer/swt/custom/shape.rb', line 76

def post_add_content
  # TODO avoid rendering unless args changed from initialize args (due to setting of piecemeal attributes)
  render
end

#render(custom_parent_dom_element: nil, brand_new: false) ⇒ Object



81
82
83
84
85
86
# File 'lib/glimmer/swt/custom/shape.rb', line 81

def render(custom_parent_dom_element: nil, brand_new: false)
  super(custom_parent_dom_element: nil, brand_new: false)
  self.background = background
  self.foreground = foreground
  self.font = font
end

#respond_to?(method_name, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
168
# File 'lib/glimmer/swt/custom/shape.rb', line 160

def respond_to?(method_name, *args, &block)
  options = args.last if args.last.is_a?(Hash)
  super_invocation = options && options[:super]
  if !super_invocation && has_attribute?(method_name)
    true
  else
    super(method_name, (args.first if args.first == true), &block)
  end
end

#set_attribute(attribute_name, *args) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/glimmer/swt/custom/shape.rb', line 128

def set_attribute(attribute_name, *args)
  attribute_getter_name = attribute_getter(attribute_name)
  attribute_setter_name = attribute_setter(attribute_name)
  if parameter_name?(attribute_name)
    return if attribute_getter_name == (args.size == 1 ? args.first : args)
    set_parameter_attribute(attribute_getter_name, *args)
  elsif (respond_to?(attribute_name, super: true) and respond_to?(attribute_setter_name, super: true))
    return if self.send(attribute_getter_name) == (args.size == 1 ? args.first : args)
    self.send(attribute_setter_name, *args)
  end
end

#set_parameter_attribute(attribute_name, *args) ⇒ Object



119
120
121
# File 'lib/glimmer/swt/custom/shape.rb', line 119

def set_parameter_attribute(attribute_name, *args)
  @args[parameter_index(attribute_getter(attribute_name))] = args.size == 1 ? args.first : args
end