bakkdoor-rswing 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rswing.rb CHANGED
@@ -1,2 +1,5 @@
1
1
  require "java"
2
2
  $: << File.expand_path(File.dirname(__FILE__) + "/rswing/components")
3
+
4
+ module RSwing
5
+ end
@@ -0,0 +1,44 @@
1
+ module RSwing
2
+ module Components
3
+ require "java"
4
+ require "events/focus_events"
5
+ require "options"
6
+ require "listener"
7
+ include_package 'javax.swing'
8
+ ActionListener = java.awt.event.ActionListener
9
+
10
+ # Button-Class. Wraps JButton-Class.
11
+ class Button < JButton
12
+ include Events::FocusEvents
13
+
14
+ # - <tt>text</tt>: The Text to be displayed on the button.
15
+ # - <tt>options</tt>: Options-Hash with the following valid values:
16
+ # 1. <tt>:visible => true</tt>
17
+ # 2. <tt>:enabled => true</tt>
18
+ # 3. <tt>:belongs_to => nil</tt> # Container to which this button should be added (default: none)
19
+ # 4. <tt>:layout => nil</tt> # Layout-Options (e.g. GridBagContraints-Object) (default: none)
20
+ # 5. <tt>:icon => nil</tt> # Icon-Symbol to be displayed next to/instead of the text on the button (default: none)
21
+ # 6. <tt>:name => :okButton</tt> Name of the buttons for access via parent-container (default: none)
22
+ def initialize(text, options = {}, &block)
23
+ super(text)
24
+
25
+ self.visible = Options.value_for(options => :visible)
26
+ self.enabled = Options.value_for(options => :enabled)
27
+ self.icon = Options.value_for(options => :icon)
28
+
29
+ # call block with current object, if given
30
+ if block_given?
31
+ yield self
32
+ end
33
+
34
+ Container.add_if_requested(self, options)
35
+ end
36
+
37
+ # Eventhandler for clicked (actionPerformed) event.
38
+ # Takes a block, which will be executed if this event occurs.
39
+ def on_click(&block)
40
+ self.add_action_listener(Listener.create(ActionListener, :actionPerformed, &block))
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,52 @@
1
+ module RSwing
2
+ module Components
3
+ module Container
4
+ # Adds a component to the component_hash with a given name symbol.
5
+ # Raises an exception if name already taken.
6
+ def add_with_name(component, name_symbol)
7
+ if self.component_hash.has_key?(name_symbol)
8
+ raise "Name in Component bereits vergeben!"
9
+ else
10
+ self.component_hash[name_symbol] = component
11
+ end
12
+ end
13
+
14
+ # Removes a component from this container.
15
+ # Also removes it from the component_hash
16
+ def remove(component)
17
+ super.remove(component)
18
+ # delete all entries with component as value in component_hash
19
+ self.component_hash.delete_if { |key,value| value == component }
20
+ end
21
+
22
+ # Returns the component with a given name inside of this container (if available).
23
+ # * <tt>name_symbol</tt> Name (as symbol) of the component to get.
24
+ def [](name_symbol)
25
+ if self.component_hash.has_key?(name_symbol)
26
+ self.component_hash[name_symbol] # erstes hash mit diesem namen als key zurückgeben
27
+ else
28
+ nil
29
+ end
30
+ end
31
+
32
+ # Adds a component to a container, if specified via :belongs_to in options.
33
+ def self.add_if_requested(component, options)
34
+ if(container = Options.value_for(options => :belongs_to))
35
+ if(Options.value_for(options => :layout) || Options.value_for(options => :name))
36
+ container.add(component, options)
37
+ else
38
+ container.add(component)
39
+ end
40
+ end
41
+ end
42
+
43
+ protected
44
+ # Name-Component-hash.
45
+ # For example:
46
+ # * <tt>{:okButton => okButtonObject, :cancelButton => cancelButtonObject}</tt>
47
+ def component_hash
48
+ @component_hash ||= {}
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,110 @@
1
+ module RSwing
2
+ module Components
3
+ require "java"
4
+ require "container"
5
+
6
+ JOptionPane = javax.swing.JOptionPane
7
+ JDialog = javax.swing.JDialog
8
+
9
+ # Dialog-Class.
10
+ # Has static methods to create Message-Dialogs.
11
+ # (<tt>show()</tt> and <tt>showOptions</tt>).
12
+ # Can also be used to create custom dialogs by extending from this class.
13
+ class Dialog < JDialog
14
+ include Container
15
+
16
+ def initialize(owner, title, options = {}, &block)
17
+ super(owner, title, Options.value_for(options => :modal))
18
+
19
+ if(name = Options.value_for(options => :name) && owner.class.include?(Container))
20
+ owner.add_with_name(self, name)
21
+ end
22
+
23
+ # call block with current object, if given
24
+ if block_given?
25
+ yield self
26
+ end
27
+
28
+ end
29
+
30
+ # Adds a component with a set of options to this dialog.
31
+ def add(component, options = {})
32
+ if(layout = Options.value_for(options => :layout))
33
+ self.content_pane.add(component, layout)
34
+ else
35
+ self.content_pane.add(component)
36
+ end
37
+
38
+
39
+ # wenn :name angegeben wurde, mit aufnehmen
40
+ if(name = Options.value_for(options => :name))
41
+ self.add_with_name(component, name)
42
+ end
43
+
44
+ component #zurückgeben
45
+ end
46
+
47
+ # Creates a MessageDialog.
48
+ # - <tt>parent</tt>: Parent-Container for this dialog.
49
+ # - <tt>message</tt>: Message, to be displayed in this dialog.
50
+ # - <tt>options</tt>: Options-Hash with the following valid values:
51
+ # 1. <tt>:dialog_type => (:error | :question | :plain | :warning | :info)</tt> (default: :info)
52
+ # 2. <tt>:title => "my title"</tt> (defailt: <tt>""</tt>)
53
+ # 3. <tt>:modal => false</tt> (default: true)
54
+ def self.show(parent, message, options = {})
55
+ message_type =
56
+ case Options.value_for(options => :dialog_type)
57
+ when :error
58
+ JOptionPane::ERROR_MESSAGE
59
+ when :question
60
+ JOptionPane::QUESTION_MESSAGE
61
+ when :plain
62
+ JOptionPane::PLAIN_MESSAGE
63
+ when :warning
64
+ JOptionPane::WARNING_MESSAGE
65
+ else
66
+ JOptionPane::INFORMATION_MESSAGE
67
+ end
68
+
69
+ JOptionPane.showMessageDialog(parent, message, Options.value_for(options => :title), message_type)
70
+ end
71
+
72
+ # Creates a OptionsDialog (Dialog with selection for different Options).
73
+ # - <tt>parent</tt>: Parent-Container for this dialog.
74
+ # - <tt>message</tt>: Message, to be displayed in the dialog.
75
+ # - <tt>options</tt>: Options-Hash with the following valid values:
76
+ # 1. <tt>:option_type => (:yes_no | :yes_no_cancel)</tt> (default: <tt>:yes_no</tt>)
77
+ # 2. <tt>:option_values => ["OK", "Cancel", "Quit"]</tt> (default: <tt>["Ja", "Nein"]</tt>)
78
+ # 3. <tt>:title => "mein titel"</tt> (default: <tt>""</tt>)
79
+ # 4. <tt>:modal => false</tt> (default: <tt>true</tt>)
80
+ # 5. <tt>:icon => nil</tt> (default: keins)
81
+ def self.showOption(parent, message, options = {})
82
+ title = options[:title].nil? ? "Bitte wählen Sie" : options[:title]
83
+ icon = Options.value_for(options => :icon)
84
+
85
+ option_type = option_type_for(Options.value_for(options => :option_type))
86
+ option_values = Options.value_for(options => :option_values)
87
+
88
+ selected_option_index = JOptionPane.showOptionDialog(parent, message, title, option_type, JOptionPane::QUESTION_MESSAGE, icon,
89
+ option_values.to_java(:Object), nil);
90
+
91
+ option_values[selected_option_index]
92
+ end
93
+
94
+ # Returns a JOptionPane-specific option_type for a given rswing-option_type.
95
+ # For example:
96
+ # 1. <tt>:yes_no</tt> => <tt>JOptionPane::YES_NO_OPTION</tt>
97
+ # 2. <tt>:yes_no_cancel</tt> => <tt>JOptionPane::YES_NO_CANCEL_OPTION</tt>
98
+ def self.option_type_for(option_type)
99
+ case option_type
100
+ when :yes_no
101
+ JOptionPane::YES_NO_OPTION
102
+ else
103
+ JOptionPane::YES_NO_CANCEL_OPTION
104
+ end
105
+ end
106
+
107
+ end
108
+ end
109
+ end
110
+
@@ -0,0 +1,21 @@
1
+ module RSwing
2
+ module Components
3
+ module Events
4
+ module FocusEvents
5
+ FocusListener = java.awt.event.FocusListener
6
+
7
+ # Eventhandler für focus (focusGained) Event.
8
+ # Nimmt einen block, welcher dann bei diesem Event ausgeführt wird.
9
+ def on_focus(&block)
10
+ self.add_focus_listener(Listener.create(FocusListener, :focusGained ,&block))
11
+ end
12
+
13
+ # Eventhandler für focus_lost (focusLost) Event.
14
+ # Nimmt einen block, welcher dann bei diesem Event ausgeführt wird.
15
+ def on_focus_lost(&block)
16
+ self.add_focus_listener(Listener.create(FocusListener, :focusLost ,&block))
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module RSwing
2
+ module Components
3
+ module Events
4
+ module KeyEvents
5
+ KeyListener = java.awt.event.KeyListener
6
+
7
+ def on_key_pressed(&block)
8
+ self.add_key_listener(Listener.create(KeyListener, :keyPressed, &block))
9
+ end
10
+
11
+ def on_key_released(&block)
12
+ self.add_key_listener(Listener.create(KeyListener, :keyReleased, &block))
13
+ end
14
+
15
+ def on_key_typed(&block)
16
+ self.add_key_listener(Listener.create(KeyListener, :keyTyped, &block))
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ module RSwing
2
+ module Components
3
+ module Events
4
+ module MouseEvents
5
+ MouseListener = java.awt.event.MouseListener
6
+
7
+ def on_mouse_clicked(&block)
8
+ self.add_key_listener(Listener.create(MouseListener, :mouseClicked, &block))
9
+ end
10
+
11
+ def on_mouse_entered(&block)
12
+ self.add_key_listener(Listener.create(MouseListener, :mouseEntered, &block))
13
+ end
14
+
15
+ def on_mouse_exited(&block)
16
+ self.add_key_listener(Listener.create(MouseListener, :mouseExited, &block))
17
+ end
18
+
19
+ def on_mouse_pressed(&block)
20
+ self.add_key_listener(Listener.create(MouseListener, :mousePressed, &block))
21
+ end
22
+
23
+ def on_mouse_released(&block)
24
+ self.add_key_listener(Listener.create(MouseListener, :mouseReleased, &block))
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,55 @@
1
+ module RSwing
2
+ module Components
3
+ require "java"
4
+ require "container"
5
+ JFrame = javax.swing.JFrame
6
+
7
+ class Frame < JFrame
8
+ include Container
9
+
10
+ def initialize(title, &block)
11
+ super(title)
12
+
13
+ # falls block übergeben wurde, mit aktuellem objekt aufrufen
14
+ if block_given?
15
+ yield self
16
+ end
17
+ end
18
+
19
+ def add(component, options = {})
20
+ if(layout = Options.value_for(options => :layout))
21
+ self.content_pane.add(component, layout)
22
+ else
23
+ self.content_pane.add(component)
24
+ end
25
+
26
+
27
+ # wenn :name angegeben wurde, mit aufnehmen
28
+ if(name = Options.value_for(options => :name))
29
+ self.add_with_name(component, name)
30
+ end
31
+
32
+ component #zurückgeben
33
+ end
34
+
35
+ # Sets the default close operation for this frame.
36
+ # Valid operations are:
37
+ # 1. <tt>:do_nothing_on_close</tt>
38
+ # 2. <tt>:hide_on_close</tt> (default)
39
+ # 3. <tt>:dispose_on_close</tt>
40
+ # 4. <tt>:exit_on_close</tt>
41
+ def default_close_operation=(op = :hide_on_close)
42
+ case op
43
+ when :do_nothing_on_close
44
+ self.setDefaultCloseOperation(WindowConstants::DO_NOTHING_ON_CLOSE)
45
+ when :hide_on_close
46
+ self.setDefaultCloseOperation(WindowConstants::HIDE_ON_CLOSE)
47
+ when :dispose_on_close
48
+ self.setDefaultCloseOperation(WindowConstants::DISPOSE_ON_CLOSE)
49
+ when :exit_on_close
50
+ self.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,24 @@
1
+ module RSwing
2
+ module Components
3
+ class Listener
4
+ # Erstellt eine Listener-Klasse des angegebenen listener_interfaces
5
+ # (Java Listener Interface), die alle Interface-Methoden leer haelt
6
+ # ausser derjenigen, die mit <tt>methodname</tt> angegeben wurde
7
+ # (und deren Inhalt mit dem angegebenen <tt>block</tt> gefüllt wird).
8
+ # Gibt anschließend eine neue Instanz dieser Listener-Klasse zurück
9
+ def self.create(listener_interface, methodname, &block)
10
+ listener_class = Class.new() do
11
+ include listener_interface
12
+ implement listener_interface
13
+ def initialize(methodname, &block)
14
+ self.class.instance_eval do
15
+ define_method(methodname, &block)
16
+ end
17
+ end
18
+ end
19
+ # neue instanz zurückgeben
20
+ listener_class.new(methodname, &block)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,42 @@
1
+ module RSwing
2
+ module Components
3
+ class Options
4
+ # Hash of all valid default-values for rswing options.
5
+ def self.gui_options
6
+ {
7
+ :enabled => true,
8
+ :visible => true,
9
+ :editable => true,
10
+ :resizable => true,
11
+ :modal => true,
12
+ :double_buffer => true,
13
+ :dialog_type => :info,
14
+ :option_type => :yes_no,
15
+ :option_values => ["Ja", "Nein"],
16
+ :icon => nil,
17
+ :belongs_to => nil,
18
+ :layout => nil,
19
+ :title => nil,
20
+ :name => nil,
21
+ :font => nil,
22
+ :doc => nil,
23
+ :columns => 10,
24
+ :text => ""
25
+ }
26
+ end
27
+
28
+ # Returns the value of a key (which is given as the value of the hash).
29
+ # If the value to the key isn't given in the hash, return the
30
+ # default-value from <tt>gui_options</tt>-hash.
31
+ # * Example:
32
+ # <tt>Options.value_for(options => :name)</tt>
33
+ def self.value_for(hash)
34
+ hash.each_pair do |options_hash, option_key|
35
+ # es sollte nur ein paar angegeben werden, sodass wir einfach nur das erste nehmen
36
+ # und den entsprechenden wert zurückgeben
37
+ return options_hash[option_key].nil? ? gui_options[option_key] : options_hash[option_key]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ module RSwing
2
+ module Components
3
+ require "java"
4
+ require "container"
5
+ require "events/key_events"
6
+ require "events/mouse_events"
7
+ require "events/focus_events"
8
+ include_package 'javax.swing'
9
+
10
+ class Panel < JPanel
11
+ include Container
12
+ include Events::KeyEvents
13
+ include Events::MouseEvents
14
+ include Events::FocusEvents
15
+
16
+ def initialize(layout_manager, options = {}, &block)
17
+ super(layout_manager, Options.value_for(options => :double_buffer))
18
+
19
+ if (border_title = Options.value_for(options => :title))
20
+ border = BorderFactory.createTitledBorder(border_title)
21
+ border.title_font = Font.new("Arial", Font::BOLD, 14)
22
+ self.border = border
23
+ end
24
+
25
+ # block aufrufen mit aktuellem objekt, falls vorhanden
26
+ if block_given?
27
+ yield self
28
+ end
29
+
30
+ Container.add_if_requested(self, options)
31
+ end
32
+
33
+ def add(component, options = {})
34
+ super.add(component)
35
+
36
+ if(name = Options.value_for(options => :name))
37
+ self.add_with_name(component, name)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,29 @@
1
+ module RSwing
2
+ module Components
3
+ require "options"
4
+
5
+ class TextField < javax.swing.JTextField
6
+ # Valid options are:
7
+ # 1. <tt>:text => "text"</tt> (default: "")
8
+ # 2. <tt>:columns => 5</tt> (default: 10)
9
+ # 3. <tt>:doc => nil</tt> (default: nil)
10
+ # 4. <tt>:font => nil</tt> (default nil)
11
+ # 5. <tt>:visible => false</tt> (default: true)
12
+ # 6. <tt>:editable => false</tt> (default: true)
13
+ def initialize(options = {})
14
+ if(options.empty?)
15
+ super()
16
+ else
17
+ if(doc = Options.value_for(options => :doc))
18
+ super(doc, Options.value_for(options => :text), Options.value_for(options => :columns))
19
+ else
20
+ super(Options.value_for(options => :text), Options.value_for(options => :columns))
21
+ end
22
+ end
23
+
24
+ Container.add_if_requested(self, options)
25
+ end
26
+ end
27
+
28
+ end
29
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bakkdoor-rswing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Bertels
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-05 00:00:00 -07:00
12
+ date: 2008-10-06 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,8 +26,17 @@ files:
26
26
  - LICENSE
27
27
  - Rakefile
28
28
  - lib/rswing.rb
29
- - lib/rswing/components/*.rb
30
- - lib/rswing/components/events/*.rb
29
+ - lib/rswing/components/button.rb
30
+ - lib/rswing/components/container.rb
31
+ - lib/rswing/components/dialog.rb
32
+ - lib/rswing/components/frame.rb
33
+ - lib/rswing/components/listener.rb
34
+ - lib/rswing/components/options.rb
35
+ - lib/rswing/components/panel.rb
36
+ - lib/rswing/components/text_field.rb
37
+ - lib/rswing/components/events/focus_events.rb
38
+ - lib/rswing/components/events/key_events.rb
39
+ - lib/rswing/components/events/mouse_events.rb
31
40
  has_rdoc: true
32
41
  homepage: http://github.com/bakkdoor/rswing
33
42
  post_install_message: