001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
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: package com.izforge.izpack.panels;
022:
023: import com.izforge.izpack.installer.InstallData;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Vector;
027:
028: import javax.swing.JPasswordField;
029:
030: /*---------------------------------------------------------------------------*/
031: /**
032: * This class can be used to manage multiple related password fields. This is used in the
033: * <code>UserInputPanel</code> to manage communication with the validator and processor for
034: * password fields.
035: *
036: * @see com.izforge.izpack.panels.UserInputPanel
037: *
038: * @version 0.0.1 / 2/22/03
039: * @author Elmar Grom
040: */
041: /*---------------------------------------------------------------------------*/
042: public class PasswordGroup implements ProcessingClient {
043:
044: // ------------------------------------------------------------------------
045: // Variable Declarations
046: // ------------------------------------------------------------------------
047: private Vector<JPasswordField> fields = new Vector<JPasswordField>();
048:
049: private List<ValidatorContainer> validatorContainers = null;
050: // private Validator validator = null;
051: // private boolean hasParams = false;
052: // private Map validatorParams = null;
053: private Processor processor = null;
054:
055: private int currentValidator = 0;
056:
057: private InstallData idata;
058:
059: /*--------------------------------------------------------------------------*/
060: /**
061: * Creates a password group to manage one or more password fields.
062: *
063: * @param idata the installation data
064: * @param validatorContainers the validator containers
065: * @param processor the processor
066: */
067: /*--------------------------------------------------------------------------*/
068: public PasswordGroup(InstallData idata,
069: List<ValidatorContainer> validatorContainers,
070: String processor) {
071: // ----------------------------------------------------
072: // attempt to create an instance of the Validator
073: // ----------------------------------------------------
074: try {
075: this .idata = idata;
076: // this.validator = (Validator) Class.forName(validator).newInstance();
077: this .validatorContainers = validatorContainers;
078: // this.validatorParams = validatorParams;
079: // if (validatorParams != null) {
080: // if (validatorParams.size() > 0) {
081: // hasParams = true;
082: // }
083: // }
084: } catch (Throwable exception) {
085: this .validatorContainers = null;
086: }
087:
088: // ----------------------------------------------------
089: // attempt to create an instance of the Processor
090: // ----------------------------------------------------
091: try {
092: this .processor = (Processor) Class.forName(processor)
093: .newInstance();
094: } catch (Throwable exception) {
095: this .processor = null;
096: }
097: }
098:
099: public InstallData getIdata() {
100: return idata;
101: }
102:
103: /*--------------------------------------------------------------------------*/
104: /**
105: * Returns the number of sub-fields.
106: *
107: * @return the number of sub-fields
108: */
109: /*--------------------------------------------------------------------------*/
110: public int getNumFields() {
111: return (fields.size());
112: }
113:
114: /*--------------------------------------------------------------------------*/
115: /**
116: * Returns the contents of the field indicated by <code>index</code>.
117: *
118: * @param index the index of the sub-field from which the contents is requested.
119: *
120: * @return the contents of the indicated sub-field.
121: *
122: * @exception IndexOutOfBoundsException if the index is out of bounds.
123: */
124: /*--------------------------------------------------------------------------*/
125: public String getFieldContents(int index)
126: throws IndexOutOfBoundsException {
127: if ((index < 0) || (index >= fields.size())) {
128: throw (new IndexOutOfBoundsException());
129: }
130:
131: String contents = new String((fields.elementAt(index))
132: .getPassword());
133: return (contents);
134: }
135:
136: /*--------------------------------------------------------------------------*/
137: /**
138: * Adds a <code>JPasswordField</code> to the group of fields being managed by this object.
139: *
140: * @param field <code>JPasswordField</code> to add
141: */
142: /*--------------------------------------------------------------------------*/
143: public void addField(JPasswordField field) {
144: if (field != null) {
145: fields.add(field);
146: }
147: }
148:
149: /*--------------------------------------------------------------------------*/
150: /**
151: * This method validates the group content. Validating is performed through a user supplied
152: * service class that provides the validation rules.
153: *
154: * @return <code>true</code> if the validation passes or no implementation of a validation
155: * rule exists. Otherwise <code>false</code> is returned.
156: */
157: /*--------------------------------------------------------------------------*/
158: public boolean validateContents(int i) {
159: boolean returnValue = true;
160: try {
161: currentValidator = i;
162: ValidatorContainer container = getValidatorContainer(i);
163: Validator validator = container.getValidator();
164: if (validator != null) {
165: returnValue = validator.validate(this );
166: }
167: } catch (Exception e) {
168: System.out.println("validateContents(" + i + ") failed: "
169: + e);
170: // just return true
171: }
172: return returnValue;
173: }
174:
175: public String getValidatorMessage(int i) {
176: String returnValue = null;
177: try {
178: ValidatorContainer container = getValidatorContainer(i);
179: if (container != null) {
180: returnValue = container.getMessage();
181: }
182: } catch (Exception e) {
183: System.out.println("getValidatorMessage(" + i
184: + ") failed: " + e);
185: // just return true
186: }
187: return returnValue;
188: }
189:
190: public int validatorSize() {
191: int size = 0;
192: if (validatorContainers != null) {
193: size = validatorContainers.size();
194: }
195: return size;
196: }
197:
198: public ValidatorContainer getValidatorContainer() {
199: return getValidatorContainer(currentValidator);
200: }
201:
202: public ValidatorContainer getValidatorContainer(int i) {
203: ValidatorContainer container = null;
204: try {
205: container = validatorContainers.get(i);
206: } catch (Exception e) {
207: container = null;
208: }
209: return container;
210: }
211:
212: public boolean hasParams() {
213: return hasParams(currentValidator);
214: }
215:
216: public boolean hasParams(int i) {
217: boolean returnValue = false;
218: try {
219: ValidatorContainer container = getValidatorContainer(i);
220: if (container != null) {
221: returnValue = container.hasParams();
222: }
223: } catch (Exception e) {
224: System.out.println("hasParams(" + i + ") failed: " + e);
225: // just return true
226: }
227: return returnValue;
228: }
229:
230: public Map<String, String> getValidatorParams() {
231: return getValidatorParams(currentValidator);
232: }
233:
234: public Map<String, String> getValidatorParams(int i) {
235: Map<String, String> returnValue = null;
236: try {
237: ValidatorContainer container = getValidatorContainer(i);
238: if (container != null) {
239: returnValue = container.getValidatorParams();
240: }
241: } catch (Exception e) {
242: System.out.println("getValidatorParams(" + i + ") failed: "
243: + e);
244: // just return true
245: }
246: return returnValue;
247: }
248:
249: // This method was added to support changes to ProcessingClient interface
250: // it's use is non-deterministic in the newly implemented text validators.
251: public String getText() {
252: return getValidatorMessage(currentValidator);
253: }
254:
255: /*--------------------------------------------------------------------------*/
256: /**
257: * Returns the password. If a processing service class was supplied it will be used to process
258: * the password before it is returned, otherwise the content of the first field will be
259: * returned.
260: *
261: * @return the password
262: */
263: /*--------------------------------------------------------------------------*/
264: public String getPassword() {
265: if (processor != null) {
266: return (processor.process(this ));
267: } else {
268: String contents = "";
269:
270: if (fields.size() > 0) {
271: contents = new String((fields.elementAt(0))
272: .getPassword());
273: }
274:
275: return (contents);
276: }
277: }
278:
279: }
280: /*---------------------------------------------------------------------------*/
|