KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > panels > PasswordGroup


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2003 Elmar Grom
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.panels;
23
24 import java.util.Vector JavaDoc;
25
26 import javax.swing.JPasswordField JavaDoc;
27
28 /*---------------------------------------------------------------------------*/
29 /**
30  * This class can be used to manage multiple related password fields. This is used in the
31  * <code>UserInputPanel</code> to manage communication with the validator and processor for
32  * password fields.
33  *
34  * @see com.izforge.izpack.panels.UserInputPanel
35  *
36  * @version 0.0.1 / 2/22/03
37  * @author Elmar Grom
38  */

39 /*---------------------------------------------------------------------------*/
40 public class PasswordGroup implements ProcessingClient
41 {
42
43     // ------------------------------------------------------------------------
44
// Variable Declarations
45
// ------------------------------------------------------------------------
46
private Vector JavaDoc fields = new Vector JavaDoc();
47
48     private Validator validator = null;
49
50     private Processor processor = null;
51
52     /*--------------------------------------------------------------------------*/
53     /**
54      * Creates a passowrd group to manage one or more password fields.
55      *
56      * @param validator A string that specifies a class that provides a password validation service.
57      * The class must implement the <code>Validator</code> interface. If an attempt to instantiate
58      * this class fails, no validation will be performed.
59      * @param processor A string that specifies a class that provides a password processing service,
60      * such as password encryption. The class must implement the <code>Processor</code> interface.
61      * If an attempt to instantiate this class fails, no processing will be performed. Insted the
62      * contents of the first field will be returned.
63      */

64     /*--------------------------------------------------------------------------*/
65     public PasswordGroup(String JavaDoc validator, String JavaDoc processor)
66     {
67         // ----------------------------------------------------
68
// attempt to create an instance of the Validator
69
// ----------------------------------------------------
70
try
71         {
72             this.validator = (Validator) Class.forName(validator).newInstance();
73         }
74         catch (Throwable JavaDoc exception)
75         {
76             this.validator = null;
77         }
78
79         // ----------------------------------------------------
80
// attempt to create an instance of the Processor
81
// ----------------------------------------------------
82
try
83         {
84             this.processor = (Processor) Class.forName(processor).newInstance();
85         }
86         catch (Throwable JavaDoc exception)
87         {
88             this.processor = null;
89         }
90     }
91
92     /*--------------------------------------------------------------------------*/
93     /**
94      * Returns the number of sub-fields.
95      *
96      * @return the number of sub-fields
97      */

98     /*--------------------------------------------------------------------------*/
99     public int getNumFields()
100     {
101         return (fields.size());
102     }
103
104     /*--------------------------------------------------------------------------*/
105     /**
106      * Returns the contents of the field indicated by <code>index</code>.
107      *
108      * @param index the index of the sub-field from which the contents is requested.
109      *
110      * @return the contents of the indicated sub-field.
111      *
112      * @exception IndexOutOfBoundsException if the index is out of bounds.
113      */

114     /*--------------------------------------------------------------------------*/
115     public String JavaDoc getFieldContents(int index) throws IndexOutOfBoundsException JavaDoc
116     {
117         if ((index < 0) || (index >= fields.size())) { throw (new IndexOutOfBoundsException JavaDoc()); }
118
119         String JavaDoc contents = new String JavaDoc(((JPasswordField JavaDoc) fields.elementAt(index)).getPassword());
120         return (contents);
121     }
122
123     /*--------------------------------------------------------------------------*/
124     /**
125      * Adds a <code>JPasswordField</code> to the group of fields being managed by this object.
126      *
127      * @param field <code>JPasswordField</code> to add
128      */

129     /*--------------------------------------------------------------------------*/
130     public void addField(JPasswordField JavaDoc field)
131     {
132         if (field != null)
133         {
134             fields.add(field);
135         }
136     }
137
138     /*--------------------------------------------------------------------------*/
139     /**
140      * This method validates the group content. Validating is performed through a user supplied
141      * service class that provides the validation rules.
142      *
143      * @return <code>true</code> if the validation passes or no implementation of a validation
144      * rule exists. Otherwise <code>false</code> is returned.
145      */

146     /*--------------------------------------------------------------------------*/
147     public boolean validateContents()
148     {
149         if (validator != null)
150         {
151             return (validator.validate(this));
152         }
153         else
154         {
155             return (true);
156         }
157     }
158
159     /*--------------------------------------------------------------------------*/
160     /**
161      * Returns the password. If a processing service class was supplied it will be used to process
162      * the password before it is returned, otherwise the content of the first field will be
163      * returned.
164      *
165      * @return the password
166      */

167     /*--------------------------------------------------------------------------*/
168     public String JavaDoc getPassword()
169     {
170         if (processor != null)
171         {
172             return (processor.process(this));
173         }
174         else
175         {
176             String JavaDoc contents = "";
177
178             if (fields.size() > 0)
179             {
180                 contents = new String JavaDoc(((JPasswordField JavaDoc) fields.elementAt(0)).getPassword());
181             }
182
183             return (contents);
184         }
185     }
186 }
187 /*---------------------------------------------------------------------------*/
188
Popular Tags