KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > customizers > common > GenericTableDialogPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * GenericTableDialogPanel.java
21  *
22  * Created on October 8, 2003, 1:47 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31 import java.text.MessageFormat JavaDoc;
32
33 import java.awt.Dimension JavaDoc;
34 import java.awt.GridBagLayout JavaDoc;
35 import java.awt.GridBagConstraints JavaDoc;
36 import java.awt.Insets JavaDoc;
37 import java.awt.event.KeyAdapter JavaDoc;
38 import java.awt.event.KeyEvent JavaDoc;
39
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JTextField JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.border.EmptyBorder JavaDoc;
44
45 import org.netbeans.modules.j2ee.sun.share.Constants;
46 import org.netbeans.modules.j2ee.sun.share.configbean.ASDDVersion;
47 import org.netbeans.modules.j2ee.sun.share.configbean.Utils;
48
49 /**
50  *
51  * @author Peter Williams
52  * @version %I%, %G%
53  */

54 public class GenericTableDialogPanel extends JPanel JavaDoc implements GenericTableDialogPanelAccessor {
55
56     /** resource bundle */
57     private static final ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
58         "org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.Bundle"); // NOI18N
59

60     private List JavaDoc fieldEntries;
61     private String JavaDoc [] values;
62     private JTextField JavaDoc [] textFields;
63     private int preferredWidth;
64
65     /** Creates generic field entry panel using unvalidated JTextFields for all
66      * inputs.
67      */

