KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > DBElement


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.text.Collator JavaDoc;
25
26 /** Base class for representations of elements corresponding to the database metadata.
27  */

28 public abstract class DBElement implements Comparable JavaDoc, DBElementProperties {
29
30     /** Implementation */
31     Impl impl;
32
33     /** Default constructor.
34      */

35     public DBElement() {
36     }
37
38     /** Creates a new element with the provided implementation. The implementation
39      * is responsible for storing all properties of the object.
40      * @param impl the implementation to use
41      */

42     protected DBElement(Impl impl) {
43         this.impl = impl;
44         impl.attachToElement(this);
45     }
46
47     /** Returns the implementation of the element.
48      * @return implementation for the element
49      */

50     public final Impl getElementImpl() {
51         return (DBElement.Impl) impl;
52     }
53
54    /** Sets the implementation factory of this database element.
55      * This method should only be used internally and for cloning
56      * and archiving.
57      * @param impl the implementation to use
58      */

59     public void setElementImpl (DBElement.Impl anImpl) {
60         impl = anImpl;
61
62         if (impl != null)
63             impl.attachToElement(this);
64     }
65
66     /** Gets the name of this element.
67      * @return the name
68      */

69     public DBIdentifier getName() { //cannot be final because of overriding in ColumnPairElement
70
DBIdentifier name = getElementImpl().getName();
71         
72         return name;
73     }
74     
75     /** Sets the name of this element.
76     * @param name the name
77     * @throws DBException if impossible
78     */

79     public final void setName(DBIdentifier name) throws DBException {
80         getElementImpl().setName(name);
81     }
82
83     /** Add a property change listener.
84      * @param l the listener to add
85      * @see DBElementProperties
86      */

87     public final void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
88         getElementImpl().addPropertyChangeListener(l);
89     }
90
91     /** Remove a property change listener.
92      * @param l the listener to remove
93      * @see DBElementProperties
94      */

95     public final void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
96         getElementImpl().removePropertyChangeListener(l);
97     }
98
99     /** Gets a string representation of the element.
100      * @return the string
101      */

102     public String JavaDoc toString() {
103         return getName().toString();
104     }
105
106     /** Compares two elements.
107      * @param obj the reference object with which to compare.
108      * @return the value 0 if the argument object is equal to
109      * this object; -1 if this object is less than the object
110      * argument; and 1 if this object is greater than the object argument.
111      * Null objects are "smaller".
112      */

113     public int compareTo(Object JavaDoc obj) {
114         // null is not allowed
115
if (obj == null)
116             throw new ClassCastException JavaDoc();
117         if (obj == this)
118             return 0;
119
120         String JavaDoc thisName = getName().getFullName();
121         String JavaDoc otherName = ((DBElement) obj).getName().getFullName();
122         
123         if (thisName == null)
124             return (otherName == null) ? 0 : -1;
125             
126         if (otherName == null)
127             return 1;
128             
129         int ret = Collator.getInstance().compare(thisName, otherName);
130         // if both names are equal, both objects might have different types.
131
// If so order both objects by their type names
132
// (necessary to be consistent with equals)
133
if ((ret == 0) && (getClass() != obj.getClass()))
134             ret = getClass().getName().compareTo(obj.getClass().getName());
135         
136         return ret;
137     }
138     
139     /** Indicates whether some other object is "equal to" this one.
140      * @return true if this object is the same as the obj argument; false otherwise.
141      */

142     public boolean equals(Object JavaDoc obj) {
143         if (obj == null)
144             return false;
145         if (obj == this)
146             return true;
147
148         // check for the right class and then do the name check
149
// by calling compareTo.
150
return (getClass() == obj.getClass()) && (compareTo(obj) == 0);
151     }
152     
153     /** Returns a hash code value for the element.
154      * @return a hash code value for this object.
155      */

156     public int hashCode() {
157         return (getName() != null && getName().getFullName() != null) ? getName().getFullName().hashCode() : 0;
158     }
159     
160     
161     /** Pluggable implementation of the storage of element properties.
162     * @see DBElement#DBElement
163     */

164     public interface Impl {
165         /** Add some items. */
166         public static final int ADD = 1;
167         /** Remove some items. */
168         public static final int REMOVE = -1;
169         /** Set some items, replacing the old ones. */
170         public static final int SET = 0;
171
172         /** Called to attach the implementation to a specific
173          * element. Will be called in the element's constructor.
174          * Allows implementors of this interface to store a reference to the
175          * holder class, useful for implementing the property change listeners.
176          *
177          * @param element the element to attach to
178          */

179         public void attachToElement(DBElement el);
180
181         /** Get the name of this element.
182          * @return the name
183          */

184         public DBIdentifier getName();
185
186         /** Set the name of this element.
187          * @param name the name
188          * @throws DBException if impossible
189          */

190         public void setName(DBIdentifier name) throws DBException;
191
192         /** Add a property change listener.
193         * @param l the listener to add
194         */

195         public void addPropertyChangeListener(PropertyChangeListener JavaDoc l);
196
197         /** Remove a property change listener.
198          * @param l the listener to remove
199          */

200         public void removePropertyChangeListener(PropertyChangeListener JavaDoc l);
201     }
202
203     /** Default implementation of the Impl interface.
204      * It just holds the property values.
205      */

206     static abstract class Memory implements DBElement.Impl {
207         /** the element for this implementation */
208         protected DBElement _element;
209
210         /** Name of this element */
211         private DBIdentifier _name;
212
213         /** Property change support */
214         private PropertyChangeSupport JavaDoc support;
215
216         /** Constructor */
217         public Memory() {
218             super();
219         }
220
221         /** Copy */
222         public Memory(DBElement el) {
223             super();
224             _name = el.getName();
225         }
226
227         /** Attaches to element */
228         public void attachToElement(DBElement element) {
229             _element = element;
230         }
231
232         /** Getter for name of the element.
233          * @return the name
234          */

235         public final synchronized DBIdentifier getName() {
236             if (_name == null) // lazy initialization !?
237
_name = DBIdentifier.create(""); //NOI18N
238

239             return _name;
240         }
241
242         /** Setter for name of the element.
243          * @param name the name of the element
244          */

245         public synchronized void setName(DBIdentifier name) {
246             DBIdentifier old = _name;
247
248             _name = name;
249             firePropertyChange(PROP_NAME, old, name);
250         }
251
252         /** Fires property change event.
253          * @param name property name
254          * @param o old value
255          * @param n new value
256          */

257         protected final void firePropertyChange(String JavaDoc name, Object JavaDoc o, Object JavaDoc n) {
258             if (support != null)
259                 support.firePropertyChange(name, o, n);
260         }
261
262         /** Adds property listener */
263         public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
264             if (support == null)
265                 synchronized (this) {
266                     // new test under synchronized block
267
if (support == null)
268                         support = new PropertyChangeSupport JavaDoc(_element);
269                 }
270
271             support.addPropertyChangeListener(l);
272         }
273
274         /** Removes property listener */
275         public void removePropertyChangeListener (PropertyChangeListener JavaDoc l) {
276             if (support != null)
277                 support.removePropertyChangeListener(l);
278         }
279     }
280 }
281
Popular Tags