lib/clevic/swing/action.rb in clevic-0.13.0.b3 vs lib/clevic/swing/action.rb in clevic-0.13.0.b5

- old
+ new

@@ -2,22 +2,38 @@ module Clevic class Action include Gather - property :shortcut, :method, :handler, :tool_tip, :visible, :name, :text, :checkable + property :shortcut, :method, :handler, :tool_tip, :visible + property :name, :text, :checkable, :enabled + # Needed to enable / disable accelerators on the fly. + def enabled=( bool ) + # test for @menu_item instead of the method to + # work around Swing Stupidity. See comments in menu_item. + menu_item.enabled = bool unless @menu_item.nil? + @enabled = bool + end + def initialize( parent, options = {}, &block ) @parent = parent + @enabled = true + + # work around the Swing Stupidity detailed in enabled= gather( options, &block ) end attr_reader :parent, :menu_item def plain_text text.gsub( /&/, '' ) end + def object_name + name.to_s + end + # find the java.awt.event.KeyEvent::VK constant # for the letter after the &. Then set it on the item's # mnemonic. Because JMenuItem.setMnemonic won't take a nil def mnemonic if @mnemonic.nil? @@ -39,17 +55,27 @@ javax.swing.JCheckBoxMenuItem.new else javax.swing.JMenuItem.new end + # Menu item always enabled, until later. + # Otherwise it prevents the assignment + # of an accelerator key. So we have to + # work around yet another Swing stupidity. + menu_item.enabled = true + menu_item.text = plain_text menu_item.mnemonic = mnemonic if mnemonic menu_item.accelerator = parse_shortcut( shortcut ) unless shortcut.nil? menu_item.tool_tip_text = tool_tip menu_item.add_action_listener do |event| handler.call( event ) end + + # Put this at the end so it doesn't interfere with the + # keystroke assignment Swing Stupidity. + menu_item.enabled = enabled end @menu_item end # parse a Qt-style Ctrl+D shortcut specification @@ -90,21 +116,19 @@ when ';' javax.swing.KeyStroke.getKeyStroke( java.awt.event.KeyEvent::VK_SEMICOLON, modifier_mask ) else keystring = javax.swing.KeyStroke.getKeyStroke( java.lang.Character.new( last.to_char ), modifier_mask ).toString - puts "keystring: #{keystring.inspect}" # have to do this conversion for Mac OS X javax.swing.KeyStroke.getKeyStroke( keystring.gsub( /typed/, 'pressed' ) ) end else # F keys # insert, delete, tab etc found = java.awt.event.KeyEvent.constants.grep( /#{last}/i ) raise "too many options for #{sequence}: #{found.inspect}" if found.size != 1 javax.swing.KeyStroke.getKeyStroke( eval( "java.awt.event.KeyEvent::#{found.first}" ), modifier_mask ) end - puts "keystroke: #{keystroke.inspect}" keystroke || raise( "unknown keystroke #{sequence} => #{modifiers.inspect} #{last}" ) end end # dummy class for creating a menu separator