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.DefaultListSelectionModel;
   27   import javax.swing.JComponent;
   28   import javax.swing.JTable;
   29   import javax.swing.ListSelectionModel;
   30   import javax.swing.TransferHandler;
   31   
   32   import org.apache.harmony.x.swing.Utilities;
   33   
   34   
   35   final class BasicTableKeyboardActions {
   36       private static abstract class AbstractTableAction extends AbstractAction {
   37           public void actionPerformed(final ActionEvent e) {
   38               JTable table = (JTable)e.getSource();
   39               if (table.getRowCount() == 0 || table.getColumnCount() == 0) {
   40                   return;
   41               }
   42   
   43               processTable(table);
   44           }
   45   
   46           protected abstract void processTable(final JTable table);
   47       }
   48   
   49       private static abstract class AbstractRowColumnAction extends AbstractTableAction {
   50           protected abstract void processRowColumn(final JTable table, final int currentRow, final int currentColumn);
   51       }
   52   
   53       private static abstract class LeadRowColumnAction extends AbstractRowColumnAction {
   54           protected void processTable(final JTable table) {
   55               if (!stopEditing(table)) {
   56                   return;
   57               }
   58               int currentRow = table.getSelectionModel().getLeadSelectionIndex();
   59               int currentColumn = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
   60               processRowColumn(table, currentRow, currentColumn);
   61           }
   62   
   63           private boolean stopEditing(final JTable table) {
   64               if (table.isEditing()) {
   65                   table.getCellEditor().stopCellEditing();
   66                   if (table.isEditing()) {
   67                       return false;
   68                   }
   69               }
   70   
   71               return true;
   72           }
   73       }
   74   
   75       private static abstract class EnablebableLeadRowColumnAction extends LeadRowColumnAction {
   76           final JTable table;
   77   
   78           public EnablebableLeadRowColumnAction(final JTable table) {
   79               this.table = table;
   80           }
   81   
   82           public boolean isEnabled() {
   83               return isEnabled(table, table.getSelectionModel().getLeadSelectionIndex(),
   84                                table.getColumnModel().getSelectionModel().getLeadSelectionIndex());
   85           }
   86   
   87           public abstract boolean isEnabled(final JTable table, final int currentRow, final int currentColumn);
   88       }
   89   
   90       private static AbstractAction selectNextColumnAction = new LeadRowColumnAction() {
   91           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
   92               if (currentColumn + 1 < table.getColumnCount()) {
   93                   table.changeSelection(currentRow, currentColumn + 1, false, false);
   94               }
   95           }
   96       };
   97       private static AbstractAction selectNextColumnChangeLeadAction = new LeadRowColumnAction() {
   98           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
   99               if (currentColumn + 1 < table.getColumnCount() && (table.getColumnModel().getSelectionModel() instanceof DefaultListSelectionModel)) {
  100                   ((DefaultListSelectionModel)table.getColumnModel().getSelectionModel()).moveLeadSelectionIndex(currentColumn + 1);
  101                   ensureCellIsVisible(table, currentRow, currentColumn + 1);
  102               }
  103           }
  104       };
  105       private static AbstractAction selectPreviousColumnAction = new LeadRowColumnAction() {
  106           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  107               if (currentColumn > 0) {
  108                   table.changeSelection(currentRow, currentColumn - 1, false, false);
  109               }
  110           }
  111       };
  112       private static AbstractAction selectPreviousColumnChangeLeadAction = new LeadRowColumnAction() {
  113           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  114               if (currentColumn > 0 && (table.getColumnModel().getSelectionModel() instanceof DefaultListSelectionModel)) {
  115                   ((DefaultListSelectionModel)table.getColumnModel().getSelectionModel()).moveLeadSelectionIndex(currentColumn - 1);
  116                   ensureCellIsVisible(table, currentRow, currentColumn - 1);
  117               }
  118           }
  119       };
  120       private static AbstractAction selectNextRowAction = new LeadRowColumnAction() {
  121           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  122               if (currentRow + 1 < table.getRowCount()) {
  123                   table.changeSelection(currentRow + 1, currentColumn, false, false);
  124               }
  125           }
  126       };
  127       private static AbstractAction selectNextRowChangeLeadAction = new LeadRowColumnAction() {
  128           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  129               if (currentRow + 1 < table.getRowCount() && (table.getSelectionModel() instanceof DefaultListSelectionModel)) {
  130                   ((DefaultListSelectionModel)table.getSelectionModel()).moveLeadSelectionIndex(currentRow + 1);
  131                   ensureCellIsVisible(table, currentRow + 1, currentColumn);
  132               }
  133           }
  134       };
  135       private static AbstractAction selectPreviousRowAction = new LeadRowColumnAction() {
  136           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  137               if (currentRow > 0) {
  138                   table.changeSelection(currentRow - 1, currentColumn, false, false);
  139               }
  140           }
  141       };
  142       private static AbstractAction selectPreviousRowChangeLeadAction = new LeadRowColumnAction() {
  143           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  144               if (currentRow > 0 && (table.getSelectionModel() instanceof DefaultListSelectionModel)) {
  145                   ((DefaultListSelectionModel)table.getSelectionModel()).moveLeadSelectionIndex(currentRow - 1);
  146                   ensureCellIsVisible(table, currentRow - 1, currentColumn);
  147               }
  148           }
  149       };
  150   
  151       private static AbstractAction selectNextColumnExtendSelectionAction = new LeadRowColumnAction() {
  152           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  153               if (currentColumn + 1 < table.getColumnCount()) {
  154                   table.changeSelection(currentRow, currentColumn + 1, false, true);
  155               }
  156           }
  157       };
  158       private static AbstractAction selectPreviousColumnExtendSelectionAction = new LeadRowColumnAction() {
  159           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  160               if (currentColumn > 0) {
  161                   table.changeSelection(currentRow, currentColumn - 1, false, true);
  162               }
  163           }
  164       };
  165       private static AbstractAction selectNextRowExtendSelectionAction = new LeadRowColumnAction() {
  166           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  167               if (currentRow + 1 < table.getRowCount()) {
  168                   table.changeSelection(currentRow + 1, currentColumn, false, true);
  169               }
  170           }
  171       };
  172       private static AbstractAction selectPreviousRowExtendSelectionAction = new LeadRowColumnAction() {
  173           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  174               if (currentRow > 0) {
  175                   table.changeSelection(currentRow - 1, currentColumn, false, true);
  176               }
  177           }
  178       };
  179   
  180       private static AbstractAction selectFirstColumnAction = new LeadRowColumnAction() {
  181           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  182               table.changeSelection(currentRow, 0, false, false);
  183           }
  184       };
  185       private static AbstractAction selectLastColumnAction = new LeadRowColumnAction() {
  186           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  187               table.changeSelection(currentRow, table.getColumnCount() - 1, false, false);
  188           }
  189       };
  190       private static AbstractAction selectFirstRowAction = new LeadRowColumnAction() {
  191           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  192               table.changeSelection(0, currentColumn, false, false);
  193           }
  194       };
  195       private static AbstractAction selectLastRowAction = new LeadRowColumnAction() {
  196           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  197               table.changeSelection(table.getRowCount() - 1, currentColumn, false, false);
  198           }
  199       };
  200   
  201       private static AbstractAction selectFirstColumnExtendSelectionAction = new LeadRowColumnAction() {
  202           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  203               table.changeSelection(currentRow, 0, false, true);
  204           }
  205       };
  206       private static AbstractAction selectLastColumnExtendSelectionAction = new LeadRowColumnAction() {
  207           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  208               table.changeSelection(currentRow, table.getColumnCount() - 1, false, true);
  209           }
  210       };
  211       private static AbstractAction selectFirstRowExtendSelectionAction = new LeadRowColumnAction() {
  212           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  213               table.changeSelection(0, currentColumn, false, true);
  214           }
  215       };
  216       private static AbstractAction selectLastRowExtendSelectionAction = new LeadRowColumnAction() {
  217           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  218               table.changeSelection(table.getRowCount() - 1, currentColumn, false, true);
  219           }
  220       };
  221       private static AbstractAction toggleAndAnchorAction = new LeadRowColumnAction() {
  222           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  223               if (currentRow >= 0 && currentColumn >=0) {
  224                   table.changeSelection(currentRow, currentColumn, true, false);
  225                   table.getSelectionModel().setAnchorSelectionIndex(currentRow);
  226                   table.getColumnModel().getSelectionModel().setAnchorSelectionIndex(currentColumn);
  227               }
  228           }
  229       };
  230       private static AbstractAction moveSelectionToAction = new LeadRowColumnAction() {
  231           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  232               if (currentRow >= 0 && currentColumn >=0) {
  233                   table.changeSelection(currentRow, currentColumn, false, false);
  234               }
  235           }
  236       };
  237       private static AbstractAction extendToAction = new LeadRowColumnAction() {
  238           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  239               if (currentRow >= 0 && currentColumn >=0) {
  240                   table.changeSelection(currentRow, currentColumn, false, true);
  241               }
  242           }
  243       };
  244   
  245   
  246       private static abstract class SelectRowColumnCellAction extends LeadRowColumnAction {
  247           protected abstract int[] nextCellCoords(final int[] cell, final int minRow, final int maxRow,
  248                                                   final int minColumn, final int maxColumn);
  249   
  250           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  251               ListSelectionModel rowSelectionModel = table.getSelectionModel();
  252               ListSelectionModel colSelectionModel = table.getColumnModel().getSelectionModel();
  253               if (rowSelectionModel.getMinSelectionIndex() == rowSelectionModel.getMaxSelectionIndex()
  254                   && colSelectionModel.getMinSelectionIndex() == colSelectionModel.getMaxSelectionIndex()) {
  255   
  256                   int[] currentCell = new int[] { currentRow, currentColumn };
  257                   currentCell = nextCellCoords(currentCell, 0, table.getRowCount() - 1, 0, table.getColumnCount() - 1);
  258                   table.changeSelection(currentCell[0], currentCell[1], false, false);
  259               } else {
  260                   int[] currentCell = new int[] { currentRow, currentColumn };
  261                   int rowMinSelectionIndex;
  262                   int rowMaxSelectionIndex;
  263                   int colMinSelectionIndex;
  264                   int colMaxSelectionIndex;
  265                   
  266                   if (table.getRowSelectionAllowed() && table.getColumnSelectionAllowed()) {
  267                       rowMinSelectionIndex = rowSelectionModel.getMinSelectionIndex();
  268                       rowMaxSelectionIndex = rowSelectionModel.getMaxSelectionIndex();
  269                       colMinSelectionIndex = colSelectionModel.getMinSelectionIndex();
  270                       colMaxSelectionIndex = colSelectionModel.getMaxSelectionIndex();
  271                   } else if (table.getRowSelectionAllowed() && !table.getColumnSelectionAllowed()) {
  272                       rowMinSelectionIndex = rowSelectionModel.getMinSelectionIndex();
  273                       rowMaxSelectionIndex = rowSelectionModel.getMaxSelectionIndex();
  274                       colMinSelectionIndex = 0;
  275                       colMaxSelectionIndex = table.getColumnCount() - 1;
  276                   } else if (!table.getRowSelectionAllowed() && table.getColumnSelectionAllowed()) {
  277                       rowMinSelectionIndex = 0;
  278                       rowMaxSelectionIndex = table.getRowCount() - 1;
  279                       colMinSelectionIndex = colSelectionModel.getMinSelectionIndex();
  280                       colMaxSelectionIndex = colSelectionModel.getMaxSelectionIndex();
  281                   } else {
  282                       rowMinSelectionIndex = 0;
  283                       rowMaxSelectionIndex = table.getRowCount() - 1;
  284                       colMinSelectionIndex = 0;
  285                       colMaxSelectionIndex = table.getColumnCount() - 1;
  286                   }
  287                   
  288                   do {
  289                       currentCell = nextCellCoords(currentCell, rowMinSelectionIndex, rowMaxSelectionIndex,
  290                                                    colMinSelectionIndex, colMaxSelectionIndex);
  291                       
  292                       if (!table.getRowSelectionAllowed() && !table.getColumnSelectionAllowed()) {
  293                           break;
  294                       }
  295                   } while (!table.isCellSelected(currentCell[0], currentCell[1]));
  296                   
  297                   colSelectionModel.addSelectionInterval(currentCell[1], currentCell[1]);
  298                   rowSelectionModel.addSelectionInterval(currentCell[0], currentCell[0]);
  299               }
  300           }
  301       }
  302   
  303       private static AbstractAction selectNextColumnCellAction = new SelectRowColumnCellAction() {
  304           protected int[] nextCellCoords(final int[] cell, final int minRow, final int maxRow, final int minColumn, final int maxColumn) {
  305               if (cell[1] + 1 <= maxColumn) {
  306                   cell[1]++;
  307               } else {
  308                   cell[1] = minColumn;
  309                   if (cell[0] + 1 <= maxRow) {
  310                       cell[0]++;
  311                   } else {
  312                       cell[0] = minRow;
  313                   }
  314               }
  315   
  316               return cell;
  317           }
  318       };
  319       private static AbstractAction selectPreviousColumnCellAction = new SelectRowColumnCellAction() {
  320           protected int[] nextCellCoords(final int[] cell, final int minRow, final int maxRow, final int minColumn, final int maxColumn) {
  321               if (cell[1] > minColumn) {
  322                   cell[1]--;
  323               } else {
  324                   cell[1] = maxColumn;
  325                   if (cell[0] > minRow) {
  326                       cell[0]--;
  327                   } else {
  328                       cell[0] = maxRow;
  329                   }
  330               }
  331   
  332               return cell;
  333           }
  334       };
  335       private static AbstractAction selectNextRowCellAction = new SelectRowColumnCellAction() {
  336           protected int[] nextCellCoords(final int[] cell, final int minRow, final int maxRow, final int minColumn, final int maxColumn) {
  337               if (cell[0] + 1 <= maxRow) {
  338                   cell[0]++;
  339               } else {
  340                   cell[0] = minRow;
  341                   if (cell[1] + 1 <= maxColumn) {
  342                       cell[1]++;
  343                   } else {
  344                       cell[1] = minColumn;
  345                   }
  346               }
  347   
  348               return cell;
  349           }
  350       };
  351       private static AbstractAction selectPreviousRowCellAction = new SelectRowColumnCellAction() {
  352           protected int[] nextCellCoords(final int[] cell, final int minRow, final int maxRow, final int minColumn, final int maxColumn) {
  353               if (cell[0] > minRow) {
  354                   cell[0]--;
  355               } else {
  356                   cell[0] = maxRow;
  357                   if (cell[1] > minColumn) {
  358                       cell[1]--;
  359                   } else {
  360                       cell[1] = maxColumn;
  361                   }
  362               }
  363   
  364               return cell;
  365           }
  366       };
  367   
  368       private static AbstractAction selectAllAction = new AbstractTableAction() {
  369           protected void processTable(final JTable table) {
  370               if (table.isEditing()) {
  371                   table.getCellEditor().cancelCellEditing();
  372               }
  373               table.selectAll();
  374           }
  375       };
  376       private static AbstractAction clearSelectionAction = new AbstractTableAction() {
  377           protected void processTable(final JTable table) {
  378               if (table.isEditing()) {
  379                   table.getCellEditor().cancelCellEditing();
  380               }
  381               table.clearSelection();
  382           }
  383       };
  384   
  385       private static AbstractAction startEditingAction = new LeadRowColumnAction() {
  386           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  387               if (table.isEditing()) {
  388                   table.getCellEditor().stopCellEditing();
  389               } else if (table.editCellAt(currentRow, currentColumn)) {
  390                   table.getEditorComponent().requestFocus();
  391               }
  392           }
  393       };
  394       private static AbstractAction cancelAction = new AbstractTableAction() {
  395           protected void processTable(final JTable table) {
  396               if (table.isEditing()) {
  397                   table.getCellEditor().cancelCellEditing();
  398               }
  399           }
  400       };
  401   
  402       private static AbstractAction scrollUpChangeSelectionAction = new LeadRowColumnAction() {
  403           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  404               int scrollUpIndex = getScrollUpIndex(table);
  405               if (scrollUpIndex > 0) {
  406                   table.changeSelection(scrollUpIndex, currentColumn, false, false);
  407               } else {
  408                   table.changeSelection(0, currentColumn, false, false);
  409               }
  410           }
  411       };
  412       private static AbstractAction scrollDownChangeSelectionAction = new LeadRowColumnAction() {
  413           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  414               int scrollDownIndex = getScrollDownIndex(table);
  415               if (scrollDownIndex < table.getRowCount()) {
  416                   table.changeSelection(scrollDownIndex, currentColumn, false, false);
  417               } else {
  418                   table.changeSelection(table.getRowCount() - 1, currentColumn, false, false);
  419               }
  420           }
  421       };
  422       private static AbstractAction scrollRightChangeSelectionAction = new LeadRowColumnAction() {
  423           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  424               int scrollRightIndex = getScrollRightIndex(table);
  425               if (scrollRightIndex < table.getColumnCount()) {
  426                   table.changeSelection(currentRow, scrollRightIndex, false, false);
  427               } else {
  428                   table.changeSelection(currentRow, table.getColumnCount() - 1, false, false);
  429               }
  430           }
  431       };
  432       private static AbstractAction scrollLeftChangeSelectionAction = new LeadRowColumnAction() {
  433           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  434               int scrollLeftIndex = getScrollLeftIndex(table);
  435               if (scrollLeftIndex > 0) {
  436                   table.changeSelection(currentRow, scrollLeftIndex, false, false);
  437               } else {
  438                   table.changeSelection(currentRow, 0, false, false);
  439               }
  440           }
  441       };
  442   
  443       private static AbstractAction scrollUpExtendSelectionAction = new LeadRowColumnAction() {
  444           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  445               int scrollUpIndex = getScrollUpIndex(table);
  446               System.err.println("scrollup: " + scrollUpIndex);
  447               if (scrollUpIndex > 0) {
  448                   table.changeSelection(scrollUpIndex, currentColumn, false, true);
  449               } else {
  450                   table.changeSelection(0, currentColumn, false, true);
  451               }
  452           }
  453       };
  454       private static AbstractAction scrollDownExtendSelectionAction = new LeadRowColumnAction() {
  455           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  456               int scrollDownIndex = getScrollDownIndex(table);
  457               if (scrollDownIndex < table.getRowCount()) {
  458                   table.changeSelection(scrollDownIndex, currentColumn, false, true);
  459               } else {
  460                   table.changeSelection(table.getRowCount() - 1, currentColumn, false, true);
  461               }
  462           }
  463       };
  464       private static AbstractAction scrollRightExtendSelectionAction = new LeadRowColumnAction() {
  465           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  466               int scrollRightIndex = getScrollRightIndex(table);
  467               if (scrollRightIndex < table.getColumnCount()) {
  468                   table.changeSelection(currentRow, scrollRightIndex, false, true);
  469               } else {
  470                   table.changeSelection(currentRow, table.getColumnCount() - 1, false, true);
  471               }
  472           }
  473       };
  474       private static AbstractAction scrollLeftExtendSelectionAction = new LeadRowColumnAction() {
  475           protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  476               int scrollLeftIndex = getScrollLeftIndex(table);
  477               if (scrollLeftIndex > 0) {
  478                   table.changeSelection(currentRow, scrollLeftIndex, false, true);
  479               } else {
  480                   table.changeSelection(currentRow, 0, false, true);
  481               }
  482           }
  483       };
  484   
  485   
  486       public static void installKeyboardActions(final JTable table) {
  487           Utilities.installKeyboardActions(table, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, "Table.ancestorInputMap", "Table.ancestorInputMap.RightToLeft");
  488   
  489           table.getActionMap().put("copy", TransferHandler.getCopyAction());
  490           table.getActionMap().put("paste", TransferHandler.getPasteAction());
  491           table.getActionMap().put("cut", TransferHandler.getCutAction());
  492   
  493           table.getActionMap().put("selectNextColumn", selectNextColumnAction);
  494           table.getActionMap().put("selectPreviousColumn", selectPreviousColumnAction);
  495           table.getActionMap().put("selectNextRow", selectNextRowAction);
  496           table.getActionMap().put("selectPreviousRow", selectPreviousRowAction);
  497           
  498           table.getActionMap().put("selectPreviousRowChangeLead", selectPreviousRowChangeLeadAction);
  499           table.getActionMap().put("selectNextRowChangeLead", selectNextRowChangeLeadAction);
  500           table.getActionMap().put("selectPreviousColumnChangeLead", selectPreviousColumnChangeLeadAction);
  501           table.getActionMap().put("selectNextColumnChangeLead", selectNextColumnChangeLeadAction);
  502   
  503           table.getActionMap().put("selectNextColumnExtendSelection", selectNextColumnExtendSelectionAction);
  504           table.getActionMap().put("selectPreviousColumnExtendSelection", selectPreviousColumnExtendSelectionAction);
  505           table.getActionMap().put("selectNextRowExtendSelection", selectNextRowExtendSelectionAction);
  506           table.getActionMap().put("selectPreviousRowExtendSelection", selectPreviousRowExtendSelectionAction);
  507   
  508           table.getActionMap().put("scrollUpChangeSelection", scrollUpChangeSelectionAction);
  509           table.getActionMap().put("scrollDownChangeSelection", scrollDownChangeSelectionAction);
  510           table.getActionMap().put("scrollUpExtendSelection", scrollUpExtendSelectionAction);
  511           table.getActionMap().put("scrollDownExtendSelection", scrollDownExtendSelectionAction);
  512           table.getActionMap().put("scrollLeftChangeSelection", scrollLeftChangeSelectionAction);
  513           table.getActionMap().put("scrollRightChangeSelection", scrollRightChangeSelectionAction);
  514           table.getActionMap().put("scrollLeftExtendSelection", scrollLeftExtendSelectionAction);
  515           table.getActionMap().put("scrollRightExtendSelection", scrollRightExtendSelectionAction);
  516   
  517           table.getActionMap().put("selectFirstColumn", selectFirstColumnAction);
  518           table.getActionMap().put("selectLastColumn", selectLastColumnAction);
  519           table.getActionMap().put("selectFirstRow", selectFirstRowAction);
  520           table.getActionMap().put("selectLastRow", selectLastRowAction);
  521   
  522           table.getActionMap().put("selectFirstColumnExtendSelection", selectFirstColumnExtendSelectionAction);
  523           table.getActionMap().put("selectLastColumnExtendSelection", selectLastColumnExtendSelectionAction);
  524           table.getActionMap().put("selectFirstRowExtendSelection", selectFirstRowExtendSelectionAction);
  525           table.getActionMap().put("selectLastRowExtendSelection", selectLastRowExtendSelectionAction);
  526   
  527           table.getActionMap().put("selectNextColumnCell", selectNextColumnCellAction);
  528           table.getActionMap().put("selectPreviousColumnCell", selectPreviousColumnCellAction);
  529           table.getActionMap().put("selectNextRowCell", selectNextRowCellAction);
  530           table.getActionMap().put("selectPreviousRowCell", selectPreviousRowCellAction);
  531   
  532           table.getActionMap().put("selectAll", selectAllAction);
  533           table.getActionMap().put("clearSelection", clearSelectionAction);
  534   
  535           table.getActionMap().put("addToSelection", new EnablebableLeadRowColumnAction(table) {
  536               protected void processRowColumn(final JTable table, final int currentRow, final int currentColumn) {
  537                   if (currentRow >= 0 && currentColumn >=0) {
  538                       table.addRowSelectionInterval(currentRow, currentRow);
  539                       table.addColumnSelectionInterval(currentColumn, currentColumn);
  540                   }
  541               }
  542               public boolean isEnabled(final JTable table, final int currentRow, final int currentColumn) {
  543                   return !table.isCellSelected(currentRow, currentColumn);
  544               }
  545           });
  546   
  547           table.getActionMap().put("toggleAndAnchor", toggleAndAnchorAction);
  548           table.getActionMap().put("moveSelectionTo", moveSelectionToAction);
  549           table.getActionMap().put("extendTo", extendToAction);
  550   
  551           table.getActionMap().put("startEditing", startEditingAction);
  552           table.getActionMap().put("cancel", cancelAction);
  553       }
  554   
  555       public static void uninstallKeyboardActions(final JTable table) {
  556           Utilities.uninstallKeyboardActions(table, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  557       }
  558   
  559       private static int getFirstVisibleTableRowIndex(final JTable table) {
  560           Rectangle visibleRect = table.getVisibleRect();
  561           if (visibleRect.isEmpty()) {
  562               return -1;
  563           }
  564   
  565           int currentColumn = table.getColumnModel().getSelectionModel().getAnchorSelectionIndex();
  566   
  567           for (int i = 0; i < table.getRowCount(); i++) {
  568               Rectangle bounds = table.getCellRect(i, currentColumn, true);
  569               if (bounds.intersects(visibleRect)) {
  570                   return i;
  571               }
  572           }
  573   
  574           return -1;
  575       }
  576   
  577       private static int getLastVisibleTableRowIndex(final JTable table) {
  578           Rectangle visibleRect = table.getVisibleRect();
  579           if (visibleRect.isEmpty()) {
  580               return -1;
  581           }
  582   
  583           int currentColumn = table.getColumnModel().getSelectionModel().getAnchorSelectionIndex();
  584   
  585           for (int i = table.getRowCount() - 1; i >= 0; i--) {
  586               Rectangle bounds = table.getCellRect(i, currentColumn, true);
  587               if (bounds.intersects(visibleRect)) {
  588                   return i;
  589               }
  590           }
  591   
  592           return -1;
  593       }
  594   
  595       private static int getRightmostVisibleTableRowIndex(final JTable table) {
  596           Rectangle visibleRect = table.getVisibleRect();
  597           if (visibleRect.isEmpty()) {
  598               return -1;
  599           }
  600   
  601           int currentRow = table.getSelectionModel().getAnchorSelectionIndex();
  602   
  603           for (int i = table.getColumnCount() - 1; i >= 0; i--) {
  604               Rectangle bounds = table.getCellRect(currentRow, i, true);
  605               if (bounds.intersects(visibleRect)) {
  606                   return i;
  607               }
  608           }
  609   
  610           return -1;
  611       }
  612   
  613       private static int getLeftmostVisibleTableRowIndex(final JTable table) {
  614           Rectangle visibleRect = table.getVisibleRect();
  615           if (visibleRect.isEmpty()) {
  616               return -1;
  617           }
  618   
  619           int currentRow = table.getSelectionModel().getAnchorSelectionIndex();
  620   
  621           for (int i = 0; i < table.getColumnCount(); i++) {
  622               Rectangle bounds = table.getCellRect(currentRow, i, true);
  623               if (bounds.intersects(visibleRect)) {
  624                   return i;
  625               }
  626           }
  627   
  628           return -1;
  629       }
  630   
  631       private static int getScrollDownIndex(final JTable table) {
  632           if (table.getRowCount() == 0) {
  633               return -1;
  634           }
  635   
  636           int currentSelection = table.getSelectionModel().getLeadSelectionIndex();
  637           int lastVisible = getLastVisibleTableRowIndex(table);
  638           if (lastVisible != currentSelection || lastVisible == table.getRowCount() - 1) {
  639               return lastVisible;
  640           } else {
  641               Rectangle visibleRect = table.getVisibleRect();
  642               int currentColumn = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
  643               int i = lastVisible + 1;
  644               int cellsHeight = table.getCellRect(i, currentColumn, true).height;
  645               while (i < table.getRowCount() - 1 && cellsHeight < visibleRect.height) {
  646                   i++;
  647                   cellsHeight += table.getCellRect(i, currentColumn, true).height;
  648               }
  649   
  650               return i;
  651           }
  652       }
  653   
  654       private static int getScrollUpIndex(final JTable table) {
  655           if (table.getRowCount() == 0) {
  656               return -1;
  657           }
  658   
  659           int currentSelection = table.getSelectionModel().getLeadSelectionIndex();
  660           int firstVisible = getFirstVisibleTableRowIndex(table);
  661           if (firstVisible != currentSelection || firstVisible == table.getRowCount() - 1) {
  662               return firstVisible;
  663           } else {
  664               Rectangle visibleRect = table.getVisibleRect();
  665               int currentColumn = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
  666               int i = firstVisible - 1;
  667               int cellsHeight = table.getCellRect(i, currentColumn, true).height;
  668               while (i > 0 && cellsHeight < visibleRect.height) {
  669                   i--;
  670                   cellsHeight += table.getCellRect(i, currentColumn, true).height;
  671               }
  672   
  673               return i;
  674           }
  675       }
  676   
  677       private static int getScrollRightIndex(final JTable table) {
  678           if (table.getColumnCount() == 0) {
  679               return -1;
  680           }
  681   
  682           int currentSelection = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
  683           int lastVisible = getRightmostVisibleTableRowIndex(table);
  684           if (lastVisible != currentSelection || lastVisible == table.getColumnCount() - 1) {
  685               return lastVisible;
  686           } else {
  687               Rectangle visibleRect = table.getVisibleRect();
  688               int currentRow = table.getSelectionModel().getLeadSelectionIndex();
  689               int i = lastVisible + 1;
  690               int cellsWidth = table.getCellRect(currentRow, i, true).width;
  691               while (i < table.getColumnCount() - 1 && cellsWidth < visibleRect.width) {
  692                   i++;
  693                   cellsWidth += table.getCellRect(currentRow, i, true).width;
  694               }
  695   
  696               return i;
  697           }
  698       }
  699   
  700       private static int getScrollLeftIndex(final JTable table) {
  701           if (table.getRowCount() == 0) {
  702               return -1;
  703           }
  704   
  705           int currentSelection = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
  706           int firstVisible = getLeftmostVisibleTableRowIndex(table);
  707           if (firstVisible != currentSelection || firstVisible == table.getColumnCount() - 1) {
  708               return firstVisible;
  709           } else {
  710               Rectangle visibleRect = table.getVisibleRect();
  711               int currentRow = table.getSelectionModel().getLeadSelectionIndex();
  712               int i = firstVisible - 1;
  713               int cellsWidth = table.getCellRect(currentRow, i, true).width;
  714               while (i > 0 && cellsWidth < visibleRect.width) {
  715                   i--;
  716                   cellsWidth += table.getCellRect(currentRow, i, true).width;
  717               }
  718   
  719               return i;
  720           }
  721       }
  722   
  723       private static void ensureCellIsVisible(final JTable table, final int row, final int column) {
  724           table.scrollRectToVisible(table.getCellRect(row, column, true));
  725        }
  726   }

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