001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Color;
023: import java.awt.FontMetrics;
024: import java.awt.Graphics;
025: import java.awt.Rectangle;
026: import java.awt.Shape;
027:
028: import org.apache.harmony.awt.text.TextFieldKit;
029: import org.apache.harmony.awt.text.TextUtils;
030:
031: import org.apache.harmony.x.swing.internal.nls.Messages;
032:
033: public class PasswordView extends FieldView {
034: private static final char DEFAULT_ECHO_CHAR = '*';
035:
036: public PasswordView(final Element element) {
037: super (element);
038: }
039:
040: protected int drawEchoCharacter(final Graphics g, final int x,
041: final int y, final char c) {
042: g.drawString(String.valueOf(c), x, y);
043: return getFontMetrics().charWidth(c) + x;
044: }
045:
046: protected int drawSelectedText(final Graphics g, final int x,
047: final int y, final int p0, final int p1)
048: throws BadLocationException {
049: return echoCharIsSet() ? drawString(g, paintParams.selColor,
050: p0, p1, x, y) : super .drawSelectedText(g, x, y, p0, p1);
051: }
052:
053: protected int drawUnselectedText(final Graphics g, final int x,
054: final int y, final int p0, final int p1)
055: throws BadLocationException {
056:
057: return echoCharIsSet() ? drawString(g, paintParams.color, p0,
058: p1, x, y) : super .drawUnselectedText(g, x, y, p0, p1);
059: }
060:
061: public float getPreferredSpan(final int axis) {
062: if (axis == Y_AXIS || !echoCharIsSet()) {
063: return super .getPreferredSpan(axis);
064: } else {
065: return getFontMetrics().stringWidth(getText());
066: }
067: }
068:
069: public Shape modelToView(final int pos, final Shape shape,
070: final Position.Bias b) throws BadLocationException {
071: if (echoCharIsSet()) {
072: String text = getText();
073: if (pos < 0 || pos > text.length()) {
074: throw new BadLocationException(Messages.getString(
075: "swing.95", pos), //$NON-NLS-1$
076: pos);
077: }
078: if (shape == null) {
079: return null;
080: }
081: text = text.substring(0, pos);
082: Rectangle rect = getOldAllocation(shape);
083: FontMetrics fontMetrics = getFontMetrics();
084: return new Rectangle(
085: rect.x + fontMetrics.stringWidth(text), rect.y, 1,
086: fontMetrics.getHeight());
087: } else {
088: return super .modelToView(pos, shape, b);
089: }
090: }
091:
092: public int viewToModel(final float x, final float y,
093: final Shape shape, final Position.Bias[] biasRet) {
094: if (echoCharIsSet()) {
095: biasRet[0] = Position.Bias.Forward;
096: String text = getText();
097: return getTextOffset(text, (int) x - shape.getBounds().x);
098: } else {
099: return super .viewToModel(x, y, shape, biasRet);
100: }
101: }
102:
103: private TextFieldKit getPasswordTextKit() {
104: return TextUtils.getTextFieldKit(getComponent());
105: }
106:
107: private boolean echoCharIsSet() {
108: TextFieldKit tfk = getPasswordTextKit();
109: return tfk != null && tfk.echoCharIsSet();
110: }
111:
112: private char getEchoChar() {
113: TextFieldKit tfk = getPasswordTextKit();
114: return tfk != null ? tfk.getEchoChar() : DEFAULT_ECHO_CHAR;
115: }
116:
117: private String getText() {
118: TextFieldKit tfk = getPasswordTextKit();
119: if (tfk == null) {
120: return "";
121: }
122: int length = getDocument().getLength();
123: char echo = tfk.getEchoChar();
124: String result = "";
125: for (int i = 0; i < length; i++) {
126: result += echo;
127: }
128: return result;
129: }
130:
131: private int getTextLength() {
132: Document doc = getDocument();
133: return doc != null ? doc.getLength() : 0;
134: }
135:
136: private int drawString(final Graphics g, final Color color,
137: final int p0, final int p1, final int x, final int y)
138: throws BadLocationException {
139: int length = getTextLength();
140: if (p0 < 0) {
141: throw new BadLocationException(Messages
142: .getString("swing.96"), p0); //$NON-NLS-1$
143: } else if (p1 < p0 || p1 > length) {
144: throw new BadLocationException(Messages
145: .getString("swing.96"), p1); //$NON-NLS-1$
146: }
147:
148: Color old = g.getColor();
149: g.setColor(color);
150: int result = x;
151: char echo = getEchoChar();
152: for (int i = 0; i < p1 - p0; i++) {
153: result = drawEchoCharacter(g, result, y, echo);
154: }
155: g.setColor(old);
156: return result;
157: }
158:
159: private int getTextOffset(final String text, final int width) {
160: int index = 0;
161: int length = text.length();
162: while (getFontMetrics().stringWidth(text.substring(0, index)) < width) {
163: if (index++ == length) {
164: return length;
165: }
166: }
167: return index;
168: }
169: }
|