jdk/src/java.desktop/share/classes/javax/swing/text/PasswordView.java
changeset 41807 f9eb6cb54fed
parent 39553 965a62655c4c
child 42216 621af0ebf6c4
equal deleted inserted replaced
41806:91176d55df60 41807:f9eb6cb54fed
    24  */
    24  */
    25 package javax.swing.text;
    25 package javax.swing.text;
    26 
    26 
    27 import sun.swing.SwingUtilities2;
    27 import sun.swing.SwingUtilities2;
    28 import java.awt.*;
    28 import java.awt.*;
       
    29 import java.awt.font.FontRenderContext;
       
    30 import java.security.AccessController;
       
    31 import java.security.PrivilegedAction;
    29 import javax.swing.JPasswordField;
    32 import javax.swing.JPasswordField;
       
    33 import static javax.swing.text.PlainView.isFPMethodOverriden;
    30 
    34 
    31 /**
    35 /**
    32  * Implements a View suitable for use in JPasswordField
    36  * Implements a View suitable for use in JPasswordField
    33  * UI implementations.  This is basically a field ui that
    37  * UI implementations.  This is basically a field ui that
    34  * renders its contents as the echo character specified
    38  * renders its contents as the echo character specified
    59      * @param y the starting Y coordinate >= 0
    63      * @param y the starting Y coordinate >= 0
    60      * @param p0 the starting offset in the model >= 0
    64      * @param p0 the starting offset in the model >= 0
    61      * @param p1 the ending offset in the model >= p0
    65      * @param p1 the ending offset in the model >= p0
    62      * @return the X location of the end of the range >= 0
    66      * @return the X location of the end of the range >= 0
    63      * @exception BadLocationException if p0 or p1 are out of range
    67      * @exception BadLocationException if p0 or p1 are out of range
    64      */
    68      *
       
    69      * @deprecated replaced by
       
    70      *     {@link #drawUnselectedText(Graphics2D, float, float, int, int)}
       
    71      */
       
    72     @Deprecated(since = "9")
       
    73     @Override
    65     protected int drawUnselectedText(Graphics g, int x, int y,
    74     protected int drawUnselectedText(Graphics g, int x, int y,
    66                                      int p0, int p1) throws BadLocationException {
    75                                      int p0, int p1) throws BadLocationException {
    67 
    76         return (int) drawUnselectedTextImpl(g, x, y, p0, p1, false);
       
    77     }
       
    78 
       
    79     @Override
       
    80     protected float drawUnselectedText(Graphics2D g, float x, float y,
       
    81                                        int p0, int p1)
       
    82             throws BadLocationException
       
    83     {
       
    84         return drawUnselectedTextImpl(g, x, y, p0, p1, true);
       
    85     }
       
    86 
       
    87     private float drawUnselectedTextImpl(Graphics g, float x, float y,
       
    88                                          int p0, int p1,
       
    89                                          boolean useFPAPI)
       
    90             throws BadLocationException
       
    91     {
    68         Container c = getContainer();
    92         Container c = getContainer();
    69         if (c instanceof JPasswordField) {
    93         if (c instanceof JPasswordField) {
    70             JPasswordField f = (JPasswordField) c;
    94             JPasswordField f = (JPasswordField) c;
    71             if (! f.echoCharIsSet()) {
    95             if (!f.echoCharIsSet()) {
    72                 return super.drawUnselectedText(g, x, y, p0, p1);
    96                 boolean useDrawUnselectedFPAPI = useFPAPI
       
    97                         && drawUnselectedTextOverridden
       
    98                         && g instanceof Graphics2D;
       
    99                 return (useDrawUnselectedFPAPI )
       
   100                         ? super.drawUnselectedText((Graphics2D) g, x, y, p0, p1)
       
   101                         : super.drawUnselectedText(g, (int) x, (int) y, p0, p1);
    73             }
   102             }
    74             if (f.isEnabled()) {
   103             if (f.isEnabled()) {
    75                 g.setColor(f.getForeground());
   104                 g.setColor(f.getForeground());
    76             }
   105             }
    77             else {
   106             else {
    78                 g.setColor(f.getDisabledTextColor());
   107                 g.setColor(f.getDisabledTextColor());
    79             }
   108             }
    80             char echoChar = f.getEchoChar();
   109             char echoChar = f.getEchoChar();
    81             int n = p1 - p0;
   110             int n = p1 - p0;
       
   111             boolean useEchoCharFPAPI = useFPAPI
       
   112                     && drawEchoCharacterOverridden
       
   113                     && g instanceof Graphics2D;
    82             for (int i = 0; i < n; i++) {
   114             for (int i = 0; i < n; i++) {
    83                 x = drawEchoCharacter(g, x, y, echoChar);
   115                 x = (useEchoCharFPAPI)
       
   116                         ? drawEchoCharacter((Graphics2D) g, x, y, echoChar)
       
   117                         : drawEchoCharacter(g, (int) x, (int) y, echoChar);
    84             }
   118             }
    85         }
   119         }
    86         return x;
   120         return x;
    87     }
   121     }
    88 
   122 
    98      * @param y the starting Y coordinate &gt;= 0
   132      * @param y the starting Y coordinate &gt;= 0
    99      * @param p0 the starting offset in the model &gt;= 0
   133      * @param p0 the starting offset in the model &gt;= 0
   100      * @param p1 the ending offset in the model &gt;= p0
   134      * @param p1 the ending offset in the model &gt;= p0
   101      * @return the X location of the end of the range &gt;= 0
   135      * @return the X location of the end of the range &gt;= 0
   102      * @exception BadLocationException if p0 or p1 are out of range
   136      * @exception BadLocationException if p0 or p1 are out of range
   103      */
   137      *
       
   138      * @deprecated replaced by
       
   139      *     {@link #drawSelectedText(Graphics2D, float, float, int, int)}
       
   140      */
       
   141     @Deprecated(since = "9")
       
   142     @Override
   104     protected int drawSelectedText(Graphics g, int x,
   143     protected int drawSelectedText(Graphics g, int x,
   105                                    int y, int p0, int p1) throws BadLocationException {
   144                                    int y, int p0, int p1) throws BadLocationException {
       
   145         return (int) drawSelectedTextImpl(g, x, y, p0, p1, false);
       
   146     }
       
   147 
       
   148     @Override
       
   149     protected float drawSelectedText(Graphics2D g, float x, float y,
       
   150                                      int p0, int p1) throws BadLocationException
       
   151     {
       
   152         return drawSelectedTextImpl(g, x, y, p0, p1, true);
       
   153     }
       
   154 
       
   155     private float drawSelectedTextImpl(Graphics g, float x, float y,
       
   156                                        int p0, int p1,
       
   157                                        boolean useFPAPI)
       
   158             throws BadLocationException {
   106         g.setColor(selected);
   159         g.setColor(selected);
   107         Container c = getContainer();
   160         Container c = getContainer();
   108         if (c instanceof JPasswordField) {
   161         if (c instanceof JPasswordField) {
   109             JPasswordField f = (JPasswordField) c;
   162             JPasswordField f = (JPasswordField) c;
   110             if (! f.echoCharIsSet()) {
   163             if (!f.echoCharIsSet()) {
   111                 return super.drawSelectedText(g, x, y, p0, p1);
   164                 boolean useDrawUnselectedFPAPI = useFPAPI
       
   165                         && drawSelectedTextOverridden
       
   166                         && g instanceof Graphics2D;
       
   167                 return (useFPAPI)
       
   168                         ? super.drawSelectedText((Graphics2D) g, x, y, p0, p1)
       
   169                         : super.drawSelectedText(g, (int) x, (int) y, p0, p1);
   112             }
   170             }
   113             char echoChar = f.getEchoChar();
   171             char echoChar = f.getEchoChar();
   114             int n = p1 - p0;
   172             int n = p1 - p0;
       
   173             boolean useEchoCharFPAPI = useFPAPI
       
   174                     && drawEchoCharacterOverridden
       
   175                     && g instanceof Graphics2D;
   115             for (int i = 0; i < n; i++) {
   176             for (int i = 0; i < n; i++) {
   116                 x = drawEchoCharacter(g, x, y, echoChar);
   177                 x = (useEchoCharFPAPI)
       
   178                         ? drawEchoCharacter((Graphics2D) g, x, y, echoChar)
       
   179                         : drawEchoCharacter(g, (int) x, (int) y, echoChar);
       
   180 
   117             }
   181             }
   118         }
   182         }
   119         return x;
   183         return x;
   120     }
   184     }
   121 
   185 
   128      * @param g the graphics context
   192      * @param g the graphics context
   129      * @param x the starting X coordinate &gt;= 0
   193      * @param x the starting X coordinate &gt;= 0
   130      * @param y the starting Y coordinate &gt;= 0
   194      * @param y the starting Y coordinate &gt;= 0
   131      * @param c the echo character
   195      * @param c the echo character
   132      * @return the updated X position &gt;= 0
   196      * @return the updated X position &gt;= 0
   133      */
   197      *
       
   198      * @deprecated replaced by
       
   199      *     {@link #drawEchoCharacter(Graphics2D, float, float, char)}
       
   200      */
       
   201     @Deprecated(since = "9")
   134     protected int drawEchoCharacter(Graphics g, int x, int y, char c) {
   202     protected int drawEchoCharacter(Graphics g, int x, int y, char c) {
   135         ONE[0] = c;
   203         return (int) drawEchoCharacterImpl(g, x, y, c, false);
   136         SwingUtilities2.drawChars(Utilities.getJComponent(this),
       
   137                                   g, ONE, 0, 1, x, y);
       
   138         return x + g.getFontMetrics().charWidth(c);
       
   139     }
   204     }
   140 
   205 
   141     /**
   206     /**
   142      * Renders the echo character, or whatever graphic should be used
   207      * Renders the echo character, or whatever graphic should be used
   143      * to display the password characters.  The color in the Graphics
   208      * to display the password characters.  The color in the Graphics
   144      * object is set to the appropriate foreground color for selected
   209      * object is set to the appropriate foreground color for selected
   145      * or unselected text.
   210      * or unselected text.
   146      *
       
   147      * @implSpec This implementation calls
       
   148      * {@link #drawEchoCharacter(Graphics, int, int, char)
       
   149      *      drawEchoCharacter((Graphics) g, (int) x, (int) y, c)}.
       
   150      *
   211      *
   151      * @param g the graphics context
   212      * @param g the graphics context
   152      * @param x the starting X coordinate {@code >= 0}
   213      * @param x the starting X coordinate {@code >= 0}
   153      * @param y the starting Y coordinate {@code >= 0}
   214      * @param y the starting Y coordinate {@code >= 0}
   154      * @param c the echo character
   215      * @param c the echo character
   155      * @return the updated X position {@code >= 0}
   216      * @return the updated X position {@code >= 0}
       
   217      *
       
   218      * @since 9
   156      */
   219      */
   157     protected float drawEchoCharacter(Graphics2D g, float x, float y, char c) {
   220     protected float drawEchoCharacter(Graphics2D g, float x, float y, char c) {
   158         return drawEchoCharacter((Graphics) g, (int) x, (int) y, c);
   221         return drawEchoCharacterImpl(g, x, y, c, true);
       
   222     }
       
   223 
       
   224     private float drawEchoCharacterImpl(Graphics g, float x, float y,
       
   225                                         char c, boolean useFPAPI) {
       
   226         ONE[0] = c;
       
   227         SwingUtilities2.drawChars(Utilities.getJComponent(this),
       
   228                                   g, ONE, 0, 1, x, y);
       
   229         if (useFPAPI) {
       
   230             return x + g.getFontMetrics().charWidth(c);
       
   231         } else {
       
   232             FontRenderContext frc = g.getFontMetrics().getFontRenderContext();
       
   233             return x + (float) g.getFont().getStringBounds(ONE, 0, 1, frc).getWidth();
       
   234         }
   159     }
   235     }
   160 
   236 
   161     /**
   237     /**
   162      * Provides a mapping from the document model coordinate space
   238      * Provides a mapping from the document model coordinate space
   163      * to the coordinate space of the view mapped to it.
   239      * to the coordinate space of the view mapped to it.
   251         }
   327         }
   252         return super.getPreferredSpan(axis);
   328         return super.getPreferredSpan(axis);
   253     }
   329     }
   254 
   330 
   255     static char[] ONE = new char[1];
   331     static char[] ONE = new char[1];
       
   332 
       
   333     private final boolean drawEchoCharacterOverridden;
       
   334 
       
   335     {
       
   336         final Class<?> CLS = getClass();
       
   337         final Class<?> INT = Integer.TYPE;
       
   338         final Class<?> FP = Float.TYPE;
       
   339         final Class<?> CHAR = Character.TYPE;
       
   340 
       
   341         drawEchoCharacterOverridden = AccessController
       
   342                 .doPrivileged(new PrivilegedAction<Boolean>() {
       
   343             @Override
       
   344             public Boolean run() {
       
   345                 Class<?>[] intTypes = {Graphics.class, INT, INT, CHAR};
       
   346                 Class<?>[] fpTypes = {Graphics2D.class, FP, FP, CHAR};
       
   347                 return isFPMethodOverriden("drawEchoCharacter", CLS, intTypes, fpTypes);
       
   348             }
       
   349         });
       
   350     }
   256 }
   351 }