swing 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/HISTORY +4 -0
  2. data/README.rdoc +23 -12
  3. data/VERSION +1 -1
  4. data/lib/awt/component.rb +11 -0
  5. data/lib/awt.rb +4 -0
  6. data/lib/swing/controls.rb +3 -3
  7. data/lib/swing/j_button.rb +12 -0
  8. data/lib/swing/j_check_box.rb +13 -0
  9. data/lib/swing/j_frame.rb +24 -0
  10. data/lib/swing/j_label.rb +6 -0
  11. data/lib/swing/j_list.rb +6 -0
  12. data/lib/swing/j_menu.rb +5 -0
  13. data/lib/swing/j_menu_bar.rb +32 -0
  14. data/lib/swing/j_menu_item.rb +9 -0
  15. data/lib/swing/j_panel.rb +6 -0
  16. data/lib/swing/j_scroll_pane.rb +8 -0
  17. data/lib/swing/j_split_pane.rb +11 -0
  18. data/lib/swing/j_table.rb +51 -0
  19. data/lib/swing/{button.rb → old/button.rb} +2 -3
  20. data/lib/swing/{check_box.rb → old/check_box.rb} +1 -2
  21. data/lib/swing/{frame.rb → old/frame.rb} +0 -0
  22. data/lib/swing/{label.rb → old/label.rb} +0 -0
  23. data/lib/swing/{list.rb → old/list.rb} +0 -0
  24. data/lib/swing/{menu.rb → old/menu.rb} +0 -0
  25. data/lib/swing/{menu_bar.rb → old/menu_bar.rb} +5 -1
  26. data/lib/swing/{menu_item.rb → old/menu_item.rb} +0 -0
  27. data/lib/swing/{panel.rb → old/panel.rb} +0 -0
  28. data/lib/swing/{scroll_pane.rb → old/scroll_pane.rb} +0 -0
  29. data/lib/swing/{split_pane.rb → old/split_pane.rb} +0 -0
  30. data/lib/swing/{table.rb → old/table.rb} +0 -0
  31. data/lib/swing.rb +18 -19
  32. data/lib/{swing → swing_support}/action_listener.rb +2 -2
  33. data/lib/{swing → swing_support/extensions}/attr_setter.rb +0 -0
  34. data/lib/swing_support/extensions/attributes.rb +65 -0
  35. data/lib/swing_support/extensions.rb +4 -0
  36. data/lib/swing_support.rb +4 -0
  37. data/spec/awt/component_spec.rb +26 -0
  38. data/spec/{button_spec.rb → swing/j_button_spec.rb} +2 -2
  39. metadata +37 -18
data/HISTORY CHANGED
@@ -21,3 +21,7 @@
21
21
  == 0.0.5 / 2011-05-24
22
22
 
23
23
  * Ignoring IDEA module info
