Statistics
| Revision:

root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / panels / PasswordGroup.java @ 15280

History | View | Annotate | Download (7.03 KB)

1
/*
2
 * $Id: PasswordGroup.java,v 1.1 2006/06/14 07:29:07 cesar Exp $
3
 * Copyright (C) 2003 Elmar Grom
4
 *
5
 * File :               PasswordGroup.java
6
 * Description :        This class supports handling of multiple
7
 *                      related password fields. The primary use
8
 *                      is in the UserInputPanel.
9
 * Author's email :     elmar@grom.net
10
 * Author's Website :   http://www.izforge.com
11
 *
12
 * This program is free software; you can redistribute it and/or
13
 * modify it under the terms of the GNU General Public License
14
 * as published by the Free Software Foundation; either version 2
15
 * of the License, or any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
 */
26

    
27
package   com.izforge.izpack.panels;
28

    
29
import java.util.Vector;
30

    
31
import javax.swing.JPasswordField;
32
  
33
/*---------------------------------------------------------------------------*/
34
/**
35
 * This class can be used to manage multiple related password fields. This is
36
 * used in the <code>UserInputPanel</code> to manage communication with the
37
 * validator and processor for password fields.
38
 *
39
 * @see      com.izforge.izpack.panels.UserInputPanel
40
 *
41
 * @version  0.0.1 / 2/22/03
42
 * @author   Elmar Grom
43
 */
44
/*---------------------------------------------------------------------------*/
45
public class PasswordGroup implements ProcessingClient
46
{
47
  // ------------------------------------------------------------------------
48
  // Variable Declarations
49
  // ------------------------------------------------------------------------
50
  private Vector      fields    = new Vector ();
51
  private Validator   validator = null;
52
  private Processor   processor = null;
53
  
54
 /*--------------------------------------------------------------------------*/
55
 /**
56
  * Creates a passowrd group to manage one or more password fields.
57
  *
58
  * @param     validator  A string that specifies a class that provides a
59
  *                       password validation service. The class must
60
  *                       implement the <code>Validator</code> interface. If
61
  *                       an attempt to instantiate this class fails, no
62
  *                       validation will be performed.
63
  * @param     processor  A string that specifies a class that provides a
64
  *                       password processing service, such as password
65
  *                       encryption. The class must implement the
66
  *                       <code>Processor</code> interface. If an attempt to
67
  *                       instantiate this class fails, no processing will
68
  *                       be performed. Insted the contents of the first
69
  *                       field will be returned.
70
  */
71
 /*--------------------------------------------------------------------------*/
72
  public PasswordGroup (String validator,
73
                        String processor)
74
  {
75
    // ----------------------------------------------------
76
    // attempt to create an instance of the Validator
77
    // ----------------------------------------------------
78
    try
79
    {
80
      this.validator = (Validator)Class.forName (validator).newInstance ();
81
    }
82
    catch (Throwable exception)
83
    {
84
      this.validator = null;
85
    }
86

    
87
    // ----------------------------------------------------
88
    // attempt to create an instance of the Processor
89
    // ----------------------------------------------------
90
    try
91
    {
92
      this.processor = (Processor)Class.forName (processor).newInstance ();
93
    }
94
    catch (Throwable exception)
95
    {
96
      this.processor = null;                   
97
    }
98
  }
99
 /*--------------------------------------------------------------------------*/
100
 /**
101
  * Returns the number of sub-fields.
102
  *
103
  * @return    the number of sub-fields
104
  */
105
 /*--------------------------------------------------------------------------*/
106
  public int getNumFields ()
107
  {
108
    return (fields.size ());
109
  }
110
 /*--------------------------------------------------------------------------*/
111
 /**
112
  * Returns the contents of the field indicated by <code>index</code>.
113
  *
114
  * @param     index  the index of the sub-field from which the contents
115
  *                   is requested.
116
  *
117
  * @return    the contents of the indicated sub-field.
118
  *
119
  * @exception IndexOutOfBoundsException if the index is out of bounds.
120
  */
121
 /*--------------------------------------------------------------------------*/
122
  public String getFieldContents (int index) throws IndexOutOfBoundsException
123
  {
124
    if ((index < 0) || (index >= fields.size ()))
125
    {
126
      throw (new IndexOutOfBoundsException ());
127
    }
128
    
129
    String contents = new String (((JPasswordField)fields.elementAt (index)).getPassword ());
130
    return (contents);
131
  }
132
 /*--------------------------------------------------------------------------*/
133
 /**
134
  * Adds a <code>JPasswordField</code> to the group of fields being managed
135
  * by this object.
136
  *
137
  * @param     field <code>JPasswordField</code> to add
138
  */
139
 /*--------------------------------------------------------------------------*/
140
  public void addField (JPasswordField field)
141
  {
142
    if (field != null)
143
    {
144
      fields.add (field);
145
    }
146
  }
147
 /*--------------------------------------------------------------------------*/
148
 /**
149
  * This method validates the group content. Validating is performed through
150
  * a user supplied service class that provides the validation rules.
151
  *
152
  * @return    <code>true</code> if the validation passes or no implementation
153
  *            of a validation rule exists. Otherwise <code>false</code> is
154
  *            returned.
155
  */
156
 /*--------------------------------------------------------------------------*/
157
  public boolean validateContents ()
158
  {
159
    if (validator != null)
160
    {
161
      return (validator.validate (this));
162
    }
163
    else
164
    {
165
      return (true);
166
    }
167
  }
168
 /*--------------------------------------------------------------------------*/
169
 /**
170
  * Returns the password. If a processing service class was supplied it will
171
  * be used to process the password before it is returned, otherwise the
172
  * content of the first field will be returned.
173
  *
174
  * @return    the password
175
  */
176
 /*--------------------------------------------------------------------------*/
177
  public String getPassword ()
178
  {
179
    if (processor != null)
180
    {
181
      return (processor.process (this));
182
    }
183
    else
184
    {
185
      String contents = "";
186
      
187
      if (fields.size () > 0)
188
      {
189
        contents = new String (((JPasswordField)fields.elementAt (0)).getPassword ());
190
      }
191

    
192
      return (contents);
193
    }
194
  }
195
}
196
/*---------------------------------------------------------------------------*/