1 19 20 package org.netbeans.modules.dbschema; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.text.Collator ; 25 26 28 public abstract class DBElement implements Comparable , DBElementProperties { 29 30 31 Impl impl; 32 33 35 public DBElement() { 36 } 37 38 42 protected DBElement(Impl impl) { 43 this.impl = impl; 44 impl.attachToElement(this); 45 } 46 47 50 public final Impl getElementImpl() { 51 return (DBElement.Impl) impl; 52 } 53 54 59 public void setElementImpl (DBElement.Impl anImpl) { 60 impl = anImpl; 61 62 if (impl != null) 63 impl.attachToElement(this); 64 } 65 66 69 public DBIdentifier getName() { DBIdentifier name = getElementImpl().getName(); 71 72 return name; 73 } 74 75 79 public final void setName(DBIdentifier name) throws DBException { 80 getElementImpl().setName(name); 81 } 82 83 87 public final void addPropertyChangeListener(PropertyChangeListener l) { 88 getElementImpl().addPropertyChangeListener(l); 89 } 90 91 95 public final void removePropertyChangeListener(PropertyChangeListener l) { 96 getElementImpl().removePropertyChangeListener(l); 97 } 98 99 102 public String toString() { 103 return getName().toString(); 104 } 105 106 113 public int compareTo(Object obj) { 114 if (obj == null) 116 throw new ClassCastException (); 117 if (obj == this) 118 return 0; 119 120 String thisName = getName().getFullName(); 121 String 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 ((ret == 0) && (getClass() != obj.getClass())) 134 ret = getClass().getName().compareTo(obj.getClass().getName()); 135 136 return ret; 137 } 138 139 142 public boolean equals(Object obj) { 143 if (obj == null) 144 return false; 145 if (obj == this) 146 return true; 147 148 return (getClass() == obj.getClass()) && (compareTo(obj) == 0); 151 } 152 153 156 public int hashCode() { 157 return (getName() != null && getName().getFullName() != null) ? getName().getFullName().hashCode() : 0; 158 } 159 160 161 164 public interface Impl { 165 166 public static final int ADD = 1; 167 168 public static final int REMOVE = -1; 169 170 public static final int SET = 0; 171 172 179 public void attachToElement(DBElement el); 180 181 184 public DBIdentifier getName(); 185 186 190 public void setName(DBIdentifier name) throws DBException; 191 192 195 public void addPropertyChangeListener(PropertyChangeListener l); 196 197 200 public void removePropertyChangeListener(PropertyChangeListener l); 201 } 202 203 206 static abstract class Memory implements DBElement.Impl { 207 208 protected DBElement _element; 209 210 211 private DBIdentifier _name; 212 213 214 private PropertyChangeSupport support; 215 216 217 public Memory() { 218 super(); 219 } 220 221 222 public Memory(DBElement el) { 223 super(); 224 _name = el.getName(); 225 } 226 227 228 public void attachToElement(DBElement element) { 229 _element = element; 230 } 231 232 235 public final synchronized DBIdentifier getName() { 236 if (_name == null) _name = DBIdentifier.create(""); 239 return _name; 240 } 241 242 245 public synchronized void setName(DBIdentifier name) { 246 DBIdentifier old = _name; 247 248 _name = name; 249 firePropertyChange(PROP_NAME, old, name); 250 } 251 252 257 protected final void firePropertyChange(String name, Object o, Object n) { 258 if (support != null) 259 support.firePropertyChange(name, o, n); 260 } 261 262 263 public synchronized void addPropertyChangeListener(PropertyChangeListener l) { 264 if (support == null) 265 synchronized (this) { 266 if (support == null) 268 support = new PropertyChangeSupport (_element); 269 } 270 271 support.addPropertyChangeListener(l); 272 } 273 274 275 public void removePropertyChangeListener (PropertyChangeListener l) { 276 if (support != null) 277 support.removePropertyChangeListener(l); 278 } 279 } 280 } 281 | Popular Tags |