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 &quot;value&quot; 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         * Get a shallow clone of the CatalogItem. This is for internal use only. 
127         * 
128         * @override Never
129         */
130        @SuppressWarnings("unchecked")
131            protected <T extends CatalogItemImpl> T getInternalShallowClone() {
132            return (T) getShallowClone();
133        }
134        
135    
136        /**
137         * Compare this CatalogItem to an object.
138         *
139         * @override Sometimes The default implementation assumes that the given object is also a CatalogItem and
140         * compares the {@link #getName() names} of these two CatalogItems.
141         *
142         * @exception ClassCastException if the given object cannot be cast into a CatalogItem.
143         */
144        public int compareTo(Object o) {
145            return getName().compareTo(((CatalogItem)o).getName());
146        }
147    
148        /**
149         * Check whether two objects are equal.
150         *
151         * @override Sometimes The default implementation returns true iff <code>o</code> is identical to this
152         * object.
153         */
154        public boolean equals(Object o) {
155            return (this == o);
156        }
157    
158        /**
159         * Add a PropertyChangeListener that will receive events whenever the &quot;value&quot; property changes.
160         *
161         * @override Never
162         */
163        public void addValueListener(PropertyChangeListener pcl) {
164            m_pcsPropertyListeners.addPropertyChangeListener(VALUE_PROPERTY, pcl);
165        }
166    
167        /**
168         * Remove a PropertyChangeListener for the &quot;value&quot; property.
169         *
170         * @override Never
171         */
172        public void removeValueListener(PropertyChangeListener pcl) {
173            m_pcsPropertyListeners.removePropertyChangeListener(VALUE_PROPERTY, pcl);
174        }
175    }