< prev index next >

src/java.desktop/share/classes/sun/swing/SwingUtilities2.java

Print this page




 645                     TextHitInfo trailing =
 646                         TextHitInfo.trailing(underlinedIndex);
 647                     Shape shape =
 648                         layout.getVisualHighlightShape(leading, trailing);
 649                     Rectangle rect = shape.getBounds();
 650                     underlineRectX = x + rect.x;
 651                     underlineRectWidth = rect.width;
 652                 }
 653             }
 654             g.fillRect(underlineRectX, underlineRectY + 1,
 655                        underlineRectWidth, underlineRectHeight);
 656         }
 657     }
 658 
 659 
 660     /**
 661      * A variation of locationToIndex() which only returns an index if the
 662      * Point is within the actual bounds of a list item (not just in the cell)
 663      * and if the JList has the "List.isFileList" client property set.
 664      * Otherwise, this method returns -1.
 665      * This is used to make WindowsL&F JFileChooser act like native dialogs.

 666      */
 667     public static int loc2IndexFileList(JList<?> list, Point point) {
 668         int index = list.locationToIndex(point);
 669         if (index != -1) {
 670             Object bySize = list.getClientProperty("List.isFileList");
 671             if (bySize instanceof Boolean && ((Boolean)bySize).booleanValue() &&
 672                 !pointIsInActualBounds(list, index, point)) {
 673                 index = -1;
 674             }
 675         }
 676         return index;
 677     }
 678 
 679 
 680     /**
 681      * Returns true if the given point is within the actual bounds of the
 682      * JList item at index (not just inside the cell).
 683      */
 684     private static <T> boolean pointIsInActualBounds(JList<T> list, int index,
 685                                                 Point point) {
 686         ListCellRenderer<? super T> renderer = list.getCellRenderer();
 687         T value = list.getModel().getElementAt(index);
 688         Component item = renderer.getListCellRendererComponent(list,
 689                           value, index, false, false);
 690         Dimension itemSize = item.getPreferredSize();
 691         Rectangle cellBounds = list.getCellBounds(index, index);
 692         if (!item.getComponentOrientation().isLeftToRight()) {
 693             cellBounds.x += (cellBounds.width - itemSize.width);
 694         }
 695         cellBounds.width = itemSize.width;
 696 
 697         return cellBounds.contains(point);
 698     }
 699 
 700 
 701     /**
 702      * Returns true if the given point is outside the preferredSize of the
 703      * item at the given row of the table.  (Column must be 0).
 704      * Does not check the "Table.isFileList" property. That should be checked
 705      * before calling this method.
 706      * This is used to make WindowsL&F JFileChooser act like native dialogs.

 707      */
 708     public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
 709         if (table.convertColumnIndexToModel(column) != 0 || row == -1) {
 710             return true;
 711         }
 712         TableCellRenderer tcr = table.getCellRenderer(row, column);
 713         Object value = table.getValueAt(row, column);
 714         Component cell = tcr.getTableCellRendererComponent(table, value, false,
 715                 false, row, column);
 716         Dimension itemSize = cell.getPreferredSize();
 717         Rectangle cellBounds = table.getCellRect(row, column, false);
 718         cellBounds.width = itemSize.width;
 719         cellBounds.height = itemSize.height;
 720 
 721         // See if coords are inside
 722         // ASSUME: mouse x,y will never be < cell's x,y
 723         assert (p.x >= cellBounds.x && p.y >= cellBounds.y);
 724         return p.x > cellBounds.x + cellBounds.width ||
 725                 p.y > cellBounds.y + cellBounds.height;
 726     }




 645                     TextHitInfo trailing =
 646                         TextHitInfo.trailing(underlinedIndex);
 647                     Shape shape =
 648                         layout.getVisualHighlightShape(leading, trailing);
 649                     Rectangle rect = shape.getBounds();
 650                     underlineRectX = x + rect.x;
 651                     underlineRectWidth = rect.width;
 652                 }
 653             }
 654             g.fillRect(underlineRectX, underlineRectY + 1,
 655                        underlineRectWidth, underlineRectHeight);
 656         }
 657     }
 658 
 659 
 660     /**
 661      * A variation of locationToIndex() which only returns an index if the
 662      * Point is within the actual bounds of a list item (not just in the cell)
 663      * and if the JList has the "List.isFileList" client property set.
 664      * Otherwise, this method returns -1.
 665      * This is used to make Windows {@literal L&F} JFileChooser act
 666      * like native dialogs.
 667      */
 668     public static int loc2IndexFileList(JList<?> list, Point point) {
 669         int index = list.locationToIndex(point);
 670         if (index != -1) {
 671             Object bySize = list.getClientProperty("List.isFileList");
 672             if (bySize instanceof Boolean && ((Boolean)bySize).booleanValue() &&
 673                 !pointIsInActualBounds(list, index, point)) {
 674                 index = -1;
 675             }
 676         }
 677         return index;
 678     }
 679 
 680 
 681     /**
 682      * Returns true if the given point is within the actual bounds of the
 683      * JList item at index (not just inside the cell).
 684      */
 685     private static <T> boolean pointIsInActualBounds(JList<T> list, int index,
 686                                                 Point point) {
 687         ListCellRenderer<? super T> renderer = list.getCellRenderer();
 688         T value = list.getModel().getElementAt(index);
 689         Component item = renderer.getListCellRendererComponent(list,
 690                           value, index, false, false);
 691         Dimension itemSize = item.getPreferredSize();
 692         Rectangle cellBounds = list.getCellBounds(index, index);
 693         if (!item.getComponentOrientation().isLeftToRight()) {
 694             cellBounds.x += (cellBounds.width - itemSize.width);
 695         }
 696         cellBounds.width = itemSize.width;
 697 
 698         return cellBounds.contains(point);
 699     }
 700 
 701 
 702     /**
 703      * Returns true if the given point is outside the preferredSize of the
 704      * item at the given row of the table.  (Column must be 0).
 705      * Does not check the "Table.isFileList" property. That should be checked
 706      * before calling this method.
 707      * This is used to make Windows {@literal L&F} JFileChooser act
 708      * like native dialogs.
 709      */
 710     public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
 711         if (table.convertColumnIndexToModel(column) != 0 || row == -1) {
 712             return true;
 713         }
 714         TableCellRenderer tcr = table.getCellRenderer(row, column);
 715         Object value = table.getValueAt(row, column);
 716         Component cell = tcr.getTableCellRendererComponent(table, value, false,
 717                 false, row, column);
 718         Dimension itemSize = cell.getPreferredSize();
 719         Rectangle cellBounds = table.getCellRect(row, column, false);
 720         cellBounds.width = itemSize.width;
 721         cellBounds.height = itemSize.height;
 722 
 723         // See if coords are inside
 724         // ASSUME: mouse x,y will never be < cell's x,y
 725         assert (p.x >= cellBounds.x && p.y >= cellBounds.y);
 726         return p.x > cellBounds.x + cellBounds.width ||
 727                 p.y > cellBounds.y + cellBounds.height;
 728     }


< prev index next >