24
+
25
+ == 0.1.0 / 2011-05-25
26
+
27
+ * API changed to conform to original Swing classes
data/README.rdoc CHANGED
@@ -4,18 +4,29 @@ url: http://github.com/arvicco/swing
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- Straightforward wrappers for javax.swing Components that simplify Swing code in
8
- JRuby-based GUI applications.
9
-
10
- == FEATURES/PROBLEMS:
11
-
12
- * Library defines Ruby wrapper classes for Swing components that do NOT shadow original
13
- javax.swing classes. So, javax.swing.JButton class is wrapped by Swing::Button class,
14
- and you need to <b> require 'swing/button' </b> to use it.
15
- * Ruby wrappers define properties that are settable in constructor options, as well
16
- as (optional) defaults for them that may be different from swing's original defaults:
17
- Swing::Button.new 'Title', :enabled => false
18
- *
7
+ Straightforward wrappers for javax.swing Components that simplify Swing code in JRuby-based GUI applications.
8
+
9
+ == PROBLEMS:
10
+
11
+ This code is NOT production-ready! For production-ready code, try something like Rubeus:
12
+ [https://github.com/akm/rubeus]. Personally, I've tried to use Rubeus and its auto-magic
13
+ worked great for some use-cases, but I had problems extending its code when I needed to
14
+ change default Rubeus behavior. For the life of me, I cannot figure out where all this
15
+ Rubeus magic happens and how to change ANYTHING without the whole stage with smoke and
16
+ mirrors falling down on me.
17
+
18
+ Maybe it's just me - but I'm uncomfortable with complicated code and prefer much more
19
+ straightforward solutions. So, I intend this lib to be straightforward - even at the
20
+ expense of performance.
21
+
22
+ == FEATURES:
23
+
24
+ Library adds extended functionality to original javax.swing classes, such as:
25
+ * Ability to set attributes via constructor options:
26
+ Swing::JButton.new 'Title', :enabled => false
27
+ * Ability to define settable attributes/properties via attr_setter class macro,
28
+ as well as (optional) defaults for them that may be different from Swing's original defaults
29
+ * Auto adding component to parent if ops[:parent] is given to initializer
19
30
 
20
31
  == SYNOPSIS:
21
32
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.1.0
@@ -0,0 +1,11 @@
1
+ require 'swing_support/extensions/attributes'
2
+
3
+ class Awt::Component
4
+
5
+ # TODO: remove from spec suite
6
+ def blah
7
+ end
8
+
9
+ include SwingSupport::Extensions::Attributes
10
+
11
+ end
data/lib/awt.rb ADDED
@@ -0,0 +1,4 @@
1
+ include Java
2
+ Awt = java.awt
3
+
4
+ require 'awt/component'
@@ -1,4 +1,4 @@
1
1
  # Swing-based GUI controls
2
- require 'swing/label'
3
- require 'swing/button'
4
- require 'swing/check_box'
2
+ require 'swing/j_label'
3
+ require 'swing/j_button'
4
+ require 'swing/j_check_box'
@@ -0,0 +1,12 @@
1
+ require 'swing'
2
+
3
+ class Swing::JButton
4
+
5
+ attr_setter :enabled
6
+
7
+ def initialize *args, &block
8
+ super *args
9
+
10
+ self.addActionListener SwingSupport::ActionListener.new &block
11
+ end
12
+ end # class JButton
@@ -0,0 +1,13 @@
1
+ require 'swing'
2
+
3
+ module Swing::JCheckBox
4
+
5
+ attr_setter :selected
6
+
7
+ def initialize *args, &block
8
+ super *args
9
+
10
+ # TODO: Probably need to implement ItemListener as well?
11
+ self.addActionListener SwingSupport::ActionListener.new &block
12
+ end
13
+ end # class JCheckBox
@@ -0,0 +1,24 @@
1
+ require 'swing'
2
+
3
+ module Swing::JFrame
4
+ # include AttrSetter
5
+
6
+ attr_setter :layout, :background, :size, :title,
7
+ :default_close_operation => JFrame::EXIT_ON_CLOSE #DISPOSE_ON_CLOSE, HIDE_ON_CLOSE
8
+
9
+ def initialize *args
10
+
11
+ super *args
12
+
13
+ # setup opts
14
+
15
+ self.location_relative_to = nil
16
+ self.visible = true
17
+ end
18
+
19
+ # Method that subclasses should override to set up their contents before
20
+ # Frame is made visible
21
+ def setup opts
22
+ end
23
+
24
+ end
@@ -0,0 +1,6 @@
1
+ require 'swing'
2
+
3
+ module Swing::JLabel
4
+
5
+ end # class Label
6
+
@@ -0,0 +1,6 @@
1
+ require 'swing'
2
+
3
+ module Swing::JList
4
+
5
+ end # class List
6
+
@@ -0,0 +1,5 @@
1
+ require 'swing'
2
+
3
+ module Swing::JMenu
4
+
5
+ end
@@ -0,0 +1,32 @@
1
+ require 'swing'
2
+ require 'swing/j_menu'
3
+ require 'swing/j_menu_item'
4
+
5
+ module Swing::JMenuBar
6
+
7
+ def initialize opts = {}
8
+ super opts
9
+
10
+ if opts[:structure]
11
+ [opts[:structure]].flatten.each do |element|
12
+ case element
13
+ when Hash # Hash defines menu structure
14
+ element.each do |menu_name, menu_structure|
15
+ menu = JMenu.new menu_name.to_s, :parent => self
16
+ menu_structure.each do |item_name, item_action|
17
+ JMenuItem.new item_name.to_s, :parent => menu, &item_action
18
+ end
19
+ end
20
+ else
21
+ self.add element
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ # Post-processing (non-setter) options given to initialize
28
+ def self.add_component component, parent
29
+ parent.setJMenuBar component if parent
30
+ end
31
+
32
+ end
@@ -0,0 +1,9 @@
1
+ require 'swing'
2
+
3
+ module Swing::JMenuItem
4
+
5
+ def initialize text, opts = {}, &block
6
+ super text
7
+ self.addActionListener SwingHelper::ActionListener.new &block
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ require 'swing'
2
+
3
+ module Swing::JPanel
4
+
5
+ attr_setter :layout, :background
6
+ end # class Panel
@@ -0,0 +1,8 @@
1
+ require 'swing'
2
+
3
+ # Scroll Pane around given scrollable component
4
+ module Swing::JScrollPane
5
+
6
+ attr_setter :horizontal_scroll_bar_policy, :vertical_scroll_bar_policy
7
+
8
+ end # class JScrollPane
@@ -0,0 +1,11 @@
1
+ require 'swing'
2
+
3
+ module Swing::JSplitPane
4
+
5
+ attr_setter :one_touch_expandable, :orientation, :continuous_layout,
6
+ :divider_size, :divider_location, :resize_weight
7
+
8
+ def initialize first, second, opts = {}
9
+ super(HORIZONTAL_SPLIT, first, second)
10
+ end
11
+ end # class JSplitPane
@@ -0,0 +1,51 @@
1
+ require 'swing'
2
+
3
+ module Swing::JTable
4
+
5
+ # autoCreateColumnsFromModel boolean � � false
6
+ # autoResizeMode int � � AUTO_RESIZE_ALL_COLUMNS
7
+ # columnModel TableColumnModel � � DefaultTableColumnModel( )
8
+ # model TableModel � � DefaultTableModel( )
9
+ # rowHeight int � � 16
10
+ # cellSelectionEnabled boolean � � false
11
+ # columnSelectionAllowed boolean � � false
12
+ # rowSelectionAllowed boolean � � true
13
+ # selectionMode int � MULTIPLE_INTERVAL_SELECTION
14
+ # selectionModel ListSelectionModel � � DefaultListSelectionModel
15
+ # cellEditor TableCellEditor � � null
16
+ # dragEnabled boolean � � false
17
+ # gridColor Color � � From L&F
18
+ # intercellSpacing Dimension � � Dimension(1, 1)
19
+ # preferredScrollableViewportSize Dimension � � Dimension(450, 400)
20
+ # rowMargin int � � 1
21
+ # selectionBackground Color � � From L&F
22
+ # selectionForeground Color � � From L&F
23
+ # showGrid boolean � true
24
+ # showHorizontalLines boolean � � true
25
+ # showVerticalLines boolean � � true
26
+ # tableHeader JTableHeader � � JTableHeader(column-Model)
27
+ # ----Getters only - NO Setters!
28
+ # columnCount int � 0
29
+ # rowCount int � 0
30
+ # selectedColumn int � -1
31
+ # selectedColumnCount int � 0
32
+ # selectedColumns int[] � int[0]
33
+ # selectedRow int � -1
34
+ # selectedRowCount int � 0
35
+ # selectedRows int[] � int[0]
36
+ # accessibleContext AccessibleContext � JTable.AccessibleJTable
37
+ # scrollableTracksViewportHeighto boolean � false
38
+ # scrollableTracksViewportWidtho boolean � false
39
+
40
+ attr_setter :auto_create_columns_from_model, :auto_resize_mode, :row_height,
41
+ :model, :column_model, :selection_model,
42
+ :drag_enabled, :cell_editor, :grid_color, :intercell_spacing,
43
+ :preferred_scrollable_viewport_size, :row_margin,
44
+ :selection_background, :selection_foreground, :show_grid,
45
+ :show_horizontal_lines, :show_vertical_lines, :table_header,
46
+ :selection_mode => javax.swing.ListSelectionModel::SINGLE_SELECTION,
47
+ :cell_selection_enabled => true,
48
+ :row_selection_allowed => false,
49
+ :column_selection_allowed => false
50
+
51
+ end # class JTable
@@ -1,10 +1,9 @@
1
+ require 'swing'
1
2
  require 'swing/action_listener'
2
- require 'swing/attr_setter'
3
3
 
4
4
  module Swing
5
5
 
6
- class Button < javax.swing.JButton
7
- include AttrSetter
6
+ class JButton
8
7
 
9
8
  attr_setter :enabled
10
9
 
@@ -1,9 +1,8 @@
1
+ require 'swing'
1
2
  require 'swing/action_listener'
2
- require 'swing/attr_setter'
3
3
 
4
4
  module Swing
5
5
  class CheckBox < javax.swing.JCheckBox
6
- include AttrSetter
7
6
 
8
7
  attr_setter :selected
9
8
 
File without changes
File without changes
File without changes
File without changes
@@ -25,8 +25,12 @@ module Swing
25
25
  end
26
26
  end
27
27
  end
28
- opts[:parent].setJMenuBar self if opts[:parent]
29
28
  end
30
29
  end
31
30
 
31
+ # Post-processing (non-setter) options given to initialize
32
+ def self.add_component component, parent
33
+ parent.setJMenuBar component if parent
34
+ end
35
+
32
36
  end
File without changes
File without changes
File without changes
File without changes
File without changes
data/lib/swing.rb CHANGED
@@ -1,23 +1,22 @@
1
- require 'version'
2
1
  require 'rubygems'
3
2
 
4
- module Swing
5
-
6
- # Requires ruby source file(s), given as single filename/glob or Array of filenames/globs.
7
- # Accepts following options:
8
- # :*file*:: Lib(s) required relative to this file - defaults to __FILE__
9
- # :*dir*:: Required lib(s) located under this dir name - defaults to gem name
10
- #
11
- def self.require_libs( libs, opts={} )
12
- file = Pathname.new(opts[:file] || __FILE__)
13
- [libs].flatten.each do |lib|
14
- name = file.dirname + (opts[:dir] || file.basename('.*')) + lib.gsub(/(?<!.rb)$/, '.rb')
15
- Pathname.glob(name.to_s).sort.each {|rb| require rb}
16
- end
17
- end
18
- end # module Swing
3
+ include Java
4
+ Swing = javax.swing
19
5
 
20
- # Require all ruby source files located under directory lib/swing
21
- # If you need files in specific order, you should specify it here before the glob
22
- Swing.require_libs %W[**/*]
6
+ require 'version'
7
+ require 'swing_support'
8
+ require 'awt'
23
9
 
10
+ #
11
+ #module Swing
12
+ # autoload :JButton, "swing/j_button"
13
+ # autoload :JCheckBox, "swing/j_check_box"
14
+ # autoload :JButton, "swing/j_button"
15
+ # autoload :JButton, "swing/j_button"
16
+ # autoload :JButton, "swing/j_button"
17
+ # autoload :JButton, "swing/j_button"
18
+ # autoload :JButton, "swing/j_button"
19
+ # autoload :Jdbc, "rubeus/jdbc"
20
+ # autoload :Reflection, "rubeus/reflection"
21
+ # autoload :Util, "rubeus/util"
22
+ #end # module Swing
@@ -1,6 +1,6 @@
1
- include Java
1
+ require 'swing_support/extensions'
2
2
 
3
- module Swing
3
+ module SwingSupport
4
4
  # Class that implements ActionListener interface around a given block
5
5
  class ActionListener
6
6
  java_implements java.awt.event.ActionListener
@@ -0,0 +1,65 @@
1
+ import java.awt.Dimension
2
+
3
+ module SwingSupport
4
+ module Extensions
5
+ # Module allows including classes to receive attribute values in an *opts* Hash
6
+ # and sets those attributes after object initialization
7
+ module Attributes
8
+ def self.included host
9
+ host.send :extend, ClassMethods
10
+ host.instance_eval do
11
+ attr_setter :font, :tool_tip_text, :enabled
12
+ alias :new_without_attributes :new
13
+ alias :new :new_with_attributes
14
+ end
15
+ end
16
+
17
+ module ClassMethods
18
+ def attributes
19
+ @attributes ||= (superclass.attributes.dup rescue {})
20
+ end
21
+
22
+ # Adds settable attributes for a given class, possibly with defaults
23
+ # If defaults are given for attributes, they should be put at the end (as opts)
24
+ def attr_setter *new_attributes
25
+ if new_attributes.last.is_a? Hash
26
+ # Some attributes are given with defaults
27
+ new_attributes_with_defaults = new_attributes.pop
28
+ new_attributes_with_defaults.each { |name, default| attributes[name] = default }
29
+ end
30
+ new_attributes.each { |name| attributes[name] = nil }
31
+ end
32
+
33
+ # Sets attributes after calling original new
34
+ def new_with_attributes(*args, &block)
35
+ opts = args.last.is_a?(Hash) ? args.pop : {}
36
+ component = self.new_without_attributes(*args, &block)
37
+
38
+ # Extract known attributes given in opts, or known defaults
39
+ attributes = attributes().map do |name, default|
40
+ value = opts.delete name
41
+ result = if default.nil? # No default, return whatever value
42
+ value
43
+ elsif default.respond_to? :call # Default is callable, call it with whatever value
44
+ default.call *value
45
+ else # Return either non-nil value or default
46
+ value.nil? ? default : value
47
+ end
48
+ [name, result] unless result.nil?
49
+ end.compact
50
+ # yield opts if block_given?
51
+ attributes.each { |(name, value)| component.send "#{name}=", *value }
52
+
53
+ # Post-process non-setter opts
54
+ add_component component, opts[:parent]
55
+ component
56
+ end
57
+
58
+ # Post-processing (non-setter) options given to initialize
59
+ def add_component component, parent
60
+ parent.add component if parent
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,4 @@
1
+ module SwingSupport
2
+ module Extensions
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module SwingSupport
2
+ end
3
+
4
+ require 'swing_support/extensions/attributes'
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require 'awt/component'
3
+
4
+ describe 'java.awt.Component' do
5
+ context 'defining new methods' do
6
+ it 'changes derived Ruby subclases' do
7
+ button = Swing::JButton.new 'Disconnect', :enabled => false
8
+ button.should_not be_enabled
9
+ button.should respond_to :blah
10
+ button.blah
11
+ end
12
+
13
+ it 'changes derived pure Java subclases' do
14
+ button = javax.swing.JButton.new 'Disconnect' #, :enabled => false
15
+ button.should respond_to :blah
16
+ button.blah
17
+ end
18
+ end
19
+
20
+ context 'with Attributes extension' do
21
+ it 'allows pure Java subclasses to accept options' do
22
+ button = javax.swing.JButton.new 'Disconnect', :enabled => false
23
+ button.should_not be_enabled
24
+ end
25
+ end
26
+ end
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Swing::Button do
3
+ describe Swing::JButton do
4
4
  it 'is possible to set :enabled attribute to false' do
5
- button = Swing::Button.new 'Disconnect', :enabled => false
5
+ button = Swing::JButton.new 'Disconnect', :enabled => false
6
6
  button.should_not be_enabled
7
7
  end
8
8
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: swing
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.5
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - arvicco
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-24 00:00:00 +04:00
13
+ date: 2011-05-25 00:00:00 +04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -49,26 +49,44 @@ extra_rdoc_files:
49
49
  - README.rdoc
50
50
  files:
51
51
  - bin/swing
52
+ - lib/awt.rb
52
53
  - lib/swing.rb
54
+ - lib/swing_support.rb
53
55
  - lib/version.rb
54
- - lib/swing/action_listener.rb
55
- - lib/swing/attr_setter.rb
56
- - lib/swing/button.rb
57
- - lib/swing/check_box.rb
56
+ - lib/awt/component.rb
58
57
  - lib/swing/controls.rb
59
- - lib/swing/frame.rb
60
- - lib/swing/label.rb
61
- - lib/swing/list.rb
62
- - lib/swing/menu.rb
63
- - lib/swing/menu_bar.rb
64
- - lib/swing/menu_item.rb
65
- - lib/swing/panel.rb
66
- - lib/swing/scroll_pane.rb
67
- - lib/swing/split_pane.rb
68
- - lib/swing/table.rb
69
- - spec/button_spec.rb
58
+ - lib/swing/j_button.rb
59
+ - lib/swing/j_check_box.rb
60
+ - lib/swing/j_frame.rb
61
+ - lib/swing/j_label.rb
62
+ - lib/swing/j_list.rb
63
+ - lib/swing/j_menu.rb
64
+ - lib/swing/j_menu_bar.rb
65
+ - lib/swing/j_menu_item.rb
66
+ - lib/swing/j_panel.rb
67
+ - lib/swing/j_scroll_pane.rb
68
+ - lib/swing/j_split_pane.rb
69
+ - lib/swing/j_table.rb
70
+ - lib/swing/old/button.rb
71
+ - lib/swing/old/check_box.rb
72
+ - lib/swing/old/frame.rb
73
+ - lib/swing/old/label.rb
74
+ - lib/swing/old/list.rb
75
+ - lib/swing/old/menu.rb
76
+ - lib/swing/old/menu_bar.rb
77
+ - lib/swing/old/menu_item.rb
78
+ - lib/swing/old/panel.rb
79
+ - lib/swing/old/scroll_pane.rb
80
+ - lib/swing/old/split_pane.rb
81
+ - lib/swing/old/table.rb
82
+ - lib/swing_support/action_listener.rb
83
+ - lib/swing_support/extensions.rb
84
+ - lib/swing_support/extensions/attr_setter.rb
85
+ - lib/swing_support/extensions/attributes.rb
70
86
  - spec/spec_helper.rb
71
87
  - spec/swing_spec.rb
88
+ - spec/awt/component_spec.rb
89
+ - spec/swing/j_button_spec.rb
72
90
  - features/swing.feature
73
91
  - features/step_definitions/swing_steps.rb
74
92
  - features/support/env.rb
@@ -119,6 +137,7 @@ signing_key:
119
137
  specification_version: 3
120
138
  summary: Straightforward wrappers for javax.swing Components
121
139
  test_files:
122
- - spec/button_spec.rb
123
140
  - spec/spec_helper.rb
124
141
  - spec/swing_spec.rb
142
+ - spec/awt/component_spec.rb
143
+ - spec/swing/j_button_spec.rb