2
* Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* o Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of
15
* its contributors may be used to endorse or promote products derived
16
* from this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
package org.pushingpixels.lafwidget.text;
33
import java.beans.PropertyChangeEvent;
34
import java.beans.PropertyChangeListener;
36
import javax.swing.JPasswordField;
37
import javax.swing.UIManager;
38
import javax.swing.border.Border;
39
import javax.swing.plaf.BorderUIResource;
41
import org.pushingpixels.lafwidget.*;
42
import org.pushingpixels.lafwidget.utils.LafConstants.PasswordStrength;
45
* Adds password strength indication on password fields.
47
* @author Kirill Grouchnikov
49
public class PasswordStrengthCheckerWidget extends
50
LafWidgetAdapter<JPasswordField> {
52
* Listens on changes to {@link LafWidget#PASSWORD_STRENGTH_CHECKER}
55
protected PropertyChangeListener strengthCheckerListener;
58
* Border with password strength indication.
60
* @author Kirill Grouchnikov
62
private static class StrengthCheckedBorder implements Border {
66
public static final int GUTTER_WIDTH = 5;
71
* @see javax.swing.border.Border#isBorderOpaque()
74
public boolean isBorderOpaque() {
81
* @see javax.swing.border.Border#getBorderInsets(java.awt.Component)
84
public Insets getBorderInsets(Component c) {
85
JPasswordField jpf = (JPasswordField) c;
86
if (LafWidgetUtilities2.getPasswordStrengthChecker(jpf) == null) {
87
return new Insets(0, 0, 0, 0);
89
if (c.getComponentOrientation().isLeftToRight())
90
return new Insets(0, 0, 0,
91
StrengthCheckedBorder.GUTTER_WIDTH);
93
return new Insets(0, StrengthCheckedBorder.GUTTER_WIDTH, 0,
101
* @see javax.swing.border.Border#paintBorder(java.awt.Component,
102
* java.awt.Graphics, int, int, int, int)
105
public void paintBorder(Component c, Graphics g, int x, int y,
106
int width, int height) {
107
JPasswordField jpf = (JPasswordField) c;
108
PasswordStrengthChecker passwordStrengthChecker = LafWidgetUtilities2
109
.getPasswordStrengthChecker(jpf);
110
if (passwordStrengthChecker == null)
113
PasswordStrength strength = passwordStrengthChecker.getStrength(jpf
115
LafWidgetSupport lafSupport = LafWidgetRepository.getRepository()
117
if (c.getComponentOrientation().isLeftToRight())
118
lafSupport.paintPasswordStrengthMarker(g, x + width
119
- StrengthCheckedBorder.GUTTER_WIDTH, y,
120
StrengthCheckedBorder.GUTTER_WIDTH, height, strength);
122
lafSupport.paintPasswordStrengthMarker(g, x, y,
123
StrengthCheckedBorder.GUTTER_WIDTH, height, strength);
125
String tooltip = passwordStrengthChecker.getDescription(strength);
126
jpf.setToolTipText(tooltip);
133
* @see org.pushingpixels.lafwidget.LafWidgetAdapter#installListeners()
136
public void installListeners() {
137
this.strengthCheckerListener = new PropertyChangeListener() {
139
public void propertyChange(PropertyChangeEvent evt) {
140
if (LafWidget.PASSWORD_STRENGTH_CHECKER.equals(evt
141
.getPropertyName())) {
142
Object newValue = evt.getNewValue();
143
Object oldValue = evt.getOldValue();
144
if ((newValue != null)
145
&& (newValue instanceof PasswordStrengthChecker)
146
&& (!(oldValue instanceof PasswordStrengthChecker))) {
148
.setBorder(new BorderUIResource.CompoundBorderUIResource(
150
new StrengthCheckedBorder()));
152
// restore core border
153
Border coreBorder = UIManager
154
.getBorder("PasswordField.border");
155
jcomp.setBorder(coreBorder);
156
jcomp.setToolTipText(null);
161
this.jcomp.addPropertyChangeListener(this.strengthCheckerListener);
167
* @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallListeners()
170
public void uninstallListeners() {
171
this.jcomp.removePropertyChangeListener(this.strengthCheckerListener);
172
this.strengthCheckerListener = null;
178
* @see org.pushingpixels.lafwidget.LafWidgetAdapter#installDefaults()
181
public void installDefaults() {
182
super.installDefaults();
184
// check if the property is already set - can happen on LAF change
185
Object checker = this.jcomp
186
.getClientProperty(LafWidget.PASSWORD_STRENGTH_CHECKER);
187
if ((checker != null) && (checker instanceof PasswordStrengthChecker)) {
188
this.jcomp.setBorder(new BorderUIResource.CompoundBorderUIResource(
189
this.jcomp.getBorder(), new StrengthCheckedBorder()));
196
* @see org.pushingpixels.lafwidget.LafWidget#requiresCustomLafSupport()
199
public boolean requiresCustomLafSupport() {