home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
jfc.bin
/
PasswordView.java
< prev
next >
Wrap
Text File
|
1998-02-26
|
4KB
|
125 lines
/*
* @(#)PasswordView.java 1.2 97/12/09
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
package com.sun.java.swing.text;
import java.awt.*;
import com.sun.java.swing.JPasswordField;
/**
* Implements a View suitable for use in JPasswordField
* UI implementations. This is basically a field ui that
* renders its contents as the echo character specified
* in the associated component (if it can narrow the
* component to a JPasswordField).
*
* @author Timothy Prinzing
* @version 1.2 12/09/97
* @see View
*/
public class PasswordView extends FieldView {
/**
* Constructs a new view wrapped on an element.
*
* @param elem the element
*/
public PasswordView(Element elem) {
super(elem);
}
/**
* Renders the given range in the model as normal unselected
* text.
*
* @param g the graphics context
* @param x the starting X coordinate
* @param y the starting Y coordinate
* @param p0 the starting offset in the model
* @param p1 the ending offset in the model
* @returns the location of the end of the range
* @exception BadLocationException if p0 or p1 are out of range
*/
protected int drawUnselectedText(Graphics g, int x, int y,
int p0, int p1) throws BadLocationException {
Container c = getContainer();
if (c instanceof JPasswordField) {
JPasswordField f = (JPasswordField) c;
g.setColor(f.getForeground());
char echoChar = f.getEchoChar();
int n = p1 - p0;
for (int i = 0; i < n; i++) {
x = drawEchoCharacter(g, x, y, echoChar);
}
}
return x;
}
/**
* Renders the given range in the model as selected text. This
* is implemented to render the text in the color specified in
* the hosting component. It assumes the highlighter will render
* the selected background.
*
* @param g the graphics context
* @param x the starting X coordinate
* @param y the starting Y coordinate
* @param p0 the starting offset in the model
* @param p1 the ending offset in the model
* @returns the location of the end of the range.
* @exception BadLocationException if p0 or p1 are out of range
*/
protected int drawSelectedText(Graphics g, int x,
int y, int p0, int p1) throws BadLocationException {
g.setColor(selected);
Container c = getContainer();
if (c instanceof JPasswordField) {
JPasswordField f = (JPasswordField) c;
g.setColor(f.getSelectedTextColor());
char echoChar = f.getEchoChar();
int n = p1 - p0;
for (int i = 0; i < n; i++) {
x = drawEchoCharacter(g, x, y, echoChar);
}
}
return x;
}
/**
* Renders the echo character, or whatever graphic should be used
* to display the password characters. The color in the Graphics
* object is set to the appropriate foreground color for selected
* or unselected text.
*
* @param g the graphics context
* @param x the starting X coordinate
* @param y the starting Y coordinate
* @param c the echo character
* @return the updated X position
*/
protected int drawEchoCharacter(Graphics g, int x, int y, char c) {
ONE[0] = c;
g.drawChars(ONE, 0, 1, x, y);
return x + g.getFontMetrics().charWidth(c);
}
static char[] ONE = new char[1];
}