Module: Glimmer::UI::CustomWidget

Includes:
DataBinding::ObservableModel
Included in:
SWT::Custom::CheckboxGroup, SWT::Custom::RadioGroup, CustomShell, GreetingLabel
Defined in:
lib/glimmer/ui/custom_widget.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



268
269
270
271
272
273
274
# File 'lib/glimmer/ui/custom_widget.rb', line 268

def method_missing(method, *args, &block)
  if can_handle_observation_request?(method)
    handle_observation_request(method, block)
  else
    body_root.send(method, *args, &block)
  end
end

Instance Attribute Details

#body_rootObject (readonly)

Returns the value of attribute body_root.



151
152
153
# File 'lib/glimmer/ui/custom_widget.rb', line 151

def body_root
  @body_root
end

#optionsObject (readonly)

Returns the value of attribute options.



151
152
153
# File 'lib/glimmer/ui/custom_widget.rb', line 151

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



151
152
153
# File 'lib/glimmer/ui/custom_widget.rb', line 151

def parent
  @parent
end

#swt_styleObject (readonly)

Returns the value of attribute swt_style.



151
152
153
# File 'lib/glimmer/ui/custom_widget.rb', line 151

def swt_style
  @swt_style
end

Class Method Details

.add_custom_widget_namespaces_for(klass) ⇒ Object



125
126
127
128
129
# File 'lib/glimmer/ui/custom_widget.rb', line 125

def add_custom_widget_namespaces_for(klass)
  Glimmer::UI::CustomWidget.namespaces_for_class(klass).drop(1).each do |namespace|
    Glimmer::UI::CustomWidget.custom_widget_namespaces << namespace
  end
end

.custom_widget_namespacesObject



139
140
141
# File 'lib/glimmer/ui/custom_widget.rb', line 139

def custom_widget_namespaces
  @custom_widget_namespaces ||= reset_custom_widget_namespaces
end

