001    package com.sptci.echo2demo;
002    
003    import javax.jdo.InstanceCallbacks;
004    import java.beans.PropertyChangeListener;
005    import java.beans.PropertyChangeSupport;
006    import java.io.Serializable;
007    import java.util.Formatter;
008    import java.util.ArrayList;
009    import java.util.List;
010    import com.sptci.echo2.ListItem;
011    import java.util.Map;
012    import java.util.TreeMap;
013    
014    /**
015     * A JavaBean that represents the data represented by 
016     * {@link com.sptci.echo2demo.InputForm}.
017     *
018     * @author com.sptci.echo2.JDOBeanGenerator 2006-02-09 10:00:15-0600
019     * @version $Id: InputFormModel.java,v 1.1 2006/02/13 23:39:45 rakesh Exp $
020     */
021    public class InputFormModel
022      implements InstanceCallbacks, Cloneable, Comparable<InputFormModel>, Serializable
023    {
024      /**
025       * The default value to use as the base for hashCode.
026       */
027      protected static final int HASH = 7;
028    
029      /**
030       * The field used to store pre-computed hashCode.
031       */
032      protected int hash = HASH;
033    
034      /**
035       * Field that represents the checkBoxes field of type
036       * checkBoxes
037       */
038      private Map<String, Boolean> checkBoxes;
039    
040      /**
041       * Field that represents the listBox field of type
042       * listBox
043       */
044      private List<ListItem> listBox;
045    
046      /**
047       * Field that represents the selectField field of type
048       * selectField
049       */
050      private List<ListItem> selectField;
051    
052      /**
053       * Field that represents the textArea field of type
054       * textArea
055       */
056      private String textArea;
057    
058      /**
059       * Field that represents the textField field of type
060       * textField
061       */
062      private String textField;
063    
064      /**
065       * The property change event dispatcher.  This object
066       * is lazily loaded.
067       */
068      private transient PropertyChangeSupport propertyChangeSupport;
069    
070      /**
071       * Default constructor.  Uses the designated constructor.
072       */
073      public InputFormModel()
074      {
075        checkBoxes = new TreeMap<String, Boolean>();
076        listBox = new ArrayList<ListItem>();
077        selectField = new ArrayList<ListItem>();
078        for ( int i = 0; i < 3; ++i )
079        {
080          checkBoxes.put( "Check " + i, false );
081          listBox.add( new ListItem( "List Item " + i ) );
082          selectField.add( new ListItem( "Select " + i ) );
083        }
084      }
085    
086      /**
087       * Designated constructor.  Initialises the
088       * fields with the specified values.
089       */
090      public InputFormModel( Map<String, Boolean> checkBoxes,
091        List<ListItem> listBox,
092        List<ListItem> selectField,
093        String textArea,
094        String textField )
095      {
096        setCheckBoxes( checkBoxes );
097        setListBox( listBox );
098        setSelectField( selectField );
099        setTextArea( textArea );
100        setTextField( textField );
101      }
102    
103      /**
104       * Return a String representation of the fields of this class.
105       * The String representation is returned as an XML document.
106       *
107       * @return String An XML representation of the class fields.
108       */
109      @Override
110      public String toString()
111      {
112        StringBuilder builder = new StringBuilder( 4096 );
113        Formatter formatter = new Formatter( builder );
114        formatter.format( "<InputFormModel package='%s'>%n", getClass().getPackage().getName() );
115    
116        formatter.format( "<checkBoxes type='Map&lt;String, Boolean&gt;'>%n" );
117        for ( Map.Entry<String, Boolean> entry : checkBoxes.entrySet() )
118        {
119          formatter.format( "<key>%n%s%n</key>%n", entry.getKey() );
120          formatter.format( "<value>%n%s%n</value>%n", entry.getValue() );
121        }
122        formatter.format( "</checkBoxes>%n" );
123    
124        formatter.format( "<listBox type='List&lt;ListItem&gt;'>%n" );
125        for ( ListItem entry : listBox )
126        {
127          formatter.format( "<entry>%n%s%n</entry>%n", entry );
128        }
129        formatter.format( "</listBox>%n" );
130    
131        formatter.format( "<selectField type='List&lt;ListItem&gt;'>%n" );
132        for ( ListItem entry : selectField )
133        {
134          formatter.format( "<entry>%n%s%n</entry>%n", entry );
135        }
136        formatter.format( "</selectField>%n" );
137    
138        formatter.format( "<textArea type='String'>%n" );
139        formatter.format( "%s%n", textArea );
140        formatter.format( "</textArea>%n" );
141    
142        formatter.format( "<textField type='String'>%n" );
143        formatter.format( "%s%n", textField );
144        formatter.format( "</textField>%n" );
145    
146        formatter.format( "</InputFormModel>%n" );
147        return builder.toString();
148      }
149      /**
150       * Compare the specified object with this object for equality.
151       * Returns <code>true</code> if the specified object is of the
152       * same type as this  object, and the values of the fields
153       * are equal.
154       *
155       * @param object The object that is to compared with this object.
156       * @return boolean Returns <code>true</code> if the values of
157       *   the class fields are the same.
158       */
159      @Override
160      public boolean equals( Object object )
161      {
162        if ( this == object ) return true;
163    
164        boolean value = false;
165        if ( ( object != null ) &&
166            ( getClass() == object.getClass() ) )
167        {
168          InputFormModel obj = (InputFormModel) object;
169    
170          value = ( hashCode() == obj.hashCode() );
171          value = value &&  ( ( checkBoxes == obj.checkBoxes ) ||
172              ( checkBoxes != null &&
173                checkBoxes.equals( obj.checkBoxes ) ) );
174          value = value &&  ( ( listBox == obj.listBox ) ||
175              ( listBox != null &&
176                listBox.equals( obj.listBox ) ) );
177          value = value &&  ( ( selectField == obj.selectField ) ||
178              ( selectField != null &&
179                selectField.equals( obj.selectField ) ) );
180          value = value &&  ( ( textArea == obj.textArea ) ||
181              ( textArea != null &&
182                textArea.equals( obj.textArea ) ) );
183          value = value &&  ( ( textField == obj.textField ) ||
184              ( textField != null &&
185                textField.equals( obj.textField ) ) );
186        }
187    
188        return value;
189      }
190      /**
191       * Return a hash code for this object.  Returns a hash code
192       * computed from the class fields.
193       *
194       * @return int The hashCode value.
195       */
196      @Override
197      public int hashCode()
198      {
199        if ( hash == HASH )
200        {
201          hash = ( 31 * hash ) +
202            ( ( checkBoxes == null ) ? 0 : checkBoxes.hashCode() );
203          hash = ( 31 * hash ) +
204            ( ( listBox == null ) ? 0 : listBox.hashCode() );
205          hash = ( 31 * hash ) +
206            ( ( selectField == null ) ? 0 : selectField.hashCode() );
207          hash = ( 31 * hash ) +
208            ( ( textArea == null ) ? 0 : textArea.hashCode() );
209          hash = ( 31 * hash ) +
210            ( ( textField == null ) ? 0 : textField.hashCode() );
211        }
212    
213        return hash;
214      }
215    
216      /**
217       * Implementation of the <code>Comparable</code> interface.
218       * Compares this object with the specified object for order. Returns
219       * a negative integer, zero, or a positive integer as this object is
220       * less than, equal to, or greater than the specified object.
221       * The comparison is done by comparing the {@link #hashCode()} values
222       * of the objects.
223       *
224       * @param object - The object with which this class is to
225       *   be compared.  No class type checking is done.
226       * @return int - A negative integer, zero, or a positive integer as
227       *   this object is less than, equal to, or greater than the
228       *   specified object.
229       */
230      public int compareTo( InputFormModel object )
231      {
232        return ( this.hashCode() - object.hashCode() );
233      }
234    
235      /**
236       * Creates and returns a copy of this object.  Implementation of
237       * the <code>Cloneable</code> interface.  No special actions are
238       * performed.  This method simply allows public access to the
239       * <code>Object.clone</code> method.
240       *
241       * @return Object A clone of this instance.
242       * @throws CloneNotSupportedException If the super-class implementation
243       *   throws an error.
244       */
245      @Override
246      public Object clone() throws CloneNotSupportedException
247      {
248        return super.clone();
249      }
250    
251      /**
252       * Adds a property change listener to this <code>bean</code>.
253       * Delegates handling to {@link #propertyChangeSupport}
254       *
255       * @param listener The listener to add.
256       */
257      public void addPropertyChangeListener( PropertyChangeListener listener )
258      {
259        if ( propertyChangeSupport == null )
260        {
261          propertyChangeSupport = new PropertyChangeSupport( this );
262        }
263    
264        propertyChangeSupport.addPropertyChangeListener( listener );
265      }
266    
267      /**
268       * Removes a property change listener from this <code>bean</code>.
269       * Delegates handling to {@link #propertyChangeSupport}
270       *
271       * @param listener The listener to be removed.
272       */
273      public void removePropertyChangeListener(
274          PropertyChangeListener listener )
275      {
276        if ( propertyChangeSupport != null )
277        {
278          propertyChangeSupport.removePropertyChangeListener( listener );
279        }
280      }
281    
282      /**
283       * Reports a bound property change to the registerd
284       * <code>PropertyChangeListener</code>s.
285       * Delegates handling to {@link #propertyChangeSupport}
286       *
287       * @param propertyName The name of the changed property.
288       * @param oldValue The previous value of the property.
289       * @param newValue The present value of the property.
290       */
291      protected void firePropertyChange( String propertyName,
292          Object oldValue, Object newValue )
293      {
294        if ( propertyChangeSupport != null )
295        {
296          propertyChangeSupport.firePropertyChange(
297              propertyName, oldValue, newValue );
298        }
299      }
300    
301    
302      /**
303       * Returns the value of {@link #checkBoxes}.
304       *
305       * @return Map<String, Boolean> The value of checkBoxes.
306       */
307      public final Map<String, Boolean> getCheckBoxes()
308      {
309        return checkBoxes;
310      }
311    
312      /**
313       * Sets the value of {@link #checkBoxes}.  Resets {@link #hash}.
314       *
315       * @param checkBoxes The value to set.
316       */
317      public final void setCheckBoxes( Map<String, Boolean> checkBoxes )
318      {
319        Map<String, Boolean> oldValue = this.checkBoxes;
320        this.checkBoxes = checkBoxes;
321        hash = HASH;
322        firePropertyChange( "checkBoxes", oldValue, checkBoxes );
323      }
324    
325      /**
326       * Returns the value of {@link #listBox}.
327       *
328       * @return List<ListItem> The value of listBox.
329       */
330      public final List<ListItem> getListBox()
331      {
332        return listBox;
333      }
334    
335      /**
336       * Sets the value of {@link #listBox}.  Resets {@link #hash}.
337       *
338       * @param listBox The value to set.
339       */
340      public final void setListBox( List<ListItem> listBox )
341      {
342        List<ListItem> oldValue = this.listBox;
343        this.listBox = listBox;
344        hash = HASH;
345        firePropertyChange( "listBox", oldValue, listBox );
346      }
347    
348      /**
349       * Returns the value of {@link #selectField}.
350       *
351       * @return List<ListItem> The value of selectField.
352       */
353      public final List<ListItem> getSelectField()
354      {
355        return selectField;
356      }
357    
358      /**
359       * Sets the value of {@link #selectField}.  Resets {@link #hash}.
360       *
361       * @param selectField The value to set.
362       */
363      public final void setSelectField( List<ListItem> selectField )
364      {
365        List<ListItem> oldValue = this.selectField;
366        this.selectField = selectField;
367        hash = HASH;
368        firePropertyChange( "selectField", oldValue, selectField );
369      }
370    
371      /**
372       * Returns the value of {@link #textArea}.
373       *
374       * @return String The value of textArea.
375       */
376      public final String getTextArea()
377      {
378        return textArea;
379      }
380    
381      /**
382       * Sets the value of {@link #textArea}.  Resets {@link #hash}.
383       *
384       * @param textArea The value to set.
385       */
386      public final void setTextArea( String textArea )
387      {
388        String oldValue = this.textArea;
389        this.textArea = textArea;
390        hash = HASH;
391        firePropertyChange( "textArea", oldValue, textArea );
392      }
393    
394      /**
395       * Returns the value of {@link #textField}.
396       *
397       * @return String The value of textField.
398       */
399      public final String getTextField()
400      {
401        return textField;
402      }
403    
404      /**
405       * Sets the value of {@link #textField}.  Resets {@link #hash}.
406       *
407       * @param textField The value to set.
408       */
409      public final void setTextField( String textField )
410      {
411        String oldValue = this.textField;
412        this.textField = textField;
413        hash = HASH;
414        firePropertyChange( "textField", oldValue, textField );
415      }
416    
417      /**
418       * Called after the values are loaded from the
419       * data store into this instance.
420       * InstanceCallbacks method implementation.  No actions required
421       * by default.
422       */
423      public void jdoPostLoad() {}
424    
425      /**
426       * Called before the values are stored from this instance
427       * to the data store.
428       * InstanceCallbacks method implementation.
429       * Pre-calculate {@link #hash} using {@link #hashCode}.
430       */
431      public void jdoPreStore()
432      {
433        hash = hashCode();
434      }
435    
436      /**
437       * Called before the values in the instance are cleared.
438       * InstanceCallbacks method implementation.  No actions required
439       * by default.
440       */
441      public void jdoPreClear() {}
442    
443      /**
444       * Called before the instance is deleted.
445       * InstanceCallbacks method implementation.  No actions required
446       * by default.
447       */
448      public void jdoPreDelete() {}
449    }