Skip to content

Commit

Permalink
Changed SWT style constant support to rely on Ruby symbols to better …
Browse files Browse the repository at this point in the history
…follow Ruby conventions and avoid the | ORing in favor of simpler comma separated combination of styles
  • Loading branch information
Andy Maleh committed Mar 17, 2011
1 parent 6f50c4e commit 439c5d5
Show file tree
Hide file tree
Showing 24 changed files with 145 additions and 145 deletions.
4 changes: 1 addition & 3 deletions lib/command_handlers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
require File.dirname(__FILE__) + "/command_handlers/data_binding_command_handler"
require File.dirname(__FILE__) + "/command_handlers/widget_method_command_handler"
require File.dirname(__FILE__) + "/command_handlers/widget_command_handler"
require File.dirname(__FILE__) + "/command_handlers/swt_constant_command_handler"

# edit to add more command handlers and extend Glimmer
CommandHandlerChainFactory.def_dsl(:swt,
Expand All @@ -24,6 +23,5 @@
TableColumnPropertiesDataBindingCommandHandler.new,
DataBindingCommandHandler.new,
WidgetMethodCommandHandler.new,
WidgetCommandHandler.new,
SwtConstantCommandHandler.new #at the bottom to avoid stealing commands from other command handlers
WidgetCommandHandler.new
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def do_handle(parent, command_symbol, *args, &block)
model.add_observer(model_observer.options_property_name, widget_observer)

property_type = :string
property_type = :array if parent.has_style?(multi)
property_type = :array if parent.has_style?(:multi)
list_observer = ListObserver.new(parent, property_type)
list_observer.update(model_observer.evaluate_property)
model.add_observer(model_observer.property_name, list_observer)
Expand Down
16 changes: 11 additions & 5 deletions lib/command_handlers/models/r_tab_item_composite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ def initialize(tab_item, parent, style, &contents)
@tab_item.widget.control = self.widget
end

def respond_to?(method_symbol, *args)
if method_symbol.to_s == "text"
def has_attribute?(attribute_name, *args)
if attribute_name.to_s == "text"
true
else
super(method_symbol, *args)
super(attribute_name, *args)
end
end

def text(text_value)
@tab_item.widget.text=text_value
def set_attribute(attribute_name, *args)
if attribute_name.to_s == "text"
text_value = args[0]
@tab_item.widget.text = text_value
else
super(attribute_name, *args)
end
end

end
65 changes: 35 additions & 30 deletions lib/command_handlers/models/r_widget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,21 @@ class RWidget
"table_column" => Proc.new { |table_column| table_column.setWidth(80) },
"group" => Proc.new {|group| group.setLayout(GridLayout.new) },
}
def initialize(underscored_widget_name, parent, style, &contents)
style = default_style(underscored_widget_name) unless style
@widget = eval underscored_widget_name.to_s.camelcase + '.new(parent, style)'

#styles is a comma separate list of symbols representing SWT styles in lower case
def initialize(underscored_widget_name, parent, styles, &contents)
@widget = underscored_widget_name.swt_widget.new(parent, style(underscored_widget_name, styles))
@@default_initializers[underscored_widget_name].call(@widget) if @@default_initializers[underscored_widget_name]
end

def default_style(underscored_widget_name)
style = @@default_styles[underscored_widget_name] if @@default_styles[underscored_widget_name]
style = SWT::NONE unless style
style
end

def respond_to?(method_symbol, *args)
@widget.respond_to?("set#{method_symbol.to_s.camelcase(:upper)}", args)
def has_attribute?(attribute_name, *args)
@widget.respond_to?(attribute_setter(attribute_name), args)
end

def method_missing(method_symbol, *args)
statement_to_eval = "@widget.send('set' + method_symbol.to_s.camelcase(:upper)"
statement_to_eval << expand_arguments(args)
statement_to_eval << ")"
eval statement_to_eval
end

def expand_arguments(args)
expanded_args = ""
index = 0
args.each do
expanded_args << ", args[#{index}]"
index += 1
end
expanded_args

def set_attribute(attribute_name, *args)
@widget.send(attribute_setter(attribute_name), *args)
end

def self.widget_exists?(underscored_widget_name)
begin
eval underscored_widget_name.camelcase
Expand Down Expand Up @@ -142,7 +123,31 @@ def sync_exec(&block)
end

