Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaMnemonicHandler.java
41154 views
/*1* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.apple.laf;2627import java.awt.*;28import java.awt.event.KeyEvent;2930import javax.swing.*;3132import com.apple.laf.AquaUtils.RecyclableSingleton;33import com.apple.laf.AquaUtils.RecyclableSingletonFromDefaultConstructor;3435public class AquaMnemonicHandler {36private static final RecyclableSingleton<AltProcessor> altProcessor = new RecyclableSingletonFromDefaultConstructor<AltProcessor>(AltProcessor.class);37public static KeyEventPostProcessor getInstance() {38return altProcessor.get();39}4041protected static boolean isMnemonicHidden = true; // true by default4243public static void setMnemonicHidden(final boolean hide) {44if (UIManager.getBoolean("Button.showMnemonics")) {45// Do not hide mnemonics if the UI defaults do not support this46isMnemonicHidden = false;47} else {48isMnemonicHidden = hide;49}50}5152/**53* Gets the state of the hide mnemonic flag. This only has meaning if this feature is supported by the underlying OS.54*55* @return true if mnemonics are hidden, otherwise, false56* @see #setMnemonicHidden57* @since 1.458*/59public static boolean isMnemonicHidden() {60if (UIManager.getBoolean("Button.showMnemonics")) {61// Do not hide mnemonics if the UI defaults do not support this62isMnemonicHidden = false;63}64return isMnemonicHidden;65}6667static class AltProcessor implements KeyEventPostProcessor {68public boolean postProcessKeyEvent(final KeyEvent ev) {69if (ev.getKeyCode() != KeyEvent.VK_ALT) {70return false;71}7273final JRootPane root = SwingUtilities.getRootPane(ev.getComponent());74final Window winAncestor = (root == null ? null : SwingUtilities.getWindowAncestor(root));7576switch(ev.getID()) {77case KeyEvent.KEY_PRESSED:78setMnemonicHidden(false);79break;80case KeyEvent.KEY_RELEASED:81setMnemonicHidden(true);82break;83}8485repaintMnemonicsInWindow(winAncestor);8687return false;88}89}9091/*92* Repaints all the components with the mnemonics in the given window and all its owned windows.93*/94static void repaintMnemonicsInWindow(final Window w) {95if (w == null || !w.isShowing()) {96return;97}9899final Window[] ownedWindows = w.getOwnedWindows();100for (final Window element : ownedWindows) {101repaintMnemonicsInWindow(element);102}103104repaintMnemonicsInContainer(w);105}106107/*108* Repaints all the components with the mnemonics in container.109* Recursively searches for all the subcomponents.110*/111static void repaintMnemonicsInContainer(final Container cont) {112for (int i = 0; i < cont.getComponentCount(); i++) {113final Component c = cont.getComponent(i);114if (c == null || !c.isVisible()) {115continue;116}117118if (c instanceof AbstractButton && ((AbstractButton)c).getMnemonic() != '\0') {119c.repaint();120continue;121}122123if (c instanceof JLabel && ((JLabel)c).getDisplayedMnemonic() != '\0') {124c.repaint();125continue;126}127128if (c instanceof Container) {129repaintMnemonicsInContainer((Container)c);130}131}132}133}134135136