68     public GenericTableDialogPanel() {
69     }
70     
71     public void init(ASDDVersion asVersion, int width, List JavaDoc entries, Object JavaDoc data) {
72         // data field not used in generic dialog.
73
fieldEntries = entries;
74         preferredWidth = width;
75         values = new String JavaDoc [entries.size()];
76         textFields = new JTextField JavaDoc [entries.size()];
77
78         initUserComponents();
79     }
80     
81     public void setValues(Object JavaDoc[] v) {
82         if(v != null && v.length == values.length) {
83             for(int i = 0; i < values.length && i < v.length; i++) {
84                 values[i] = (v[i] != null) ? v[i].toString() : ""; // NOI18N
85
}
86         } else {
87             if(v != null) {
88                 assert (v.length == values.length); // Should fail
89
}
90             
91             // default values
92
for(int i = 0; i < values.length; i++) {
93                 values[i] = ""; // NOI18N
94
}
95         }
96
97         setComponentValues();
98     }
99     
100     public Object JavaDoc [] getValues() {
101         return values;
102     }
103     
104     private void setComponentValues() {
105         for(int i = 0; i < values.length; i++) {
106             textFields[i].setText(values[i]);
107         }
108     }
109
110     private void initUserComponents() {
111         GridBagConstraints JavaDoc gridBagConstraints;
112         
113         // panel parameters
114
setLayout(new GridBagLayout JavaDoc());
115         setBorder(new EmptyBorder JavaDoc(new Insets JavaDoc(5, 5, 0, 5)));
116         setPreferredSize(new Dimension JavaDoc(preferredWidth, 22*fieldEntries.size()+8));
117         
118         for(int i = 0; i < fieldEntries.size(); i++) {
119             GenericTableModel.TableEntry entry = (GenericTableModel.TableEntry) fieldEntries.get(i);
120             JLabel JavaDoc requiredMark = new JLabel JavaDoc();
121             JLabel JavaDoc label = new JLabel JavaDoc();
122             textFields[i] = new JTextField JavaDoc();
123
124             // First control is either empty label or '*' label to mark required
125
// field.
126
if(entry.isRequiredField()) {
127                 requiredMark.setText(bundle.getString("LBL_RequiredMark")); // NOI18N
128
requiredMark.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_RequiredMark")); // NOI18N
129
requiredMark.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_RequiredMark")); // NOI18N
130
}
131             requiredMark.setLabelFor(textFields[i]);
132             gridBagConstraints = new GridBagConstraints JavaDoc();
133             gridBagConstraints.anchor = GridBagConstraints.WEST;
134             gridBagConstraints.insets = new Insets JavaDoc(0, 0, 4, 4);
135             add(requiredMark, gridBagConstraints);
136             
137             // Initialize and add label
138
label.setLabelFor(textFields[i]);
139             label.setText(entry.getLabelName()); // NOI18N
140
label.setDisplayedMnemonic(entry.getLabelMnemonic());
141             gridBagConstraints = new GridBagConstraints JavaDoc();
142             gridBagConstraints.anchor = GridBagConstraints.WEST;
143             gridBagConstraints.insets = new Insets JavaDoc(0, 0, 4, 4);
144             add(label, gridBagConstraints);
145
146             // Initialize and add text field
147
textFields[i].addKeyListener(new TextFieldHandler(textFields[i], i));
148             textFields[i].getAccessibleContext().setAccessibleName(entry.getAccessibleName());
149             textFields[i].getAccessibleContext().setAccessibleDescription(entry.getAccessibleDescription());
150
151             gridBagConstraints = new GridBagConstraints JavaDoc();
152             gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
153             gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
154             gridBagConstraints.weightx = 1.0;
155             gridBagConstraints.insets = new Insets JavaDoc(0, 0, 4, 0);
156             add(textFields[i], gridBagConstraints);
157         }
158     }
159     
160     public Collection JavaDoc getErrors(ValidationSupport validationSupport) {
161         ArrayList JavaDoc errorList = new ArrayList JavaDoc();
162         
163         // Only record one error per field.
164
for(int i = 0; i < fieldEntries.size(); i++) {
165             GenericTableModel.TableEntry entry = (GenericTableModel.TableEntry) fieldEntries.get(i);
166             if(entry.isRequiredField()) {
167                 if(!Utils.notEmpty(values[i])) {
168                     Object JavaDoc [] args = new Object JavaDoc [1];
169                     args[0] = entry.getColumnName();
170                     errorList.add(MessageFormat.format(bundle.getString("ERR_SpecifiedFieldIsEmpty"), args));
171                     continue;
172                 }
173             }
174             
175             if(entry.isNameField()) {
176                 if(Utils.containsWhitespace(values[i])) {
177                     Object JavaDoc [] args = new Object JavaDoc [1];
178                     args[0] = entry.getColumnName();
179                     errorList.add(MessageFormat.format(bundle.getString("ERR_NameFieldContainsWhitespace"), args));
180                     continue;
181                 }
182             }
183
184                         // Validate that this field is an integer value. Empty is acceptable
185
// as well (use required field flag to force non-empty fields.)
186
// if(entry.isIntegerField()) {
187
// if(Utils.notInteger(values[i])) {
188
// Object [] args = new Object [1];
189
// args[0] = entry.getColumnName();
190
// errorList.add(MessageFormat.format(bundle.getString("ERR_FieldMustBeInteger"), args));
191
// continue;
192
// }
193
// }
194
}
195         
196         return errorList;
197     }
198
199     public boolean requiredFieldsFilled() {
200         boolean result = true;
201         
202         for(int i = 0; i < fieldEntries.size(); i++) {
203             GenericTableModel.TableEntry entry = (GenericTableModel.TableEntry) fieldEntries.get(i);
204             if(entry.isRequiredField()) {
205                 if(!Utils.notEmpty(values[i])) {
206                     result = false;
207                     break;
208                 }
209             }
210         }
211         
212         return result;
213     }
214     
215     /** private class to allow easy binding between each text field in the array
216      * of JTextFields and the corresponding String in the array of values entered
217      * by the user.
218      */

219     private class TextFieldHandler extends KeyAdapter JavaDoc {
220         private JTextField JavaDoc textField;
221         private int controlIndex;
222         
223         public TextFieldHandler(JTextField JavaDoc tf, int index) {
224             textField = tf;
225             controlIndex = index;
226         }
227         
228         public void keyReleased(KeyEvent JavaDoc evt) {
229             values[controlIndex] = textField.getText();
230             firePropertyChange(Constants.USER_DATA_CHANGED, null, null);
231         }
232     }
233 }
234
Popular Tags