1 19 package org.netbeans.tax; 20 21 import java.util.Map ; 22 import java.util.Set ; 23 import java.util.List ; 24 import java.util.LinkedList ; 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 31 36 public class TreeNamedObjectMap extends TreeObjectList { 37 38 39 private Map map; 41 42 private KeyListener keyListener; 44 45 49 52 protected TreeNamedObjectMap (ContentManager contentManager) { 53 super (contentManager); 54 55 this.map = null; 56 this.keyListener = null; 57 } 58 59 60 protected TreeNamedObjectMap (TreeNamedObjectMap namedObjectMap) { 61 super (namedObjectMap); 62 } 63 64 65 69 70 72 private KeyListener getKeyListener () { 73 if ( this.keyListener == null ) { 74 this.keyListener = new KeyListener (); 75 } 76 return keyListener; 77 } 78 79 80 84 86 private Map getMap () { 87 if ( this.map == null ) { 88 this.map = new HashMap (3); 89 } 90 return map; 91 } 92 93 95 private void mapClear () { 96 Iterator it = getMap ().values ().iterator (); 97 while (it.hasNext ()) { 98 NamedObject namedObject = (NamedObject)it.next (); 99 namedObject.setKeyListener (null); 100 } 102 getMap ().clear (); 103 } 104 105 107 private Object mapPut (NamedObject namedObject) { 108 Object obj = getMap ().put (namedObject.mapKey (), namedObject); 109 namedObject.setKeyListener (getKeyListener ()); 110 112 return obj; 113 } 114 115 117 private Object mapRemove (NamedObject namedObject) { 118 Object obj = getMap ().remove (namedObject.mapKey ()); 119 namedObject.setKeyListener (null); 120 122 return obj; 123 } 124 125 126 130 132 public Object clone () { 133 return new TreeNamedObjectMap (this); 134 } 135 136 138 public boolean equals (Object object, boolean deep) { 139 if (!!! super.equals (object, deep)) 140 return false; 141 142 TreeNamedObjectMap peer = (TreeNamedObjectMap) object; 143 144 if ( this.getMap ().size () != peer.getMap ().size () ) 145 return false; 146 147 Iterator thisIt = this.getMap ().keySet ().iterator (); 148 Iterator peerIt = peer.getMap ().keySet ().iterator (); 149 while ( thisIt.hasNext () ) { 150 Object thisNext = thisIt.next (); 151 Object peerNext = peerIt.next (); 152 if (!!! Util.equals (thisNext, peerNext)) 153 return false; 154 if (!!! Util.equals (this.getMap ().get (thisNext), peer.getMap ().get (peerNext))) 155 return false; 156 } 157 158 return true; 159 } 160 161 165 public void merge (TreeObject treeObject) throws CannotMergeException { 166 mapClear (); 167 168 super.merge (treeObject); 169 } 170 171 174 protected int findMergeCandidate (final TreeObject original, final TreeObject[] candidates) { 175 if ((original instanceof NamedObject) == false) 176 return -1; 177 178 for (int i = 0; i<candidates.length; i++) { 179 TreeObject candidate = candidates[i]; 180 if (candidate == null) 181 continue; 182 if (candidate instanceof NamedObject) { 183 Object key1 = ((NamedObject)candidate).mapKey (); 184 Object key2 = ((NamedObject)original).mapKey (); 185 186 if (key2 != null && key2.equals (key1)) 187 return i; 188 } 189 } 190 191 return -1; 192 } 193 194 198 199 201 public final Object get (Object mapKey) { 202 return getMap ().get (mapKey); 203 } 204 205 206 210 212 protected boolean removeImpl (Object obj) throws ClassCastException { 213 boolean removed = super.removeImpl (obj); 214 215 if (removed) { 216 mapRemove ((NamedObject)obj); 217 } 218 219 return removed; 220 } 221 222 224 protected Object setImpl (int index, Object obj) throws ClassCastException { 225 Object oldObj = super.setImpl (index, obj); 226 227 mapRemove ((NamedObject)oldObj); 228 mapPut ((NamedObject)obj); 229 230 return oldObj; 231 } 232 233 235 protected Object removeImpl (int index) { 236 Object oldObj = super.removeImpl (index); 237 238 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("\nTreeNamedObjectMap::removeImpl [ " + index + " ] = " + oldObj); 240 if (oldObj != null) { 241 mapRemove ((NamedObject)oldObj); 242 } 243 244 return (oldObj); 245 } 246 247 249 protected void addImpl (int index, Object obj) throws ClassCastException { 250 Object oldObj = getMap ().get (((NamedObject)obj).mapKey ()); 251 252 if ( oldObj != null ) { 253 remove (oldObj); 254 } 255 256 super.addImpl (index, obj); 257 258 mapPut ((NamedObject)obj); 259 } 260 261 263 protected boolean addImpl (Object obj) throws ClassCastException { 264 Object oldObj = getMap ().get (((NamedObject)obj).mapKey ()); 265 266 if ( oldObj != null ) { 267 remove (oldObj); 268 } 269 270 boolean added = super.addImpl (obj); 271 272 if ( added ) { 273 mapPut ((NamedObject)obj); 274 } 275 276 return added; 277 } 278 279 280 284 286 296 297 298 300 private void keyChanged (Object oldKey) { 301 Object oldValue = getMap ().remove (oldKey); 302 Object newKey = ((NamedObject)oldValue).mapKey (); 303 Object newValue = getMap ().get (newKey); 304 305 if ( newValue != null ) { 306 remove (newValue); 307 } 308 309 getMap ().put (newKey, oldValue); 310 } 311 312 313 314 318 321 public static interface NamedObject { 322 323 325 public Object mapKey (); 326 327 328 public void setKeyListener (KeyListener keyListener); 329 330 332 334 336 339 341 344 } 346 347 348 352 355 protected static abstract class ContentManager extends TreeObjectList.ContentManager { 356 357 359 public void checkAssignableObject (Object obj) throws ClassCastException { 360 super.checkAssignableObject (obj); 361 if (!!! (obj instanceof NamedObject)) { 362 String msg = Util.THIS.getString ("EXC_instance_of_NamedObject"); throw new ClassCastException (msg); 364 } 365 } 366 367 } 369 370 374 377 public class KeyListener { 378 379 private KeyListener () { 380 } 381 382 383 public void mapKeyChanged (Object oldKey) { 384 TreeNamedObjectMap.this.keyChanged (oldKey); 385 } 386 387 388 392 } 394 } 395 | Popular Tags |