Class: Glimmer::SWT::SWTProxy

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

Overview

Proxy for org.eclipse.swt.SWT

Follows the Proxy Design Pattern

Constant Summary collapse

ERROR_INVALID_STYLE =
" is an invalid SWT style! Please choose a style from org.eclipse.swt.SWT class constants."
EXTRA_STYLES =
{
  NO_RESIZE: SWTProxy[:shell_trim] & (~SWTProxy[:resize]) & (~SWTProxy[:max])
}

Class Method Summary collapse

Class Method Details

.[](*symbols) ⇒ Object

Gets SWT constants as if calling SWT::CONSTANT where constant is passed in as a lower case symbol



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/glimmer/swt/swt_proxy.rb', line 16

def [](*symbols)
  symbols = symbols.first if symbols.size == 1 && symbols.first.is_a?(Array)
  symbols.compact.reduce(0) do |output, symbol|
    constant_value = constant(symbol)
    if constant_value.is_a?(Integer)
      output | constant(symbol)
    else
      raise Error, symbol.to_s + ERROR_INVALID_STYLE
    end
  end
end

.constant(symbol) ⇒ Object

Returns SWT style integer value for passed in symbol or allows passed in object to pass through (e.g. Integer). This makes is convenient to use symbols or actual SWT style integers in Glimmer Does not raise error for invalid values. Just lets them pass as is. (look into [] operator if you want an error raised on invalid values)



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/glimmer/swt/swt_proxy.rb', line 33

def constant(symbol)
  return symbol unless symbol.is_a?(Symbol) || symbol.is_a?(String)
  symbol_string = symbol.to_s
  swt_constant_symbol = symbol_string.downcase == symbol_string ? symbol_string.upcase.to_sym : symbol_string.to_sym
  SWT.const_get(swt_constant_symbol)
rescue
  begin
    alternative_swt_constant_symbol = SWT.constants.find {|c| c.to_s.upcase == swt_constant_symbol.to_s.upcase}
    SWT.const_get(alternative_swt_constant_symbol)
  rescue
    EXTRA_STYLES[swt_constant_symbol] || symbol
  end
end

.constantify_args(args) ⇒ Object



52
53
54
# File 'lib/glimmer/swt/swt_proxy.rb', line 52

def constantify_args(args)
  args.map {|arg| constant(arg)}
end

.deconstruct(integer) ⇒ Object

Deconstructs a style integer into symbols Useful for debugging



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

def deconstruct(integer)
  SWT.constants.reduce([]) do |found, c|
    constant_value = SWT.const_get(c) rescue -1
    is_found = constant_value.is_a?(Integer) && (constant_value & style) == constant_value
    is_found ? found += [c] : found
  end
end

.has_constant?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/glimmer/swt/swt_proxy.rb', line 47

def has_constant?(symbol)
  return false unless symbol.is_a?(Symbol) || symbol.is_a?(String)
  constant(symbol).is_a?(Integer)
end