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

Print this page


   1 /*
   2  * Copyright (c) 2002, 2013, 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


 110                           new StringBuffer("AATextInfoPropertyKey");
 111 
 112     /**
 113      * Attribute key for the content elements.  If it is set on an element, the
 114      * element is considered to be a line break.
 115      */
 116     public static final String IMPLIED_CR = "CR";
 117 
 118     /**
 119      * Used to tell a text component, being used as an editor for table
 120      * or tree, how many clicks it took to start editing.
 121      */
 122     private static final StringBuilder SKIP_CLICK_COUNT =
 123         new StringBuilder("skipClickCount");
 124 
 125     /* Presently this class assumes default fractional metrics.
 126      * This may need to change to emulate future platform L&Fs.
 127      */
 128     public static class AATextInfo {
 129 
 130         private static AATextInfo getAATextInfoFromMap(Map hints) {
 131 
 132             Object aaHint   = hints.get(KEY_TEXT_ANTIALIASING);
 133             Object contHint = hints.get(KEY_TEXT_LCD_CONTRAST);
 134 
 135             if (aaHint == null ||
 136                 aaHint == VALUE_TEXT_ANTIALIAS_OFF ||
 137                 aaHint == VALUE_TEXT_ANTIALIAS_DEFAULT) {
 138                 return null;
 139             } else {
 140                 return new AATextInfo(aaHint, (Integer)contHint);
 141             }
 142         }
 143 

 144         public static AATextInfo getAATextInfo(boolean lafCondition) {
 145             SunToolkit.setAAFontSettingsCondition(lafCondition);
 146             Toolkit tk = Toolkit.getDefaultToolkit();
 147             Object map = tk.getDesktopProperty(SunToolkit.DESKTOPFONTHINTS);
 148             if (map instanceof Map) {
 149                 return getAATextInfoFromMap((Map)map);
 150             } else {
 151                 return null;
 152             }
 153         }
 154 
 155         Object aaHint;
 156         Integer lcdContrastHint;
 157         FontRenderContext frc;
 158 
 159         /* These are rarely constructed objects, and only when a complete
 160          * UI is being updated, so the cost of the tests here is minimal
 161          * and saves tests elsewhere.
 162          * We test that the values are ones we support/expect.
 163          */
 164         public AATextInfo(Object aaHint, Integer lcdContrastHint) {
 165             if (aaHint == null) {
 166                 throw new InternalError("null not allowed here");
 167             }
 168             if (aaHint == VALUE_TEXT_ANTIALIAS_OFF ||
 169                 aaHint == VALUE_TEXT_ANTIALIAS_DEFAULT) {


 646                     Shape shape =
 647                         layout.getVisualHighlightShape(leading, trailing);
 648                     Rectangle rect = shape.getBounds();
 649                     underlineRectX = x + rect.x;
 650                     underlineRectWidth = rect.width;
 651                 }
 652             }
 653             g.fillRect(underlineRectX, underlineRectY + 1,
 654                        underlineRectWidth, underlineRectHeight);
 655         }
 656     }
 657 
 658 
 659     /**
 660      * A variation of locationToIndex() which only returns an index if the
 661      * Point is within the actual bounds of a list item (not just in the cell)
 662      * and if the JList has the "List.isFileList" client property set.
 663      * Otherwise, this method returns -1.
 664      * This is used to make WindowsL&F JFileChooser act like native dialogs.
 665      */
 666     public static int loc2IndexFileList(JList list, Point point) {
 667         int index = list.locationToIndex(point);
 668         if (index != -1) {
 669             Object bySize = list.getClientProperty("List.isFileList");
 670             if (bySize instanceof Boolean && ((Boolean)bySize).booleanValue() &&
 671                 !pointIsInActualBounds(list, index, point)) {
 672                 index = -1;
 673             }
 674         }
 675         return index;
 676     }
 677 
 678 
 679     /**
 680      * Returns true if the given point is within the actual bounds of the
 681      * JList item at index (not just inside the cell).
 682      */
 683     private static boolean pointIsInActualBounds(JList list, int index,
 684                                                 Point point) {
 685         ListCellRenderer renderer = list.getCellRenderer();
 686         ListModel dataModel = list.getModel();
 687         Object value = dataModel.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      */


   1 /*
   2  * Copyright (c) 2002, 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


 110                           new StringBuffer("AATextInfoPropertyKey");
 111 
 112     /**
 113      * Attribute key for the content elements.  If it is set on an element, the
 114      * element is considered to be a line break.
 115      */
 116     public static final String IMPLIED_CR = "CR";
 117 
 118     /**
 119      * Used to tell a text component, being used as an editor for table
 120      * or tree, how many clicks it took to start editing.
 121      */
 122     private static final StringBuilder SKIP_CLICK_COUNT =
 123         new StringBuilder("skipClickCount");
 124 
 125     /* Presently this class assumes default fractional metrics.
 126      * This may need to change to emulate future platform L&Fs.
 127      */
 128     public static class AATextInfo {
 129 
 130         private static AATextInfo getAATextInfoFromMap(Map<java.awt.RenderingHints.Key, Object> hints) {
 131 
 132             Object aaHint   = hints.get(KEY_TEXT_ANTIALIASING);
 133             Object contHint = hints.get(KEY_TEXT_LCD_CONTRAST);
 134 
 135             if (aaHint == null ||
 136                 aaHint == VALUE_TEXT_ANTIALIAS_OFF ||
 137                 aaHint == VALUE_TEXT_ANTIALIAS_DEFAULT) {
 138                 return null;
 139             } else {
 140                 return new AATextInfo(aaHint, (Integer)contHint);
 141             }
 142         }
 143 
 144         @SuppressWarnings("unchecked")
 145         public static AATextInfo getAATextInfo(boolean lafCondition) {
 146             SunToolkit.setAAFontSettingsCondition(lafCondition);
 147             Toolkit tk = Toolkit.getDefaultToolkit();
 148             Object map = tk.getDesktopProperty(SunToolkit.DESKTOPFONTHINTS);
 149             if (map instanceof Map) {
 150                 return getAATextInfoFromMap((Map<java.awt.RenderingHints.Key, Object>)map);
 151             } else {
 152                 return null;
 153             }
 154         }
 155 
 156         Object aaHint;
 157         Integer lcdContrastHint;
 158         FontRenderContext frc;
 159 
 160         /* These are rarely constructed objects, and only when a complete
 161          * UI is being updated, so the cost of the tests here is minimal
 162          * and saves tests elsewhere.
 163          * We test that the values are ones we support/expect.
 164          */
 165         public AATextInfo(Object aaHint, Integer lcdContrastHint) {
 166             if (aaHint == null) {
 167                 throw new InternalError("null not allowed here");
 168             }
 169             if (aaHint == VALUE_TEXT_ANTIALIAS_OFF ||
 170                 aaHint == VALUE_TEXT_ANTIALIAS_DEFAULT) {


 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      */