< prev index next >

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

Print this page




  43 import javax.swing.text.DefaultHighlighter;
  44 import javax.swing.text.DefaultCaret;
  45 import javax.swing.table.TableCellRenderer;
  46 import javax.swing.table.TableColumnModel;
  47 import javax.swing.tree.TreeModel;
  48 import javax.swing.tree.TreePath;
  49 
  50 import sun.print.ProxyPrintGraphics;
  51 import sun.awt.*;
  52 import java.io.*;
  53 import java.security.AccessController;
  54 import java.security.PrivilegedAction;
  55 import java.util.*;
  56 import sun.font.FontDesignMetrics;
  57 import sun.font.FontUtilities;
  58 import sun.java2d.SunGraphicsEnvironment;
  59 
  60 import java.util.concurrent.Callable;
  61 import java.util.concurrent.Future;
  62 import java.util.concurrent.FutureTask;



  63 
  64 /**
  65  * A collection of utility methods for Swing.
  66  * <p>
  67  * <b>WARNING:</b> While this class is public, it should not be treated as
  68  * public API and its API may change in incompatable ways between dot dot
  69  * releases and even patch releases. You should not rely on this class even
  70  * existing.
  71  *
  72  */
  73 public class SwingUtilities2 {
  74     /**
  75      * The {@code AppContext} key for our one {@code LAFState}
  76      * instance.
  77      */
  78     public static final Object LAF_STATE_KEY =
  79             new StringBuffer("LookAndFeel State");
  80 
  81     public static final Object MENU_SELECTION_MANAGER_LISTENER_KEY =
  82             new StringBuffer("MenuSelectionManager listener key");


 166     }
 167 
 168     /**
 169      * checks whether TextLayout is required to handle characters.
 170      *
 171      * @param text characters to be tested
 172      * @param start start
 173      * @param limit limit
 174      * @return {@code true}  if TextLayout is required
 175      *         {@code false} if TextLayout is not required
 176      */
 177     public static final boolean isComplexLayout(char[] text, int start, int limit) {
 178         return FontUtilities.isComplexText(text, start, limit);
 179     }
 180 
 181     //
 182     // WARNING WARNING WARNING WARNING WARNING WARNING
 183     // Many of the following methods are invoked from older API.
 184     // As this older API was not passed a Component, a null Component may
 185     // now be passsed in.  For example, SwingUtilities.computeStringWidth
 186     // is implemented to call SwingUtilities2.stringWidth, the
 187     // SwingUtilities variant does not take a JComponent, as such
 188     // SwingUtilities2.stringWidth can be passed a null Component.
 189     // In other words, if you add new functionality to these methods you
 190     // need to gracefully handle null.
 191     //
 192 
 193     /**
 194      * Returns the left side bearing of the first character of string. The
 195      * left side bearing is calculated from the passed in
 196      * FontMetrics.  If the passed in String is less than one
 197      * character {@code 0} is returned.
 198      *
 199      * @param c JComponent that will display the string
 200      * @param fm FontMetrics used to measure the String width
 201      * @param string String to get the left side bearing for.
 202      * @throws NullPointerException if {@code string} is {@code null}
 203      *
 204      * @return the left side bearing of the first character of string
 205      * or {@code 0} if the string is empty
 206      */
 207     public static int getLeftSideBearing(JComponent c, FontMetrics fm,
 208                                          String string) {


1988      * @param model  corresponing tree model
1989      * @return  the path to the changed nodes
1990      */
1991     public static TreePath getTreePath(TreeModelEvent event, TreeModel model) {
1992         TreePath path = event.getTreePath();
1993         if ((path == null) && (model != null)) {
1994             Object root = model.getRoot();
1995             if (root != null) {
1996                 path = new TreePath(root);
1997             }
1998         }
1999         return path;
2000     }
2001 
2002     /**
2003      * Used to listen to "blit" repaints in RepaintManager.
2004      */
2005     public interface RepaintListener {
2006         void repaintPerformed(JComponent c, int x, int y, int w, int h);
2007     }



































































































































2008 }


  43 import javax.swing.text.DefaultHighlighter;
  44 import javax.swing.text.DefaultCaret;
  45 import javax.swing.table.TableCellRenderer;
  46 import javax.swing.table.TableColumnModel;
  47 import javax.swing.tree.TreeModel;
  48 import javax.swing.tree.TreePath;
  49 
  50 import sun.print.ProxyPrintGraphics;
  51 import sun.awt.*;
  52 import java.io.*;
  53 import java.security.AccessController;
  54 import java.security.PrivilegedAction;
  55 import java.util.*;
  56 import sun.font.FontDesignMetrics;
  57 import sun.font.FontUtilities;
  58 import sun.java2d.SunGraphicsEnvironment;
  59 
  60 import java.util.concurrent.Callable;
  61 import java.util.concurrent.Future;
  62 import java.util.concurrent.FutureTask;
  63 import javax.swing.plaf.ComponentUI;
  64 import javax.swing.plaf.TextUIDrawing;
  65 import javax.swing.plaf.UIResource;
  66 
  67 /**
  68  * A collection of utility methods for Swing.
  69  * <p>
  70  * <b>WARNING:</b> While this class is public, it should not be treated as
  71  * public API and its API may change in incompatable ways between dot dot
  72  * releases and even patch releases. You should not rely on this class even
  73  * existing.
  74  *
  75  */
  76 public class SwingUtilities2 {
  77     /**
  78      * The {@code AppContext} key for our one {@code LAFState}
  79      * instance.
  80      */
  81     public static final Object LAF_STATE_KEY =
  82             new StringBuffer("LookAndFeel State");
  83 
  84     public static final Object MENU_SELECTION_MANAGER_LISTENER_KEY =
  85             new StringBuffer("MenuSelectionManager listener key");


 169     }
 170 
 171     /**
 172      * checks whether TextLayout is required to handle characters.
 173      *
 174      * @param text characters to be tested
 175      * @param start start
 176      * @param limit limit
 177      * @return {@code true}  if TextLayout is required
 178      *         {@code false} if TextLayout is not required
 179      */
 180     public static final boolean isComplexLayout(char[] text, int start, int limit) {
 181         return FontUtilities.isComplexText(text, start, limit);
 182     }
 183 
 184     //
 185     // WARNING WARNING WARNING WARNING WARNING WARNING
 186     // Many of the following methods are invoked from older API.
 187     // As this older API was not passed a Component, a null Component may
 188     // now be passsed in.  For example, SwingUtilities.computeStringWidth
 189     // is implemented to call getTextUIDrawing().getStringWidth, the
 190     // SwingUtilities variant does not take a JComponent, as such
 191     // getTextUIDrawing().getStringWidth can be passed a null Component.
 192     // In other words, if you add new functionality to these methods you
 193     // need to gracefully handle null.
 194     //
 195 
 196     /**
 197      * Returns the left side bearing of the first character of string. The
 198      * left side bearing is calculated from the passed in
 199      * FontMetrics.  If the passed in String is less than one
 200      * character {@code 0} is returned.
 201      *
 202      * @param c JComponent that will display the string
 203      * @param fm FontMetrics used to measure the String width
 204      * @param string String to get the left side bearing for.
 205      * @throws NullPointerException if {@code string} is {@code null}
 206      *
 207      * @return the left side bearing of the first character of string
 208      * or {@code 0} if the string is empty
 209      */
 210     public static int getLeftSideBearing(JComponent c, FontMetrics fm,
 211                                          String string) {


1991      * @param model  corresponing tree model
1992      * @return  the path to the changed nodes
1993      */
1994     public static TreePath getTreePath(TreeModelEvent event, TreeModel model) {
1995         TreePath path = event.getTreePath();
1996         if ((path == null) && (model != null)) {
1997             Object root = model.getRoot();
1998             if (root != null) {
1999                 path = new TreePath(root);
2000             }
2001         }
2002         return path;
2003     }
2004 
2005     /**
2006      * Used to listen to "blit" repaints in RepaintManager.
2007      */
2008     public interface RepaintListener {
2009         void repaintPerformed(JComponent c, int x, int y, int w, int h);
2010     }
2011 
2012 
2013     public static final TextUIDrawing DEFAULT_UI_TEXT_DRAWING
2014             = new DefaultUITextDrawing();
2015 
2016 
2017     public static TextUIDrawing getTextUIDrawing(JComponent comp) {
2018 
2019         if (comp != null) {
2020             ComponentUI compUI = comp.getUI();
2021             if (compUI != null) {
2022                 TextUIDrawing uiTextDrawing = compUI.getTextUIDrawing();
2023                 if (uiTextDrawing != null) {
2024                     return uiTextDrawing;
2025                 }
2026             }
2027         }
2028 
2029         return DEFAULT_UI_TEXT_DRAWING;
2030     }
2031 
2032     public static class DefaultUITextDrawing implements TextUIDrawing, UIResource {
2033 
2034         /**
2035          * Draws the passed in string at the specified location using the given
2036          * graphics context. Anti-aliasing hints and a numeric shaper from
2037          * the provided component are used to draw the string for the non-print
2038          * graphics context.
2039          * Nothing is drawn for the null string.
2040          *
2041          * @param c the component that will display the string, may be null
2042          * @param g the graphics context, must not be null
2043          * @param string the string to display, may be null
2044          * @param x the x coordinate to draw the text at
2045          * @param y the y coordinate to draw the text at
2046          * @throws NullPointerException if the specified {@code g} is {@code null}
2047          *
2048          * @since 9
2049          */
2050         @Override
2051         public void drawString(JComponent c, Graphics g, String string,
2052                 int x, int y) {
2053             SwingUtilities2.drawString(c, g, string, x, y);
2054         }
2055 
2056         /**
2057          * Draws the passed in string at the specified location underlining
2058          * the specified character using the given graphics context.
2059          * The provided component is used to query a numeric shaper and anti-aliasing
2060          * hints for the non-print graphics context.
2061          * <p>
2062          * The {@code underlinedIndex} parameter points to a char value
2063          * (Unicode code unit) in the given string.
2064          * If the char value specified at the underlined index is in
2065          * the high-surrogate range and the char value at the following index is in
2066          * the low-surrogate range then the supplementary character corresponding
2067          * to this surrogate pair is underlined.
2068          * <p>
2069          * No character is underlined if the index is negative or greater
2070          * than the string length {@code (index < 0 || index >= string.length())}
2071          * or if the char value specified at the given index
2072          * is in the low-surrogate range.
2073          *
2074          * @param c the component that will display the string, may be null
2075          * @param g the graphics context, must not be null
2076          * @param string the string to display, may be null
2077          * @param underlinedIndex index of a a char value (Unicode code unit)
2078          *        in the string to underline
2079          * @param x the x coordinate to draw the text at
2080          * @param y the y coordinate to draw the text at
2081          * @throws NullPointerException if the specified {@code g} is {@code null}
2082          *
2083          * @see #getStringWidth
2084          *
2085          * @since 9
2086          */
2087         @Override
2088         public void drawStringUnderlineCharAt(JComponent c, Graphics g,
2089                 String string, int underlinedIndex, int x, int y) {
2090             SwingUtilities2.drawStringUnderlineCharAt(c, g, string, underlinedIndex, x, y);
2091         }
2092 
2093         /**
2094          * Replaces last characters of the passed in string with ellipsis that it
2095          * width fits to the space provided. A numeric shaper from the given
2096          * component is used for the string width calculation.
2097          * The unchanged string is returned if the space provided is greater than
2098          * the string width. Ellipsis is returned if the string width is not greater
2099          * than ellipsis width.
2100          *
2101          * @param c the component
2102          * @param fm the FontMetrics used to measure the string width, must not be null
2103          * @param string the string to clip, may be null
2104          * @param availTextWidth the amount of space that the string can be drawn in
2105          * @return the clipped string that fits in the provided space, an empty
2106          *         string if the given string argument is {@code null} or empty,
2107          *         ellipses if the string width is not greater than ellipsis width.
2108          * @throws NullPointerException if the specified {@code fm} is {@code null}
2109          *
2110          * @see #getStringWidth
2111          *
2112          * @since 9
2113          */
2114         @Override
2115         public String getClippedString(JComponent c, FontMetrics fm,
2116                 String string, int availTextWidth) {
2117             return SwingUtilities2.clipStringIfNecessary(c, fm, string, availTextWidth);
2118         }
2119 
2120         /**
2121          * Returns the total advance width of the passed in string using the given
2122          * font metrics and numeric shaper from the given component.
2123          * The advance is the distance from the leftmost point to the rightmost point
2124          * on the string's baseline.
2125          * If the passed string is {@code null}, returns zero.
2126          *
2127          * @param c the component, may be null
2128          * @param fm the FontMetrics used to measure the advance string width,
2129          *        must not be null
2130          * @param string the string to get the advance width of, may be null
2131          * @return the advance width of the specified string, zero is returned for
2132          *         {@code null} string
2133          * @throws NullPointerException if the specified {@code fm} is {@code null}
2134          *
2135          * @since 9
2136          */
2137         @Override
2138         public int getStringWidth(JComponent c, FontMetrics fm, String string) {
2139             return SwingUtilities2.stringWidth(c, fm, string);
2140         }
2141     }
2142 }
< prev index next >