Skip to content
Browse files

Changed SWT style constant support to rely on Ruby symbols to better …

…follow Ruby conventions and avoid the | ORing in favor of simpler comma separated combination of styles
  • Loading branch information...
1 parent 6f50c4e commit 439c5d58106fe44feee08b1367f2b3cf314a289e Andy Maleh committed Mar 16, 2011
View
4 lib/command_handlers.rb
@@ -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,
@@ -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
)
View
2 lib/command_handlers/list_selection_data_binding_command_handler.rb
@@ -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)
View
16 lib/command_handlers/models/r_tab_item_composite.rb
@@ -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
View
65 lib/command_handlers/models/r_widget.rb
@@ -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
@@ -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
View
6 lib/command_handlers/models/table_items_updater.rb
@@ -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
View
19 lib/command_handlers/swt_constant_command_handler.rb
@@ -1,19 +0,0 @@
-require File.dirname(__FILE__) + "/../command_handler"
-require File.dirname(__FILE__) + "/models/r_widget"
-
-class SwtConstantCommandHandler
- include CommandHandler
-
- include_package 'org.eclipse.swt'
-
- def can_handle?(parent, command_symbol, *args, &block)
- args.size == 0 and
- block == nil
- end
-
- def do_handle(parent, command_symbol, *args, &block)
- puts 'org.eclipse.swt.SWT::' + command_symbol.to_s.upcase
- eval 'org.eclipse.swt.SWT::' + command_symbol.to_s.upcase
- end
-
-end
View
9 lib/command_handlers/tab_item_command_handler.rb
@@ -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
View
7 lib/command_handlers/widget_command_handler.rb
@@ -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
View
4 lib/command_handlers/widget_method_command_handler.rb
@@ -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
View
4 lib/glimmer.rb
@@ -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
@@ -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"
View
8 lib/string.rb
@@ -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
View
10 lib/symbol.rb
@@ -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
View
2 samples/contactmanager/contact_manager.rb
@@ -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
View
2 samples/hello_combo.rb
@@ -15,7 +15,7 @@ def launch
person = Person.new
shell {
composite {
- combo(read_only) {
+ combo(:read_only) {
selection bind(person, :country)
}
button {
View
8 samples/hello_world.rb
@@ -10,11 +10,9 @@ class HelloWorld
def launch
@shell = shell {
text "SWT"
- composite {
- label {
- text "Hello World!"
- }
- }
+ label {
+ text "Hello World!"
+ }
}
@shell.open
end
View
2 samples/login.rb
@@ -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)
}
View
2 samples/tictactoe/tic_tac_toe.rb
@@ -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 {
View
2 test/glimmer_combo_data_binding_test.rb
@@ -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)
}
}
View
6 test/glimmer_constant_test.rb
@@ -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
View
6 test/glimmer_data_binding_test.rb
@@ -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)
}
}
@@ -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)
}
}
@@ -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)
}
}
View
4 test/glimmer_list_data_binding_test.rb
@@ -153,7 +153,7 @@ def test_multi_selection_property
person = Person.new
@target = shell {
- @list = list(multi) {
+ @list = list(:multi) {
selection bind(person, :provinces)
}
}
@@ -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)
}
}
View
2 test/glimmer_shine_data_binding_test.rb
@@ -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) {}
}
}
View
98 test/glimmer_test.rb
@@ -25,98 +25,98 @@ def test_shell_with_default_layout
assert_not_nil @target.widget.getLayout
assert_instance_of FillLayout, @target.widget.getLayout
end
-
+
def test_shell_with_title_and_layout
shell_layout = GridLayout.new
@target = shell {
text "Title"
layout shell_layout
}
-
+
assert_equal "Title", @target.widget.getText
assert_equal shell_layout, @target.widget.getLayout
end
-
+
def test_shell_with_bounds
@target = shell {
bounds 50, 75, 800, 600
}
-
+
assert_equal Rectangle.new(50, 75, 800, 600), @target.widget.getBounds
end
-
+
def test_shell_with_size
GlimmerTest.class_eval("undef_method :size") #hack to remove existing size method in test class
-
+
@target = shell {
size 800, 600
}
-
+
assert_equal Point.new(800, 600), @target.widget.getSize
end
-
+
def test_shell_and_composite_with_default_style_and_layout
@target = shell {
composite
}
-
+
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::NONE, composite_widget
+ assert_has_style :none, composite_widget
assert_instance_of GridLayout, composite_widget.getLayout
grid_layout = composite_widget.getLayout
assert_equal 1, grid_layout.numColumns
assert_equal false, grid_layout.makeColumnsEqualWidth
end
-
+
def test_shell_and_group_with_default_style_and_layout
@target = shell {
group {
text "Title"
}
}
-
+
assert_equal 1, @target.widget.children.size
assert_instance_of Group, @target.widget.children[0]
group_widget = @target.widget.children[0]
- assert_has_style SWT::NONE, group_widget
+ assert_has_style :none, group_widget
assert_instance_of GridLayout, group_widget.getLayout
grid_layout = group_widget.getLayout
assert_equal 1, grid_layout.numColumns
assert_equal false, grid_layout.makeColumnsEqualWidth
assert_equal "Title", group_widget.getText
end
-
+
def test_shell_and_composite_with_style_and_layout
composite_layout = RowLayout.new
@target = shell {
- composite(SWT::NO_FOCUS) {
+ composite(:no_focus) {
layout composite_layout
}
}
-
+
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 :no_focus, composite_widget
assert_equal composite_layout, composite_widget.getLayout
end
-
+
def test_shell_and_composite_and_text_with_default_style
@target = shell {
composite {
text
}
}
-
+
composite_widget = @target.widget.children[0]
assert_equal 1, composite_widget.children.size
assert_instance_of Text, composite_widget.children[0]
text_widget = composite_widget.children[0]
- assert_has_style SWT::BORDER, text_widget
+ assert_has_style :border, text_widget
end
-
+
def test_shell_and_composite_with_custom_layout_and_text_with_default_style
composite_layout = RowLayout.new
@target = shell {
@@ -125,112 +125,112 @@ def test_shell_and_composite_with_custom_layout_and_text_with_default_style
layout composite_layout
}
}
-
+
composite_widget = @target.widget.children[0]
assert_equal composite_layout, composite_widget.getLayout
assert_equal 1, composite_widget.children.size
assert_instance_of Text, composite_widget.children[0]
text_widget = composite_widget.children[0]
- assert_has_style SWT::BORDER, text_widget
+ assert_has_style :border, text_widget
end
-
+
def test_shell_and_composite_and_text_with_style_and_text
@target = shell {
composite {
- text(SWT::PASSWORD) {
+ text(:password) {
text "Hello"
}
}
}
-
+
composite_widget = @target.widget.children[0]
assert_equal 1, composite_widget.children.size
assert_instance_of Text, composite_widget.children[0]
text_widget = composite_widget.children[0]
- assert_has_style SWT::PASSWORD, text_widget
+ assert_has_style :password, text_widget
assert_equal "Hello", text_widget.getText
end
-
+
def test_shell_and_spinner_default
@target = shell {
@spinner = spinner {
selection 55
}
}
-
+
assert_instance_of Spinner, @spinner.widget
- assert_has_style SWT::BORDER, @spinner.widget
+ assert_has_style :border, @spinner.widget
assert_equal 55, @spinner.widget.getSelection
end
-
+
def test_shell_and_spinner_default
@target = shell {
@list = list {
}
}
-
+
assert_instance_of List, @list.widget
- assert_has_style SWT::BORDER, @list.widget
- assert_has_style SWT::SINGLE, @list.widget
- assert_has_style SWT::V_SCROLL, @list.widget
+ assert_has_style :border, @list.widget
+ assert_has_style :single, @list.widget
+ assert_has_style :v_scroll, @list.widget
end
-
+
def test_shell_and_button_default
@target = shell {
@button = button {
text "Push Me"
}
}
-
+
assert_instance_of Button, @button.widget
- assert_has_style SWT::PUSH, @button.widget
+ assert_has_style :push, @button.widget
assert_equal "Push Me", @button.widget.text
end
-
+
def test_shell_and_table_and_table_column_defaults
@target = shell {
@table = table {
@table_column = table_column
}
}
- assert_has_style SWT::BORDER, @table.widget
+ assert_has_style :border, @table.widget
assert @table.widget.getHeaderVisible
assert @table.widget.getLinesVisible
assert_equal 80, @table_column.widget.getWidth
end
-
+
def test_shell_containing_undefined_command
@target = shell {
- undefined_command(:undefined_parameter) {
+ undefined_command(:undefined_parameter) {
}
}
-
+
assert_not_nil @target
assert_not_nil @target.widget
assert_instance_of Shell, @target.widget
assert_not_nil @target.widget.getLayout
assert_instance_of FillLayout, @target.widget.getLayout
end
-
-
+
+
def test_add_contents
@target = shell {
}
-
+
add_contents(@target) {
composite {
- text(SWT::PASSWORD) {
+ text(:password) {
text "Hello"
}
}
}
-
+
composite_widget = @target.widget.children[0]
assert_equal 1, composite_widget.children.size
assert_instance_of Text, composite_widget.children[0]
text_widget = composite_widget.children[0]
- assert_has_style SWT::PASSWORD, text_widget
+ assert_has_style :password, text_widget
assert_equal "Hello", text_widget.getText
end
View
2 test/helper.rb
@@ -16,6 +16,6 @@
class Test::Unit::TestCase
def assert_has_style(style, widget)
- assert_equal style, widget.getStyle & style
+ assert_equal style.swt_constant, widget.getStyle & style.swt_constant
end
end

0 comments on commit 439c5d5

Please sign in to comment.
Something went wrong with that request. Please try again.