KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > looks > SelectorEvent


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.looks;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.EventObject JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import org.netbeans.spi.looks.LookSelector;
29
30 /** Event fired from the LookSelector when it's content changes.
31  *
32  * @see org.netbeans.spi.looks.LookSelector
33  * @see org.netbeans.spi.looks.SelectorListener
34  * @author Petr Hrebejk, Jaroslav Tulach
35  */

36 public class SelectorEvent extends EventObject JavaDoc {
37
38     private SelectorImpl impl;
39     private HashMap JavaDoc oldCache;
40     
41     /** Creates a new instance of SelectorEvent
42      * @param source LookSelector whose content was changed
43      */

44     public SelectorEvent( LookSelector source ) {
45         super( source );
46         impl = Accessor.DEFAULT.getSelectorImpl( source );
47         oldCache = impl.getCache();
48     }
49     
50     /** Determines whether the change in the content of the LookSelector
51      * affects nodes which are used for visualisation of given represented
52      * object.
53      * <P>
54      * The default implementation returns true for all objects. If extending
55      * LookSelector the implementor is responsible for keeping semantics of
56      * looking for Looks/LookSelectors in sync with the semantics of decision
57      * whether objects are affected or node.
58      * @param representedObject The represented object of node which may be
59      * affected by the change of LookSelector content.
60      * @return <CODE>True</CODE> if node with given represented object is affected
61      * <CODE>false</CODE> otherwise.
62      */

63     public boolean affectsObject( Object JavaDoc representedObject ) {
64         return true;
65     }
66     
67     public Collection JavaDoc getAddedLooks( Object JavaDoc representedObject ) {
68         if ( oldCache == null ) {
69             // No cache, no problem
70
return Collections.EMPTY_SET;
71         }
72         
73         Object JavaDoc key = impl.getKey4Object( representedObject );
74         
75         Set JavaDoc diff[] = getDiff4Key( key );
76         if ( diff == null ) {
77             return Collections.EMPTY_SET;
78         }
79         else {
80             return diff[0];
81         }
82         
83     }
84     
85     public Collection JavaDoc getRemovedLooks( Object JavaDoc representedObject ) {
86         
87         if ( oldCache == null ) {
88             // No cache, no problem
89
return Collections.EMPTY_SET;
90         }
91         
92         Object JavaDoc key = impl.getKey4Object( representedObject );
93         
94         Set JavaDoc diff[] = getDiff4Key( key );
95         if ( diff == null ) {
96             return Collections.EMPTY_SET;
97         }
98         else {
99             return diff[1];
100         }
101         
102     }
103     
104     // Private methods ---------------------------------------------------------
105

106     /** Computes diff for given key. When the diff is computed for the first
107      * time it will put ito the old cache instad of the original CacheItem
108      */

109     private Set JavaDoc[] getDiff4Key( Object JavaDoc key ) {
110         Object JavaDoc o = oldCache.get( key );
111         
112         if ( key instanceof Set JavaDoc[] ) {
113             // It was already computed, just return
114
return (Set JavaDoc[])o;
115         }
116         else {
117             // We have to compute, And put it back into the cache
118

119             Set JavaDoc[] diff = new Set JavaDoc[2];
120             
121             SelectorImplFactory.CacheItem oldItem = (SelectorImplFactory.CacheItem)oldCache.get( key );
122             Collection JavaDoc oldLooks = oldItem.getCachedLooks( false );
123             Collection JavaDoc newLooks = Collections.list( impl.getLooks4Key( key ) );
124             
125             // Newly added looks
126
diff[0] = new HashSet JavaDoc( newLooks );
127             diff[0].removeAll( oldLooks );
128             
129             // Removed looks
130
diff[1] = new HashSet JavaDoc( oldLooks );
131             diff[1].removeAll( newLooks );
132                         
133             return diff;
134         }
135             
136     }
137     
138     
139 }
140
Popular Tags