< prev index next >

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

Print this page
rev 1527 : 6727662: Code improvement and warnings removing from swing packages
Summary: Removed unnecessary castings and other warnings
Reviewed-by: malenkov


 433      * Clips the passed in String to the space provided.  NOTE: this assumes
 434      * the string does not fit in the available space.
 435      *
 436      * @param c JComponent that will display the string, may be null
 437      * @param fm FontMetrics used to measure the String width
 438      * @param string String to display
 439      * @param availTextWidth Amount of space that the string can be drawn in
 440      * @return Clipped string that can fit in the provided space.
 441      */
 442     public static String clipString(JComponent c, FontMetrics fm,
 443                                     String string, int availTextWidth) {
 444         // c may be null here.
 445         String clipString = "...";
 446         int stringLength = string.length();
 447         availTextWidth -= SwingUtilities2.stringWidth(c, fm, clipString);
 448         if (availTextWidth <= 0) {
 449             //can not fit any characters
 450             return clipString;
 451         }
 452 
 453         boolean needsTextLayout = false;
 454 
 455         synchronized (charsBufferLock) {
 456             if (charsBuffer == null || charsBuffer.length < stringLength) {
 457                 charsBuffer  = string.toCharArray();
 458             } else {
 459                 string.getChars(0, stringLength, charsBuffer, 0);
 460             }
 461             needsTextLayout =
 462                 isComplexLayout(charsBuffer, 0, stringLength);
 463             if (!needsTextLayout) {
 464                 int width = 0;
 465                 for (int nChars = 0; nChars < stringLength; nChars++) {
 466                     width += fm.charWidth(charsBuffer[nChars]);
 467                     if (width > availTextWidth) {
 468                         string = string.substring(0, nChars);
 469                         break;
 470                     }
 471                 }
 472             }
 473         }


 688      * Does not check the "Table.isFileList" property. That should be checked
 689      * before calling this method.
 690      * This is used to make WindowsL&F JFileChooser act like native dialogs.
 691      */
 692     public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
 693         if (table.convertColumnIndexToModel(column) != 0 || row == -1) {
 694             return true;
 695         }
 696         TableCellRenderer tcr = table.getCellRenderer(row, column);
 697         Object value = table.getValueAt(row, column);
 698         Component cell = tcr.getTableCellRendererComponent(table, value, false,
 699                 false, row, column);
 700         Dimension itemSize = cell.getPreferredSize();
 701         Rectangle cellBounds = table.getCellRect(row, column, false);
 702         cellBounds.width = itemSize.width;
 703         cellBounds.height = itemSize.height;
 704 
 705         // See if coords are inside
 706         // ASSUME: mouse x,y will never be < cell's x,y
 707         assert (p.x >= cellBounds.x && p.y >= cellBounds.y);
 708         if (p.x > cellBounds.x + cellBounds.width ||
 709                 p.y > cellBounds.y + cellBounds.height) {
 710             return true;
 711         }
 712         return false;
 713     }
 714 
 715     /**
 716      * Set the lead and anchor without affecting selection.
 717      */
 718     public static void setLeadAnchorWithoutSelection(ListSelectionModel model,
 719                                                      int lead, int anchor) {
 720         if (anchor == -1) {
 721             anchor = lead;
 722         }
 723         if (lead == -1) {
 724             model.setAnchorSelectionIndex(-1);
 725             model.setLeadSelectionIndex(-1);
 726         } else {
 727             if (model.isSelectedIndex(lead)) {
 728                 model.addSelectionInterval(lead, lead);
 729             } else {
 730                 model.removeSelectionInterval(lead, lead);
 731             }
 732             model.setAnchorSelectionIndex(anchor);


1212 
1213     /**
1214      * Returns true if the given event has permissions to access the
1215      * system clipboard
1216      *
1217      * @param e AWTEvent to check
1218      */
1219     public static boolean canEventAccessSystemClipboard(AWTEvent e) {
1220         return isTrustedContext()
1221             || canEventAccessSystemClipboard(e, false);
1222     }
1223 
1224     /**
1225      * returns canAccessSystemClipboard field from InputEvent
1226      *
1227      * @param ie InputEvent to get the field from
1228      */
1229     private static synchronized boolean inputEvent_canAccessSystemClipboard(InputEvent ie) {
1230         if (inputEvent_CanAccessSystemClipboard_Field == null) {
1231             inputEvent_CanAccessSystemClipboard_Field =
1232                 (Field)AccessController.doPrivileged(
1233                     new java.security.PrivilegedAction() {
1234                         public Object run() {
1235                             Field field = null;
1236                             try {
1237                                 field = InputEvent.class.
1238                                     getDeclaredField("canAccessSystemClipboard");
1239                                 field.setAccessible(true);
1240                                 return field;
1241                             } catch (SecurityException e) {
1242                             } catch (NoSuchFieldException e) {
1243                             }
1244                             return null;
1245                         }
1246                     });
1247         }
1248         if (inputEvent_CanAccessSystemClipboard_Field == null) {
1249             return false;
1250         }
1251         boolean ret = false;
1252         try {
1253             ret = inputEvent_CanAccessSystemClipboard_Field.
1254                 getBoolean(ie);
1255         } catch(IllegalAccessException e) {
1256         }
1257         return ret;


1405      * @param rootClass an ancestor of <code>baseClass</code> to finish the
1406      *                  search at
1407      * @param imageFile the name of the file to be found
1408      * @return a lazy value that creates the <code>ImageIcon</code>
1409      *         <code>UIResource</code> for the image,
1410      *         or null if it cannot be found
1411      */
1412     public static Object makeIcon(final Class<?> baseClass,
1413                                   final Class<?> rootClass,
1414                                   final String imageFile) {
1415 
1416         return new UIDefaults.LazyValue() {
1417             public Object createValue(UIDefaults table) {
1418                 /* Copy resource into a byte array.  This is
1419                  * necessary because several browsers consider
1420                  * Class.getResource a security risk because it
1421                  * can be used to load additional classes.
1422                  * Class.getResourceAsStream just returns raw
1423                  * bytes, which we can convert to an image.
1424                  */
1425                 byte[] buffer = (byte[])
1426                     java.security.AccessController.doPrivileged(
1427                         new java.security.PrivilegedAction() {
1428                     public Object run() {
1429                         try {
1430                             InputStream resource = null;
1431                             Class<?> srchClass = baseClass;
1432 
1433                             while (srchClass != null) {
1434                                 resource = srchClass.getResourceAsStream(imageFile);
1435 
1436                                 if (resource != null || srchClass == rootClass) {
1437                                     break;
1438                                 }
1439 
1440                                 srchClass = srchClass.getSuperclass();
1441                             }
1442 
1443                             if (resource == null) {
1444                                 return null;
1445                             }
1446 
1447                             BufferedInputStream in =
1448                                 new BufferedInputStream(resource);




 433      * Clips the passed in String to the space provided.  NOTE: this assumes
 434      * the string does not fit in the available space.
 435      *
 436      * @param c JComponent that will display the string, may be null
 437      * @param fm FontMetrics used to measure the String width
 438      * @param string String to display
 439      * @param availTextWidth Amount of space that the string can be drawn in
 440      * @return Clipped string that can fit in the provided space.
 441      */
 442     public static String clipString(JComponent c, FontMetrics fm,
 443                                     String string, int availTextWidth) {
 444         // c may be null here.
 445         String clipString = "...";
 446         int stringLength = string.length();
 447         availTextWidth -= SwingUtilities2.stringWidth(c, fm, clipString);
 448         if (availTextWidth <= 0) {
 449             //can not fit any characters
 450             return clipString;
 451         }
 452 
 453         boolean needsTextLayout;
 454 
 455         synchronized (charsBufferLock) {
 456             if (charsBuffer == null || charsBuffer.length < stringLength) {
 457                 charsBuffer  = string.toCharArray();
 458             } else {
 459                 string.getChars(0, stringLength, charsBuffer, 0);
 460             }
 461             needsTextLayout =
 462                 isComplexLayout(charsBuffer, 0, stringLength);
 463             if (!needsTextLayout) {
 464                 int width = 0;
 465                 for (int nChars = 0; nChars < stringLength; nChars++) {
 466                     width += fm.charWidth(charsBuffer[nChars]);
 467                     if (width > availTextWidth) {
 468                         string = string.substring(0, nChars);
 469                         break;
 470                     }
 471                 }
 472             }
 473         }


 688      * Does not check the "Table.isFileList" property. That should be checked
 689      * before calling this method.
 690      * This is used to make WindowsL&F JFileChooser act like native dialogs.
 691      */
 692     public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
 693         if (table.convertColumnIndexToModel(column) != 0 || row == -1) {
 694             return true;
 695         }
 696         TableCellRenderer tcr = table.getCellRenderer(row, column);
 697         Object value = table.getValueAt(row, column);
 698         Component cell = tcr.getTableCellRendererComponent(table, value, false,
 699                 false, row, column);
 700         Dimension itemSize = cell.getPreferredSize();
 701         Rectangle cellBounds = table.getCellRect(row, column, false);
 702         cellBounds.width = itemSize.width;
 703         cellBounds.height = itemSize.height;
 704 
 705         // See if coords are inside
 706         // ASSUME: mouse x,y will never be < cell's x,y
 707         assert (p.x >= cellBounds.x && p.y >= cellBounds.y);
 708         return p.x > cellBounds.x + cellBounds.width ||
 709                 p.y > cellBounds.y + cellBounds.height;



 710     }
 711 
 712     /**
 713      * Set the lead and anchor without affecting selection.
 714      */
 715     public static void setLeadAnchorWithoutSelection(ListSelectionModel model,
 716                                                      int lead, int anchor) {
 717         if (anchor == -1) {
 718             anchor = lead;
 719         }
 720         if (lead == -1) {
 721             model.setAnchorSelectionIndex(-1);
 722             model.setLeadSelectionIndex(-1);
 723         } else {
 724             if (model.isSelectedIndex(lead)) {
 725                 model.addSelectionInterval(lead, lead);
 726             } else {
 727                 model.removeSelectionInterval(lead, lead);
 728             }
 729             model.setAnchorSelectionIndex(anchor);


1209 
1210     /**
1211      * Returns true if the given event has permissions to access the
1212      * system clipboard
1213      *
1214      * @param e AWTEvent to check
1215      */
1216     public static boolean canEventAccessSystemClipboard(AWTEvent e) {
1217         return isTrustedContext()
1218             || canEventAccessSystemClipboard(e, false);
1219     }
1220 
1221     /**
1222      * returns canAccessSystemClipboard field from InputEvent
1223      *
1224      * @param ie InputEvent to get the field from
1225      */
1226     private static synchronized boolean inputEvent_canAccessSystemClipboard(InputEvent ie) {
1227         if (inputEvent_CanAccessSystemClipboard_Field == null) {
1228             inputEvent_CanAccessSystemClipboard_Field =
1229                 AccessController.doPrivileged(
1230                     new java.security.PrivilegedAction<Field>() {
1231                         public Field run() {

1232                             try {
1233                                 Field field = InputEvent.class.
1234                                     getDeclaredField("canAccessSystemClipboard");
1235                                 field.setAccessible(true);
1236                                 return field;
1237                             } catch (SecurityException e) {
1238                             } catch (NoSuchFieldException e) {
1239                             }
1240                             return null;
1241                         }
1242                     });
1243         }
1244         if (inputEvent_CanAccessSystemClipboard_Field == null) {
1245             return false;
1246         }
1247         boolean ret = false;
1248         try {
1249             ret = inputEvent_CanAccessSystemClipboard_Field.
1250                 getBoolean(ie);
1251         } catch(IllegalAccessException e) {
1252         }
1253         return ret;


1401      * @param rootClass an ancestor of <code>baseClass</code> to finish the
1402      *                  search at
1403      * @param imageFile the name of the file to be found
1404      * @return a lazy value that creates the <code>ImageIcon</code>
1405      *         <code>UIResource</code> for the image,
1406      *         or null if it cannot be found
1407      */
1408     public static Object makeIcon(final Class<?> baseClass,
1409                                   final Class<?> rootClass,
1410                                   final String imageFile) {
1411 
1412         return new UIDefaults.LazyValue() {
1413             public Object createValue(UIDefaults table) {
1414                 /* Copy resource into a byte array.  This is
1415                  * necessary because several browsers consider
1416                  * Class.getResource a security risk because it
1417                  * can be used to load additional classes.
1418                  * Class.getResourceAsStream just returns raw
1419                  * bytes, which we can convert to an image.
1420                  */
1421                 byte[] buffer =
1422                     java.security.AccessController.doPrivileged(
1423                         new java.security.PrivilegedAction<byte[]>() {
1424                     public byte[] run() {
1425                         try {
1426                             InputStream resource = null;
1427                             Class<?> srchClass = baseClass;
1428 
1429                             while (srchClass != null) {
1430                                 resource = srchClass.getResourceAsStream(imageFile);
1431 
1432                                 if (resource != null || srchClass == rootClass) {
1433                                     break;
1434                                 }
1435 
1436                                 srchClass = srchClass.getSuperclass();
1437                             }
1438 
1439                             if (resource == null) {
1440                                 return null;
1441                             }
1442 
1443                             BufferedInputStream in =
1444                                 new BufferedInputStream(resource);


< prev index next >