Skip to content

Commit

Permalink
add() method extracted to container-module. removed duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkdoor committed Oct 8, 2008
1 parent eb9ff36 commit 4375a59
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 56 deletions.
31 changes: 16 additions & 15 deletions lib/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,30 @@ module Main
Frame.new("hallo, welt", :size => [800, 600]) do |frame|
frame.default_close_operation = :exit_on_close

Button.new("test", :belongs_to => frame, :name => :testButton) do |btn|
Panel.new(java.awt.GridLayout.new(1,1), :size => [300, 500], :name => :panel, :belongs_to => frame) do |panel|
Button.new("test", :belongs_to => panel, :name => :testButton) do |btn|
btn.on_click do
Dialog.showOption "bitte auswählen:", :option_type => :yes_no, :option_values => ["Aha", "ohno"],
:title => "auswahl treffen!", :belongs_to => frame
end

btn.on_click do
Dialog.showOption "bitte auswählen:", :option_type => :yes_no, :option_values => ["Aha", "ohno"],
:title => "auswahl treffen!", :belongs_to => frame
end
btn.on_click do
Dialog.show("mein text", :dialog_type => :error, :title => "hello, world", :belongs_to => frame)
end

btn.on_click do
Dialog.show("mein text", :dialog_type => :error, :title => "hello, world", :belongs_to => frame)
end

btn.on_focus do
puts "button hat jetzt focus"
end

btn.on_focus do
puts "button hat jetzt focus"
btn.on_focus_lost do
puts "button hat focus verloren"
end
end

btn.on_focus_lost do
puts "button hat focus verloren"
end
puts "text von testButton: #{panel[:testButton].text}"
end

puts "text von testButton: #{frame[:testButton].text}"

#frame.size = [400, 400]

options = ["Herr", "Frau", "Geek"]
Expand Down
34 changes: 34 additions & 0 deletions lib/rswing/components/container.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
module RSwing
module Components
module Container

# Adds a component with a set of options to this container.
def add(component, options = {})
if(self.respond_to?(:getContentPane))
self.add_to_content_pane(component, options)
else
# this should be handled somehow later on:
# if(layout = Options.value_for(options => :layout))
# super(component, layout)
# else
super(component)
# end
end

# if :name given, add with name
if(name = Options.value_for(options => :name))
self.add_with_name(component, name)
end

component # return component
end

# Adds a component to the component_hash with a given name symbol.
# Raises an exception if name already taken.
def add_with_name(component, name_symbol)
Expand Down Expand Up @@ -47,6 +69,18 @@ def self.add_if_requested(component, options)
def component_hash
@component_hash ||= {}
end

# Adds a component with given options to the <tt>content_pane</tt> of a container.
# This will only work, if the container actually has a <tt>content_pane</tt>
# and should only be called by the <tt>add()</tt> method inside of the
# <tt>container</tt> module.
def add_to_content_pane(component, options)
if(layout = Options.value_for(options => :layout))
self.content_pane.add(component, layout)
else
self.content_pane.add(component)
end
end
end
end
end
17 changes: 0 additions & 17 deletions lib/rswing/components/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,6 @@ def initialize(title, options = {}, &block)

end

# Adds a component with a set of options to this dialog.
def add(component, options = {})
if(layout = Options.value_for(options => :layout))
self.content_pane.add(component, layout)
else
self.content_pane.add(component)
end


# wenn :name angegeben wurde, mit aufnehmen
if(name = Options.value_for(options => :name))
self.add_with_name(component, name)
end

component #zurückgeben
end

# Creates a MessageDialog.
# - <tt>message</tt>: Message, to be displayed in this dialog.
# - <tt>options</tt>: Options-Hash with the following valid values:
Expand Down
16 changes: 0 additions & 16 deletions lib/rswing/components/frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ def initialize(title, options = {}, &block)
end
end

def add(component, options = {})
if(layout = Options.value_for(options => :layout))
self.content_pane.add(component, layout)
else
self.content_pane.add(component)
end


# wenn :name angegeben wurde, mit aufnehmen
if(name = Options.value_for(options => :name))
self.add_with_name(component, name)
end

component #zurückgeben
end

# Sets the default close operation for this frame.
# Valid operations are:
# 1. <tt>:do_nothing_on_close</tt>
Expand Down
8 changes: 0 additions & 8 deletions lib/rswing/components/panel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ def initialize(layout_manager, options = {}, &block)

Container.add_if_requested(self, options)
end

def add(component, options = {})
super.add(component)

if(name = Options.value_for(options => :name))
self.add_with_name(component, name)
end
end
end
end
end

0 comments on commit 4375a59

Please sign in to comment.