001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2008 Piotr Skowronek
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:
022: package com.izforge.izpack.panels;
023:
024: import java.util.Map;
025:
026: import javax.swing.JComponent;
027: import javax.swing.JTextField;
028:
029: import com.izforge.izpack.util.Debug;
030:
031: /*---------------------------------------------------------------------------*/
032: /**
033: * This class is a wrapper for JTextField to allow field validation.
034: * Based on RuleInputField.
035: *
036: * @author Piotr Skowronek
037: */
038: /*---------------------------------------------------------------------------*/
039: public class TextInputField extends JComponent implements
040: ProcessingClient {
041:
042: /**
043: *
044: */
045: private static final long serialVersionUID = 8611515659787697087L;
046:
047: /**
048: * Validator parameters.
049: */
050: private Map<String, String> validatorParams;
051:
052: /**
053: * Holds an instance of the <code>Validator</code> if one was specified and available
054: */
055: private Validator validationService;
056:
057: /**
058: * This composite can only contain one component ie JTextField
059: */
060: private JTextField field;
061:
062: /**
063: * Do we have parameters for validator?
064: */
065: private boolean hasParams = false;
066:
067: /*--------------------------------------------------------------------------*/
068: /**
069: * Constructs a text input field.
070: *
071: * @param set A default value for field.
072: * @param size The size of the field.
073: * @param validator A string that specifies a class to perform validation services. The string
074: * must completely identify the class, so that it can be instantiated. The class must implement
075: * the <code>RuleValidator</code> interface. If an attempt to instantiate this class fails, no
076: * validation will be performed.
077: * @param validatorParams validator parameters.
078: */
079: /*--------------------------------------------------------------------------*/
080: public TextInputField(String set, int size, String validator,
081: Map<String, String> validatorParams) {
082: this (set, size, validator);
083: this .validatorParams = validatorParams;
084: this .hasParams = true;
085: }
086:
087: /*--------------------------------------------------------------------------*/
088: /**
089: * Constructs a text input field.
090: *
091: * @param set A default value for field.
092: * @param size The size of the field.
093: * @param validator A string that specifies a class to perform validation services. The string
094: * must completely identify the class, so that it can be instantiated. The class must implement
095: * the <code>RuleValidator</code> interface. If an attempt to instantiate this class fails, no
096: * validation will be performed.
097: */
098: /*--------------------------------------------------------------------------*/
099: public TextInputField(String set, int size, String validator) {
100: // ----------------------------------------------------
101: // attempt to create an instance of the Validator
102: // ----------------------------------------------------
103: try {
104: if (validator != null) {
105: validationService = (Validator) Class
106: .forName(validator).newInstance();
107: }
108: } catch (Throwable exception) {
109: validationService = null;
110: Debug.trace(exception);
111: }
112:
113: com.izforge.izpack.gui.FlowLayout layout = new com.izforge.izpack.gui.FlowLayout();
114: layout.setAlignment(com.izforge.izpack.gui.FlowLayout.LEADING);
115: layout.setVgap(0);
116: setLayout(layout);
117:
118: // ----------------------------------------------------
119: // construct the UI element and add it to the composite
120: // ----------------------------------------------------
121: field = new JTextField(set, size);
122: field.setCaretPosition(0);
123: add(field);
124: }
125:
126: /*--------------------------------------------------------------------------*/
127: /**
128: * Returns the validator parameters, if any. The caller should check for the existence of
129: * validator parameters via the <code>hasParams()</code> method prior to invoking this method.
130: *
131: * @return a java.util.Map containing the validator parameters.
132: */
133: public Map<String, String> getValidatorParams() {
134: return validatorParams;
135: }
136:
137: /*---------------------------------------------------------------------------*/
138: /**
139: * Returns the field contents, assembled acording to the encryption and separator rules.
140: *
141: * @return the field contents
142: */
143: /*--------------------------------------------------------------------------*/
144: public String getText() {
145: return (field.getText());
146: }
147:
148: // javadoc inherited
149: public void setText(String value) {
150: field.setText(value);
151: }
152:
153: // javadoc inherited
154: public String getFieldContents(int index) {
155: return field.getText();
156: }
157:
158: // javadoc inherited
159: public int getNumFields() {
160: // We've got only one field
161: return 1;
162: }
163:
164: /*--------------------------------------------------------------------------*/
165: /**
166: * This method validates the field content. Validating is performed through a user supplied
167: * service class that provides the validation rules.
168: *
169: * @return <code>true</code> if the validation passes or no implementation of a validation
170: * rule exists. Otherwise <code>false</code> is returned.
171: */
172: /*--------------------------------------------------------------------------*/
173: public boolean validateContents() {
174: if (validationService != null) {
175: return (validationService.validate(this ));
176: } else {
177: return (true);
178: }
179: }
180:
181: // javadoc inherited
182: public boolean hasParams() {
183: return hasParams;
184: }
185:
186: // ----------------------------------------------------------------------------
187: }
188: /*---------------------------------------------------------------------------*/
|