.for(underscored_custom_widget_name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/glimmer/ui/custom_widget.rb', line 97

def for(underscored_custom_widget_name)
  extracted_namespaces = underscored_custom_widget_name.
    to_s.
    split(/__/).map do |namespace|
      namespace.camelcase(:upper)
    end
  custom_widget_namespaces.each do |base|
    extracted_namespaces.reduce(base) do |result, namespace|
      if !result.constants.include?(namespace)
        namespace = result.constants.detect {|c| c.to_s.upcase == namespace.to_s.upcase } || namespace
      end
      begin
        constant = result.const_get(namespace)
        return constant if constant&.respond_to?(:ancestors) && constant&.ancestors&.to_a.include?(Glimmer::UI::CustomWidget)
        constant
      rescue => e
        # Glimmer::Config.logger.debug {"#{e.message}\n#{e.backtrace.join("\n")}"}
        result
      end
    end
  end
  raise "#{underscored_custom_widget_name} has no custom widget class!"
rescue => e
  Glimmer::Config.logger.debug {e.message}
  Glimmer::Config.logger.debug {"#{e.message}\n#{e.backtrace.join("\n")}"}
  nil
end

.included(klass) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/glimmer/ui/custom_widget.rb', line 89

def included(klass)
  klass.extend(ClassMethods)
  unless klass.name.include?('Glimmer::UI::CustomShell')
    klass.include(Glimmer)
    Glimmer::UI::CustomWidget.add_custom_widget_namespaces_for(klass)
  end
end

.namespaces_for_class(m) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/glimmer/ui/custom_widget.rb', line 131

def namespaces_for_class(m)
  return [m] if m.name.nil?
  namespace_constants = m.name.split(/::/).map(&:to_sym)
  namespace_constants.reduce([Object]) do |output, namespace_constant|
    output += [output.last.const_get(namespace_constant)]
  end[1..-1].uniq.reverse
end

.reset_custom_widget_namespacesObject



143
144
145
# File 'lib/glimmer/ui/custom_widget.rb', line 143

def reset_custom_widget_namespaces
  @custom_widget_namespaces = Set[Object, Glimmer::UI]
end

Instance Method Details

#add_observer(observer, attribute_name) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/glimmer/ui/custom_widget.rb', line 204

def add_observer(observer, attribute_name)
  if has_instance_method?(attribute_name)
    super
  else
    @body_root.add_observer(observer, attribute_name)
  end
end

#async_exec(&block) ⇒ Object



249
250
251
# File 'lib/glimmer/ui/custom_widget.rb', line 249

def async_exec(&block)
  SWT::DisplayProxy.instance.async_exec(&block)
end

#attribute_setter(attribute_name) ⇒ Object



241
242
243
# File 'lib/glimmer/ui/custom_widget.rb', line 241

def attribute_setter(attribute_name)
  "#{attribute_name}="
end

#can_add_observer?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/glimmer/ui/custom_widget.rb', line 200

def can_add_observer?(attribute_name)
  has_instance_method?(attribute_name) || has_instance_method?("#{attribute_name}?") || @body_root.can_add_observer?(attribute_name)
end

#can_handle_observation_request?(observation_request) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
# File 'lib/glimmer/ui/custom_widget.rb', line 180

def can_handle_observation_request?(observation_request)
  observation_request = observation_request.to_s
  result = false
  if observation_request.start_with?('on_updated_')
    property = observation_request.sub(/^on_updated_/, '')
    result = can_add_observer?(property)
  end
  result || body_root&.can_handle_observation_request?(observation_request)
end

#content(&block) ⇒ Object

Returns content block if used as an attribute reader (no args) Otherwise, if a block is passed, it adds it as content to this custom widget



260
261
262
263
264
265
266
# File 'lib/glimmer/ui/custom_widget.rb', line 260

def content(&block)
  if block_given?
    body_root.content(&block)
  else
    @content
  end
end

#get_attribute(attribute_name) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/glimmer/ui/custom_widget.rb', line 233

def get_attribute(attribute_name)
  if has_instance_method?(attribute_name)
    send(attribute_name)
  else
    @body_root.get_attribute(attribute_name)
  end
end

#handle_observation_request(observation_request, block) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/glimmer/ui/custom_widget.rb', line 190

def handle_observation_request(observation_request, block)
  observation_request = observation_request.to_s
  if observation_request.start_with?('on_updated_')
    property = observation_request.sub(/^on_updated_/, '') # TODO look into eliminating duplication from above
    add_observer(DataBinding::Observer.proc(&block), property) if can_add_observer?(property)
  else
    body_root.handle_observation_request(observation_request, block)
  end
end

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
215
# File 'lib/glimmer/ui/custom_widget.rb', line 212

def has_attribute?(attribute_name, *args)
  has_instance_method?(attribute_setter(attribute_name)) ||
    @body_root.has_attribute?(attribute_name, *args)
end

#has_instance_method?(method_name) ⇒ Boolean

This method ensures it has an instance method not coming from Glimmer DSL

Returns:

  • (Boolean)


226
227
228
229
230
231
# File 'lib/glimmer/ui/custom_widget.rb', line 226

def has_instance_method?(method_name)
  respond_to?(method_name) and
    !body_root&.respond_to?(method_name) and
    !method(method_name)&.source_location&.first&.include?('glimmer/dsl/engine.rb') and
    !method(method_name)&.source_location&.first&.include?('glimmer/swt/widget_proxy.rb')
end

#has_style?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


245
246
247
# File 'lib/glimmer/ui/custom_widget.rb', line 245

def has_style?(symbol)
  @args.include?(symbol) # not a very solid implementation. Bring SWT constants eventually
end

#initialize(parent, args, options, &content) ⇒ Object

Raises:

  • (Glimmer::Error)


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/glimmer/ui/custom_widget.rb', line 153

def initialize(parent, args, options, &content)
  @parent = parent
  options = args.delete_at(-1) if args.is_a?(Array) && args.last.is_a?(Hash)
  if args.is_a?(Hash)
    options = args
    args = []
  end
  options ||= {}
  args = options.delete('swt_style').split(',').map(&:to_sym) if options['swt_style']
  @args = args
  @swt_style = SWT::SWTProxy[*@args]
  options ||= {}
  @options = self.class.options.merge(options)
  @content = Util::ProcTracker.new(content) if content
  execute_hooks('before_body')
  body_block = self.class.instance_variable_get("@body_block")
  raise Glimmer::Error, 'Invalid custom widget for having no body! Please define body block!' if body_block.nil?
  @body_root = instance_exec(&body_block)
  raise Glimmer::Error, 'Invalid custom widget for having an empty body! Please fill body block!' if @body_root.nil?
  execute_hooks('after_body')
end

#local_respond_to?Object



276
# File 'lib/glimmer/ui/custom_widget.rb', line 276

alias local_respond_to? respond_to?

#post_initialize_child(child) ⇒ Object

Subclasses may override to perform post initialization work on an added child



176
177
178
# File 'lib/glimmer/ui/custom_widget.rb', line 176

def post_initialize_child(child)
  # No Op by default
end

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

Returns:

  • (Boolean)


277
278
279
280
281
# File 'lib/glimmer/ui/custom_widget.rb', line 277

def respond_to?(method, *args, &block)
  super or
    can_handle_observation_request?(method) or
    body_root.respond_to?(method, *args, &block)
end

#set_attribute(attribute_name, *args) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/glimmer/ui/custom_widget.rb', line 217

def set_attribute(attribute_name, *args)
  if has_instance_method?(attribute_setter(attribute_name))
    send(attribute_setter(attribute_name), *args)
  else
    @body_root.set_attribute(attribute_name, *args)
  end
end

#sync_exec(&block) ⇒ Object

sync_exec kept for API compatibility reasons



254
255
256
# File 'lib/glimmer/ui/custom_widget.rb', line 254

def sync_exec(&block)
  SWT::DisplayProxy.instance.async_exec(&block)
end