001    package data.ooimpl;
002    
003    import data.*;
004    
005    import java.beans.*;
006    
007    /**
008     * Pure Java implementation of the {@link CatalogItem} interface. See the documentation for
009     * {@link CatalogItem} for a description of the semantics of this class.
010     *
011     * @author Steffen Zschaler
012     * @version 2.0 19/08/1999
013     * @since v2.0
014     */
015    public abstract class CatalogItemImpl extends AbstractNameable implements CatalogItem, DataBasketKeys, Cloneable {
016    
017        /**
018         * The Catalog that owns this CatalogItem, if any.
019         *
020         * @serial
021         */
022        private CatalogImpl m_ciOwner;
023    
024        /**
025         * The value of this CatalogItem.
026         *
027         * @serial
028         */
029        private Value m_vValue;
030    
031        /**
032         * Create a new CatalogItemImpl. The item's value defaults to an {@link IntegerValue} of 0.
033         *
034         * @param sName the name of the new item.
035         */
036        public CatalogItemImpl(String sName) {
037            this(sName, new IntegerValue(0));
038        }
039    
040        /**
041         * Create a new CatalogItemImpl.
042         *
043         * @param sName the name of the new item.
044         * @param vValue the value of the item.
045         */
046        public CatalogItemImpl(String sName, Value vValue) {
047            super(sName);
048    
049            m_vValue = vValue;
050        }
051    
052        /**
053         * Get the item's value.
054         *
055         * @override Never
056         *
057         * @return a clone of the item's value.
058         */
059        public Value getValue() {
060            return (Value)m_vValue.clone();
061        }
062    
063        /**
064         * Set the item's value. This method is protected so that subclasses can decide whether or not to allow
065         * clients to set the value of a CatalogItem once it has been created.
066         *
067         * <p>The method will fire a property change event for the "value" property.</p>
068         *
069         * @override Sometimes
070         */
071        protected void setValue(Value vNew) {
072            Value v = m_vValue;
073    
074            m_vValue = vNew;
075    
076            m_pcsPropertyListeners.firePropertyChange(VALUE_PROPERTY, v, m_vValue);
077        }
078    
079        /**
080         * Return true if this CatalogItem is editable.
081         *
082         * @override Never
083         */
084        public boolean isEditable() {
085            return ((getCatalog() == null) ||
086                    (((CatalogImpl)getCatalog()).getEditingItemsContainer().get(getName()) == this));
087        }
088    
089        /**
090         * Get the Catalog of this CatalogItem.
091         *
092         * @override Never
093         */
094        public Catalog getCatalog() {
095            return m_ciOwner;
096        }
097    
098        /**
099         * Set the Catalog of this CatalogItem. Used internally only.
100         *
101         * @override Never
102         */
103        void setCatalog(CatalogImpl ci) {
104            m_ciOwner = ci;
105            attach(ci);
106        }
107    
108        /**
109         * Return a String representation of the item.
110         *
111         * @override Sometimes
112         */
113        public String toString() {
114            return "Abstract CatalogItem \"" + getName() + "\"";
115        }
116    
117        /**
118         * Get a shallow clone of the CatalogItem. For normal CatalogItems there is no difference between a shallow
119         * and a deep clone.
120         *
121         * @override Always
122         */
123        protected abstract CatalogItemImpl getShallowClone();
124    
125        /**
126         * Compare this CatalogItem to an object.
127         *
128         * @override Sometimes The default implementation assumes that the given object is also a CatalogItem and
129         * compares the {@link #getName() names} of these two CatalogItems.
130         *
131         * @exception ClassCastException if the given object cannot be cast into a CatalogItem.
132         */
133        public int compareTo(Object o) {
134            return getName().compareTo(((CatalogItem)o).getName());
135        }
136    
137        /**
138         * Check whether two objects are equal.
139         *
140         * @override Sometimes The default implementation returns true iff <code>o</code> is identical to this
141         * object.
142         */
143        public boolean equals(Object o) {
144            return (this == o);
145        }
146    
147        /**
148         * Add a PropertyChangeListener that will receive events whenever the "value" property changes.
149         *
150         * @override Never
151         */
152        public void addValueListener(PropertyChangeListener pcl) {
153            m_pcsPropertyListeners.addPropertyChangeListener(VALUE_PROPERTY, pcl);
154        }
155    
156        /**
157         * Remove a PropertyChangeListener for the "value" property.
158         *
159         * @override Never
160         */
161        public void removeValueListener(PropertyChangeListener pcl) {
162            m_pcsPropertyListeners.removePropertyChangeListener(VALUE_PROPERTY, pcl);
163        }
164    }