KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > TreeNamedObjectMap


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 package org.netbeans.tax;
20
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 //import java.beans.PropertyChangeListener;
29
//import java.beans.PropertyChangeEvent;
30

31 /**
32  *
33  * @author Libor Kramolis
34  * @version 0.1
35  */

36 public class TreeNamedObjectMap extends TreeObjectList {
37
38     /** */
39     private Map JavaDoc map; // index to underlaying list -- lazy initialized by getMap()
40

41     /** */
42     private KeyListener keyListener; // lazy initialized by getKeyListener()
43

44     
45     //
46
// init
47
//
48

49     /**
50      * Creates new TreeNamedObjectMap.
51      */

52     protected TreeNamedObjectMap (ContentManager contentManager) {
53         super (contentManager);
54         
55         this.map = null;
56         this.keyListener = null;
57     }
58     
59     /** Creates new TreeNamedObjectMap -- copy constructor. */
60     protected TreeNamedObjectMap (TreeNamedObjectMap namedObjectMap) {
61         super (namedObjectMap);
62     }
63     
64     
65     //
66
// itself
67
//
68

69     
70     /**
71      */

72     private KeyListener getKeyListener () {
73         if ( this.keyListener == null ) {
74             this.keyListener = new KeyListener ();
75         }
76         return keyListener;
77     }
78     
79     
80     //
81
// Map
82
//
83

84     /**
85      */

86     private Map JavaDoc getMap () {
87         if ( this.map == null ) {
88             this.map = new HashMap JavaDoc (3);
89         }
90         return map;
91     }
92     
93     /**
94      */

95     private void mapClear () {
96         Iterator JavaDoc it = getMap ().values ().iterator ();
97         while (it.hasNext ()) {
98             NamedObject namedObject = (NamedObject)it.next ();
99             namedObject.setKeyListener (null);
100             // namedObject.removePropertyChangeListener (namedObject.mapKeyPropertyName(), getKeyListener());
101
}
102         getMap ().clear ();
103     }
104     
105     /**
106      */

107     private Object JavaDoc mapPut (NamedObject namedObject) {
108         Object JavaDoc obj = getMap ().put (namedObject.mapKey (), namedObject);
109         namedObject.setKeyListener (getKeyListener ());
110         // namedObject.addPropertyChangeListener (namedObject.mapKeyPropertyName(), getKeyListener());
111

112         return obj;
113     }
114     
115     /**
116      */

117     private Object JavaDoc mapRemove (NamedObject namedObject) {
118         Object JavaDoc obj = getMap ().remove (namedObject.mapKey ());
119         namedObject.setKeyListener (null);
120         // namedObject.removePropertyChangeListener (namedObject.mapKeyPropertyName(), getKeyListener());
121

122         return obj;
123     }
124     
125     
126     //
127
// from TreeObject
128
//
129

130     /**
131      */

132     public Object JavaDoc clone () {
133         return new TreeNamedObjectMap (this);
134     }
135     
136     /**
137      */

138     public boolean equals (Object JavaDoc 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 JavaDoc thisIt = this.getMap ().keySet ().iterator ();
148         Iterator JavaDoc peerIt = peer.getMap ().keySet ().iterator ();
149         while ( thisIt.hasNext () ) {
150             Object JavaDoc thisNext = thisIt.next ();
151             Object JavaDoc 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     /*
162      * Update index to underlaying list.
163      * @see #findMergeCandidate
164      */

165     public void merge (TreeObject treeObject) throws CannotMergeException {
166         mapClear ();
167         
168         super.merge (treeObject);
169     }
170     
171     /*
172      * Suitable is a member with equalent mapKey.
173      */

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 JavaDoc key1 = ((NamedObject)candidate).mapKey ();
184                 Object JavaDoc key2 = ((NamedObject)original).mapKey ();
185                 
186                 if (key2 != null && key2.equals (key1))
187                     return i;
188             }
189         }
190         
191         return -1;
192     }
193     
194     //
195
// itself
196
//
197

198     
199     /**
200      */

201     public final Object JavaDoc get (Object JavaDoc mapKey) {
202         return getMap ().get (mapKey);
203     }
204     
205     
206     //
207
// from TreeObjectList
208
//
209

210     /**
211      */