def has_style?(style)
(widget.style & style) == style
(widget.style & style.swt_constant) == style.swt_constant
end

private

def style(underscored_widget_name, styles)
styles.empty? ? default_style(underscored_widget_name) : swt_style(styles)
end

def default_style(underscored_widget_name)
style = @@default_styles[underscored_widget_name] if @@default_styles[underscored_widget_name]
style = SWT::NONE unless style
style
end

def swt_style(styles)
swt_styles(styles).inject(0) { |combined_style, style| combined_style | style }
end

def swt_styles(styles)
styles.map(&:swt_constant)
end

def attribute_setter(attribute_name)
"set#{attribute_name.to_s.camelcase(:upper)}"
end

end
6 changes: 2 additions & 4 deletions lib/command_handlers/models/table_items_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ def initialize(parent, model_observer, column_properties)
model.add_observer(model_observer.property_name, self)
end
def update(model_collection=nil)
if (model_collection and
model_collection.is_a?(Array) and
!model_collection.is_a?(ObservableArray))
model_collection.extend(ObservableArray)
if model_collection and model_collection.is_a?(Array)
model_collection.extend(ObservableArray) unless model_collection.is_a?(ObservableArray)
model_collection.add_observer(@column_properties, self)
@model_collection = model_collection
end
Expand Down
19 changes: 0 additions & 19 deletions lib/command_handlers/swt_constant_command_handler.rb

This file was deleted.

9 changes: 3 additions & 6 deletions lib/command_handlers/tab_item_command_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ class TabItemCommandHandler

def can_handle?(parent, command_symbol, *args, &block)
parent.is_a?(RWidget) and
command_symbol.to_s == "tab_item" and
(args.size == 0 or
(args.size == 1 and args[0].is_a?(Fixnum)))
command_symbol.to_s == "tab_item"
end

def do_handle(parent, command_symbol, *args, &block)
style = args[0] if args.size == 1
tab_item = RWidget.new(command_symbol.to_s, parent.widget, style)
RTabItemComposite.new(tab_item, parent.widget, style)
tab_item = RWidget.new(command_symbol.to_s, parent.widget, args)
RTabItemComposite.new(tab_item, parent.widget, args)
end

end
7 changes: 2 additions & 5 deletions lib/command_handlers/widget_command_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ class WidgetCommandHandler
def can_handle?(parent, command_symbol, *args, &block)
parent.is_a?(RWidget) and
command_symbol.to_s != "shell" and
(args.size == 0 or
(args.size == 1 and args[0].is_a?(Fixnum))) and
RWidget.widget_exists?(command_symbol.to_s)
end

def do_handle(parent, command_symbol, *args, &block)
style = args[0] if args.size == 1
puts "style argument is: " + style.to_s
RWidget.new(command_symbol.to_s, parent.widget, style)
puts "widget styles are: " + args.inspect
RWidget.new(command_symbol.to_s, parent.widget, args)
end

end
4 changes: 2 additions & 2 deletions lib/command_handlers/widget_method_command_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def can_handle?(parent, command_symbol, *args, &block)
parent.is_a?(RWidget) and
args.size > 0 and
block == nil and
parent.respond_to?(command_symbol, *args)
parent.has_attribute?(command_symbol, *args)
end

def do_handle(parent, command_symbol, *args, &block)
parent.send(command_symbol, *args)
parent.set_attribute(command_symbol, *args)
nil
end

Expand Down
4 changes: 3 additions & 1 deletion lib/glimmer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
require "rubygems"
require "facets"
require "java"
require File.dirname(__FILE__) + "/string"
require File.dirname(__FILE__) + "/symbol"
require File.dirname(__FILE__) + "/parent"

module Glimmer
Expand Down Expand Up @@ -48,5 +50,5 @@ def dsl(dsl)
end

# Command handlers may rely on Glimmer, so this is put here to avoid an infinite loop.
require File.dirname(__FILE__) + "/command_handlers"
require File.dirname(__FILE__) + "/xml_command_handlers"
require File.dirname(__FILE__) + "/command_handlers"
8 changes: 8 additions & 0 deletions lib/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class String
def swt_widget
org.eclipse.swt.widgets.__send__(camelcase(:upper))
end
def swt_constant
org.eclipse.swt.__send__("SWT").class_eval(upcase)
end
end
10 changes: 10 additions & 0 deletions lib/symbol.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require File.dirname(__FILE__) + "/string"

