0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.text;
0N/A
0N/Aimport sun.swing.SwingUtilities2;
0N/Aimport java.awt.*;
0N/Aimport javax.swing.JPasswordField;
0N/A
0N/A/**
0N/A * Implements a View suitable for use in JPasswordField
0N/A * UI implementations. This is basically a field ui that
0N/A * renders its contents as the echo character specified
0N/A * in the associated component (if it can narrow the
0N/A * component to a JPasswordField).
0N/A *
0N/A * @author Timothy Prinzing
0N/A * @see View
0N/A */
0N/Apublic class PasswordView extends FieldView {
0N/A
0N/A /**
0N/A * Constructs a new view wrapped on an element.
0N/A *
0N/A * @param elem the element
0N/A */
0N/A public PasswordView(Element elem) {
0N/A super(elem);
0N/A }
0N/A
0N/A /**
0N/A * Renders the given range in the model as normal unselected
0N/A * text. This sets the foreground color and echos the characters
0N/A * using the value returned by getEchoChar().
0N/A *
0N/A * @param g the graphics context
0N/A * @param x the starting X coordinate >= 0
0N/A * @param y the starting Y coordinate >= 0
0N/A * @param p0 the starting offset in the model >= 0
0N/A * @param p1 the ending offset in the model >= p0
0N/A * @return the X location of the end of the range >= 0
0N/A * @exception BadLocationException if p0 or p1 are out of range
0N/A */
0N/A protected int drawUnselectedText(Graphics g, int x, int y,
0N/A int p0, int p1) throws BadLocationException {
0N/A
0N/A Container c = getContainer();
0N/A if (c instanceof JPasswordField) {
0N/A JPasswordField f = (JPasswordField) c;
0N/A if (! f.echoCharIsSet()) {
0N/A return super.drawUnselectedText(g, x, y, p0, p1);
0N/A }
0N/A if (f.isEnabled()) {
0N/A g.setColor(f.getForeground());
0N/A }
0N/A else {
0N/A g.setColor(f.getDisabledTextColor());
0N/A }
0N/A char echoChar = f.getEchoChar();
0N/A int n = p1 - p0;
0N/A for (int i = 0; i < n; i++) {
0N/A x = drawEchoCharacter(g, x, y, echoChar);
0N/A }
0N/A }
0N/A return x;
0N/A }
0N/A
0N/A /**
0N/A * Renders the given range in the model as selected text. This
0N/A * is implemented to render the text in the color specified in
0N/A * the hosting component. It assumes the highlighter will render
0N/A * the selected background. Uses the result of getEchoChar() to
0N/A * display the characters.
0N/A *
0N/A * @param g the graphics context
0N/A * @param x the starting X coordinate >= 0
0N/A * @param y the starting Y coordinate >= 0
0N/A * @param p0 the starting offset in the model >= 0
0N/A * @param p1 the ending offset in the model >= p0
0N/A * @return the X location of the end of the range >= 0
0N/A * @exception BadLocationException if p0 or p1 are out of range
0N/A */
0N/A protected int drawSelectedText(Graphics g, int x,
0N/A int y, int p0, int p1) throws BadLocationException {
0N/A g.setColor(selected);
0N/A Container c = getContainer();
0N/A if (c instanceof JPasswordField) {
0N/A JPasswordField f = (JPasswordField) c;
0N/A if (! f.echoCharIsSet()) {
0N/A return super.drawSelectedText(g, x, y, p0, p1);
0N/A }
0N/A char echoChar = f.getEchoChar();
0N/A int n = p1 - p0;
0N/A for (int i = 0; i < n; i++) {
0N/A x = drawEchoCharacter(g, x, y, echoChar);
0N/A }
0N/A }
0N/A return x;
0N/A }
0N/A
0N/A /**
0N/A * Renders the echo character, or whatever graphic should be used
0N/A * to display the password characters. The color in the Graphics
0N/A * object is set to the appropriate foreground color for selected
0N/A * or unselected text.
0N/A *
0N/A * @param g the graphics context
0N/A * @param x the starting X coordinate >= 0
0N/A * @param y the starting Y coordinate >= 0
0N/A * @param c the echo character
0N/A * @return the updated X position >= 0
0N/A */
0N/A protected int drawEchoCharacter(Graphics g, int x, int y, char c) {
0N/A ONE[0] = c;
0N/A SwingUtilities2.drawChars(Utilities.getJComponent(this),
0N/A g, ONE, 0, 1, x, y);
0N/A return x + g.getFontMetrics().charWidth(c);
0N/A }
0N/A
0N/A /**
0N/A * Provides a mapping from the document model coordinate space
0N/A * to the coordinate space of the view mapped to it.
0N/A *
0N/A * @param pos the position to convert >= 0
0N/A * @param a the allocated region to render into
0N/A * @return the bounding box of the given position
0N/A * @exception BadLocationException if the given position does not
0N/A * represent a valid location in the associated document
0N/A * @see View#modelToView
0N/A */
0N/A public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
0N/A Container c = getContainer();
0N/A if (c instanceof JPasswordField) {
0N/A JPasswordField f = (JPasswordField) c;
0N/A if (! f.echoCharIsSet()) {
0N/A return super.modelToView(pos, a, b);
0N/A }
0N/A char echoChar = f.getEchoChar();
0N/A FontMetrics m = f.getFontMetrics(f.getFont());
0N/A
0N/A Rectangle alloc = adjustAllocation(a).getBounds();
0N/A int dx = (pos - getStartOffset()) * m.charWidth(echoChar);
0N/A alloc.x += dx;
0N/A alloc.width = 1;
0N/A return alloc;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Provides a mapping from the view coordinate space to the logical
0N/A * coordinate space of the model.
0N/A *
0N/A * @param fx the X coordinate >= 0.0f
0N/A * @param fy the Y coordinate >= 0.0f
0N/A * @param a the allocated region to render into
0N/A * @return the location within the model that best represents the
0N/A * given point in the view
0N/A * @see View#viewToModel
0N/A */
0N/A public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
0N/A bias[0] = Position.Bias.Forward;
0N/A int n = 0;
0N/A Container c = getContainer();
0N/A if (c instanceof JPasswordField) {
0N/A JPasswordField f = (JPasswordField) c;
0N/A if (! f.echoCharIsSet()) {
0N/A return super.viewToModel(fx, fy, a, bias);
0N/A }
0N/A char echoChar = f.getEchoChar();
0N/A int charWidth = f.getFontMetrics(f.getFont()).charWidth(echoChar);
0N/A a = adjustAllocation(a);
0N/A Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
0N/A a.getBounds();
0N/A n = (charWidth > 0 ?
0N/A ((int)fx - alloc.x) / charWidth : Integer.MAX_VALUE);
0N/A if (n < 0) {
0N/A n = 0;
0N/A }
0N/A else if (n > (getStartOffset() + getDocument().getLength())) {
0N/A n = getDocument().getLength() - getStartOffset();
0N/A }
0N/A }
0N/A return getStartOffset() + n;
0N/A }
0N/A
0N/A /**
0N/A * Determines the preferred span for this view along an
0N/A * axis.
0N/A *
0N/A * @param axis may be either View.X_AXIS or View.Y_AXIS
0N/A * @return the span the view would like to be rendered into >= 0.
0N/A * Typically the view is told to render into the span
0N/A * that is returned, although there is no guarantee.
0N/A * The parent may choose to resize or break the view.
0N/A */
0N/A public float getPreferredSpan(int axis) {
0N/A switch (axis) {
0N/A case View.X_AXIS:
0N/A Container c = getContainer();
0N/A if (c instanceof JPasswordField) {
0N/A JPasswordField f = (JPasswordField) c;
0N/A if (f.echoCharIsSet()) {
0N/A char echoChar = f.getEchoChar();
0N/A FontMetrics m = f.getFontMetrics(f.getFont());
0N/A Document doc = getDocument();
0N/A return m.charWidth(echoChar) * getDocument().getLength();
0N/A }
0N/A }
0N/A }
0N/A return super.getPreferredSpan(axis);
0N/A }
0N/A
0N/A static char[] ONE = new char[1];
0N/A}