001 package com.croftsoft.apps.agoracast.c2p;
002
003 import java.awt.*;
004 import java.awt.event.*;
005 import java.util.*;
006 import javax.swing.*;
007 import javax.swing.event.*;
008
009 import com.croftsoft.core.lang.NullArgumentException;
010 import com.croftsoft.core.lang.Pair;
011
012 /*********************************************************************
013 * @version
014 * 2002-01-29
015 * @since
016 * 2001-09-12
017 * @author
018 * <a href="http://croftsoft.com/">David Wallace Croft</a>
019 *********************************************************************/
020
021 public final class AgoracastInputPanel
022 extends JPanel
023 implements ChangeListener
024 //////////////////////////////////////////////////////////////////////
025 //////////////////////////////////////////////////////////////////////
026 {
027
028 private final ChangeEvent changeEvent;
029
030 private final ChangeListener changeListener;
031
032 private final AgoracastMediator agoracastMediator;
033
034 private final Map nameToAgoracastFieldViewMap = new HashMap ( );
035
036 //////////////////////////////////////////////////////////////////////
037 //////////////////////////////////////////////////////////////////////
038
039 public AgoracastInputPanel (
040 AgoracastMediator agoracastMediator,
041 String [ ] fieldNames,
042 ChangeListener changeListener )
043 //////////////////////////////////////////////////////////////////////
044 {
045 super ( new GridBagLayout ( ), true );
046
047 NullArgumentException.check (
048 this.agoracastMediator = agoracastMediator );
049
050 NullArgumentException.check ( fieldNames );
051
052 NullArgumentException.check (
053 this.changeListener = changeListener );
054
055 changeEvent = new ChangeEvent ( this );
056
057 GridBagConstraints gridBagConstraints = new GridBagConstraints ( );
058
059 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
060
061 gridBagConstraints.ipadx = 0;
062
063 gridBagConstraints.ipady = 0;
064
065 gridBagConstraints.insets = new Insets ( 8, 8, 8, 8 );
066
067 for ( int i = 0; i < fieldNames.length; i++ )
068 {
069 AgoracastField agoracastField
070 = agoracastMediator.getAgoracastField ( fieldNames [ i ] );
071
072 AgoracastFieldView agoracastFieldView
073 = new AgoracastFieldView (
074 agoracastMediator, agoracastField, true,
075 ( ChangeListener ) this );
076
077 gridBagConstraints.gridx = 0;
078
079 gridBagConstraints.gridy = i;
080
081 gridBagConstraints.weightx = 0.0;
082
083 add ( agoracastFieldView.getCheckBox ( ), gridBagConstraints );
084
085 gridBagConstraints.gridx = 1;
086
087 add ( agoracastFieldView.getLabel ( ), gridBagConstraints );
088
089 gridBagConstraints.gridx = 2;
090
091 gridBagConstraints.weightx = 1.0;
092
093 add ( agoracastFieldView.getComponent ( ), gridBagConstraints );
094
095 gridBagConstraints.gridx = 3;
096
097 gridBagConstraints.weightx = 0.0;
098
099 add ( agoracastFieldView.getDescriptorLabel ( ),
100 gridBagConstraints );
101
102 nameToAgoracastFieldViewMap.put (
103 agoracastField.getName ( ), agoracastFieldView );
104 }
105
106 AgoracastLib.setColor ( this, agoracastMediator );
107 }
108
109 //////////////////////////////////////////////////////////////////////
110 //////////////////////////////////////////////////////////////////////
111
112 public void setSelected (
113 String name,
114 boolean selected )
115 //////////////////////////////////////////////////////////////////////
116 {
117 AgoracastFieldView agoracastFieldView = ( AgoracastFieldView )
118 nameToAgoracastFieldViewMap.get ( name );
119
120 agoracastFieldView.setSelected ( selected );
121 }
122
123 public void setSelectedAll ( boolean selected )
124 //////////////////////////////////////////////////////////////////////
125 {
126 Iterator iterator
127 = nameToAgoracastFieldViewMap.values ( ).iterator ( );
128
129 while ( iterator.hasNext ( ) )
130 {
131 AgoracastFieldView agoracastFieldView
132 = ( AgoracastFieldView ) iterator.next ( );
133
134 agoracastFieldView.setSelected ( selected );
135 }
136 }
137
138 public boolean isSelected ( String name )
139 //////////////////////////////////////////////////////////////////////
140 {
141 AgoracastFieldView agoracastFieldView = ( AgoracastFieldView )
142 nameToAgoracastFieldViewMap.get ( name );
143
144 return agoracastFieldView.isSelected ( );
145 }
146
147 public boolean hasSelectedField ( )
148 //////////////////////////////////////////////////////////////////////
149 {
150 Iterator iterator
151 = nameToAgoracastFieldViewMap.values ( ).iterator ( );
152
153 while ( iterator.hasNext ( ) )
154 {
155 AgoracastFieldView agoracastFieldView
156 = ( AgoracastFieldView ) iterator.next ( );
157
158 if ( agoracastFieldView.isSelected ( ) )
159 {
160 return true;
161 }
162 }
163
164 return false;
165 }
166
167 public String [ ] getSelectedNames ( )
168 //////////////////////////////////////////////////////////////////////
169 {
170 java.util.List nameList = new ArrayList ( );
171
172 Iterator iterator
173 = nameToAgoracastFieldViewMap.values ( ).iterator ( );
174
175 while ( iterator.hasNext ( ) )
176 {
177 AgoracastFieldView agoracastFieldView
178 = ( AgoracastFieldView ) iterator.next ( );
179
180 if ( agoracastFieldView.isSelected ( ) )
181 {
182 nameList.add ( agoracastFieldView.getName ( ) );
183 }
184 }
185
186 return ( String [ ] ) nameList.toArray ( new String [ ] { } );
187 }
188
189 public String getValue ( String name )
190 //////////////////////////////////////////////////////////////////////
191 {
192 AgoracastFieldView agoracastFieldView
193 = ( AgoracastFieldView )
194 nameToAgoracastFieldViewMap.get ( name );
195
196 if ( agoracastFieldView == null )
197 {
198 return null;
199 }
200
201 return agoracastFieldView.getValue ( );
202 }
203
204 public void setValue (
205 String name,
206 String value )
207 //////////////////////////////////////////////////////////////////////
208 {
209 AgoracastFieldView agoracastFieldView
210 = ( AgoracastFieldView )
211 nameToAgoracastFieldViewMap.get ( name );
212
213 agoracastFieldView.setValue ( value );
214 }
215
216 public synchronized void useDefaults ( )
217 //////////////////////////////////////////////////////////////////////
218 {
219 Iterator iterator
220 = nameToAgoracastFieldViewMap.keySet ( ).iterator ( );
221
222 while ( iterator.hasNext ( ) )
223 {
224 String name = ( String ) iterator.next ( );
225
226 AgoracastField agoracastField
227 = agoracastMediator.getAgoracastField ( name );
228
229 AgoracastFieldView agoracastFieldView = ( AgoracastFieldView )
230 nameToAgoracastFieldViewMap.get ( name );
231
232 agoracastFieldView.setValue ( agoracastField.getValue ( ) );
233 }
234 }
235
236 //////////////////////////////////////////////////////////////////////
237 //////////////////////////////////////////////////////////////////////
238
239 public synchronized void stateChanged ( ChangeEvent changeEvent )
240 //////////////////////////////////////////////////////////////////////
241 {
242 changeListener.stateChanged ( this.changeEvent );
243 }
244
245 //////////////////////////////////////////////////////////////////////
246 //////////////////////////////////////////////////////////////////////
247 }