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 Anton Avtamonov
   19    */
   20   package javax.swing.plaf.basic;
   21   
   22   import java.awt.Rectangle;
   23   import java.awt.event.ActionEvent;
   24   
   25   import javax.swing.AbstractAction;
   26   import javax.swing.JComponent;
   27   import javax.swing.JTree;
   28   import javax.swing.SwingConstants;
   29   import javax.swing.TransferHandler;
   30   import javax.swing.tree.TreePath;
   31   
   32   import org.apache.harmony.x.swing.Utilities;
   33   
   34   
   35   final class BasicTreeKeyboardActions {
   36       private static abstract class TreeAction extends AbstractAction {
   37           public void actionPerformed(final ActionEvent e) {
   38               JTree tree = (JTree)e.getSource();
   39               if (tree.getRowCount() == 0) {
   40                   return;
   41               }
   42   
   43               actionPerformed(tree);
   44           }
   45   
   46           public abstract void actionPerformed(JTree tree);
   47   
   48           protected TreePath getPreviousPath(final JTree tree, final int currentRow) {
   49               if (currentRow == -1) {
   50                   return tree.getPathForRow(tree.getRowCount() - 1);
   51               }
   52               return currentRow > 0 ? tree.getPathForRow(currentRow - 1)
   53                                     : tree.getPathForRow(0);
   54           }
   55   
   56           protected TreePath getNextPath(final JTree tree, final int currentRow) {
   57               if (currentRow == -1) {
   58                   return tree.getPathForRow(0);
   59               }
   60               return currentRow < tree.getRowCount() - 1 ? tree.getPathForRow(currentRow + 1)
   61                                                          : tree.getPathForRow(tree.getRowCount() - 1);
   62           }
   63   
   64           protected void extendSelection(final JTree tree, final TreePath toPath) {
   65               TreePath anchorPath = tree.getAnchorSelectionPath();
   66               if (anchorPath == null) {
   67                   tree.setSelectionPath(toPath);
   68               } else {
   69                   int anchorRow = tree.getRowForPath(anchorPath);
   70                   int leadRow = tree.getRowForPath(toPath);
   71                   tree.setSelectionInterval(anchorRow, leadRow);
   72                   tree.setAnchorSelectionPath(anchorPath);
   73                   tree.setLeadSelectionPath(toPath);
   74               }
   75               tree.scrollPathToVisible(toPath);
   76           }
   77   
   78           protected boolean isLeaf(final JTree tree, final TreePath path) {
   79               return tree.getModel().isLeaf(path.getLastPathComponent());
   80           }
   81   
   82           protected TreePath getLastPath(final JTree tree) {
   83               return tree.getPathForRow(tree.getRowCount() - 1);
   84           }
   85   
   86           protected TreePath getUpPath(final JTree tree) {
   87               Rectangle visibleRect = tree.getVisibleRect();
   88               TreePath result = tree.getClosestPathForLocation(visibleRect.x, visibleRect.y + 1);
   89               TreePath leadPath = tree.getLeadSelectionPath();
   90               if (result.equals(leadPath)) {
   91                   result = tree.getClosestPathForLocation(visibleRect.x, visibleRect.y - visibleRect.height);
   92               }
   93   
   94               return result;
   95           }
   96   
   97           protected TreePath getDownPath(final JTree tree) {
   98               Rectangle visibleRect = tree.getVisibleRect();
   99               TreePath result = tree.getClosestPathForLocation(visibleRect.x, visibleRect.y + visibleRect.height - 1);
  100               TreePath leadPath = tree.getLeadSelectionPath();
  101               if (result.equals(leadPath)) {
  102                   result = tree.getClosestPathForLocation(visibleRect.x, visibleRect.y + 2 * visibleRect.height);
  103               }
  104   
  105               return result;
  106           }
  107       }
  108   
  109       private static abstract class ChangeLeadAction extends TreeAction {
  110           public void actionPerformed(final JTree tree) {
  111               changeLeadActionPerformed(tree, getNewLeadPath(tree));
  112           }
  113   
  114           protected void changeLeadActionPerformed(final JTree tree, final TreePath newLeadPath) {
  115               TreePath oldLeadPath = tree.getLeadSelectionPath();
  116               tree.setLeadSelectionPath(newLeadPath);
  117               tree.scrollPathToVisible(newLeadPath);
  118               tree.repaint(tree.getPathBounds(oldLeadPath));
  119               tree.repaint(tree.getPathBounds(newLeadPath));
  120           }
  121   
  122           protected abstract TreePath getNewLeadPath(JTree tree);
  123       }
  124   
  125       private static abstract class PreserveLeadAnchorAction extends TreeAction {
  126           public void actionPerformed(final JTree tree) {
  127               TreePath leadPath = tree.getLeadSelectionPath();
  128               TreePath anchorPath = tree.getAnchorSelectionPath();
  129               preserveActionPerformed(tree);
  130               if (leadPath != null) {
  131                   tree.setLeadSelectionPath(leadPath);
  132               }
  133               if (anchorPath != null) {
  134                   tree.setAnchorSelectionPath(anchorPath);
  135               }
  136           }
  137   
  138           protected abstract void preserveActionPerformed(JTree tree);
  139       }
  140   
  141   
  142       private static AbstractAction startEditingAction = new TreeAction() {
  143           public void actionPerformed(final JTree tree) {
  144               TreePath path = tree.getLeadSelectionPath();
  145               if (path == null) {
  146                   return;
  147               }
  148   
  149               tree.startEditingAtPath(path);
  150           }
  151       };
  152       static AbstractAction cancelAction = new AbstractAction() {
  153           public void actionPerformed(final ActionEvent e) {
  154               JTree tree = (JTree)e.getSource();
  155               tree.cancelEditing();
  156           }
  157       };
  158       static AbstractAction selectPreviousAction = new TreeAction() {
  159           public void actionPerformed(final JTree tree) {
  160               int leadRow = tree.getLeadSelectionRow();
  161               TreePath prevPath = getPreviousPath(tree, leadRow);
  162               tree.setSelectionPath(prevPath);
  163               tree.scrollPathToVisible(prevPath);
  164           }
  165       };
  166       private static AbstractAction selectPreviousExtendSelectionAction = new TreeAction() {
  167           public void actionPerformed(final JTree tree) {
  168               TreePath prevPath = getPreviousPath(tree, tree.getLeadSelectionRow());
  169               extendSelection(tree, prevPath);
  170           }
  171       };
  172       static AbstractAction selectNextAction = new TreeAction() {
  173           public void actionPerformed(final JTree tree) {
  174               int leadRow = tree.getLeadSelectionRow();
  175               TreePath nextPath = getNextPath(tree, leadRow);
  176               tree.setSelectionPath(nextPath);
  177               tree.scrollPathToVisible(nextPath);
  178           }
  179       };
  180       private static AbstractAction selectNextExtendSelectionAction = new TreeAction() {
  181           public void actionPerformed(final JTree tree) {
  182               TreePath nextPath = getNextPath(tree, tree.getLeadSelectionRow());
  183               extendSelection(tree, nextPath);
  184           }
  185       };
  186       static AbstractAction selectChildAction = new TreeAction() {
  187           public void actionPerformed(final JTree tree) {
  188               TreePath leadPath = tree.getLeadSelectionPath();
  189               if (leadPath == null) {
  190                   tree.setSelectionRow(0);
  191                   tree.scrollRowToVisible(0);
  192                   return;
  193               }
  194   
  195               if (!isLeaf(tree, leadPath) && !tree.isExpanded(leadPath)) {
  196                   tree.expandPath(leadPath);
  197                   return;
  198               }
  199   
  200               TreePath nextPath = getNextPath(tree, tree.getLeadSelectionRow());
  201               tree.setSelectionPath(nextPath);
  202               tree.scrollPathToVisible(nextPath);
  203           }
  204       };
  205       static AbstractAction selectParentAction = new TreeAction() {
  206           public void actionPerformed(final JTree tree) {
  207               TreePath leadPath = tree.getLeadSelectionPath();
  208               if (leadPath == null) {
  209                   TreePath lastPath = getLastPath(tree);
  210                   tree.setSelectionPath(lastPath);
  211                   tree.scrollPathToVisible(lastPath);
  212                   return;
  213               }
  214   
  215               if (!isLeaf(tree, leadPath) && tree.isExpanded(leadPath)) {
  216                   tree.collapsePath(leadPath);
  217                   return;
  218               }
  219   
  220               TreePath prevPath = leadPath.getParentPath();
  221               if (!tree.isVisible(prevPath)) {
  222                   return;
  223               }
  224               tree.setSelectionPath(prevPath);
  225               tree.scrollPathToVisible(prevPath);
  226           }
  227       };
  228       static AbstractAction selectFirstAction = new TreeAction() {
  229           public void actionPerformed(final JTree tree) {
  230               tree.setSelectionPath(tree.getPathForRow(0));
  231           }
  232       };
  233       private static AbstractAction selectFirstExtendSelectionAction = new TreeAction() {
  234           public void actionPerformed(final JTree tree) {
  235               TreePath firstPath = tree.getPathForRow(0);
  236               extendSelection(tree, firstPath);
  237           }
  238       };
  239       static AbstractAction selectLastAction = new TreeAction() {
  240           public void actionPerformed(final JTree tree) {
  241               tree.setSelectionPath(getLastPath(tree));
  242           }
  243       };
  244       private static AbstractAction selectLastExtendSelectionAction = new TreeAction() {
  245           public void actionPerformed(final JTree tree) {
  246               extendSelection(tree, getLastPath(tree));
  247           }
  248       };
  249       private static AbstractAction selectAllAction = new PreserveLeadAnchorAction() {
  250           public void preserveActionPerformed(final JTree tree) {
  251               tree.setSelectionInterval(0, tree.getRowCount() - 1);
  252           }
  253       };
  254       private static AbstractAction clearSelectionAction = new PreserveLeadAnchorAction() {
  255           public void preserveActionPerformed(final JTree tree) {
  256               tree.clearSelection();
  257           }
  258       };
  259       private static AbstractAction toggleSelectionPreserveAnchorAction = new PreserveLeadAnchorAction() {
  260           public void preserveActionPerformed(final JTree tree) {
  261               TreePath leadPath = tree.getLeadSelectionPath();
  262               if (leadPath == null) {
  263                   return;
  264               }
  265   
  266               if (tree.isPathSelected(leadPath)) {
  267                   tree.removeSelectionPath(leadPath);
  268               } else {
  269                   tree.addSelectionPath(leadPath);
  270               }
  271           }
  272       };
  273       private static AbstractAction moveSelectionToAction = new TreeAction() {
  274           public void actionPerformed(final JTree tree) {
  275               TreePath leadPath = tree.getLeadSelectionPath();
  276               if (leadPath == null) {
  277                   return;
  278               }
  279               tree.setSelectionPath(leadPath);
  280           }
  281       };
  282       private static AbstractAction extendSelectionAction = new TreeAction() {
  283           public void actionPerformed(final JTree tree) {
  284               TreePath leadPath = tree.getLeadSelectionPath();
  285               if (leadPath == null) {
  286                   return;
  287               }
  288               extendSelection(tree, leadPath);
  289           }
  290       };
  291       private static AbstractAction selectFirstChangeLeadAction = new ChangeLeadAction() {
  292           protected TreePath getNewLeadPath(final JTree tree) {
  293               return tree.getPathForRow(0);
  294           }
  295       };
  296       private static AbstractAction selectLastChangeLeadAction = new ChangeLeadAction() {
  297           protected TreePath getNewLeadPath(final JTree tree) {
  298               return getLastPath(tree);
  299           }
  300       };
  301       private static AbstractAction selectPreviousChangeLeadAction = new ChangeLeadAction() {
  302           protected TreePath getNewLeadPath(final JTree tree) {
  303               return getPreviousPath(tree, tree.getLeadSelectionRow());
  304           }
  305       };
  306       private static AbstractAction selectNextChangeLeadAction = new ChangeLeadAction() {
  307           protected TreePath getNewLeadPath(final JTree tree) {
  308               return getNextPath(tree, tree.getLeadSelectionRow());
  309           }
  310       };
  311   
  312       static AbstractAction scrollUpChangeSelectionAction = new TreeAction() {
  313           public void actionPerformed(final JTree tree) {
  314               TreePath upPath = getUpPath(tree);
  315               tree.setSelectionPath(upPath);
  316               tree.scrollPathToVisible(upPath);
  317           }
  318       };
  319       private static AbstractAction scrollUpExtendSelectionAction = new TreeAction() {
  320           public void actionPerformed(final JTree tree) {
  321               TreePath upPath = getUpPath(tree);
  322               extendSelection(tree, upPath);
  323               tree.scrollPathToVisible(upPath);
  324           }
  325       };
  326       private static AbstractAction scrollUpChangeLeadAction = new ChangeLeadAction() {
  327           protected TreePath getNewLeadPath(final JTree tree) {
  328               return getUpPath(tree);
  329           }
  330       };
  331       static AbstractAction scrollDownChangeSelectionAction = new TreeAction() {
  332           public void actionPerformed(final JTree tree) {
  333               TreePath downPath = getDownPath(tree);
  334               tree.setSelectionPath(downPath);
  335               tree.scrollPathToVisible(downPath);
  336           }
  337       };
  338       private static AbstractAction scrollDownExtendSelectionAction = new TreeAction() {
  339           public void actionPerformed(final JTree tree) {
  340               TreePath downPath = getDownPath(tree);
  341               extendSelection(tree, downPath);
  342               tree.scrollPathToVisible(downPath);
  343           }
  344       };
  345       private static AbstractAction scrollDownChangeLeadAction = new ChangeLeadAction() {
  346           protected TreePath getNewLeadPath(final JTree tree) {
  347               return getDownPath(tree);
  348           }
  349       };
  350       private static AbstractAction scrollLeftAction = new TreeAction() {
  351           public void actionPerformed(final JTree tree) {
  352               Rectangle visibleRect = tree.getVisibleRect();
  353               int increment = tree.getScrollableUnitIncrement(visibleRect, SwingConstants.HORIZONTAL, -1);
  354               visibleRect.translate(-increment, 0);
  355               tree.scrollRectToVisible(visibleRect);
  356           }
  357       };
  358       private static AbstractAction scrollRightAction = new TreeAction() {
  359           public void actionPerformed(final JTree tree) {
  360               Rectangle visibleRect = tree.getVisibleRect();
  361               int increment = tree.getScrollableUnitIncrement(visibleRect, SwingConstants.HORIZONTAL, 1);
  362               visibleRect.translate(increment, 0);
  363               tree.scrollRectToVisible(visibleRect);
  364           }
  365       };
  366       private static AbstractAction expandAction = new TreeAction() {
  367           public void actionPerformed(final JTree tree) {
  368               int leadRow = tree.getLeadSelectionRow();
  369               tree.expandRow(leadRow);
  370           }
  371       };
  372       private static AbstractAction collapseAction = new TreeAction() {
  373           public void actionPerformed(final JTree tree) {
  374               int leadRow = tree.getLeadSelectionRow();
  375               tree.collapseRow(leadRow);
  376           }
  377       };
  378   
  379   
  380       public static void installKeyboardActions(final JTree tree) {
  381           Utilities.installKeyboardActions(tree, JComponent.WHEN_FOCUSED, "Tree.focusInputMap", "Tree.focusInputMap.RightToLeft");
  382           Utilities.installKeyboardActions(tree, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, "Tree.ancestorInputMap", null);
  383   
  384           tree.getActionMap().put("copy", TransferHandler.getCopyAction());
  385           tree.getActionMap().put("paste", TransferHandler.getPasteAction());
  386           tree.getActionMap().put("cut", TransferHandler.getCutAction());
  387   
  388           tree.getActionMap().put("selectPrevious", selectPreviousAction);
  389           tree.getActionMap().put("selectPreviousExtendSelection", selectPreviousExtendSelectionAction);
  390           tree.getActionMap().put("selectNext", selectNextAction);
  391           tree.getActionMap().put("selectNextExtendSelection", selectNextExtendSelectionAction);
  392           tree.getActionMap().put("selectChild", selectChildAction);
  393           tree.getActionMap().put("selectParent", selectParentAction);
  394   
  395           tree.getActionMap().put("selectFirst", selectFirstAction);
  396           tree.getActionMap().put("selectFirstExtendSelection", selectFirstExtendSelectionAction);
  397           tree.getActionMap().put("selectLast", selectLastAction);
  398           tree.getActionMap().put("selectLastExtendSelection", selectLastExtendSelectionAction);
  399   
  400           tree.getActionMap().put("selectAll", selectAllAction);
  401           tree.getActionMap().put("clearSelection", clearSelectionAction);
  402   
  403           tree.getActionMap().put("toggleSelectionPreserveAnchor", toggleSelectionPreserveAnchorAction);
  404           tree.getActionMap().put("extendSelection", extendSelectionAction);
  405           tree.getActionMap().put("moveSelectionTo", moveSelectionToAction);
  406   
  407           tree.getActionMap().put("selectFirstChangeLead", selectFirstChangeLeadAction);
  408           tree.getActionMap().put("selectLastChangeLead", selectLastChangeLeadAction);
  409           tree.getActionMap().put("selectPreviousChangeLead", selectPreviousChangeLeadAction);
  410           tree.getActionMap().put("selectNextChangeLead", selectNextChangeLeadAction);
  411   
  412           tree.getActionMap().put("scrollUpChangeSelection", scrollUpChangeSelectionAction);
  413           tree.getActionMap().put("scrollUpExtendSelection", scrollUpExtendSelectionAction);
  414           tree.getActionMap().put("scrollUpChangeLead", scrollUpChangeLeadAction);
  415           tree.getActionMap().put("scrollDownChangeSelection", scrollDownChangeSelectionAction);
  416           tree.getActionMap().put("scrollDownExtendSelection", scrollDownExtendSelectionAction);
  417           tree.getActionMap().put("scrollDownChangeLead", scrollDownChangeLeadAction);
  418   
  419           tree.getActionMap().put("scrollLeft", scrollLeftAction);
  420           tree.getActionMap().put("scrollRight", scrollRightAction);
  421   
  422           tree.getActionMap().put("startEditing", startEditingAction);
  423           tree.getActionMap().put("cancel", cancelAction);
  424   
  425           tree.getActionMap().put("expand", expandAction);
  426           tree.getActionMap().put("collapse", collapseAction);
  427       }
  428   
  429       public static void uninstallKeyboardActions(final JTree tree) {
  430           Utilities.uninstallKeyboardActions(tree, JComponent.WHEN_FOCUSED);
  431           Utilities.uninstallKeyboardActions(tree, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  432       }
  433   }

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