001         package com.croftsoft.core.gui;
002    
003         import java.awt.*;
004         import java.awt.event.*;
005         import java.util.*;
006         import javax.swing.*;
007    
008         import com.croftsoft.core.lang.NullArgumentException;
009         import com.croftsoft.core.lang.Pair;
010    
011         /*********************************************************************
012         * A JPanel of JTextFields with identifying JLabels.
013         *
014         * <p>
015         * Created JTextField objects are available via an accessor method.
016         * </p>
017         *
018         * <p>
019         * Example:
020         * <code>
021         * <pre>
022         * LabeledFieldsPanel2  labeledFieldsPanel2 = new LabeledFieldsPanel2 (
023         *   new String [ ] { "username", "password" } );
024         *
025         * JTextField  passwordJTextField
026         *   = labeledFieldsPanel2.getJTextField ( "password" );
027         *
028         * passwordJTextField.setEchoChar ( '*' );
029         * </pre>
030         * </code>
031         * </p>
032         *
033         * @version
034         *   2001-07-30
035         * @since
036         *   2001-07-06
037         * @author
038         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
039         *********************************************************************/
040    
041         public final class  LabeledFieldsPanel2
042           extends JPanel
043         //////////////////////////////////////////////////////////////////////
044         //////////////////////////////////////////////////////////////////////
045         {
046    
047         private final Hashtable  labelNameToTextFieldMap = new Hashtable ( );
048    
049         //////////////////////////////////////////////////////////////////////
050         //////////////////////////////////////////////////////////////////////
051    
052         public  LabeledFieldsPanel2 (
053           Pair [ ]  pairs,
054           Color     panelBackgroundColor,
055           Color     textFieldBackgroundColor )
056         //////////////////////////////////////////////////////////////////////
057         {
058           super ( new GridBagLayout ( ), true );
059    
060           NullArgumentException.check ( pairs );
061    
062           GridBagConstraints  gridBagConstraints = new GridBagConstraints ( );
063    
064           gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
065    
066    //       gridBagConstraints.ipadx = 10;
067    
068    //       gridBagConstraints.ipady = 10;
069    
070           gridBagConstraints.insets = new Insets ( 10, 10, 10, 10 );
071    
072           for ( int  i = 0; i < pairs.length; i++ )
073           {
074             gridBagConstraints.gridx = 0;
075    
076             gridBagConstraints.gridy = i;
077    
078             gridBagConstraints.weightx = 0.0;
079    
080             String  labelName = pairs [ i ].name;
081    
082             add ( new JLabel ( labelName ), gridBagConstraints );
083    
084             JTextField  jTextField = new JTextField ( );
085    
086             if ( pairs [ i ].value != null )
087             {
088               jTextField.setText ( pairs [ i ].value );
089             }
090    
091             labelNameToTextFieldMap.put ( labelName, jTextField );
092    
093             gridBagConstraints.gridx = 1;
094    
095             gridBagConstraints.weightx = 1.0;
096    
097             add ( jTextField, gridBagConstraints );
098    
099             if ( textFieldBackgroundColor != null )
100             {
101               jTextField.setBackground ( textFieldBackgroundColor );
102             }
103           }
104    
105           if ( panelBackgroundColor != null )
106           {
107             setBackground ( panelBackgroundColor );
108           }
109         }
110    
111         public  LabeledFieldsPanel2 (
112           String [ ]  labelNames,
113           Color       panelBackgroundColor,
114           Color       textFieldBackgroundColor )
115         //////////////////////////////////////////////////////////////////////
116         {
117           this ( Pair.toPairs ( labelNames ),
118             panelBackgroundColor, textFieldBackgroundColor );
119         }
120    
121         public  LabeledFieldsPanel2 (
122           String  labelName,
123           Color   panelBackgroundColor,
124           Color   textFieldBackgroundColor )
125         //////////////////////////////////////////////////////////////////////
126         {
127           this ( new String [ ] { labelName },
128             panelBackgroundColor, textFieldBackgroundColor );
129         }
130    
131         public  LabeledFieldsPanel2 ( String [ ]  labelNames )
132         //////////////////////////////////////////////////////////////////////
133         {
134           this ( labelNames, null, null );
135         }
136    
137         public  LabeledFieldsPanel2 ( String  labelName )
138         //////////////////////////////////////////////////////////////////////
139         {
140           this ( new String [ ] { labelName }, null, null );
141         }
142    
143         //////////////////////////////////////////////////////////////////////
144         //////////////////////////////////////////////////////////////////////
145    
146         public JTextField  getJTextField ( String  labelName )
147         //////////////////////////////////////////////////////////////////////
148         {
149           JTextField  jTextField
150             = ( JTextField ) labelNameToTextFieldMap.get ( labelName );
151    
152           if ( jTextField == null )
153           {
154             throw new IllegalArgumentException ( labelName );
155           }
156    
157           return jTextField;
158         }
159    
160         public String  getText ( String  labelName )
161         //////////////////////////////////////////////////////////////////////
162         {
163           return getJTextField ( labelName ).getText ( );        
164         }
165    
166         public void  setText ( Pair  pair )
167         //////////////////////////////////////////////////////////////////////
168         {
169           getJTextField ( pair.name ).setText (
170             pair.value != null ? pair.value : "" );
171         }
172    
173         public void  setText ( Pair [ ]  pairs )
174         //////////////////////////////////////////////////////////////////////
175         {
176           for ( int  i = 0; i < pairs.length; i++ )
177           {
178             setText ( pairs [ i ] );
179           }
180         }
181    
182         /*********************************************************************
183         * Adds the KeyListener to all of the JTextFields.
184         *********************************************************************/
185         public void  addKeyListener ( KeyListener  keyListener )
186         //////////////////////////////////////////////////////////////////////
187         {
188           Iterator  iterator
189             = labelNameToTextFieldMap.values ( ).iterator ( );
190    
191           while ( iterator.hasNext ( ) )
192           {
193             JTextField  jTextField = ( JTextField ) iterator.next ( );
194    
195             jTextField.addKeyListener ( keyListener );
196           }
197         }
198    
199         /*********************************************************************
200         * Removes the KeyListener from all of the JTextFields.
201         *********************************************************************/
202         public void  removeKeyListener ( KeyListener  keyListener )
203         //////////////////////////////////////////////////////////////////////
204         {
205           Iterator  iterator
206             = labelNameToTextFieldMap.values ( ).iterator ( );
207    
208           while ( iterator.hasNext ( ) )
209           {
210             JTextField  jTextField = ( JTextField ) iterator.next ( );
211    
212             jTextField.removeKeyListener ( keyListener );
213           }
214         }
215    
216         //////////////////////////////////////////////////////////////////////
217         //////////////////////////////////////////////////////////////////////
218         }