Request for review 7151821: [macosx] Mnemonic doesn't work in JTabbedPane
Anton V. Tarasov
anton.tarasov at oracle.com
Wed Mar 14 05:04:18 PDT 2012
On 14.03.2012 14:42, Leonid Romanov wrote:
> Ok, I've done some digging through Swing code and found that mnemonics implementation does rely on key char value. See SwingUtilities.notifyAction method, which delivers Action event when one of the registered key strokes has been triggered by user. Here is the relevant code snippet:
>
> // Convert it to a string.
> String command;
>
> if (commandO != null) {
> command = commandO.toString();
> }
> else if (!stayNull&& event.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
> command = String.valueOf(event.getKeyChar());
> }
> else {
> // Do null for undefined chars, or if registerKeyboardAction
> // was called with a null.
> command = null;
> }
> action.actionPerformed(new ActionEvent(sender,
> ActionEvent.ACTION_PERFORMED, command, event.getWhen(),
> modifiers));
>
> Also, see BasicTabbedPaneUI's actionPerformed() method which uses that key char like this:
>
> else if (key == SET_SELECTED) {
> String command = e.getActionCommand();
>
> if (command != null&& command.length()> 0) {
> int mnemonic = (int)e.getActionCommand().charAt(0);
> if (mnemonic>= 'a'&& mnemonic<='z') {
> mnemonic -= ('a' - 'A');
> }
> Integer index = ui.mnemonicToIndexMap.get(Integer.valueOf(mnemonic));
> if (index != null&& pane.isEnabledAt(index.intValue())) {
> pane.setSelectedIndex(index.intValue());
> }
> }
> }
>
> I can't say I completely understand what the code above is trying to achieve (and why), but it seems like Alexandr's fix is actually correct. The problem is the following: on Cocoa level, there is no difference between pressing ctrl+c and ctrl+alt+c, in both cases NSEvent's -characters method returns 0x03 as the character typed. While this is exactly what we want in case of ctrl+c, in order for the ctrl+alt+c mnemonic to work, we have to return 'c' instead, even if it contradicts the way OS X works.
Well, I see. Thanks for the analysis.
I asked Alexander P. (cc'ed) about the mnemonics. He said that using a keyChar is a "historical
reason". And indeed, there's an obsolete setMnemonic(char) method:
http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setMnemonic%28char%29
Thanks,
Anton.
>
>
> On 14.03.2012, at 13:38, Anton V. Tarasov wrote:
>
>> Hi All,
>>
>> On 13.03.2012 20:26, Leonid Romanov wrote:
>>> Thanks! I've played with javax/swing/JTabbedPane/4624207/bug4624207.java and there is an interesting thing about it: although mnemonics for switching between tabs don't work, setting focus to the "Name" text field via ctrl+alt+m works just fine. So, could it be that there is something broken about the way how JTabbedPane mnemonics are handled? I'm not an expert when it comes to mnemonics, but it seems to me that mnemonics use key codes, so they shouldn't be affected by key char value, right?
>> This is a good question.
>>
>> A valid keyChar value is supposed to be delivered by KEY_TYPED event [1]:
>>
>> <<The getKeyChar method always returns a valid Unicode character or CHAR_UNDEFINED. Character input is reported by KEY_TYPED events: KEY_PRESSED and KEY_RELEASED events are not necessarily associated with character input. Therefore, the result of the getKeyChar method is guaranteed to be meaningful only for KEY_TYPED events.>>
>>
>> So, we shouldn't actually rely on keyChar in non KEY_TYPED events.
>>
>> I also experimented with the test and found out that, for instance, ctrl+alt+Number mnemonic did work for JTabbedPane. The reason is that such a key stroke produces a valid keyChar (Number). So here's another question: what is the difference b/w ctrl+alt+Number and ctrl+alt+Letter in terms of a keyChar? May the fix for 7134826 be the right place to look at (CPlatformResponder.handleKeyEvent(..))?
>>
>> Thanks,
>> Anton.
>>
>> [1] http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html
>>
>>>
>>> On 13.03.2012, at 20:05, Alexandr Scherbatiy wrote:
>>>
>>>> 13.03.2012 18:35, Leonid Romanov пишет:
>>>>> Hit "Send" too early. Basically, what you are describing in 7151821 is exactly the way JDK 6 works. So, what is wrong with it? Does it break anything?
>>>> It is definitely a regression from the b225.
>>>>
>>>> The key listener got a KeyEvent with the key char 'a' after pressing for example the Ctrl+Alt+a.
>>>> And it is expected that the key char should be 'a' in this case.
>>>>
>>>> In the latest build the key char is not 'a' for a KeyEvent.
>>>> The test case with the KeyListener installed to the JPanel is described in the issue 7151821:
>>>> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7151821
>>>>
>>>> The Alt+Key combination is a system combination on Mac and can't be used for mnemonics.
>>>>
>>>> It seems that the Mac OS X is not friendly to the Java mnemonics.
>>>> Here is some comments from the document:
>>>> https://developer.apple.com/library/mac/#documentation/Java/Conceptual/Java14Development/07-NativePlatformIntegration/NativePlatformIntegration.html
>>>> -----------------------------------------
>>>> Using mnemonics is discouraged in Mac OS X, because mnemonics violate the principles of Mac OS X Human Interface Guidelines. If you are developing a Java application for multiple platforms and some of those platforms recommend the use of mnemonics, just include a single setMnemonics() method that is conditionally called (based on the platform) when constructing your interface.
>>>>
>>>> Note: Since the ALT_MASK modifier is the Option key in Mac OS X, Control-Alt masks set for Windows become Command-Option masks if you use getMenuShortcutKeyMask() in conjunction with ALT_MASK.
>>>> -----------------------------------------
>>>>
>>>> So the Ctrl+Alt+char used for the mnemonics on all tests instead of the Alt+char.
>>>>
>>>> Thanks,
>>>> Alexandr.
>>>>
>>>>
>>>>> On 13.03.2012, at 19:24, Leonid Romanov wrote:
>>>>>
>>>>>> Hi,
>>>>>> What is the test case for this issue? What Control+alt+char is supposed to do?
>>>>>>
>>>>>> On 13.03.2012, at 17:13, Alexander Scherbatiy wrote:
>>>>>>
>>>>>>> Please review a fix for 7151821.
>>>>>>>
>>>>>>> webrev: http://cr.openjdk.java.net/~alexsch/7151821/webrev.00/
>>>>>>>
>>>>>>>
>>>>>>> This is a fix for regression after switching using [event charactersIgnoringModifiers] string to [event characters] during the MACOSX_PORT-568 issue fixing:
>>>>>>> http://java.net/jira/browse/MACOSX_PORT-568
>>>>>>> http://hg.openjdk.java.net/jdk7u/jdk7u-osx/jdk/rev/5780795f381e
>>>>>>>
>>>>>>> The characters string is null during Ctrl+Alt+Char mnemonic pressing.
>>>>>>>
>>>>>>> According to the NSEvent charactersIgnoringModifiers doc:
>>>>>>> This method returns the non-modifier key character pressed for dead keys, such as Option-e.
>>>>>>> For example, Option-e (no shift key) returns an “e" for this method, whereas the characters method returns an empty string.
>>>>>>>
>>>>>>> The fix uses the charactersIgnoringModifiers string for the keychar when the Ctrl+Alt mnemonic key combination is pressed.
>>>>>>>
>>>>>>> Thanks,
>>>>>>> Alexandr.
>>>>>>>
More information about the macosx-port-dev
mailing list