| 1 |
/* |
| 2 |
* Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are met: |
| 6 |
* |
| 7 |
* o Redistributions of source code must retain the above copyright notice, |
| 8 |
* this list of conditions and the following disclaimer. |
| 9 |
* |
| 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. |
| 13 |
* |
| 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. |
| 17 |
* |
| 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. |
| 29 |
*/ |
| 30 |
package org.pushingpixels.lafwidget.text; |
| 31 |
|
| 32 |
import java.awt.*; |
| 33 |
import java.beans.PropertyChangeEvent; |
| 34 |
import java.beans.PropertyChangeListener; |
| 35 |
|
| 36 |
import javax.swing.JPasswordField; |
| 37 |
import javax.swing.UIManager; |
| 38 |
import javax.swing.border.Border; |
| 39 |
import javax.swing.plaf.BorderUIResource; |
| 40 |
|
| 41 |
import org.pushingpixels.lafwidget.*; |
| 42 |
import org.pushingpixels.lafwidget.utils.LafConstants.PasswordStrength; |
| 43 |
|
| 44 |
/** |
| 45 |
* Adds password strength indication on password fields. |
| 46 |
* |
| 47 |
* @author Kirill Grouchnikov |
| 48 |
*/ |
| 49 |
public class PasswordStrengthCheckerWidget extends |
| 50 |
LafWidgetAdapter<JPasswordField> { |
| 51 |
/** |
| 52 |
* Listens on changes to {@link LafWidget#PASSWORD_STRENGTH_CHECKER} |
| 53 |
* property. |
| 54 |
*/ |
| 55 |
protected PropertyChangeListener strengthCheckerListener; |
| 56 |
|
| 57 |
/** |
| 58 |
* Border with password strength indication. |
| 59 |
* |
| 60 |
* @author Kirill Grouchnikov |
| 61 |
*/ |
| 62 |
private static class StrengthCheckedBorder implements Border { |
| 63 |
/** |
| 64 |
* Gutter width. |
| 65 |
*/ |
| 66 |
public static final int GUTTER_WIDTH = 5; |
| 67 |
|
| 68 |
/* |
| 69 |
* (non-Javadoc) |
| 70 |
* |
| 71 |
* @see javax.swing.border.Border#isBorderOpaque() |
| 72 |
*/ |
| 73 |
@Override |
| 74 |
public boolean isBorderOpaque() { |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
/* |
| 79 |
* (non-Javadoc) |
| 80 |
* |
| 81 |
* @see javax.swing.border.Border#getBorderInsets(java.awt.Component) |
| 82 |
*/ |
| 83 |
@Override |
| 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); |
| 88 |
} else { |
| 89 |
if (c.getComponentOrientation().isLeftToRight()) |
| 90 |
return new Insets(0, 0, 0, |
| 91 |
StrengthCheckedBorder.GUTTER_WIDTH); |
| 92 |
else |
| 93 |
return new Insets(0, StrengthCheckedBorder.GUTTER_WIDTH, 0, |
| 94 |
0); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
* (non-Javadoc) |
| 100 |
* |
| 101 |
* @see javax.swing.border.Border#paintBorder(java.awt.Component, |
| 102 |
* java.awt.Graphics, int, int, int, int) |
| 103 |
*/ |
| 104 |
@Override |
| 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) |
| 111 |
return; |
| 112 |
|
| 113 |
PasswordStrength strength = passwordStrengthChecker.getStrength(jpf |
| 114 |
.getPassword()); |
| 115 |
LafWidgetSupport lafSupport = LafWidgetRepository.getRepository() |
| 116 |
.getLafSupport(); |
| 117 |
if (c.getComponentOrientation().isLeftToRight()) |
| 118 |
lafSupport.paintPasswordStrengthMarker(g, x + width |
| 119 |
- StrengthCheckedBorder.GUTTER_WIDTH, y, |
| 120 |
StrengthCheckedBorder.GUTTER_WIDTH, height, strength); |
| 121 |
else |
| 122 |
lafSupport.paintPasswordStrengthMarker(g, x, y, |
| 123 |
StrengthCheckedBorder.GUTTER_WIDTH, height, strength); |
| 124 |
|
| 125 |
String tooltip = passwordStrengthChecker.getDescription(strength); |
| 126 |
jpf.setToolTipText(tooltip); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/* |
| 131 |
* (non-Javadoc) |
| 132 |
* |
| 133 |
* @see org.pushingpixels.lafwidget.LafWidgetAdapter#installListeners() |
| 134 |
*/ |
| 135 |
@Override |
| 136 |
public void installListeners() { |
| 137 |
this.strengthCheckerListener = new PropertyChangeListener() { |
| 138 |
@Override |
| 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))) { |
| 147 |
jcomp |
| 148 |
.setBorder(new BorderUIResource.CompoundBorderUIResource( |
| 149 |
jcomp.getBorder(), |
| 150 |
new StrengthCheckedBorder())); |
| 151 |
} else { |
| 152 |
// restore core border |
| 153 |
Border coreBorder = UIManager |
| 154 |
.getBorder("PasswordField.border"); |
| 155 |
jcomp.setBorder(coreBorder); |
| 156 |
jcomp.setToolTipText(null); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
}; |
| 161 |
this.jcomp.addPropertyChangeListener(this.strengthCheckerListener); |
| 162 |
} |
| 163 |
|
| 164 |
/* |
| 165 |
* (non-Javadoc) |
| 166 |
* |
| 167 |
* @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallListeners() |
| 168 |
*/ |
| 169 |
@Override |
| 170 |
public void uninstallListeners() { |
| 171 |
this.jcomp.removePropertyChangeListener(this.strengthCheckerListener); |
| 172 |
this.strengthCheckerListener = null; |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
* (non-Javadoc) |
| 177 |
* |
| 178 |
* @see org.pushingpixels.lafwidget.LafWidgetAdapter#installDefaults() |
| 179 |
*/ |
| 180 |
@Override |
| 181 |
public void installDefaults() { |
| 182 |
super.installDefaults(); |
| 183 |
|
| 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())); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/* |
| 194 |
* (non-Javadoc) |
| 195 |
* |
| 196 |
* @see org.pushingpixels.lafwidget.LafWidget#requiresCustomLafSupport() |
| 197 |
*/ |
| 198 |
@Override |
| 199 |
public boolean requiresCustomLafSupport() { |
| 200 |
return false; |
| 201 |
} |
| 202 |
} |