212     protected boolean removeImpl (Object JavaDoc obj) throws ClassCastException JavaDoc {
213         boolean removed = super.removeImpl (obj);
214         
215         if (removed) {
216             mapRemove ((NamedObject)obj);
217         }
218         
219         return removed;
220     }
221     
222     /**
223      */

224     protected Object JavaDoc setImpl (int index, Object JavaDoc obj) throws ClassCastException JavaDoc {
225         Object JavaDoc oldObj = super.setImpl (index, obj);
226         
227         mapRemove ((NamedObject)oldObj);
228         mapPut ((NamedObject)obj);
229         
230         return oldObj;
231     }
232     
233     /**
234      */

235     protected Object JavaDoc removeImpl (int index) {
236         Object JavaDoc oldObj = super.removeImpl (index);
237         
238         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\nTreeNamedObjectMap::removeImpl [ " + index + " ] = " + oldObj); // NOI18N
239

240         if (oldObj != null) {
241             mapRemove ((NamedObject)oldObj);
242         }
243         
244         return (oldObj);
245     }
246     
247     /**
248      */

249     protected void addImpl (int index, Object JavaDoc obj) throws ClassCastException JavaDoc {
250         Object JavaDoc 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     /**
262      */

263     protected boolean addImpl (Object JavaDoc obj) throws ClassCastException JavaDoc {
264         Object JavaDoc 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     //
281
// util
282
//
283

284     /**
285      */

286 /* private void keyChanged (Object oldKey, Object newKey) {
287         Object oldValue = getMap().remove (oldKey);
288         Object newValue = getMap().get (newKey);
289  
290         if ( newValue != null ) {
291             remove (newValue);
292         }
293  
294         getMap().put (newKey, oldValue);
295     }*/

296     
297     
298     /**
299      */

300     private void keyChanged (Object JavaDoc oldKey) {
301         Object JavaDoc oldValue = getMap ().remove (oldKey);
302         Object JavaDoc newKey = ((NamedObject)oldValue).mapKey ();
303         Object JavaDoc newValue = getMap ().get (newKey);
304         
305         if ( newValue != null ) {
306             remove (newValue);
307         }
308         
309         getMap ().put (newKey, oldValue);
310     }
311     
312     
313     
314     //
315
// class NamedObject
316
//
317

318     /**
319      * Gives possibility to TreeNamedObjectMap to create a key for the object.
320      */

321     public static interface NamedObject {
322         
323         /** Used as key in map.
324          */

325         public Object JavaDoc mapKey ();
326         
327         /** Attach NamedObject to NamedObject Map. */
328         public void setKeyListener (KeyListener keyListener);
329         
330         /** Used to listen on key value change.
331          */

332         // public String mapKeyPropertyName ();
333

334         /**
335          */

336         // public void addPropertyChangeListener (String propertyName,
337
// PropertyChangeListener listener);
338

339         /**
340          */

341         // public void removePropertyChangeListener (String propertyName,
342
// PropertyChangeListener listener);
343

344     } // end: interface NamedObject
345

346     
347     
348     //
349
// class ContentManager
350
//
351

352     /**
353      *
354      */

355     protected static abstract class ContentManager extends TreeObjectList.ContentManager {
356         
357         /** @throws ClassCastException
358          */

359         public void checkAssignableObject (Object JavaDoc obj) throws ClassCastException JavaDoc {
360             super.checkAssignableObject (obj);
361             if (!!! (obj instanceof NamedObject)) {
362                 String JavaDoc msg = Util.THIS.getString ("EXC_instance_of_NamedObject"); //,obj.getClass().getName());
363
throw new ClassCastException JavaDoc (msg);
364             }
365         }
366         
367     } // end: interface ContentManager
368

369     
370     //
371
// class KeyListener
372
//
373

374     /**
375      *
376      */

377     public class KeyListener {
378         
379         private KeyListener () {
380         }
381         
382         /** Map Key changed. */
383         public void mapKeyChanged (Object JavaDoc oldKey) {
384             TreeNamedObjectMap.this.keyChanged (oldKey);
385         }
386         
387         /** */
388         // public void propertyChange (PropertyChangeEvent pche) {
389
// TreeNamedObjectMap.this.keyChanged (pche.getOldValue(), pche.getNewValue());
390
// }
391

392     } // end: class KeyListener
393

394 }
395
Popular Tags