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   /**
   19    * @author Anton Avtamonov
   20    */
   21   
   22   package javax.swing.plaf.basic;
   23   
   24   import java.awt.Rectangle;
   25   import java.awt.event.ActionEvent;
   26   
   27   import javax.swing.AbstractAction;
   28   import javax.swing.DefaultListSelectionModel;
   29   import javax.swing.JComponent;
   30   import javax.swing.JList;
   31   import javax.swing.TransferHandler;
   32   
   33   import org.apache.harmony.x.swing.Utilities;
   34   
   35   
   36   final class BasicListKeyboardActions {
   37       private static AbstractAction selectPreviousRowAction = new AbstractAction() {
   38           public void actionPerformed(final ActionEvent e) {
   39               JList list = (JList)e.getSource();
   40   
   41               int previousIndex = getPreviousRow(list);
   42               if (previousIndex == -1) {
   43                   return;
   44               }
   45   
   46               list.setSelectedIndex(previousIndex);
   47               list.ensureIndexIsVisible(previousIndex);
   48           }
   49       };
   50   
   51       private static AbstractAction selectPreviousRowChangeLeadAction = new AbstractAction() {
   52           public void actionPerformed(final ActionEvent e) {
   53               JList list = (JList)e.getSource();
   54               if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
   55                   return;
   56               }
   57   
   58               int previousIndex = getPreviousRow(list);
   59               if (previousIndex == -1) {
   60                   return;
   61               }
   62   
   63               ((DefaultListSelectionModel)list.getSelectionModel()).moveLeadSelectionIndex(previousIndex);
   64               list.ensureIndexIsVisible(previousIndex);
   65           }
   66       };
   67   
   68       private static AbstractAction selectPreviousRowExtendSelectionAction = new AbstractAction() {
   69           public void actionPerformed(final ActionEvent e) {
   70               JList list = (JList)e.getSource();
   71   
   72               int previousIndex = getPreviousRow(list);
   73               if (previousIndex == -1) {
   74                   return;
   75               }
   76   
   77               if (list.isSelectionEmpty()) {
   78                   list.setSelectedIndex(previousIndex);
   79               } else {
   80                   list.setValueIsAdjusting(true);
   81                   list.clearSelection();
   82                   list.addSelectionInterval(list.getAnchorSelectionIndex(), previousIndex);
   83                   list.setValueIsAdjusting(false);
   84                   list.ensureIndexIsVisible(previousIndex);
   85               }
   86           }
   87       };
   88   
   89       private static AbstractAction selectNextRowAction = new AbstractAction() {
   90           public void actionPerformed(final ActionEvent e) {
   91               JList list = (JList)e.getSource();
   92               int nextIndex = getNextRow(list);
   93               if (nextIndex == -1) {
   94                   return;
   95               }
   96   
   97               list.setSelectedIndex(nextIndex);
   98               list.ensureIndexIsVisible(nextIndex);
   99           }
  100       };
  101   
  102       private static AbstractAction selectNextRowChangeLeadAction = new AbstractAction() {
  103           public void actionPerformed(final ActionEvent e) {
  104               JList list = (JList)e.getSource();
  105               if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
  106                   return;
  107               }
  108   
  109               int nextIndex = getNextRow(list);
  110               if (nextIndex == -1) {
  111                   return;
  112               }
  113   
  114               ((DefaultListSelectionModel)list.getSelectionModel()).moveLeadSelectionIndex(nextIndex);
  115               list.ensureIndexIsVisible(nextIndex);
  116           }
  117       };
  118   
  119       private static AbstractAction selectNextRowExtendSelectionAction = new AbstractAction() {
  120           public void actionPerformed(final ActionEvent e) {
  121               JList list = (JList)e.getSource();
  122               int nextIndex = getNextRow(list);
  123               if (nextIndex == -1) {
  124                   return;
  125               }
  126   
  127               if (list.isSelectionEmpty()) {
  128                   list.setSelectedIndex(nextIndex);
  129               } else {
  130                   list.setValueIsAdjusting(true);
  131                   list.clearSelection();
  132                   list.addSelectionInterval(list.getAnchorSelectionIndex(), nextIndex);
  133                   list.setValueIsAdjusting(false);
  134                   list.ensureIndexIsVisible(nextIndex);
  135               }
  136           }
  137       };
  138   
  139       private static AbstractAction selectPreviousColumnAction = new AbstractAction() {
  140           public void actionPerformed(final ActionEvent e) {
  141               JList list = (JList)e.getSource();
  142               int previousIndex = getPreviousColumn(list);
  143               if (previousIndex == -1) {
  144                   return;
  145               }
  146   
  147               list.setSelectedIndex(previousIndex);
  148               list.ensureIndexIsVisible(previousIndex);
  149           }
  150       };
  151   
  152       private static AbstractAction selectPreviousColumnChangeLeadAction = new AbstractAction() {
  153           public void actionPerformed(final ActionEvent e) {
  154               JList list = (JList)e.getSource();
  155               if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
  156                   return;
  157               }
  158   
  159               int previousIndex = getPreviousColumn(list);
  160               if (previousIndex == -1) {
  161                   return;
  162               }
  163   
  164               ((DefaultListSelectionModel)list.getSelectionModel()).moveLeadSelectionIndex(previousIndex);
  165               list.ensureIndexIsVisible(previousIndex);
  166           }
  167       };
  168   
  169       private static AbstractAction selectPreviousColumnExtendSelectionAction = new AbstractAction() {
  170           public void actionPerformed(final ActionEvent e) {
  171               JList list = (JList)e.getSource();
  172   
  173               int previousIndex = getPreviousColumn(list);
  174               if (previousIndex == -1) {
  175                   return;
  176               }
  177   
  178               list.setValueIsAdjusting(true);
  179               list.clearSelection();
  180               list.addSelectionInterval(list.getAnchorSelectionIndex(), previousIndex);
  181               list.setValueIsAdjusting(false);
  182               list.ensureIndexIsVisible(previousIndex);
  183           }
  184       };
  185   
  186       private static AbstractAction selectNextColumnAction = new AbstractAction() {
  187           public void actionPerformed(final ActionEvent e) {
  188               JList list = (JList)e.getSource();
  189   
  190               int nextIndex = getNextColumn(list);
  191               if (nextIndex == -1) {
  192                   return;
  193               }
  194   
  195               list.setSelectedIndex(nextIndex);
  196               list.ensureIndexIsVisible(nextIndex);
  197           }
  198       };
  199   
  200       private static AbstractAction selectNextColumnChangeLeadAction = new AbstractAction() {
  201           public void actionPerformed(final ActionEvent e) {
  202               JList list = (JList)e.getSource();
  203               if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
  204                   return;
  205               }
  206   
  207               int nextIndex = getNextColumn(list);
  208               if (nextIndex == -1) {
  209                   return;
  210               }
  211   
  212               ((DefaultListSelectionModel)list.getSelectionModel()).moveLeadSelectionIndex(nextIndex);
  213               list.ensureIndexIsVisible(nextIndex);
  214           }
  215       };
  216   
  217       private static AbstractAction selectNextColumnExtendSelectionAction = new AbstractAction() {
  218           public void actionPerformed(final ActionEvent e) {
  219               JList list = (JList)e.getSource();
  220   
  221               int nextIndex = getNextColumn(list);
  222               if (nextIndex == -1) {
  223                   return;
  224               }
  225   
  226               list.setValueIsAdjusting(true);
  227               list.clearSelection();
  228               list.addSelectionInterval(list.getAnchorSelectionIndex(), nextIndex);
  229               list.setValueIsAdjusting(false);
  230               list.ensureIndexIsVisible(nextIndex);
  231           }
  232       };
  233   
  234       private static AbstractAction selectFirstRowAction = new AbstractAction() {
  235           public void actionPerformed(final ActionEvent e) {
  236               JList list = (JList)e.getSource();
  237               if (list.getModel().getSize() == 0) {
  238                   return;
  239               }
  240   
  241               int candidateIndex = 0;
  242               BasicListUI ui = (BasicListUI)list.getUI();
  243               if (ui.extendedSupportEnabled) {
  244                   while (candidateIndex < list.getModel().getSize()) {
  245                       if (ui.isChoosable(candidateIndex)) {
  246                           break;
  247                       }
  248                       candidateIndex++;
  249                   }
  250                   if (candidateIndex < list.getModel().getSize()) {
  251                       return;
  252                   }
  253               }
  254               list.setSelectedIndex(candidateIndex);
  255               list.ensureIndexIsVisible(candidateIndex);
  256           }
  257       };
  258   
  259       private static AbstractAction selectFirstRowChangeLeadAction = new AbstractAction() {
  260           public void actionPerformed(final ActionEvent e) {
  261               JList list = (JList)e.getSource();
  262               if (list.getModel().getSize() == 0) {
  263                   return;
  264               }
  265               if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
  266                   return;
  267               }
  268   
  269               ((DefaultListSelectionModel)list.getSelectionModel()).moveLeadSelectionIndex(0);
  270               list.ensureIndexIsVisible(0);
  271           }
  272       };
  273   
  274       private static AbstractAction selectFirstRowExtendSelectionAction = new AbstractAction() {
  275           public void actionPerformed(final ActionEvent e) {
  276               JList list = (JList)e.getSource();
  277               if (list.getModel().getSize() == 0) {
  278                   return;
  279               }
  280   
  281               int beginIndex = list.getAnchorSelectionIndex();
  282               if (beginIndex != -1) {
  283                   list.setValueIsAdjusting(true);
  284                   list.clearSelection();
  285                   list.addSelectionInterval(beginIndex, 0);
  286                   list.setValueIsAdjusting(false);
  287               }
  288   
  289               list.ensureIndexIsVisible(0);
  290           }
  291       };
  292   
  293       private static AbstractAction selectLastRowAction = new AbstractAction() {
  294           public void actionPerformed(final ActionEvent e) {
  295               JList list = (JList)e.getSource();
  296               if (list.getModel().getSize() == 0) {
  297                   return;
  298               }
  299   
  300               int candidateIndex = list.getModel().getSize() - 1;
  301               BasicListUI ui = (BasicListUI)list.getUI();
  302               if (ui.extendedSupportEnabled) {
  303                   while (candidateIndex >= 0) {
  304                       if (ui.isChoosable(candidateIndex)) {
  305                           break;
  306                       }
  307                       candidateIndex--;
  308                   }
  309                   if (candidateIndex == -1) {
  310                       return;
  311                   }
  312               }
  313   
  314               list.setSelectedIndex(candidateIndex);
  315               list.ensureIndexIsVisible(candidateIndex);
  316           }
  317       };
  318   
  319       private static AbstractAction selectLastRowChangeLeadAction = new AbstractAction() {
  320           public void actionPerformed(final ActionEvent e) {
  321               JList list = (JList)e.getSource();
  322               if (list.getModel().getSize() == 0) {
  323                   return;
  324               }
  325               if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
  326                   return;
  327               }
  328   
  329               int lastIndex = list.getModel().getSize() - 1;
  330               ((DefaultListSelectionModel)list.getSelectionModel()).moveLeadSelectionIndex(lastIndex);
  331               list.ensureIndexIsVisible(lastIndex);
  332           }
  333       };
  334   
  335       private static AbstractAction selectLastRowExtendSelectionAction = new AbstractAction() {
  336           public void actionPerformed(final ActionEvent e) {
  337               JList list = (JList)e.getSource();
  338               if (list.getModel().getSize() == 0) {
  339                   return;
  340               }
  341   
  342               int lastIndex = list.getModel().getSize() - 1;
  343               int beginIndex = list.getAnchorSelectionIndex();
  344               if (beginIndex != -1) {
  345                   list.setValueIsAdjusting(true);
  346                   list.clearSelection();
  347                   list.addSelectionInterval(beginIndex, lastIndex);
  348                   list.setValueIsAdjusting(false);
  349               }
  350   
  351               list.ensureIndexIsVisible(lastIndex);
  352           }
  353       };
  354   
  355       private static AbstractAction scrollUpAction = new AbstractAction() {
  356           public void actionPerformed(final ActionEvent e) {
  357               JList list = (JList)e.getSource();
  358   
  359               int upIndex = getScrollUpIndex(list);
  360               if (upIndex == -1) {
  361                   return;
  362               }
  363   
  364               list.setSelectedIndex(upIndex);
  365               list.ensureIndexIsVisible(upIndex);
  366           }
  367       };
  368   
  369       private static AbstractAction scrollUpChangeLeadAction = new AbstractAction() {
  370           public void actionPerformed(final ActionEvent e) {
  371               JList list = (JList)e.getSource();
  372               if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
  373                   return;
  374               }
  375   
  376               int upIndex = getScrollUpIndex(list);
  377               if (upIndex == -1) {
  378                   return;
  379               }
  380   
  381               ((DefaultListSelectionModel)list.getSelectionModel()).moveLeadSelectionIndex(upIndex);
  382               list.ensureIndexIsVisible(upIndex);
  383           }
  384       };
  385   
  386       private static AbstractAction scrollUpExtendSelectionAction = new AbstractAction() {
  387           public void actionPerformed(final ActionEvent e) {
  388               JList list = (JList)e.getSource();
  389   
  390               int upIndex = getScrollUpIndex(list);
  391               if (upIndex == -1) {
  392                   return;
  393               }
  394   
  395               int beginIndex = list.getAnchorSelectionIndex();
  396               if (beginIndex != -1) {
  397                   list.setValueIsAdjusting(true);
  398                   list.clearSelection();
  399                   list.addSelectionInterval(beginIndex, upIndex);
  400                   list.setValueIsAdjusting(false);
  401               }
  402   
  403               list.ensureIndexIsVisible(upIndex);
  404           }
  405       };
  406   
  407       private static AbstractAction scrollDownAction = new AbstractAction() {
  408           public void actionPerformed(final ActionEvent e) {
  409               JList list = (JList)e.getSource();
  410   
  411               int downIndex = getScrollDownIndex(list);
  412               if (downIndex == -1) {
  413                   return;
  414               }
  415   
  416               list.setSelectedIndex(downIndex);
  417               list.ensureIndexIsVisible(downIndex);
  418           }
  419       };
  420   
  421       private static AbstractAction scrollDownChangeLeadAction = new AbstractAction() {
  422           public void actionPerformed(final ActionEvent e) {
  423               JList list = (JList)e.getSource();
  424               if (!(list.getSelectionModel() instanceof DefaultListSelectionModel)) {
  425                   return;
  426               }
  427   
  428               int downIndex = getScrollDownIndex(list);
  429               if (downIndex == -1) {
  430                   return;
  431               }
  432   
  433               ((DefaultListSelectionModel)list.getSelectionModel()).moveLeadSelectionIndex(downIndex);
  434               list.ensureIndexIsVisible(downIndex);
  435           }
  436       };
  437   
  438       private static AbstractAction scrollDownExtendSelectionAction = new AbstractAction() {
  439           public void actionPerformed(final ActionEvent e) {
  440               JList list = (JList)e.getSource();
  441   
  442               int downIndex = getScrollDownIndex(list);
  443               if (downIndex == -1) {
  444                   return;
  445               }
  446   
  447               int beginIndex = list.getAnchorSelectionIndex();
  448               if (beginIndex != -1) {
  449                   list.setValueIsAdjusting(true);
  450                   list.clearSelection();
  451                   list.addSelectionInterval(beginIndex, downIndex);
  452                   list.setValueIsAdjusting(false);
  453               }
  454   
  455               list.ensureIndexIsVisible(downIndex);
  456           }
  457       };
  458   
  459       private static AbstractAction selectAllAction = new AbstractAction() {
  460           public void actionPerformed(final ActionEvent e) {
  461               JList list = (JList)e.getSource();
  462               if (list.getModel().getSize() == 0) {
  463                   return;
  464               }
  465   
  466               list.setSelectionInterval(0, list.getModel().getSize() - 1);
  467           }
  468       };
  469   
  470       private static AbstractAction clearSelectionAction = new AbstractAction() {
  471           public void actionPerformed(final ActionEvent e) {
  472               JList list = (JList)e.getSource();
  473               if (list.getModel().getSize() == 0) {
  474                   return;
  475               }
  476   
  477               list.clearSelection();
  478           }
  479       };
  480   
  481       private static AbstractAction addToSelectionAction = new AbstractAction() {
  482           public void actionPerformed(final ActionEvent e) {
  483               JList list = (JList)e.getSource();
  484   
  485               int addIndex = list.getLeadSelectionIndex();
  486               if (addIndex == -1) {
  487                   return;
  488               }
  489   
  490               list.addSelectionInterval(addIndex, addIndex);
  491           }
  492       };
  493   
  494       private static AbstractAction toggleAndAnchorAction = new AbstractAction() {
  495           public void actionPerformed(final ActionEvent e) {
  496               JList list = (JList)e.getSource();
  497   
  498               int toggleIndex = list.getLeadSelectionIndex();
  499               if (toggleIndex == -1) {
  500                   return;
  501               }
  502   
  503               if (list.isSelectedIndex(toggleIndex)) {
  504                   list.removeSelectionInterval(toggleIndex, toggleIndex);
  505               } else {
  506                   list.addSelectionInterval(toggleIndex, toggleIndex);
  507               }
  508               list.getSelectionModel().setAnchorSelectionIndex(toggleIndex);
  509           }
  510       };
  511   
  512       private static AbstractAction moveSelectionToAction = new AbstractAction() {
  513           public void actionPerformed(final ActionEvent e) {
  514               JList list = (JList)e.getSource();
  515   
  516               int moveIndex = list.getLeadSelectionIndex();
  517               if (moveIndex == -1) {
  518                   return;
  519               }
  520   
  521               list.setSelectedIndex(moveIndex);
  522           }
  523       };
  524   
  525       private static AbstractAction extendToAction = new AbstractAction() {
  526           public void actionPerformed(final ActionEvent e) {
  527               JList list = (JList)e.getSource();
  528               int extendIndex = list.getLeadSelectionIndex();
  529               if (extendIndex == -1) {
  530                   return;
  531               }
  532   
  533               list.setValueIsAdjusting(true);
  534               list.clearSelection();
  535               list.addSelectionInterval(list.getAnchorSelectionIndex(), extendIndex);
  536               list.setValueIsAdjusting(false);
  537           }
  538       };
  539   
  540   
  541       public static void installKeyboardActions(final JList list) {
  542           Utilities.installKeyboardActions(list, JComponent.WHEN_FOCUSED, "List.focusInputMap", "List.focusInputMap.RightToLeft");
  543   
  544           list.getActionMap().put("selectPreviousRow", selectPreviousRowAction);
  545           list.getActionMap().put("selectNextRow", selectNextRowAction);
  546           list.getActionMap().put("selectPreviousRowExtendSelection", selectPreviousRowExtendSelectionAction);
  547           list.getActionMap().put("selectNextRowExtendSelection", selectNextRowExtendSelectionAction);
  548   
  549           list.getActionMap().put("selectPreviousRowChangeLead", selectPreviousRowChangeLeadAction);
  550           list.getActionMap().put("selectNextRowChangeLead", selectNextRowChangeLeadAction);
  551           list.getActionMap().put("selectPreviousColumnChangeLead", selectPreviousColumnChangeLeadAction);
  552           list.getActionMap().put("selectNextColumnChangeLead", selectNextColumnChangeLeadAction);
  553   
  554           list.getActionMap().put("selectPreviousColumn", selectPreviousColumnAction);
  555           list.getActionMap().put("selectNextColumn", selectNextColumnAction);
  556           list.getActionMap().put("selectPreviousColumnExtendSelection", selectPreviousColumnExtendSelectionAction);
  557           list.getActionMap().put("selectNextColumnExtendSelection", selectNextColumnExtendSelectionAction);
  558   
  559           list.getActionMap().put("selectFirstRow", selectFirstRowAction);
  560           list.getActionMap().put("selectFirstRowExtendSelection", selectFirstRowExtendSelectionAction);
  561           list.getActionMap().put("selectLastRow", selectLastRowAction);
  562           list.getActionMap().put("selectLastRowExtendSelection", selectLastRowExtendSelectionAction);
  563   
  564           list.getActionMap().put("selectLastRowChangeLead", selectLastRowChangeLeadAction);
  565           list.getActionMap().put("selectFirstRowChangeLead", selectFirstRowChangeLeadAction);
  566   
  567           list.getActionMap().put("scrollUp", scrollUpAction);
  568           list.getActionMap().put("scrollUpExtendSelection", scrollUpExtendSelectionAction);
  569           list.getActionMap().put("scrollDown", scrollDownAction);
  570           list.getActionMap().put("scrollDownExtendSelection", scrollDownExtendSelectionAction);
  571   
  572           list.getActionMap().put("scrollUpChangeLead", scrollUpChangeLeadAction);
  573           list.getActionMap().put("scrollDownChangeLead", scrollDownChangeLeadAction);
  574   
  575           list.getActionMap().put("selectAll", selectAllAction);
  576           list.getActionMap().put("clearSelection", clearSelectionAction);
  577   
  578           list.getActionMap().put("addToSelection", addToSelectionAction);
  579           list.getActionMap().put("toggleAndAnchor", toggleAndAnchorAction);
  580           list.getActionMap().put("moveSelectionTo", moveSelectionToAction);
  581           list.getActionMap().put("extendTo", extendToAction);
  582   
  583           list.getActionMap().put("copy", TransferHandler.getCopyAction());
  584           list.getActionMap().put("paste", TransferHandler.getPasteAction());
  585           list.getActionMap().put("cut", TransferHandler.getCutAction());
  586       }
  587   
  588       public static void uninstallKeyboardActions(final JList list) {
  589           Utilities.uninstallKeyboardActions(list, JComponent.WHEN_FOCUSED);
  590       }
  591   
  592   
  593       private static int getNextRow(final JList list) {
  594           if (list.getModel().getSize() == 0) {
  595               return -1;
  596           }
  597   
  598           BasicListUI ui = (BasicListUI)list.getUI();
  599   
  600   
  601           int currectSelection = list.getLeadSelectionIndex();
  602           if (currectSelection == -1 || currectSelection >= list.getModel().getSize()) {
  603               currectSelection = 0;
  604               if (!ui.extendedSupportEnabled || ui.isChoosable(currectSelection)) {
  605                   return currectSelection;
  606               }
  607           }
  608   
  609           ui.maybeUpdateLayoutState();
  610           BasicListUI.LayoutStrategy strategy = ui.layouter.getLayoutStrategy();
  611   
  612           int candidateIndex = currectSelection;
  613           while(candidateIndex < list.getModel().getSize()) {
  614               int selectedRow = strategy.getRow(candidateIndex);
  615               int selectedColumn = strategy.getColumn(candidateIndex);
  616               if (selectedRow < strategy.getRowCount() - 1 && strategy.getIndex(selectedRow + 1, selectedColumn) < list.getModel().getSize()) {
  617                   candidateIndex = strategy.getIndex(selectedRow + 1, selectedColumn);
  618               } else if (list.getLayoutOrientation() == JList.VERTICAL_WRAP && selectedColumn < strategy.getColumnCount() - 1 && strategy.getIndex(0, selectedColumn + 1) < list.getModel().getSize()) {
  619                   candidateIndex = strategy.getIndex(0, selectedColumn + 1);
  620               } else {
  621                   return -1;
  622               }
  623   
  624               if (ui.isChoosable(candidateIndex)) {
  625                   return candidateIndex;
  626               }
  627           }
  628           
  629           return -1;
  630       }
  631   
  632       private static int getPreviousRow(final JList list) {
  633           if (list.getModel().getSize() == 0) {
  634               return -1;
  635           }
  636   
  637           BasicListUI ui = (BasicListUI)list.getUI();
  638           int currectSelection = list.getLeadSelectionIndex();
  639           if (currectSelection == -1 || currectSelection >= list.getModel().getSize()) {
  640               currectSelection = list.getModel().getSize() - 1;
  641               if (!ui.extendedSupportEnabled || ui.isChoosable(currectSelection)) {
  642                   return currectSelection;
  643               }
  644           }
  645   
  646           ui.maybeUpdateLayoutState();
  647           BasicListUI.LayoutStrategy strategy = ui.layouter.getLayoutStrategy();
  648   
  649           int candidateIndex = currectSelection;
  650           while(candidateIndex >= 0) {
  651               int selectedRow = strategy.getRow(candidateIndex);
  652               int selectedColumn = strategy.getColumn(candidateIndex);
  653   
  654               if (selectedRow > 0) {
  655                   candidateIndex = strategy.getIndex(selectedRow - 1, selectedColumn);
  656               } else if (list.getLayoutOrientation() == JList.VERTICAL_WRAP && selectedColumn > 0) {
  657                   candidateIndex = strategy.getIndex(strategy.getRowCount() - 1, selectedColumn - 1);
  658               } else {
  659                   return -1;
  660               }
  661   
  662               if (ui.isChoosable(candidateIndex)) {
  663                   return candidateIndex;
  664               }
  665           }
  666   
  667           return -1;
  668       }
  669   
  670       private static int getNextColumn(final JList list) {
  671           if (list.getModel().getSize() == 0) {
  672               return -1;
  673           }
  674   
  675           int currectSelection = list.getLeadSelectionIndex();
  676           if (currectSelection == -1 || currectSelection >= list.getModel().getSize()) {
  677               list.setSelectedIndex(0);
  678               return -1;
  679           }
  680   
  681           BasicListUI ui = (BasicListUI)list.getUI();
  682           ui.maybeUpdateLayoutState();
  683           BasicListUI.LayoutStrategy strategy = ui.layouter.getLayoutStrategy();
  684   
  685           int candidateIndex = currectSelection;
  686           while(candidateIndex < list.getModel().getSize()) {
  687               int selectedRow = strategy.getRow(candidateIndex);
  688               int selectedColumn = strategy.getColumn(candidateIndex);
  689               if (selectedColumn < strategy.getColumnCount() - 1) {
  690                   if (strategy.getIndex(selectedRow, selectedColumn + 1) < list.getModel().getSize()) {
  691                       candidateIndex = strategy.getIndex(selectedRow, selectedColumn + 1);
  692                   } else {
  693                       candidateIndex = list.getModel().getSize() - 1;
  694                   }
  695               } else if (list.getLayoutOrientation() == JList.VERTICAL_WRAP && selectedRow < strategy.getRowCount() - 1 && strategy.getIndex(selectedRow + 1, selectedColumn) < list.getModel().getSize()) {
  696                   return strategy.getIndex(selectedRow + 1, selectedColumn);
  697               } else {
  698                   return -1;
  699               }
  700               
  701               if (ui.isChoosable(candidateIndex)) {
  702                   return candidateIndex;
  703               }
  704           }
  705   
  706           return -1;
  707       }
  708   
  709       private static int getPreviousColumn(final JList list) {
  710           if (list.getModel().getSize() == 0) {
  711               return -1;
  712           }
  713   
  714           int currectSelection = list.getLeadSelectionIndex();
  715           if (currectSelection == -1 || currectSelection >= list.getModel().getSize()) {
  716               list.setSelectedIndex(list.getModel().getSize() - 1);
  717               return -1;
  718           }
  719   
  720           BasicListUI ui = (BasicListUI)list.getUI();
  721           ui.maybeUpdateLayoutState();
  722           BasicListUI.LayoutStrategy strategy = ui.layouter.getLayoutStrategy();
  723   
  724           int candidateIndex = currectSelection;
  725           while(candidateIndex >= 0) {
  726               int selectedRow = strategy.getRow(candidateIndex);
  727               int selectedColumn = strategy.getColumn(candidateIndex);
  728               if (selectedColumn > 0) {
  729                   candidateIndex = strategy.getIndex(selectedRow, selectedColumn - 1);
  730               } else if (list.getLayoutOrientation() == JList.VERTICAL_WRAP && selectedRow > 0) {
  731                   candidateIndex = strategy.getIndex(selectedRow - 1, selectedColumn);
  732               } else {
  733                   return -1;
  734               }
  735   
  736               if (ui.isChoosable(candidateIndex)) {
  737                   return candidateIndex;
  738                }
  739           }
  740   
  741           return -1;
  742       }
  743   
  744       private static int getScrollDownIndex(final JList list) {
  745           if (list.getModel().getSize() == 0) {
  746               return -1;
  747           }
  748   
  749           int candidateIndex;
  750           int currentSelection = list.getLeadSelectionIndex();
  751           int lastVisible = list.getLastVisibleIndex();
  752           if (lastVisible != currentSelection || lastVisible == list.getModel().getSize() - 1) {
  753               candidateIndex = lastVisible;
  754           } else {
  755               Rectangle visibleRect = list.getVisibleRect();
  756               int i = lastVisible + 1;
  757               while (i < list.getModel().getSize() - 1 && list.getCellBounds(lastVisible + 1, i).height < visibleRect.height) {
  758                   i++;
  759               }
  760   
  761               candidateIndex = i;
  762           }
  763   
  764           BasicListUI ui = (BasicListUI)list.getUI();
  765           if (!ui.extendedSupportEnabled) {
  766               return candidateIndex;
  767           }
  768           for (int i = candidateIndex; i < list.getModel().getSize(); i++) {
  769               if (ui.isChoosable(i)) {
  770                   return i;
  771               }
  772           }
  773           for (int i = candidateIndex; i > currentSelection; i--) {
  774               if (ui.isChoosable(i)) {
  775                   return i;
  776               }
  777           }
  778   
  779           return -1;
  780       }
  781   
  782       private static int getScrollUpIndex(final JList list) {
  783           if (list.getModel().getSize() == 0) {
  784               return -1;
  785           }
  786   
  787           int candidateIndex;
  788           int currentSelection = list.getLeadSelectionIndex();
  789   
  790           int firstVisible = list.getFirstVisibleIndex();
  791           if (firstVisible != currentSelection || firstVisible == 0) {
  792               candidateIndex = firstVisible;
  793           } else {
  794               Rectangle visibleRect = list.getVisibleRect();
  795               int i = firstVisible - 1;
  796               while (i > 0 && list.getCellBounds(firstVisible - 1, i).height < visibleRect.height) {
  797                   i--;
  798               }
  799   
  800               candidateIndex = i;
  801           }
  802           
  803           BasicListUI ui = (BasicListUI)list.getUI();
  804           if (!ui.extendedSupportEnabled) {
  805               return candidateIndex;
  806           }
  807           for (int i = candidateIndex; i >= 0; i--) {
  808               if (ui.isChoosable(i)) {
  809                   return i;
  810               }
  811           }
  812           for (int i = candidateIndex; i < currentSelection; i++) {
  813               if (ui.isChoosable(i)) {
  814                   return i;
  815               }
  816           }
  817   
  818           return -1;
  819       }
  820   }

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