Class: Glimmer::SWT::ImageProxy

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ 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.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/glimmer/swt/image_proxy.rb', line 49

def initialize(*args)
  @args = args
  options = @args.last.is_a?(Hash) ? @args.delete_at(-1) : {}
  options[:swt_image] = @args.first if @args.size == 1 && @args.first.is_a?(Image)
  @file_path = @args.first if @args.size == 1 && @args.first.is_a?(String)
  @args = @args.first if @args.size == 1 && @args.first.is_a?(Array)
  if options&.keys&.include?(:swt_image)
    @swt_image = options[:swt_image]
    @image_data = @swt_image.image_data
  elsif @file_path
    @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?
  else
    @swt_image = Image.new(*@args)
    @image_data = @swt_image.image_data
  end        
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



93
94
95
96
97
98
# File 'lib/glimmer/swt/image_proxy.rb', line 93

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.



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

def file_path
  @file_path
end

#image_dataObject (readonly)

Returns the value of attribute image_data.



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

def image_data
  @image_data
end

#jar_file_pathObject (readonly)

Returns the value of attribute jar_file_path.



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

def jar_file_path
  @jar_file_path
end

#swt_imageObject (readonly)

Returns the value of attribute swt_image.



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

def swt_image
  @swt_image
end

Class Method Details

.create(*args) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/glimmer/swt/image_proxy.rb', line 31

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

Instance Method Details

#input_streamObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/glimmer/swt/image_proxy.rb', line 72

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

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

Returns:

  • (Boolean)


100
101
102
# File 'lib/glimmer/swt/image_proxy.rb', line 100

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

#scale_to(width, height) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/glimmer/swt/image_proxy.rb', line 84

def scale_to(width, height)
  scaled_image_data = 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