Class: Glimmer::SWT::WidgetProxy
- Inherits:
-
Object
- Object
- Glimmer::SWT::WidgetProxy
- Includes:
- DataBinding::ObservableWidget, Packages
- Defined in:
- lib/glimmer/swt/widget_proxy.rb
Overview
Proxy for SWT Widget objects
Sets default SWT styles to widgets upon inititalizing as per DEFAULT_STYLES
Also, auto-initializes widgets as per initializer blocks in DEFAULT_INITIALIZERS (e.g. setting Composite default layout)
Follows the Proxy Design Pattern
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_STYLES =
{ "text" => [:border], "table" => [:border], "spinner" => [:border], "styled_text" => [:border], "list" => [:border, :v_scroll], "button" => [:push], "menu_item" => [:push], }
- DEFAULT_INITIALIZERS =
{ "composite" => proc do |composite| composite.setLayout(GridLayout.new) end, "table" => proc do |table| table.setHeaderVisible(true) table.setLinesVisible(true) end, "table_column" => proc do |table_column| table_column.setWidth(80) end, "group" => proc do |group| group.setLayout(GridLayout.new) end, }
Instance Attribute Summary collapse
-
#swt_widget ⇒ Object
readonly
Returns the value of attribute swt_widget.
Class Method Summary collapse
-
.swt_widget_class_for(underscored_widget_name) ⇒ Object
This supports widgets in and out of basic SWT.
- .widget_exists?(underscored_widget_name) ⇒ Boolean
Instance Method Summary collapse
-
#add_observer(observer, property_name) ⇒ Object
Used for data-binding only.
- #async_exec(&block) ⇒ Object
-
#can_add_observer?(property_name) ⇒ Boolean
TODO Consider renaming these methods as they are mainly used for data-binding.
-
#can_handle_observation_request?(observation_request) ⇒ Boolean
TODO eliminate duplication in the following methods perhaps by relying on exceptions.
- #content(&block) ⇒ Object
- #dispose ⇒ Object
- #extract_args(underscored_widget_name, args) ⇒ Object
- #get_attribute(attribute_name) ⇒ Object
- #handle_observation_request(observation_request, &block) ⇒ Object
- #has_attribute?(attribute_name, *args) ⇒ Boolean
- #has_style?(style) ⇒ Boolean
-
#initialize(underscored_widget_name, parent, args) ⇒ WidgetProxy
constructor
Initializes a new SWT Widget.
- #remove_observer(observer, property_name) ⇒ Object
- #set_attribute(attribute_name, *args) ⇒ Object
- #sync_exec(&block) ⇒ Object
- #widget_property_listener_installers ⇒ Object
Methods included from DataBinding::ObservableWidget
Constructor Details
#initialize(underscored_widget_name, parent, args) ⇒ WidgetProxy
Initializes a new SWT Widget
Styles is a comma separate list of symbols representing SWT styles in lower case
54 55 56 57 58 59 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 54 def initialize(, parent, args) styles, = extract_args(, args) = self.class.() = .new(parent., style(, styles), *) DEFAULT_INITIALIZERS[]&.call() end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Glimmer::DataBinding::ObservableWidget
Instance Attribute Details
#swt_widget ⇒ Object (readonly)
Returns the value of attribute swt_widget.
49 50 51 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 49 def end |
Class Method Details
.swt_widget_class_for(underscored_widget_name) ⇒ Object
This supports widgets in and out of basic SWT
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 190 def self.() = .camelcase(:upper) = eval() unless .ancestors.include?(org.eclipse.swt..Widget) Glimmer.logger&.debug("Class #{swt_widget_class} matching #{underscored_widget_name} is not a subclass of org.eclipse.swt.widgets.Widget") return nil end rescue NameError => e Glimmer.logger&.debug e. # Glimmer.logger&.debug("#{e.message}\n#{e.backtrace.join("\n")}") nil rescue => e Glimmer.logger&.debug e. # Glimmer.logger&.debug("#{e.message}\n#{e.backtrace.join("\n")}") nil end |
.widget_exists?(underscored_widget_name) ⇒ Boolean
185 186 187 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 185 def self.() !!() end |
Instance Method Details
#add_observer(observer, property_name) ⇒ Object
Used for data-binding only. Consider renaming or improving to avoid the confusion it causes
231 232 233 234 235 236 237 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 231 def add_observer(observer, property_name) property_listener_installers = .class.ancestors.map {|ancestor| [ancestor]}.compact = property_listener_installers.map{|installer| installer[property_name.to_s.to_sym]}.compact if !property_listener_installers.empty? .each do || .call(observer) end end |
#async_exec(&block) ⇒ Object
208 209 210 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 208 def async_exec(&block) DisplayProxy.instance.async_exec(&block) end |
#can_add_observer?(property_name) ⇒ Boolean
TODO Consider renaming these methods as they are mainly used for data-binding
226 227 228 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 226 def can_add_observer?(property_name) .class.ancestors.map {|ancestor| [ancestor]}.compact.map(&:keys).flatten.map(&:to_s).include?(property_name.to_s) end |
#can_handle_observation_request?(observation_request) ⇒ Boolean
TODO eliminate duplication in the following methods perhaps by relying on exceptions
245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 245 def can_handle_observation_request?(observation_request) observation_request = observation_request.to_s if observation_request.start_with?('on_event_') constant_name = observation_request.sub(/^on_event_/, '') SWTProxy.has_constant?(constant_name) elsif observation_request.start_with?('on_') event = observation_request.sub(/^on_/, '') can_add_listener?(event) else false end end |
#content(&block) ⇒ Object
268 269 270 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 268 def content(&block) Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::SWT::WidgetExpression.new, &block) end |
#dispose ⇒ Object
220 221 222 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 220 def dispose .dispose end |
#extract_args(underscored_widget_name, args) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 61 def extract_args(, args) @arg_extractor_mapping ||= { 'menu_item' => lambda do |args| index = args.delete(args.last) if args.last.is_a?(Numeric) = [index].compact styles = args [styles, ] end, } if @arg_extractor_mapping[] @arg_extractor_mapping[].call(args) else [args, []] end end |
#get_attribute(attribute_name) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 96 def get_attribute(attribute_name) = [attribute_name.to_s] if .send([:getter][:name]) else .send(attribute_getter(attribute_name)) end end |
#handle_observation_request(observation_request, &block) ⇒ Object
258 259 260 261 262 263 264 265 266 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 258 def handle_observation_request(observation_request, &block) if observation_request.start_with?('on_event_') constant_name = observation_request.sub(/^on_event_/, '') add_swt_event_listener(constant_name, &block) elsif observation_request.start_with?('on_') event = observation_request.sub(/^on_/, '') add_listener(event, &block) end end |
#has_attribute?(attribute_name, *args) ⇒ Boolean
77 78 79 80 81 82 83 84 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 77 def has_attribute?(attribute_name, *args) = [attribute_name.to_s] if .respond_to?([:setter][:name]) else .respond_to?(attribute_setter(attribute_name), args) end end |
#has_style?(style) ⇒ Boolean
216 217 218 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 216 def has_style?(style) (.style & SWTProxy[style]) == SWTProxy[style] end |
#remove_observer(observer, property_name) ⇒ Object
239 240 241 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 239 def remove_observer(observer, property_name) # TODO consider implementing if remove_observer is needed (consumers can remove listener via SWT API) end |
#set_attribute(attribute_name, *args) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 86 def set_attribute(attribute_name, *args) = [attribute_name.to_s] if [:setter][:invoker].call(, args) else apply_property_type_converters(attribute_name, args) .send(attribute_setter(attribute_name), *args) unless .send(attribute_getter(attribute_name)) == args.first end end |
#sync_exec(&block) ⇒ Object
212 213 214 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 212 def sync_exec(&block) DisplayProxy.instance.sync_exec(&block) end |
#widget_property_listener_installers ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/glimmer/swt/widget_proxy.rb', line 105 def ||= { Java::OrgEclipseSwtWidgets::Control => { :focus => proc do |observer| on_focus_gained { |focus_event| observer.call(true) } on_focus_lost { |focus_event| observer.call(false) } end, }, Java::OrgEclipseSwtWidgets::Text => { :text => proc do |observer| on_modify_text { |modify_event| observer.call(.getText) } end, :caret_position => proc do |observer| on_event_keydown { |event| observer.call(.getCaretPosition) } on_event_keyup { |event| observer.call(.getCaretPosition) } on_event_mousedown { |event| observer.call(.getCaretPosition) } on_event_mouseup { |event| observer.call(.getCaretPosition) } end, :selection_count => proc do |observer| on_event_keydown { |event| observer.call(.getSelectionCount) } on_event_keyup { |event| observer.call(.getSelectionCount) } on_event_mousedown { |event| observer.call(.getSelectionCount) } on_event_mouseup { |event| observer.call(.getSelectionCount) } end, :top_index => proc do |observer| @last_top_index = .getTopIndex on_paint_control { |event| if .getTopIndex != @last_top_index @last_top_index = .getTopIndex observer.call(@last_top_index) end } end, }, Java::OrgEclipseSwtCustom::StyledText => { :text => proc do |observer| on_modify_text { |modify_event| observer.call(.getText) } end, }, Java::OrgEclipseSwtWidgets::Button => { :selection => proc do |observer| { |selection_event| observer.call(.getSelection) } end }, Java::OrgEclipseSwtWidgets::Spinner => { :selection => proc do |observer| { |selection_event| observer.call(.getSelection) } end } } end |