Class: Glimmer::SWT::DisplayProxy

Inherits:
Object
  • Object
show all
Includes:
Custom::Drawable
Defined in:
lib/glimmer/swt/display_proxy.rb

Overview

Proxy for org.eclipse.swt.widgets.Display

Maintains a singleton instance since SWT only supports a single active display at a time.

Supports SWT Display’s very useful asyncExec and syncExec methods to support proper multi-threaded manipulation of SWT UI objects

Invoking ‘#swt_display` returns the SWT Display object wrapped by this proxy

Follows the Proxy Design Pattern

Defined Under Namespace

Classes: FilterListener

Constant Summary collapse

OBSERVED_MENU_ITEMS =
['about', 'preferences', 'quit']

Instance Attribute Summary collapse

Attributes included from Custom::Drawable

#image_double_buffered, #requires_shape_disposal

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Custom::Drawable

#add_shape, #clear_shapes, #deregister_shape_painting, #image_buffered_shapes, #setup_shape_painting, #shapes

Constructor Details

#initialize(*args) ⇒ DisplayProxy

Returns a new instance of DisplayProxy.



70
71
72
73
74
75
76
77
# File 'lib/glimmer/swt/display_proxy.rb', line 70

def initialize(*args)
  Display.app_name ||= 'Glimmer'
  @swt_display = Display.new(*args)
  @swt_display.set_data('proxy', self)
  on_swt_Dispose {
    clear_shapes
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



103
104
105
106
107
108
109
110
111
112
# File 'lib/glimmer/swt/display_proxy.rb', line 103

def method_missing(method, *args, &block)
  if can_handle_observation_request?(method)
    handle_observation_request(method, &block)
  else
    swt_display.send(method, *args, &block)
  end
rescue => e
  Glimmer::Config.logger.debug {"Neither DisplayProxy nor #{swt_display.class.name} can handle the method ##{method}"}
  super
end

Instance Attribute Details

#swt_displayObject (readonly)

SWT Display object wrapped



68
69
70
# File 'lib/glimmer/swt/display_proxy.rb', line 68

def swt_display
  @swt_display
end

Class Method Details

.instance(*args) ⇒ Object

Returns singleton instance



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

def instance(*args)
  if @instance.nil? || @instance.swt_display.nil? || @instance.swt_display.isDisposed
    @instance = new(*args)
  end
  @instance
end

Instance Method Details

#add_swt_event_filter(swt_constant, &block) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/glimmer/swt/display_proxy.rb', line 151

def add_swt_event_filter(swt_constant, &block)
  event_type = SWTProxy[swt_constant]
  @swt_display.addFilter(event_type, FilterListener.new(&block))
  #WidgetListenerProxy.new(@swt_display.getListeners(event_type).last)
  WidgetListenerProxy.new(
    swt_display: @swt_display,
    event_type: event_type,
    filter: true,
    swt_listener: block,
    widget_add_listener_method: 'addFilter',
    swt_listener_class:  FilterListener,
    swt_listener_method: 'handleEvent'
  )
end

#async_exec(&block) ⇒ Object



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

def async_exec(&block)
  @swt_display.asyncExec(&block)
end

#can_handle_observation_request?(observation_request) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/glimmer/swt/display_proxy.rb', line 120

def can_handle_observation_request?(observation_request)
  observation_request = observation_request.to_s
  if observation_request.start_with?('on_swt_')
    constant_name = observation_request.sub(/^on_swt_/, '')
    SWTProxy.has_constant?(constant_name)
  elsif observation_request.start_with?('on_')
    event_name = observation_request.sub(/^on_/, '')
    OBSERVED_MENU_ITEMS.include?(event_name)
  else
    false
  end
end

#content(&block) ⇒ Object



79
80
81
# File 'lib/glimmer/swt/display_proxy.rb', line 79

def content(&block)
  Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::SWT::DisplayExpression.new, &block)
end

#disposed?Boolean

Returns:

  • (Boolean)


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

def disposed?
  @swt_display.isDisposed
end

#handle_observation_request(observation_request, &block) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/glimmer/swt/display_proxy.rb', line 133

def handle_observation_request(observation_request, &block)
  observation_request = observation_request.to_s
  if observation_request.start_with?('on_swt_')
    constant_name = observation_request.sub(/^on_swt_/, '')
    add_swt_event_filter(constant_name, &block)
  elsif observation_request.start_with?('on_')
    event_name = observation_request.sub(/^on_/, '')
    if OBSERVED_MENU_ITEMS.include?(event_name) && OS.mac?
      system_menu = swt_display.getSystemMenu
      menu_item = system_menu.getItems.find {|menu_item| menu_item.getID == SWTProxy["ID_#{event_name.upcase}"]}
      display_mac_event_registration = menu_item.addListener(SWTProxy[:Selection], &block)
      # TODO enable this code and test on the Mac to ensure automatic cleanup of mac event registrations in custom widgets
#             Glimmer::UI::CustomWidget.current_custom_widgets.last&.observer_registrations&.push(display_mac_event_registration)
      display_mac_event_registration
    end
  end
end

#on_widget_disposed(&block) ⇒ Object



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

def on_widget_disposed(&block)
  on_swt_Dispose(&block)
end

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

Returns:

  • (Boolean)


114
115
116
117
118
# File 'lib/glimmer/swt/display_proxy.rb', line 114

def respond_to?(method, *args, &block)
  super ||
    can_handle_observation_request?(method) ||
    swt_display.respond_to?(method, *args, &block)
end

#sync_exec(&block) ⇒ Object



87
88
89
# File 'lib/glimmer/swt/display_proxy.rb', line 87

def sync_exec(&block)
  @swt_display.syncExec(&block)
end

#timer_exec(delay_in_millis, &block) ⇒ Object



91
92
93
# File 'lib/glimmer/swt/display_proxy.rb', line 91

def timer_exec(delay_in_millis, &block)
  @swt_display.timerExec(delay_in_millis, &block)
end