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."
REGEX_SYMBOL_NEGATIVITY =
/^([^!]+)(!)?$/
EXTRA_STYLES =
{
  NO_RESIZE: self[:shell_trim, :resize!, :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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/glimmer/swt/swt_proxy.rb', line 17

def [](*symbols)
  symbols = symbols.first if symbols.size == 1 && symbols.first.is_a?(Array)
  result = symbols.compact.map do |symbol|
    constant(symbol).tap do |constant_value|
      raise Error, symbol.to_s + ERROR_INVALID_STYLE unless constant_value.is_a?(Integer)
    end
  end.reduce do |output, constant_value|
    if constant_value < 0
      output & constant_value
    else
      output | constant_value
    end
  end
  result.nil? ? SWT::NONE : result
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)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/glimmer/swt/swt_proxy.rb', line 38

def constant(symbol)
  return symbol unless symbol.is_a?(Symbol) || symbol.is_a?(String)
  symbol_string, negative = extract_symbol_string_negativity(symbol)
  swt_constant_symbol = symbol_string.downcase == symbol_string ? symbol_string.upcase.to_sym : symbol_string.to_sym
  bit_value = SWT.const_get(swt_constant_symbol)
  negative ? ~bit_value : bit_value
rescue => e
  begin
#             Glimmer::Config.logger&.debug(e.full_message)
    alternative_swt_constant_symbol = SWT.constants.find {|c| c.to_s.upcase == swt_constant_symbol.to_s.upcase}
    bit_value = SWT.const_get(alternative_swt_constant_symbol)
    negative ? ~bit_value : bit_value
  rescue => e
#             Glimmer::Config.logger&.debug(e.full_message)
    bit_value = Glimmer::SWT::SWTProxy::EXTRA_STYLES[swt_constant_symbol]
    if bit_value
      negative ? ~bit_value : bit_value
    else
      symbol
    end
  end
end

.constantify_args(args) ⇒ Object



82
83
84
# File 'lib/glimmer/swt/swt_proxy.rb', line 82

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

.deconstruct(integer) ⇒ Object

Deconstructs a style integer into symbols Useful for debugging



88
89
90
91
92
93
94
# File 'lib/glimmer/swt/swt_proxy.rb', line 88

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 & integer) == constant_value
    is_found ? found += [c] : found
  end
end

.extract_symbol_string_negativity(symbol) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/glimmer/swt/swt_proxy.rb', line 61

def extract_symbol_string_negativity(symbol)
 if symbol.is_a?(Symbol) || symbol.is_a?(String)
   symbol_negativity_match = symbol.to_s.match(REGEX_SYMBOL_NEGATIVITY)
   symbol = symbol_negativity_match[1]
   negative = !!symbol_negativity_match[2]
   [symbol, negative]
 else
   negative = symbol < 0
   [symbol, negative]
 end
end

.has_constant?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/glimmer/swt/swt_proxy.rb', line 77

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

.include?(swt_constant, *symbols) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/glimmer/swt/swt_proxy.rb', line 96

def include?(swt_constant, *symbols)
  swt_constant & self[symbols] == self[symbols]
end

.negative?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/glimmer/swt/swt_proxy.rb', line 73

def negative?(symbol)
  extract_symbol_string_negativity(symbol)[1]
end