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 Sergey Burlak
   19    */
   20   package javax.swing.plaf.basic;
   21   
   22   import java.awt.Component;
   23   import java.awt.Container;
   24   import java.awt.KeyboardFocusManager;
   25   import java.awt.event.ActionEvent;
   26   import java.util.HashSet;
   27   import java.util.Set;
   28   
   29   import javax.swing.AbstractAction;
   30   import javax.swing.Action;
   31   import javax.swing.JComponent;
   32   import javax.swing.JSplitPane;
   33   import javax.swing.KeyStroke;
   34   
   35   import org.apache.harmony.x.swing.Utilities;
   36   
   37   
   38   class BasicSplitPaneKeyboardActions {
   39       private static Action newNegativeIncrementAction() {
   40           return new AbstractAction() {
   41               public void actionPerformed(final ActionEvent e) {
   42                   JSplitPane splitPane = (JSplitPane)e.getSource();
   43                   if (splitPane.isFocusOwner()) {
   44                       splitPane.setDividerLocation(splitPane.getDividerLocation() - 1);
   45                       splitPane.revalidate();
   46                   }
   47               }
   48           };
   49       }
   50   
   51       private static Action newPositiveIncrementAction() {
   52           return new AbstractAction() {
   53               public void actionPerformed(final ActionEvent e) {
   54                   JSplitPane splitPane = (JSplitPane)e.getSource();
   55                   if (splitPane.isFocusOwner()) {
   56                       splitPane.setDividerLocation(splitPane.getDividerLocation() + 1);
   57                       splitPane.revalidate();
   58                   }
   59               }
   60           };
   61       }
   62   
   63       private static Action newSelectMinAction() {
   64           return new AbstractAction() {
   65               public void actionPerformed(final ActionEvent e) {
   66                   JSplitPane splitPane = (JSplitPane)e.getSource();
   67                   if (splitPane.isFocusOwner()) {
   68                       int splitPaneSize = splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT
   69                                           ? splitPane.getInsets().left
   70                                           : splitPane.getInsets().top;
   71                       splitPane.setDividerLocation(splitPaneSize);
   72                       splitPane.revalidate();
   73                   }
   74               }
   75           };
   76       }
   77   
   78       private static Action newSelectMaxAction() {
   79           return new AbstractAction() {
   80               public void actionPerformed(final ActionEvent e) {
   81                   JSplitPane splitPane = (JSplitPane)e.getSource();
   82                   if (splitPane.isFocusOwner()) {
   83                       int splitPaneSize = splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT
   84                                               ? splitPane.getWidth() - splitPane.getInsets().right
   85                                               : splitPane.getHeight() - splitPane.getInsets().bottom;
   86                       splitPane.setDividerLocation(splitPaneSize - splitPane.getDividerSize());
   87                       splitPane.revalidate();
   88                   }
   89               }
   90           };
   91       }
   92   
   93       private static Action newToggleFocusAction() {
   94           return new AbstractAction() {
   95               public void actionPerformed(final ActionEvent e) {
   96                   JSplitPane splitPane = (JSplitPane)e.getSource();
   97                   isFocusRequested = false;
   98                   if (splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
   99                       if (splitPane.getLeftComponent() instanceof Container 
  100                           && isHierarchyFocused((Container)splitPane.getLeftComponent()) 
  101                           || splitPane.getLeftComponent().isFocusOwner()) {
  102                           
  103                           requestFocusInHierarchy((Container)splitPane.getRightComponent());
  104                       } else {
  105                           requestFocusInHierarchy((Container)splitPane.getLeftComponent());
  106                       }
  107                   } else {
  108                       if (splitPane.getTopComponent() instanceof Container 
  109                           && isHierarchyFocused((Container)splitPane.getLeftComponent()) 
  110                           || splitPane.getTopComponent().isFocusOwner()) {
  111   
  112                           requestFocusInHierarchy((Container)splitPane.getBottomComponent());
  113                       } else {
  114                           requestFocusInHierarchy((Container)splitPane.getTopComponent());
  115                       }
  116                   }
  117               }
  118           };
  119       }
  120       
  121       private static boolean isHierarchyFocused(final Container root) {
  122           if (root.isFocusOwner()) {
  123               return root.isFocusOwner();
  124           }
  125   
  126           for (int i = 0; i < root.getComponentCount(); i++) {
  127               Component child = root.getComponent(i);
  128               if (child instanceof Container) {
  129                   return isHierarchyFocused((Container)child);
  130               } else {
  131                   return child.isFocusOwner();
  132               }
  133           }
  134   
  135           return false;
  136       }
  137   
  138       private static boolean isFocusRequested;
  139       private static void requestFocusInHierarchy(final Container root) {
  140           if (isFocusRequested) {
  141               return;
  142           }
  143           
  144           if (root.getComponentCount() == 0) {
  145               root.requestFocus();
  146               isFocusRequested = true;
  147               
  148               return;
  149           }
  150   
  151           for (int i = 0; i < root.getComponentCount(); i++) {
  152               Component child = root.getComponent(i);
  153               if (!(child instanceof JComponent)) {
  154                   root.requestFocus();
  155                   isFocusRequested = true;
  156                   
  157                   return;
  158               }
  159               if (child instanceof Container) {
  160                   requestFocusInHierarchy((Container)child);
  161                   if (isFocusRequested) {
  162                       return;
  163                   }
  164               }
  165           }
  166       }
  167   
  168       private static Action newStartResizeAction() {
  169           return new AbstractAction() {
  170               public void actionPerformed(final ActionEvent e) {
  171                   JSplitPane splitPane = (JSplitPane)e.getSource();
  172                   splitPane.requestFocus();
  173                   ((BasicSplitPaneUI)splitPane.getUI()).getDivider().repaint();
  174               }
  175           };
  176       }
  177   
  178       private static Action newFocusOutForwardAction() {
  179           return new AbstractAction() {
  180               public void actionPerformed(final ActionEvent e) {
  181                   JSplitPane splitPane = (JSplitPane)e.getSource();
  182                   Container ancestor = splitPane.getFocusCycleRootAncestor();
  183                   if (ancestor == null) {
  184                       return;
  185                   }
  186                   Component rightComponent = splitPane.getComponentOrientation().isLeftToRight()
  187                                                        ? splitPane.getRightComponent()
  188                                                        : splitPane.getLeftComponent();
  189                   Component result = ancestor.getFocusTraversalPolicy().getComponentAfter(ancestor, rightComponent);
  190                   if (result == null) {
  191                       return;
  192                   }
  193   
  194                   result.requestFocus();
  195               }
  196           };
  197       }
  198   
  199       private static Action newFocusOutBackwardAction() {
  200           return new AbstractAction() {
  201               public void actionPerformed(final ActionEvent e) {
  202                   JSplitPane splitPane = (JSplitPane)e.getSource();
  203                   Container ancestor = splitPane.getFocusCycleRootAncestor();
  204                   if (ancestor == null) {
  205                       return;
  206                   }
  207                   Component leftComponent = splitPane.getComponentOrientation().isLeftToRight()
  208                                                        ? splitPane.getLeftComponent()
  209                                                        : splitPane.getRightComponent();
  210                   Component result = ancestor.getFocusTraversalPolicy().getComponentBefore(ancestor, leftComponent);
  211                   if (result == null) {
  212                       return;
  213                   }
  214   
  215                   result.requestFocus();
  216               }
  217           };
  218       }
  219   
  220       public static void installKeyboardActions(final JSplitPane splitPane) {
  221           Set forwardSet = new HashSet();
  222           forwardSet.add(KeyStroke.getKeyStroke("pressed TAB"));
  223           splitPane.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardSet);
  224   
  225           Set backwardSet = new HashSet();
  226           backwardSet.add(KeyStroke.getKeyStroke("shift pressed TAB"));
  227           splitPane.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backwardSet);
  228   
  229           Utilities.installKeyboardActions(splitPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, "SplitPane.ancestorInputMap", null);
  230   
  231           splitPane.getActionMap().put("negativeIncrement", newNegativeIncrementAction());
  232           splitPane.getActionMap().put("positiveIncrement", newPositiveIncrementAction());
  233           splitPane.getActionMap().put("selectMin", newSelectMinAction());
  234           splitPane.getActionMap().put("selectMax", newSelectMaxAction());
  235           splitPane.getActionMap().put("startResize", newStartResizeAction());
  236           splitPane.getActionMap().put("toggleFocus", newToggleFocusAction());
  237           splitPane.getActionMap().put("focusOutForward", newFocusOutForwardAction());
  238           splitPane.getActionMap().put("focusOutBackward", newFocusOutBackwardAction());
  239       }
  240   }

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