001: /*
002: * Copyright (c) 2001-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.looks.common;
032:
033: import java.awt.*;
034:
035: import javax.swing.JPasswordField;
036: import javax.swing.UIManager;
037: import javax.swing.text.BadLocationException;
038: import javax.swing.text.Element;
039: import javax.swing.text.PasswordView;
040: import javax.swing.text.Position;
041:
042: /**
043: * Differs from its superclass in that it uses the UIManager's echo char,
044: * not a star ("*").
045: * Used in Java 1.4 and Java 5 only.
046: *
047: * @author Karsten Lentzsch
048: * @version $Revision: 1.7 $
049: */
050: public final class ExtPasswordView extends PasswordView {
051:
052: public ExtPasswordView(Element element) {
053: super (element);
054: }
055:
056: public float getPreferredSpan(int axis) {
057: overrideEchoChar();
058: return super .getPreferredSpan(axis);
059: }
060:
061: public Shape modelToView(int pos, Shape a, Position.Bias b)
062: throws BadLocationException {
063: overrideEchoChar();
064: return super .modelToView(pos, a, b);
065: }
066:
067: public int viewToModel(float fx, float fy, Shape a,
068: Position.Bias[] bias) {
069: overrideEchoChar();
070: return super .viewToModel(fx, fy, a, bias);
071: }
072:
073: /**
074: * Overrides the superclass behavior to draw the Windows dot,
075: * not the star ("*") character.
076: *
077: * @param g the graphics context
078: * @param x the starting X coordinate >= 0
079: * @param y the starting Y coordinate >= 0
080: * @param c the echo character
081: * @return the updated X position >= 0
082: */
083: protected int drawEchoCharacter(Graphics g, int x, int y, char c) {
084: Container container = getContainer();
085: if (!(container instanceof JPasswordField)) {
086: return super .drawEchoCharacter(g, x, y, c);
087: }
088: JPasswordField field = (JPasswordField) container;
089: if (canOverrideEchoChar(field)) {
090: c = getEchoChar();
091: }
092: // Painting the dot with anti-alias enabled.
093: Graphics2D g2 = (Graphics2D) g;
094: Object newAAHint = RenderingHints.VALUE_ANTIALIAS_ON;
095: Object oldAAHint = g2
096: .getRenderingHint(RenderingHints.KEY_ANTIALIASING);
097: if (newAAHint != oldAAHint) {
098: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
099: newAAHint);
100: } else {
101: oldAAHint = null;
102: }
103:
104: int newX = super .drawEchoCharacter(g, x, y, c);
105:
106: if (oldAAHint != null) {
107: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
108: oldAAHint);
109: }
110: return newX;
111: }
112:
113: private void overrideEchoChar() {
114: Container container = getContainer();
115: if (!(container instanceof JPasswordField)) {
116: return;
117: }
118: JPasswordField field = (JPasswordField) container;
119: if (canOverrideEchoChar(field)) {
120: setFieldEchoChar(field, getEchoChar());
121: }
122: }
123:
124: private boolean canOverrideEchoChar(JPasswordField field) {
125: return field.echoCharIsSet() && field.getEchoChar() == '*';
126: }
127:
128: /**
129: * Sets a new echo char in the given password field,
130: * if and only if the new echo char differs from the old one.
131: *
132: * @param field the JPasswordField to change
133: * @param newEchoChar the echo char that shall be set
134: */
135: private void setFieldEchoChar(JPasswordField field, char newEchoChar) {
136: char oldEchoChar = field.getEchoChar();
137: if (oldEchoChar == newEchoChar)
138: return;
139: field.setEchoChar(newEchoChar);
140: }
141:
142: private static char getEchoChar() {
143: return ((Character) UIManager.get("PasswordField.echoChar"))
144: .charValue();
145: }
146:
147: }
|