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

Inherits:
Object
  • Object
show all
Includes:
Packages, Properties
Defined in:
lib/glimmer/swt/custom/shape.rb

Overview

Represents a shape (graphics) to be drawn on a control/widget/canvas/display swt_widget returns the parent (e.g. a ‘canvas` WidgetProxy), equivalent to `parent.swt_widget` That is because Shape is drawn on a parent as graphics and doesn’t have an SWT widget for itself

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Properties

#attribute_getter, #attribute_setter, #normalized_attribute, #ruby_attribute_setter

Methods included from Packages

included

Constructor Details

#initialize(parent, keyword, *args, &property_block) ⇒ Shape

Returns a new instance of Shape.



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

def initialize(parent, keyword, *args, &property_block)
  @parent = parent
  @name = keyword
  @method_name = self.class.method_name(keyword, args)
  @options = self.class.arg_options(args, extract: true)
  @args = args
  @swt_widget = parent.respond_to?(:swt_display) ? parent.swt_display : parent.swt_widget
  @properties = {}
  @parent.shapes << self
  post_add_content if property_block.nil?
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#paint_listener_proxyObject (readonly)

Returns the value of attribute paint_listener_proxy.



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

def paint_listener_proxy
  @paint_listener_proxy
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#swt_widgetObject (readonly)

Returns the value of attribute swt_widget.



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

def swt_widget
  @swt_widget
end

Class Method Details

.arg_options(args, extract: false) ⇒ Object



60
61
62
63
64
# File 'lib/glimmer/swt/custom/shape.rb', line 60

def arg_options(args, extract: false)
  arg_options_method = extract ? :pop : :last
  options = args.send(arg_options_method) if args.last.is_a?(Hash)
  options.nil? ? {} : options.symbolize_keys
end

.flyweight_method_namesObject



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

def flyweight_method_names
  @flyweight_method_names ||= {}
end

.gc_instance_methodsObject



46
47
48
# File 'lib/glimmer/swt/custom/shape.rb', line 46

def gc_instance_methods
  @gc_instance_methods ||= org.eclipse.swt.graphics.GC.instance_methods.map(&:to_s)
end

.keywordsObject



50
51
52
53
54
55
56
57
58
# File 'lib/glimmer/swt/custom/shape.rb', line 50

def keywords
  @keywords ||= gc_instance_methods.select do |method_name|
    !method_name.end_with?('=') && (method_name.start_with?('draw_') || method_name.start_with?('fill_'))
  end.reject do |method_name|
    gc_instance_methods.include?("#{method_name}=") || gc_instance_methods.include?("set_#{method_name}")
  end.map do |method_name|
    method_name.gsub(/(draw|fill|gradient|round)_/, '')
  end.uniq.compact.to_a
end

.method_name(keyword, args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/glimmer/swt/custom/shape.rb', line 66

def method_name(keyword, args)
  method_arg_options = arg_options(args)
  unless flyweight_method_names.keys.include?([keyword, method_arg_options])
    keyword = keyword.to_s
    gradient = 'gradient_' if method_arg_options[:gradient]
    round = 'round_' if method_arg_options[:round]
    gc_instance_method_name_prefix = !['polyline', 'point', 'image', 'focus'].include?(keyword) && (method_arg_options[:fill] || method_arg_options[:gradient]) ? 'fill_' : 'draw_'
    flyweight_method_names[[keyword, method_arg_options]] = "#{gc_instance_method_name_prefix}#{gradient}#{round}#{keyword}"
  end
  flyweight_method_names[[keyword, method_arg_options]]
end

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

Returns:

  • (Boolean)


42
43
44
# File 'lib/glimmer/swt/custom/shape.rb', line 42

def valid?(parent, keyword, *args, &block)
  gc_instance_methods.include?(method_name(keyword, args))
end

Instance Method Details

#apply_property_arg_conversions(method_name, property, args) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/glimmer/swt/custom/shape.rb', line 118

def apply_property_arg_conversions(method_name, property, args)
  args = args.dup
  the_java_method = org.eclipse.swt.graphics.GC.java_class.declared_instance_methods.detect {|m| m.name == method_name}
  if (args.first.is_a?(Symbol) || args.first.is_a?(String))
    if the_java_method.parameter_types.first == Color.java_class
      args[0] = ColorProxy.new(args[0])
    end
    if the_java_method.parameter_types.first == Java::int.java_class
      args[0] = SWTProxy.constant(args[0])
    end
  end
  if args.first.is_a?(ColorProxy)
    args[0] = args[0].swt_color
  end
  if args.first.is_a?(Hash) && the_java_method.parameter_types.first == Font.java_class
    args[0] = FontProxy.new(args[0])
  end
  if args.first.is_a?(FontProxy)
    args[0] = args[0].swt_font
  end
  if args.first.is_a?(TransformProxy)
    args[0] = args[0].swt_transform
  end
  if ['setBackgroundPattern', 'setForegroundPattern'].include?(method_name.to_s)
    args.each_with_index do |arg, i|
      if arg.is_a?(Symbol) || arg.is_a?(String)
        args[i] = ColorProxy.new(arg).swt_color
      elsif arg.is_a?(ColorProxy)
        args[i] = arg.swt_color
      end
    end
    new_args = [DisplayProxy.instance.swt_display] + args
    args[0] = org.eclipse.swt.graphics.Pattern.new(*new_args)
    args[1..-1] = []
  end
  args
end

#apply_shape_arg_conversions(method_name, args) ⇒ Object



156
157
158
159
160
161
# File 'lib/glimmer/swt/custom/shape.rb', line 156

def apply_shape_arg_conversions(method_name, args)
  if args.size > 1 && (method_name.include?('polygon') || method_name.include?('polyline'))
    args[0] = args.dup
    args[1..-1] = []
  end
end

#apply_shape_arg_defaults(method_name, args) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/glimmer/swt/custom/shape.rb', line 163

def apply_shape_arg_defaults(method_name, args)
  if method_name.include?('round_rectangle') && args.size.between?(4, 5)
    (6 - args.size).times {args << 60}
  elsif method_name.include?('rectangle') && gradient? && args.size == 4
    args << true
  elsif (method_name.include?('text') || method_name.include?('string')) && !@properties.keys.map(&:to_s).include?('background') && args.size == 3
    args << true
  end
  if method_name.include?('image') && args.first.is_a?(String)
    args[0] = ImageProxy.new(args[0])
  end
  if method_name.include?('image') && args.first.is_a?(ImageProxy)
    args[0] = args[0].swt_image
  end
end

#draw?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/glimmer/swt/custom/shape.rb', line 97

def draw?
  !fill?
end

#fill?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/glimmer/swt/custom/shape.rb', line 101

def fill?
  @options[:fill]
end

#get_attribute(attribute_name) ⇒ Object



202
203
204
# File 'lib/glimmer/swt/custom/shape.rb', line 202

def get_attribute(attribute_name)
  @properties.symbolize_keys[attribute_name.to_s.to_sym]
end

#gradient?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/glimmer/swt/custom/shape.rb', line 105

def gradient?
  @options[:gradient]
end

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/glimmer/swt/custom/shape.rb', line 190

def has_attribute?(attribute_name, *args)
  self.class.gc_instance_methods.include?(attribute_setter(attribute_name))
end

#paint(paint_event) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/glimmer/swt/custom/shape.rb', line 215

def paint(paint_event)
  @properties['background'] = [@parent.background] if fill? && !@properties.keys.map(&:to_s).include?('background')
  @properties['foreground'] = [@parent.foreground] if draw? && !@properties.keys.map(&:to_s).include?('foreground')
  @properties['font'] = [@parent.font] if draw? && !@properties.keys.map(&:to_s).include?('font')
  @properties['transform'] = [nil] if !@properties.keys.map(&:to_s).include?('transform')
  @properties.each do |property, args|
    method_name = attribute_setter(property)
    converted_args = apply_property_arg_conversions(method_name, property, args)
    paint_event.gc.send(method_name, *converted_args)
    if property == 'transform' && args.first.is_a?(TransformProxy)
      args.first.swt_transform.dispose
    end
  end
  apply_shape_arg_conversions(@method_name, @args)
  apply_shape_arg_defaults(@method_name, @args)
  tolerate_shape_extra_args(@method_name, @args)
  paint_event.gc.send(@method_name, *@args)
end

#post_add_contentObject



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

def post_add_content
  setup_paint_listener
  @content_added = true
end

#round?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/glimmer/swt/custom/shape.rb', line 109

def round?
  @options[:round]
end

#set_attribute(attribute_name, *args) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/glimmer/swt/custom/shape.rb', line 194

def set_attribute(attribute_name, *args)
  @properties[attribute_name] = args
  if @content_added && !@parent.is_disposed
    @parent.resetup_shape_paint_listeners
    @parent.redraw
  end
end

#setup_paint_listenerObject



206
207
208
209
210
211
212
213
# File 'lib/glimmer/swt/custom/shape.rb', line 206

def setup_paint_listener
  return if @parent.is_disposed
  if parent.respond_to?(:swt_display)
    @paint_listener_proxy = @parent.on_swt_paint(&method(:paint))
  elsif parent.respond_to?(:swt_widget)
    @paint_listener_proxy = @parent.on_paint_control(&method(:paint))
  end
end

#tolerate_shape_extra_args(method_name, args) ⇒ Object

Tolerates shape extra args added by user by mistake (e.g. happens when switching from round rectangle to a standard one without removing all extra args)



181
182
183
184
185
186
187
188
# File 'lib/glimmer/swt/custom/shape.rb', line 181

def tolerate_shape_extra_args(method_name, args)
  the_java_method_arg_count = org.eclipse.swt.graphics.GC.java_class.declared_instance_methods.select do |m|
    m.name == method_name.camelcase(:lower)
  end.map(&:parameter_types).map(&:size).max
  if args.size > the_java_method_arg_count
    args[the_java_method_arg_count..-1] = []
  end
end