class Symbol
def swt_widget
to_s.swt_widget
end
def swt_constant
to_s.swt_constant
end
end
2 changes: 1 addition & 1 deletion samples/contactmanager/contact_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ContactManager
}

table {
layout_data GridData.new(fill, fill, true, true)
layout_data GridData.new(:fill.swt_constant, :fill.swt_constant, true, true)
table_column {
text "First Name"
width 80
Expand Down
2 changes: 1 addition & 1 deletion samples/hello_combo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def launch
person = Person.new
shell {
composite {
combo(read_only) {
combo(:read_only) {
selection bind(person, :country)
}
button {
Expand Down
8 changes: 3 additions & 5 deletions samples/hello_world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ class HelloWorld
def launch
@shell = shell {
text "SWT"
composite {
label {
text "Hello World!"
}
}
label {
text "Hello World!"
}
}
@shell.open
end
Expand Down
2 changes: 1 addition & 1 deletion samples/login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def launch
}

label { text "Password:" }
text(SWT::PASSWORD | SWT::BORDER) {
text(:password, :border) {
text bind(presenter, :password)
enabled bind(presenter, :logged_out)
}
Expand Down
2 changes: 1 addition & 1 deletion samples/tictactoe/tic_tac_toe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize
(1..3).each { |row_number|
(1..3).each { |column_number|
button {
layout_data GridData.new(fill, fill, true, true)
layout_data GridData.new(:fill.swt_constant, :fill.swt_constant, true, true)
text bind(@tic_tac_toe_board.box(row_number, column_number), :sign)
enabled bind(@tic_tac_toe_board.box(row_number, column_number), :empty)
on_widget_selected {
Expand Down
2 changes: 1 addition & 1 deletion test/glimmer_combo_data_binding_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_read_only_widget_data_binding_selection_property
person.country = "Canada"

@target = shell {
@combo = combo(read_only) {
@combo = combo(:read_only) {
selection bind(person, :country)
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/glimmer_constant_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def teardown

def test_shell_with_default_layout
@target = shell {
composite(border | no_focus) {
composite(:border, :no_focus) {
}
}

assert_equal 1, @target.widget.children.size
assert_instance_of Composite, @target.widget.children[0]
composite_widget = @target.widget.children[0]
assert_has_style SWT::NO_FOCUS, composite_widget
assert_has_style SWT::BORDER, composite_widget
assert_has_style :no_focus, composite_widget
assert_has_style :border, composite_widget
end
end

6 changes: 3 additions & 3 deletions test/glimmer_data_binding_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_checkbox_widget_data_binding_boolean_property

@target = shell {
composite {
@check_box = button(SWT::CHECK) {
@check_box = button(:check) {
selection bind(person, :adult)
}
}
Expand All @@ -107,7 +107,7 @@ def test_radio_widget_data_binding_boolean_property

@target = shell {
composite {
@radio = button(SWT::RADIO) {
@radio = button(:radio) {
selection bind(person, :adult)
}
}
Expand Down Expand Up @@ -205,7 +205,7 @@ def test_multiple_widget_data_bindings_to_different_model_properties
@text = text {
text bind(person, :age, :fixnum)
}
@check_box = button(SWT::CHECK) {
@check_box = button(:check) {
selection bind(person, :adult)
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/glimmer_list_data_binding_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def test_multi_selection_property
person = Person.new

@target = shell {
@list = list(multi) {
@list = list(:multi) {
selection bind(person, :provinces)
}
}
Expand Down Expand Up @@ -189,7 +189,7 @@ def test_multi_selection_property_with_model_preinitialized
person.provinces = []

@target = shell {
@list = list(multi) {
@list = list(:multi) {
selection bind(person, :provinces)
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/glimmer_shine_data_binding_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_multiple_widget_data_bindings_to_different_model_properties_spaceship
composite {
@label = label {}
@text = text {}
@check_box = button(SWT::CHECK) {}
@check_box = button(:check) {}
}
}

Expand Down
Loading

0 comments on commit 439c5d5

Please sign in to comment.