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