Class: Glimmer::SWT::ColorProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/glimmer/swt/color_proxy.rb

Overview

Proxy for org.eclipse.swt.graphics.Color

Invoking ‘#swt_color` returns the SWT Color object wrapped by this proxy

Follows the Proxy Design Pattern and Flyweight Design Pattern (caching memoization)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ColorProxy

Initializes a proxy for an SWT Color object

Takes a standard color single argument, rgba 3 args, or rgba 4 args

A standard color is a string/symbol representing one of the SWT.COLOR_*** constants like SWT.COLOR_RED, but in underscored string format (e.g :color_red). Glimmer can also accept standard color names without the color_ prefix, and it will automatically figure out the SWT.COLOR_*** constant (e.g. :red)

rgb is 3 arguments representing Red, Green, Blue numeric values

rgba is 4 arguments representing Red, Green, Blue, and Alpha numeric values



61
62
63
64
65
66
# File 'lib/glimmer/swt/color_proxy.rb', line 61

def initialize(*args)
  @options = args.last.is_a?(Hash) ? args.pop : {}
  @args = args
  @args = @args.first if @args.first.is_a?(Array)
  ensure_arg_values_within_valid_bounds unless @options[:ensure_bounds] == false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

Class Method Details

.flyweight(*args) ⇒ Object



36
37
38
# File 'lib/glimmer/swt/color_proxy.rb', line 36

def flyweight(*args)
  flyweight_color_proxies[args] ||= new(*args)
end

.flyweight_color_proxiesObject

Flyweight Design Pattern memoization cache. Can be cleared if memory is needed.



41
42
43
# File 'lib/glimmer/swt/color_proxy.rb', line 41

def flyweight_color_proxies
  @flyweight_color_proxies ||= {}
end

Instance Method Details

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

Returns:

  • (Boolean)


93
94
95
# File 'lib/glimmer/swt/color_proxy.rb', line 93

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

#swt_colorObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/glimmer/swt/color_proxy.rb', line 68

def swt_color
  unless @swt_color
    case @args.size
    when 1
      if @args.first.is_a?(String) || @args.first.is_a?(Symbol)
        standard_color = @args.first
        standard_color = "color_#{standard_color}".to_sym unless standard_color.to_s.downcase.include?('color_')
        @swt_color = DisplayProxy.instance.swt_display.getSystemColor(SWTProxy[standard_color])
      else
        @swt_color = @args.first
      end
    when 3..4
      @swt_color = Color.new(*@args)
    end
  end
  @swt_color
end