Class: Glimmer::SWT::ImageProxy

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

Overview

Proxy for org.eclipse.swt.graphics.Image

Invoking ‘#swt_image` returns the SWT Image object wrapped by this proxy

Follows the Proxy Design Pattern

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 Properties

attribute_getter, #attribute_getter, attribute_setter, #attribute_setter, normalized_attribute, #normalized_attribute, ruby_attribute_getter, #ruby_attribute_setter, ruby_attribute_setter

Methods included from Custom::Drawable

#add_shape, #clear_shapes, #deregister_shape_painting, #expanded_shapes, #image_buffered_shapes, #paint_pixel_by_pixel, #setup_shape_painting, #shape_at_location, #shapes, #swt_drawable

Constructor Details

#initialize(*args, &content) ⇒ ImageProxy

Initializes a proxy for an SWT Image object

Takes the same args as the SWT Image class Alternatively, takes a file path string or a uri:classloader file path string (generated by JRuby when invoking ‘File.expand_path` inside a JAR file) and returns an image object. Last but not least, it could serve as a parent for nesting shapes underneath to build an image with the Canvas Shape DSL



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/glimmer/swt/image_proxy.rb', line 68

def initialize(*args, &content)
  @args = args
  @parent_proxy = nil
  if @args.first.is_a?(WidgetProxy)
    @parent_proxy = @args.shift
    @parent = @parent_proxy.swt_widget
  end
  options = @args.last.is_a?(Hash) ? @args.delete_at(-1) : {}
  options[:swt_image] = @args.first if @args.size == 1 && @args.first.is_a?(Image)
  @args = @args.first if @args.size == 1 && @args.first.is_a?(Array)
  @file_path = @args.first if @args.first.is_a?(String)
  if options&.keys&.include?(:swt_image)
    @swt_image = options[:swt_image]
    @original_image_data = @image_data = @swt_image.image_data
  elsif args.size == 1 && args.first.is_a?(ImageProxy)
    @swt_image = @args.first.swt_image
    @original_image_data = @image_data = @swt_image.image_data
  elsif @file_path
    @original_image_data = @image_data = ImageData.new(input_stream || @file_path)
    @swt_image = Image.new(DisplayProxy.instance.swt_display, @image_data)
    width = options[:width]
    height = options[:height]
    height = (@image_data.height.to_f / @image_data.width.to_f)*width.to_f if !width.nil? && height.nil?
    width = (@image_data.width.to_f / @image_data.height.to_f)*height.to_f if !height.nil? && width.nil?
    scale_to(width, height) unless width.nil? || height.nil?
  elsif !@args.first.is_a?(ImageProxy) && !@args.first.is_a?(Image)
    @args.prepend(DisplayProxy.instance.swt_display) unless @args.first.is_a?(Display)
    @swt_image = Image.new(*@args)
    @original_image_data = @image_data = @swt_image.image_data
  end
  proxy = self
  # TODO consider adding a get_data/set_data method to conform with other SWT widgets
  @swt_image.singleton_class.define_method(:dispose) do
    proxy.clear_shapes
    super()
  end
  post_add_content if content.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



198
199
200
201
202
203
# File 'lib/glimmer/swt/image_proxy.rb', line 198

def method_missing(method, *args, &block)
  swt_image.send(method, *args, &block)
rescue => e
  Glimmer::Config.logger.debug {"Neither ImageProxy nor #{swt_image.class.name} can handle the method ##{method}"}
  super
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



60
61
62
# File 'lib/glimmer/swt/image_proxy.rb', line 60

def file_path
  @file_path
end

#image_dataObject (readonly)

Returns the value of attribute image_data.



60
61
62
# File 'lib/glimmer/swt/image_proxy.rb', line 60

def image_data
  @image_data
end

#jar_file_pathObject (readonly)

Returns the value of attribute jar_file_path.



60
61
62
# File 'lib/glimmer/swt/image_proxy.rb', line 60

def jar_file_path
  @jar_file_path
end

#parentObject (readonly)

Returns the value of attribute parent.



60
61
62
# File 'lib/glimmer/swt/image_proxy.rb', line 60

def parent
  @parent
end

#parent_proxyObject (readonly)

Returns the value of attribute parent_proxy.



60
61
62
# File 'lib/glimmer/swt/image_proxy.rb', line 60

def parent_proxy
  @parent_proxy
end

#swt_imageObject (readonly)

Returns the value of attribute swt_image.



60
61
62
# File 'lib/glimmer/swt/image_proxy.rb', line 60

def swt_image
  @swt_image
end

Class Method Details

.create(*args, &content) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/glimmer/swt/image_proxy.rb', line 39

def create(*args, &content)
  if args.size == 1 && args.first.is_a?(ImageProxy)
    args.first
  else
    new(*args, &content)
  end
end

.create_pixel_by_pixel(*args, &each_pixel_color) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/glimmer/swt/image_proxy.rb', line 47

def create_pixel_by_pixel(*args, &each_pixel_color)
  image_proxy = create(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  height = args[-1]
  width = args[-2]
  image_proxy.paint_pixel_by_pixel(width, height, &each_pixel_color)
  image_proxy
end

Instance Method Details

#disposed?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/glimmer/swt/image_proxy.rb', line 156

def disposed?
  @swt_image.isDisposed
end

#gcObject



148
149
150
# File 'lib/glimmer/swt/image_proxy.rb', line 148

def gc
  @gc ||= reset_gc
end

#get_attribute(attribute_name) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/glimmer/swt/image_proxy.rb', line 184

def get_attribute(attribute_name)
  if @swt_image.respond_to?(attribute_getter(attribute_name))
    @swt_image.send(attribute_getter(attribute_name))
  elsif @swt_image.respond_to?(ruby_attribute_getter(attribute_name))
    @swt_image.send(ruby_attribute_getter(attribute_name))
  elsif @swt_image.respond_to?(attribute_name)
    @swt_image.send(attribute_name)
  elsif respond_to?(ruby_attribute_getter(attribute_name))
    send(ruby_attribute_getter(attribute_name))
  else
    send(attribute_name)
  end
end

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/glimmer/swt/image_proxy.rb', line 160

def has_attribute?(attribute_name, *args)
  @swt_image.respond_to?(attribute_setter(attribute_name), args) || respond_to?(ruby_attribute_setter(attribute_name), args)
end

#input_streamObject



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

def input_stream
  if @file_path.start_with?('uri:classloader')
    @jar_file_path = @file_path
    file_path = @jar_file_path.sub(/^uri\:classloader\:/, '').sub('//', '/') # the latter sub is needed for Mac
    object = java.lang.Object.new
    file_input_stream = object.java_class.resource_as_stream(file_path)
  else
    file_input_stream = java.io.FileInputStream.new(@file_path)
  end
  java.io.BufferedInputStream.new(file_input_stream) if file_input_stream
end

#post_add_contentObject



107
108
109
110
111
112
113
114
# File 'lib/glimmer/swt/image_proxy.rb', line 107

def post_add_content
  if shapes.any?
    setup_shape_painting
  end
  if @parent.respond_to?('image=') && !@parent.is_disposed
    @parent&.image = swt_image
  end
end

#reset_gcObject



152
153
154
# File 'lib/glimmer/swt/image_proxy.rb', line 152

def reset_gc
  @gc = org.eclipse.swt.graphics.GC.new(swt_image)
end

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

Returns:

  • (Boolean)


205
206
207
# File 'lib/glimmer/swt/image_proxy.rb', line 205

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

#scale_to(width, height) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/glimmer/swt/image_proxy.rb', line 134

def scale_to(width, height)
  return self if @image_data.width == width && @image_data.height == height
  scaled_image_data = @original_image_data.scaledTo(width, height)
  device = swt_image.device
  swt_image.dispose
  @swt_image = Image.new(device, scaled_image_data)
  @image_data = @swt_image.image_data
  self
end

#set_attribute(attribute_name, *args) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/glimmer/swt/image_proxy.rb', line 164

def set_attribute(attribute_name, *args)
  # TODO consider refactoring/unifying this code with WidgetProxy and elsewhere
  if args.count == 1
    if args.first.is_a?(Symbol) || args.first.is_a?(String)
      args[0] = ColorProxy.new(args.first).swt_color
    end
    if args.first.is_a?(ColorProxy)
      args[0] = args.first.swt_color
    end
  end

  if @swt_image.respond_to?(attribute_setter(attribute_name))
    @swt_image.send(attribute_setter(attribute_name), *args) unless @swt_image.send(attribute_getter(attribute_name)) == args.first
  elsif @swt_image.respond_to?(ruby_attribute_setter(attribute_name))
    @swt_image.send(ruby_attribute_setter(attribute_name), args)
  else
    send(ruby_attribute_setter(attribute_name), args)
  end
end

#shape(parent_proxy = nil, args = nil) ⇒ Object



116
117
118
119
120
# File 'lib/glimmer/swt/image_proxy.rb', line 116

def shape(parent_proxy = nil, args = nil)
  parent_proxy ||= @parent_proxy
  args ||= [self] # TODO consider passing args if available
  @shape ||= Glimmer::SWT::Custom::Shape.new(parent_proxy, 'image', *args)
end

#sizeObject



144
145
146
# File 'lib/glimmer/swt/image_proxy.rb', line 144

def size
  org.eclipse.swt.graphics.Point.new(bounds.width, bounds.height)
end