--- old/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java 2016-01-25 00:13:42.000000000 +0400 +++ new/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java 2016-01-25 00:13:41.000000000 +0400 @@ -60,6 +60,9 @@ import java.util.concurrent.Callable; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; +import javax.swing.plaf.ComponentUI; +import javax.swing.plaf.TextUIDrawing; +import javax.swing.plaf.UIResource; /** * A collection of utility methods for Swing. @@ -183,9 +186,9 @@ // Many of the following methods are invoked from older API. // As this older API was not passed a Component, a null Component may // now be passsed in. For example, SwingUtilities.computeStringWidth - // is implemented to call SwingUtilities2.stringWidth, the + // is implemented to call getTextUIDrawing().getStringWidth, the // SwingUtilities variant does not take a JComponent, as such - // SwingUtilities2.stringWidth can be passed a null Component. + // getTextUIDrawing().getStringWidth can be passed a null Component. // In other words, if you add new functionality to these methods you // need to gracefully handle null. // @@ -2005,4 +2008,135 @@ public interface RepaintListener { void repaintPerformed(JComponent c, int x, int y, int w, int h); } + + + public static final TextUIDrawing DEFAULT_UI_TEXT_DRAWING + = new DefaultUITextDrawing(); + + + public static TextUIDrawing getTextUIDrawing(JComponent comp) { + + if (comp != null) { + ComponentUI compUI = comp.getUI(); + if (compUI != null) { + TextUIDrawing uiTextDrawing = compUI.getTextUIDrawing(); + if (uiTextDrawing != null) { + return uiTextDrawing; + } + } + } + + return DEFAULT_UI_TEXT_DRAWING; + } + + public static class DefaultUITextDrawing implements TextUIDrawing, UIResource { + + /** + * Draws the passed in string at the specified location using the given + * graphics context. Anti-aliasing hints and a numeric shaper from + * the provided component are used to draw the string for the non-print + * graphics context. + * Nothing is drawn for the null string. + * + * @param c the component that will display the string, may be null + * @param g the graphics context, must not be null + * @param string the string to display, may be null + * @param x the x coordinate to draw the text at + * @param y the y coordinate to draw the text at + * @throws NullPointerException if the specified {@code g} is {@code null} + * + * @since 9 + */ + @Override + public void drawString(JComponent c, Graphics g, String string, + int x, int y) { + SwingUtilities2.drawString(c, g, string, x, y); + } + + /** + * Draws the passed in string at the specified location underlining + * the specified character using the given graphics context. + * The provided component is used to query a numeric shaper and anti-aliasing + * hints for the non-print graphics context. + *

+ * The {@code underlinedIndex} parameter points to a char value + * (Unicode code unit) in the given string. + * If the char value specified at the underlined index is in + * the high-surrogate range and the char value at the following index is in + * the low-surrogate range then the supplementary character corresponding + * to this surrogate pair is underlined. + *

+ * No character is underlined if the index is negative or greater + * than the string length {@code (index < 0 || index >= string.length())} + * or if the char value specified at the given index + * is in the low-surrogate range. + * + * @param c the component that will display the string, may be null + * @param g the graphics context, must not be null + * @param string the string to display, may be null + * @param underlinedIndex index of a a char value (Unicode code unit) + * in the string to underline + * @param x the x coordinate to draw the text at + * @param y the y coordinate to draw the text at + * @throws NullPointerException if the specified {@code g} is {@code null} + * + * @see #getStringWidth + * + * @since 9 + */ + @Override + public void drawStringUnderlineCharAt(JComponent c, Graphics g, + String string, int underlinedIndex, int x, int y) { + SwingUtilities2.drawStringUnderlineCharAt(c, g, string, underlinedIndex, x, y); + } + + /** + * Replaces last characters of the passed in string with ellipsis that it + * width fits to the space provided. A numeric shaper from the given + * component is used for the string width calculation. + * The unchanged string is returned if the space provided is greater than + * the string width. Ellipsis is returned if the string width is not greater + * than ellipsis width. + * + * @param c the component + * @param fm the FontMetrics used to measure the string width, must not be null + * @param string the string to clip, may be null + * @param availTextWidth the amount of space that the string can be drawn in + * @return the clipped string that fits in the provided space, an empty + * string if the given string argument is {@code null} or empty, + * ellipses if the string width is not greater than ellipsis width. + * @throws NullPointerException if the specified {@code fm} is {@code null} + * + * @see #getStringWidth + * + * @since 9 + */ + @Override + public String getClippedString(JComponent c, FontMetrics fm, + String string, int availTextWidth) { + return SwingUtilities2.clipStringIfNecessary(c, fm, string, availTextWidth); + } + + /** + * Returns the total advance width of the passed in string using the given + * font metrics and numeric shaper from the given component. + * The advance is the distance from the leftmost point to the rightmost point + * on the string's baseline. + * If the passed string is {@code null}, returns zero. + * + * @param c the component, may be null + * @param fm the FontMetrics used to measure the advance string width, + * must not be null + * @param string the string to get the advance width of, may be null + * @return the advance width of the specified string, zero is returned for + * {@code null} string + * @throws NullPointerException if the specified {@code fm} is {@code null} + * + * @since 9 + */ + @Override + public int getStringWidth(JComponent c, FontMetrics fm, String string) { + return SwingUtilities2.stringWidth(c, fm, string); + } + } }