Class: Clevic::Action

Inherits:
Object show all
Includes:
Gather
Defined in:
lib/clevic/swing/action.rb

Direct Known Subclasses

Separator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, options = {}, &block) ⇒ Action

Returns a new instance of Action.



18
19
20
21
22
23
24
# File 'lib/clevic/swing/action.rb', line 18

def initialize( parent, options = {}, &block )
  @parent = parent
  @enabled = true

  # work around the Swing Stupidity detailed in enabled=
  gather( options, &block )
end

Instance Attribute Details

Returns the value of attribute menu_item.



25
26
27
# File 'lib/clevic/swing/action.rb', line 25

def menu_item
  @menu_item
end

#parentObject (readonly)

Returns the value of attribute parent.



25
26
27
# File 'lib/clevic/swing/action.rb', line 25

def parent
  @parent
end

Instance Method Details

#enabled=(bool) ⇒ Object

Needed to enable / disable accelerators on the fly.



11
12
13
14
15
16
# File 'lib/clevic/swing/action.rb', line 11

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

#mnemonicObject

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



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/clevic/swing/action.rb', line 38

def mnemonic
  if @mnemonic.nil?
    ix = text.index '&'
    @mnemonic =
    if ix
      eval( "java.awt.event.KeyEvent::VK_#{text[ix+1..ix+1].upcase}" )
    else
      false
    end
  end
  @mnemonic
end

#object_nameObject



31
32
33
# File 'lib/clevic/swing/action.rb', line 31

def object_name
  name.to_s
end

#parse_shortcut(sequence) ⇒ Object

parse a Qt-style Ctrl+D shortcut specification and return a javax.swing.KeyStroke



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/clevic/swing/action.rb', line 83

def parse_shortcut( sequence )
  # munge keystroke to something getKeyStroke recognises
  # file:///usr/share/doc/java-sdk-docs-1.6.0.10/html/api/javax/swing/KeyStroke.html#getKeyStroke%28java.lang.String%29
  # Yes, the space MUST be last in the charset, otherwise Ctrl-" fails
  modifiers = sequence.split( /[-+ ]/ )
  last = modifiers.pop

  modifier_mask = modifiers.inject(0) do |mask,value|
    mask |
    if value =~ /ctrl/i
      java.awt.Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()
    else
      eval( "java.awt.event.InputEvent::#{value.upcase}_DOWN_MASK" )
    end
  end

  keystroke =
  if last.length == 1
    case last
      # these two seem to break the KeyStroke parsing algorithm
      when "'"
        javax.swing.KeyStroke.getKeyStroke( java.awt.event.KeyEvent::VK_QUOTE, modifier_mask )

      when '"'
        javax.swing.KeyStroke.getKeyStroke( java.awt.event.KeyEvent::VK_QUOTE, modifier_mask | java.awt.event.InputEvent::SHIFT_DOWN_MASK )

      # the conversion in else doesn't work for these
      when '['
        javax.swing.KeyStroke.getKeyStroke( java.awt.event.KeyEvent::VK_OPEN_BRACKET, modifier_mask )

      when ']'
        javax.swing.KeyStroke.getKeyStroke( java.awt.event.KeyEvent::VK_CLOSE_BRACKET, modifier_mask )

      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
        # 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
  keystroke || raise( "unknown keystroke #{sequence} => #{modifiers.inspect} #{last}" )
end

#plain_textObject



27
28
29
# File 'lib/clevic/swing/action.rb', line 27

def plain_text
  text.gsub( /&/, '' )
end