001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Copyright 2003 Elmar Grom
008 * 
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *     
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package com.izforge.izpack.panels;
023
024import java.util.Vector;
025
026import javax.swing.JPasswordField;
027
028/*---------------------------------------------------------------------------*/
029/**
030 * This class can be used to manage multiple related password fields. This is used in the
031 * <code>UserInputPanel</code> to manage communication with the validator and processor for
032 * password fields.
033 * 
034 * @see com.izforge.izpack.panels.UserInputPanel
035 * 
036 * @version 0.0.1 / 2/22/03
037 * @author Elmar Grom
038 */
039/*---------------------------------------------------------------------------*/
040public class PasswordGroup implements ProcessingClient
041{
042
043    // ------------------------------------------------------------------------
044    // Variable Declarations
045    // ------------------------------------------------------------------------
046    private Vector fields = new Vector();
047
048    private Validator validator = null;
049
050    private Processor processor = null;
051
052    /*--------------------------------------------------------------------------*/
053    /**
054     * Creates a passowrd group to manage one or more password fields.
055     * 
056     * @param validator A string that specifies a class that provides a password validation service.
057     * The class must implement the <code>Validator</code> interface. If an attempt to instantiate
058     * this class fails, no validation will be performed.
059     * @param processor A string that specifies a class that provides a password processing service,
060     * such as password encryption. The class must implement the <code>Processor</code> interface.
061     * If an attempt to instantiate this class fails, no processing will be performed. Insted the
062     * contents of the first field will be returned.
063     */
064    /*--------------------------------------------------------------------------*/
065    public PasswordGroup(String validator, String processor)
066    {
067        // ----------------------------------------------------
068        // attempt to create an instance of the Validator
069        // ----------------------------------------------------
070        try
071        {
072            this.validator = (Validator) Class.forName(validator).newInstance();
073        }
074        catch (Throwable exception)
075        {
076            this.validator = null;
077        }
078
079        // ----------------------------------------------------
080        // attempt to create an instance of the Processor
081        // ----------------------------------------------------
082        try
083        {
084            this.processor = (Processor) Class.forName(processor).newInstance();
085        }
086        catch (Throwable exception)
087        {
088            this.processor = null;
089        }
090    }
091
092    /*--------------------------------------------------------------------------*/
093    /**
094     * Returns the number of sub-fields.
095     * 
096     * @return the number of sub-fields
097     */
098    /*--------------------------------------------------------------------------*/
099    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 getFieldContents(int index) throws IndexOutOfBoundsException
116    {
117        if ((index < 0) || (index >= fields.size())) { throw (new IndexOutOfBoundsException()); }
118
119        String contents = new String(((JPasswordField) 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 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 getPassword()
169    {
170        if (processor != null)
171        {
172            return (processor.process(this));
173        }
174        else
175        {
176            String contents = "";
177
178            if (fields.size() > 0)
179            {
180                contents = new String(((JPasswordField) fields.elementAt(0)).getPassword());
181            }
182
183            return (contents);
184        }
185    }
186}
187/*---------------------------------------------------------------------------*/