src/share/classes/javax/swing/plaf/basic/BasicTableHeaderUI.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  86             }
  87         }
  88     };
  89 
  90     /**
  91      * This class should be treated as a "protected" inner class.
  92      * Instantiate it only within subclasses of {@code BasicTableHeaderUI}.
  93      */
  94     public class MouseInputHandler implements MouseInputListener {
  95 
  96         private int mouseXOffset;
  97         private Cursor otherCursor = resizeCursor;
  98 
  99         public void mouseClicked(MouseEvent e) {
 100             if (!header.isEnabled()) {
 101                 return;
 102             }
 103             if (e.getClickCount() % 2 == 1 &&
 104                     SwingUtilities.isLeftMouseButton(e)) {
 105                 JTable table = header.getTable();
 106                 RowSorter sorter;
 107                 if (table != null && (sorter = table.getRowSorter()) != null) {
 108                     int columnIndex = header.columnAtPoint(e.getPoint());
 109                     if (columnIndex != -1) {
 110                         columnIndex = table.convertColumnIndexToModel(
 111                                 columnIndex);
 112                         sorter.toggleSortOrder(columnIndex);
 113                     }
 114                 }
 115             }
 116         }
 117 
 118         private TableColumn getResizingColumn(Point p) {
 119             return getResizingColumn(p, header.columnAtPoint(p));
 120         }
 121 
 122         private TableColumn getResizingColumn(Point p, int column) {
 123             if (column == -1) {
 124                  return null;
 125             }
 126             Rectangle r = header.getHeaderRect(column);


 755             }
 756         }
 757         return height;
 758     }
 759 
 760     private Dimension createHeaderSize(long width) {
 761         // None of the callers include the intercell spacing, do it here.
 762         if (width > Integer.MAX_VALUE) {
 763             width = Integer.MAX_VALUE;
 764         }
 765         return new Dimension((int)width, getHeaderHeight());
 766     }
 767 
 768 
 769     /**
 770      * Return the minimum size of the header. The minimum width is the sum
 771      * of the minimum widths of each column (plus inter-cell spacing).
 772      */
 773     public Dimension getMinimumSize(JComponent c) {
 774         long width = 0;
 775         Enumeration enumeration = header.getColumnModel().getColumns();
 776         while (enumeration.hasMoreElements()) {
 777             TableColumn aColumn = (TableColumn)enumeration.nextElement();
 778             width = width + aColumn.getMinWidth();
 779         }
 780         return createHeaderSize(width);
 781     }
 782 
 783     /**
 784      * Return the preferred size of the header. The preferred height is the
 785      * maximum of the preferred heights of all of the components provided
 786      * by the header renderers. The preferred width is the sum of the
 787      * preferred widths of each column (plus inter-cell spacing).
 788      */
 789     public Dimension getPreferredSize(JComponent c) {
 790         long width = 0;
 791         Enumeration enumeration = header.getColumnModel().getColumns();
 792         while (enumeration.hasMoreElements()) {
 793             TableColumn aColumn = (TableColumn)enumeration.nextElement();
 794             width = width + aColumn.getPreferredWidth();
 795         }
 796         return createHeaderSize(width);
 797     }
 798 
 799     /**
 800      * Return the maximum size of the header. The maximum width is the sum
 801      * of the maximum widths of each column (plus inter-cell spacing).
 802      */
 803     public Dimension getMaximumSize(JComponent c) {
 804         long width = 0;
 805         Enumeration enumeration = header.getColumnModel().getColumns();
 806         while (enumeration.hasMoreElements()) {
 807             TableColumn aColumn = (TableColumn)enumeration.nextElement();
 808             width = width + aColumn.getMaxWidth();
 809         }
 810         return createHeaderSize(width);
 811     }
 812 
 813     private static class Actions extends UIAction {
 814         public static final String TOGGLE_SORT_ORDER =
 815             "toggleSortOrder";
 816         public static final String SELECT_COLUMN_TO_LEFT =
 817             "selectColumnToLeft";
 818         public static final String SELECT_COLUMN_TO_RIGHT =
 819             "selectColumnToRight";
 820         public static final String MOVE_COLUMN_LEFT =
 821             "moveColumnLeft";
 822         public static final String MOVE_COLUMN_RIGHT =
 823             "moveColumnRight";
 824         public static final String RESIZE_LEFT =
 825             "resizeLeft";
 826         public static final String RESIZE_RIGHT =
 827             "resizeRight";


 858                         return (th.getTable() != null);
 859                     }
 860                 }
 861             }
 862             return true;
 863         }
 864 
 865         public void actionPerformed(ActionEvent e) {
 866             JTableHeader th = (JTableHeader)e.getSource();
 867             BasicTableHeaderUI ui =
 868                 (BasicTableHeaderUI)BasicLookAndFeel.
 869                                         getUIOfType(th.getUI(),
 870                                             BasicTableHeaderUI.class);
 871             if (ui == null) {
 872                 return;
 873             }
 874 
 875             String name = getName();
 876             if (TOGGLE_SORT_ORDER == name) {
 877                 JTable table = th.getTable();
 878                 RowSorter sorter = table == null ? null : table.getRowSorter();
 879                 if (sorter != null) {
 880                     int columnIndex = ui.getSelectedColumnIndex();
 881                     columnIndex = table.convertColumnIndexToModel(
 882                                                       columnIndex);
 883                     sorter.toggleSortOrder(columnIndex);
 884                 }
 885             } else if (SELECT_COLUMN_TO_LEFT == name) {
 886                 if (th.getComponentOrientation().isLeftToRight()) {
 887                     ui.selectPreviousColumn(true);
 888                 } else {
 889                     ui.selectNextColumn(true);
 890                 }
 891             } else if (SELECT_COLUMN_TO_RIGHT == name) {
 892                 if (th.getComponentOrientation().isLeftToRight()) {
 893                     ui.selectNextColumn(true);
 894                 } else {
 895                     ui.selectPreviousColumn(true);
 896                 }
 897             } else if (MOVE_COLUMN_LEFT == name) {
 898                 moveColumn(true, th, ui);


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  86             }
  87         }
  88     };
  89 
  90     /**
  91      * This class should be treated as a "protected" inner class.
  92      * Instantiate it only within subclasses of {@code BasicTableHeaderUI}.
  93      */
  94     public class MouseInputHandler implements MouseInputListener {
  95 
  96         private int mouseXOffset;
  97         private Cursor otherCursor = resizeCursor;
  98 
  99         public void mouseClicked(MouseEvent e) {
 100             if (!header.isEnabled()) {
 101                 return;
 102             }
 103             if (e.getClickCount() % 2 == 1 &&
 104                     SwingUtilities.isLeftMouseButton(e)) {
 105                 JTable table = header.getTable();
 106                 RowSorter<?> sorter;
 107                 if (table != null && (sorter = table.getRowSorter()) != null) {
 108                     int columnIndex = header.columnAtPoint(e.getPoint());
 109                     if (columnIndex != -1) {
 110                         columnIndex = table.convertColumnIndexToModel(
 111                                 columnIndex);
 112                         sorter.toggleSortOrder(columnIndex);
 113                     }
 114                 }
 115             }
 116         }
 117 
 118         private TableColumn getResizingColumn(Point p) {
 119             return getResizingColumn(p, header.columnAtPoint(p));
 120         }
 121 
 122         private TableColumn getResizingColumn(Point p, int column) {
 123             if (column == -1) {
 124                  return null;
 125             }
 126             Rectangle r = header.getHeaderRect(column);


 755             }
 756         }
 757         return height;
 758     }
 759 
 760     private Dimension createHeaderSize(long width) {
 761         // None of the callers include the intercell spacing, do it here.
 762         if (width > Integer.MAX_VALUE) {
 763             width = Integer.MAX_VALUE;
 764         }
 765         return new Dimension((int)width, getHeaderHeight());
 766     }
 767 
 768 
 769     /**
 770      * Return the minimum size of the header. The minimum width is the sum
 771      * of the minimum widths of each column (plus inter-cell spacing).
 772      */
 773     public Dimension getMinimumSize(JComponent c) {
 774         long width = 0;
 775         Enumeration<TableColumn> enumeration = header.getColumnModel().getColumns();
 776         while (enumeration.hasMoreElements()) {
 777             TableColumn aColumn = enumeration.nextElement();
 778             width = width + aColumn.getMinWidth();
 779         }
 780         return createHeaderSize(width);
 781     }
 782 
 783     /**
 784      * Return the preferred size of the header. The preferred height is the
 785      * maximum of the preferred heights of all of the components provided
 786      * by the header renderers. The preferred width is the sum of the
 787      * preferred widths of each column (plus inter-cell spacing).
 788      */
 789     public Dimension getPreferredSize(JComponent c) {
 790         long width = 0;
 791         Enumeration<TableColumn> enumeration = header.getColumnModel().getColumns();
 792         while (enumeration.hasMoreElements()) {
 793             TableColumn aColumn = enumeration.nextElement();
 794             width = width + aColumn.getPreferredWidth();
 795         }
 796         return createHeaderSize(width);
 797     }
 798 
 799     /**
 800      * Return the maximum size of the header. The maximum width is the sum
 801      * of the maximum widths of each column (plus inter-cell spacing).
 802      */
 803     public Dimension getMaximumSize(JComponent c) {
 804         long width = 0;
 805         Enumeration<TableColumn> enumeration = header.getColumnModel().getColumns();
 806         while (enumeration.hasMoreElements()) {
 807             TableColumn aColumn = enumeration.nextElement();
 808             width = width + aColumn.getMaxWidth();
 809         }
 810         return createHeaderSize(width);
 811     }
 812 
 813     private static class Actions extends UIAction {
 814         public static final String TOGGLE_SORT_ORDER =
 815             "toggleSortOrder";
 816         public static final String SELECT_COLUMN_TO_LEFT =
 817             "selectColumnToLeft";
 818         public static final String SELECT_COLUMN_TO_RIGHT =
 819             "selectColumnToRight";
 820         public static final String MOVE_COLUMN_LEFT =
 821             "moveColumnLeft";
 822         public static final String MOVE_COLUMN_RIGHT =
 823             "moveColumnRight";
 824         public static final String RESIZE_LEFT =
 825             "resizeLeft";
 826         public static final String RESIZE_RIGHT =
 827             "resizeRight";


 858                         return (th.getTable() != null);
 859                     }
 860                 }
 861             }
 862             return true;
 863         }
 864 
 865         public void actionPerformed(ActionEvent e) {
 866             JTableHeader th = (JTableHeader)e.getSource();
 867             BasicTableHeaderUI ui =
 868                 (BasicTableHeaderUI)BasicLookAndFeel.
 869                                         getUIOfType(th.getUI(),
 870                                             BasicTableHeaderUI.class);
 871             if (ui == null) {
 872                 return;
 873             }
 874 
 875             String name = getName();
 876             if (TOGGLE_SORT_ORDER == name) {
 877                 JTable table = th.getTable();
 878                 RowSorter<?> sorter = table == null ? null : table.getRowSorter();
 879                 if (sorter != null) {
 880                     int columnIndex = ui.getSelectedColumnIndex();
 881                     columnIndex = table.convertColumnIndexToModel(
 882                                                       columnIndex);
 883                     sorter.toggleSortOrder(columnIndex);
 884                 }
 885             } else if (SELECT_COLUMN_TO_LEFT == name) {
 886                 if (th.getComponentOrientation().isLeftToRight()) {
 887                     ui.selectPreviousColumn(true);
 888                 } else {
 889                     ui.selectNextColumn(true);
 890                 }
 891             } else if (SELECT_COLUMN_TO_RIGHT == name) {
 892                 if (th.getComponentOrientation().isLeftToRight()) {
 893                     ui.selectNextColumn(true);
 894                 } else {
 895                     ui.selectPreviousColumn(true);
 896                 }
 897             } else if (MOVE_COLUMN_LEFT == name) {
 898                 moveColumn(true, th, ui);