001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. 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 Substance Kirill Grouchnikov 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: package test.samples.lafwidget.clientprop;
031:
032: import java.awt.BorderLayout;
033: import java.awt.FlowLayout;
034: import java.awt.event.ActionEvent;
035: import java.awt.event.ActionListener;
036:
037: import javax.swing.*;
038:
039: import org.jvnet.lafwidget.LafWidget;
040: import org.jvnet.lafwidget.text.PasswordStrengthChecker;
041: import org.jvnet.lafwidget.utils.LafConstants.PasswordStrength;
042: import org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel;
043:
044: /**
045: * Test application that shows the use of the
046: * {@link LafWidget#PASSWORD_STRENGTH_CHECKER} client property.
047: *
048: * @author Kirill Grouchnikov
049: * @see LafWidget#PASSWORD_STRENGTH_CHECKER
050: */
051: public class PasswordStrengthCheckerProperty extends JFrame {
052: /**
053: * Creates the main frame for <code>this</code> sample.
054: */
055: public PasswordStrengthCheckerProperty() {
056: super ("Password strength checker");
057:
058: this .setLayout(new BorderLayout());
059: final JPanel panel = new JPanel(new FlowLayout());
060: this .add(panel, BorderLayout.CENTER);
061:
062: final JPasswordField jpf = new JPasswordField();
063: jpf.setColumns(20);
064: panel.add(new JLabel("Start typing password"));
065: panel.add(jpf);
066:
067: JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
068: final JCheckBox hasPasswordStrengthChecker = new JCheckBox(
069: "Has password strength checker");
070: hasPasswordStrengthChecker
071: .addActionListener(new ActionListener() {
072: public void actionPerformed(ActionEvent e) {
073: if (hasPasswordStrengthChecker.isSelected()) {
074: jpf
075: .putClientProperty(
076: LafWidget.PASSWORD_STRENGTH_CHECKER,
077: new PasswordStrengthChecker() {
078: public PasswordStrength getStrength(
079: char[] password) {
080: if (password == null)
081: return PasswordStrength.WEAK;
082: int length = password.length;
083: if (length < 3)
084: return PasswordStrength.WEAK;
085: if (length < 6)
086: return PasswordStrength.MEDIUM;
087: return PasswordStrength.STRONG;
088: }
089:
090: public String getDescription(
091: PasswordStrength strength) {
092: if (strength == PasswordStrength.WEAK)
093: return "<html>This password is <b>way</b> too weak</html>";
094: if (strength == PasswordStrength.MEDIUM)
095: return "<html>Come on, you can do<br> a little better than that</html>";
096: if (strength == PasswordStrength.STRONG)
097: return "OK";
098: return null;
099: }
100: });
101: } else {
102: jpf
103: .putClientProperty(
104: LafWidget.PASSWORD_STRENGTH_CHECKER,
105: null);
106: }
107: }
108: });
109:
110: controls.add(hasPasswordStrengthChecker);
111: this .add(controls, BorderLayout.SOUTH);
112:
113: this .setSize(400, 200);
114: this .setLocationRelativeTo(null);
115: this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
116: }
117:
118: /**
119: * The main method for <code>this</code> sample. The arguments are
120: * ignored.
121: *
122: * @param args
123: * Ignored.
124: * @throws Exception
125: * If some exception occured. Note that there is no special
126: * treatment of exception conditions in <code>this</code>
127: * sample code.
128: */
129: public static void main(String[] args) throws Exception {
130: UIManager
131: .setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
132: JFrame.setDefaultLookAndFeelDecorated(true);
133: SwingUtilities.invokeLater(new Runnable() {
134: public void run() {
135: new PasswordStrengthCheckerProperty().setVisible(true);
136: }
137: });
138: }
139: }
|