001    package ca.uhn.hl7v2.conf.spec.message;
002    
003    import ca.uhn.hl7v2.conf.ProfileException;
004    
005    /**
006     * An abstraction of SegGroup and MessageProfile (both are containers for segment specs).  
007     * @author Bryan Tripp
008     */
009    public class AbstractSegmentContainer {
010        
011        private String description;
012        private String reference;
013        private String impNote;    
014        private ProfileStructure[] children;
015        
016        /** Utility field used by bound properties. */
017        private java.beans.PropertyChangeSupport propertyChangeSupport =  new java.beans.PropertyChangeSupport(this);
018        
019        /** Utility field used by constrained properties. */
020        private java.beans.VetoableChangeSupport vetoableChangeSupport =  new java.beans.VetoableChangeSupport(this);
021        
022        /** Creates a new instance of AbstractSegmentContainer */
023        public AbstractSegmentContainer() {
024            children = new ProfileStructure[0];
025        }
026        
027        /** Adds a PropertyChangeListener to the listener list.
028         * @param l The listener to add.
029         */
030        public void addPropertyChangeListener(java.beans.PropertyChangeListener l) {
031            propertyChangeSupport.addPropertyChangeListener(l);
032        }
033        
034        /** Removes a PropertyChangeListener from the listener list.
035         * @param l The listener to remove.
036         */
037        public void removePropertyChangeListener(java.beans.PropertyChangeListener l) {
038            propertyChangeSupport.removePropertyChangeListener(l);
039        }
040        
041        /** Adds a VetoableChangeListener to the listener list.
042         * @param l The listener to add.
043         */
044        public void addVetoableChangeListener(java.beans.VetoableChangeListener l) {
045            vetoableChangeSupport.addVetoableChangeListener(l);
046        }
047        
048        /** Removes a VetoableChangeListener from the listener list.
049         * @param l The listener to remove.
050         */
051        public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) {
052            vetoableChangeSupport.removeVetoableChangeListener(l);
053        }
054        
055        /** Getter for property description.
056         * @return Value of property description.
057         */
058        public String getDescription() {
059            return this.description;
060        }
061        
062        /** Setter for property description.
063         * @param description New value of property description.
064         *
065         * @throws ProfileException
066         */
067        public void setDescription(String description) throws ProfileException {
068            String oldDescription = this.description;
069            try {
070                vetoableChangeSupport.fireVetoableChange("description", oldDescription, description);
071            } catch (Exception e) {
072                throw new ProfileException(null, e);
073            }
074            this.description = description;
075            propertyChangeSupport.firePropertyChange("description", oldDescription, description);
076        }
077        
078        /** Getter for property reference.
079         * @return Value of property reference.
080         */
081        public String getReference() {
082            return this.reference;
083        }
084        
085        /** Setter for property reference.
086         * @param reference New value of property reference.
087         *
088         * @throws ProfileException
089         */
090        public void setReference(String reference) throws ProfileException {
091            String oldReference = this.reference;
092            try {
093                vetoableChangeSupport.fireVetoableChange("reference", oldReference, reference);
094            } catch (Exception e) {
095                throw new ProfileException(null, e);
096            }
097            this.reference = reference;
098            propertyChangeSupport.firePropertyChange("reference", oldReference, reference);
099        }
100        
101        /** Getter for property impNote.
102         * @return Value of property impNote.
103         */
104        public String getImpNote() {
105            return this.impNote;
106        }
107        
108        /** Setter for property impNote.
109         * @param impNote New value of property impNote.
110         *
111         * @throws ProfileException
112         */
113        public void setImpNote(String impNote) throws ProfileException {
114            String oldImpNote = this.impNote;
115            try {
116                vetoableChangeSupport.fireVetoableChange("impNote", oldImpNote, impNote);
117            } catch (Exception e) {
118                throw new ProfileException(null, e);
119            }
120            this.impNote = impNote;
121            propertyChangeSupport.firePropertyChange("impNote", oldImpNote, impNote);
122        }
123            
124        
125        /** Indexed getter for property structure (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 ProfileStructure getChild(int index) {
130            return this.children[index - 1];
131        }
132        
133        /** Indexed setter for property structure.  Lengthens child list if necessary.  
134         * @param index Index of the property (starts at 1 following HL7 convention).
135         * @param structure New value of the property at <CODE>index</CODE>.
136         *
137         * @throws ProfileException
138         */
139        public void setChild(int index, ProfileStructure structure) throws ProfileException {
140            index--;
141            extendChildList(index);
142            ProfileStructure oldStructure = this.children[index];
143            this.children[index] = structure;
144            try {
145                vetoableChangeSupport.fireVetoableChange("structure", null, null );
146            }
147            catch(java.beans.PropertyVetoException vetoException ) {
148                this.children[index] = oldStructure;
149                throw new ProfileException(null, vetoException);
150            }
151            propertyChangeSupport.firePropertyChange("structure", null, null );
152        }
153        
154        /** Returns the number of children */
155        public int getChildren() {
156            return this.children.length;
157        }
158        
159        /** Makes child list long enough to accommodate setter.  */
160        private void extendChildList(int index) {
161            if (index >= this.children.length) {
162                ProfileStructure[] newCopy = new ProfileStructure[index + 1];
163                System.arraycopy(this.children, 0, newCopy, 0, this.children.length);
164                this.children = newCopy;
165            }        
166        }
167        
168    }