001package ca.uhn.hl7v2.conf.spec.message;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import ca.uhn.hl7v2.conf.ProfileException;
007
008/**
009 * The specification for a specific field in a message profile.  
010 * @author Bryan Tripp
011 */
012public class Field extends AbstractComponent<Field> {
013    
014    /** Utility field used by bound properties. */
015    private java.beans.PropertyChangeSupport propertyChangeSupport =  new java.beans.PropertyChangeSupport(this);
016    
017    /** Utility field used by constrained properties. */
018    private java.beans.VetoableChangeSupport vetoableChangeSupport =  new java.beans.VetoableChangeSupport(this);
019    
020    private short min;
021    private short max;
022    private short itemNo;
023
024    private final List<Component> components = new ArrayList<Component>();
025    
026    /** Creates a new instance of Field */
027    public Field() {
028    }
029    
030    /** Adds a PropertyChangeListener to the listener list.
031     * @param l The listener to add.
032     */
033    public void addPropertyChangeListener(java.beans.PropertyChangeListener l) {
034        propertyChangeSupport.addPropertyChangeListener(l);
035    }
036    
037    /** Removes a PropertyChangeListener from the listener list.
038     * @param l The listener to remove.
039     */
040    public void removePropertyChangeListener(java.beans.PropertyChangeListener l) {
041        propertyChangeSupport.removePropertyChangeListener(l);
042    }
043    
044    /** Adds a VetoableChangeListener to the listener list.
045     * @param l The listener to add.
046     */
047    public void addVetoableChangeListener(java.beans.VetoableChangeListener l) {
048        vetoableChangeSupport.addVetoableChangeListener(l);
049    }
050    
051    /** Removes a VetoableChangeListener from the listener list.
052     * @param l The listener to remove.
053     */
054    public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) {
055        vetoableChangeSupport.removeVetoableChangeListener(l);
056    }
057    
058    /** Getter for property min.
059     * @return Value of property min.
060     */
061    public short getMin() {
062        return this.min;
063    }
064    
065    /** Setter for property min.
066     * @param min New value of property min.
067     *
068     * @throws ProfileException
069     */
070    public void setMin(short min) throws ProfileException {
071        short oldMin = this.min;
072        try {
073            vetoableChangeSupport.fireVetoableChange("min", oldMin, min);
074        } catch (Exception e) {
075            throw new ProfileException(null, e);
076        }
077        this.min = min;
078        propertyChangeSupport.firePropertyChange("min", oldMin, min);
079    }
080    
081    /** Getter for property max.
082     * @return Value of property max.
083     */
084    public short getMax() {
085        return this.max;
086    }
087    
088    /** Setter for property max.
089     * @param max New value of property max.
090     *
091     * @throws ProfileException
092     */
093    public void setMax(short max) throws ProfileException {
094        short oldMax = this.max;
095        try {
096            vetoableChangeSupport.fireVetoableChange("max", oldMax, max);
097        } catch (Exception e) {
098            throw new ProfileException(null, e);
099        }
100        this.max = max;
101        propertyChangeSupport.firePropertyChange("max", oldMax, max);
102    }
103    
104    /** Getter for property itemNo.
105     * @return Value of property itemNo.
106     */
107    public short getItemNo() {
108        return this.itemNo;
109    }
110    
111    /** Setter for property itemNo.
112     * @param itemNo New value of property itemNo.
113     *
114     * @throws ProfileException
115     */
116    public void setItemNo(short itemNo) throws ProfileException {
117        short oldItemNo = this.itemNo;
118        try {
119            vetoableChangeSupport.fireVetoableChange("itemNo", new Short(oldItemNo), new Short(itemNo));
120        } catch (Exception e) {
121            throw new ProfileException(null, e);
122        }            
123        this.itemNo = itemNo;
124        propertyChangeSupport.firePropertyChange("itemNo", new Short(oldItemNo), new Short(itemNo));
125    }    
126    
127    /** Indexed getter for property components (index starts at 1 following HL7 convention).
128     * @param index Index of the property (starts at 1 following HL7 convention).
129     * @return Value of the property at <CODE>index</CODE>.
130     */
131    public Component getComponent(int index) {
132        return this.components.get(index - 1);
133    }
134    
135    /** Indexed setter for property components (index starts at 1 following HL7 convention).
136     * @param index Index of the property (starts at 1 following HL7 convention).
137     * @param component New value of the property at <CODE>index</CODE>.
138     *
139     * @throws ProfileException
140     */
141    public void setComponent(int index, Component component) throws ProfileException {
142        index--;
143        while (components.size() <= index) {
144                components.add(null);
145        }
146        Component oldComponent = this.components.get(index);
147        this.components.set(index, component);
148        try {
149            vetoableChangeSupport.fireVetoableChange("components", null, null );
150        }
151        catch(java.beans.PropertyVetoException vetoException ) {
152            this.components.set(index, oldComponent);
153            throw new ProfileException(null, vetoException);
154        }
155        propertyChangeSupport.firePropertyChange("components", null, null );
156    }    
157
158    
159    /** Returns the number of components */
160    public int getComponents() {
161                return this.components.size();
162    }
163
164    /** Returns the number of components */
165    public List<Component> getChildrenAsList() {
166        return (this.components);
167    }
168    
169}
170    
171