Home » openjdk-7 » javax » swing » plaf » basic » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   /**
   18    * @author Alexander T. Simbirtsev
   19    */
   20   package javax.swing.plaf.basic;
   21   
   22   import java.awt.Component;
   23   import java.awt.KeyEventPostProcessor;
   24   import java.awt.event.KeyEvent;
   25   
   26   import javax.swing.Action;
   27   import javax.swing.JMenu;
   28   import javax.swing.JMenuBar;
   29   import javax.swing.JMenuItem;
   30   import javax.swing.JRootPane;
   31   import javax.swing.KeyStroke;
   32   import javax.swing.SwingUtilities;
   33   
   34   import org.apache.harmony.x.swing.StringConstants;
   35   
   36   
   37   class AcceleratorsProcessor implements KeyEventPostProcessor {
   38   
   39       public boolean postProcessKeyEvent(final KeyEvent event) {
   40           if (event.isConsumed()) {
   41               return false;
   42           }
   43   
   44           final Component source = (Component)event.getSource();
   45           final KeyStroke ks = KeyStroke.getKeyStrokeForEvent(event);
   46   
   47           JRootPane parent = SwingUtilities.getRootPane(source);
   48           while (parent != null) {
   49               final JMenuBar menu = parent.getJMenuBar();
   50               if (menu != null && processMenuAccelerator(menu, ks, event)) {
   51                   return true;
   52               }
   53               parent = (JRootPane)SwingUtilities.getAncestorOfClass(JRootPane.class, parent);
   54           }
   55   
   56           return false;
   57       }
   58   
   59       private boolean processMenuAccelerator(final JMenuBar menu, final KeyStroke ks, final KeyEvent event) {
   60           final int menuCount = menu.getMenuCount();
   61           for (int i = 0; i < menuCount; i++) {
   62               final JMenu child = menu.getMenu(i);
   63               if (child != null && processMenuAccelerator(child, ks, event)) {
   64                   return true;
   65               }
   66           }
   67           return false;
   68       }
   69   
   70       private boolean processMenuAccelerator(final JMenu menu, final KeyStroke ks, final KeyEvent event) {
   71           final int menuCount = menu.getMenuComponentCount();
   72           for (int i = 0; i < menuCount; i++) {
   73               final Component child = menu.getMenuComponent(i);
   74               if (child instanceof JMenu) {
   75                   if (processMenuAccelerator((JMenu)child, ks, event) ){
   76                       return true;
   77                   }
   78               } else if (child instanceof JMenuItem) {
   79                   final JMenuItem menuItem = (JMenuItem)child;
   80                   if (ks.equals(menuItem.getAccelerator())) {
   81                       final Action action = menuItem.getActionMap().get(StringConstants.MNEMONIC_ACTION);
   82                       if (action != null) {
   83                           SwingUtilities.notifyAction(action, ks, event, menuItem, event.getModifiersEx());
   84                           return true;
   85                       }
   86                   }
   87               }
   88           }
   89           return false;
   90       }
   91   
   92   }
   93   

Home » openjdk-7 » javax » swing » plaf » basic » [javadoc | source]