KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > nodes2looks > Cache


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.api.nodes2looks;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import org.openide.nodes.Node;
30 import org.openide.util.Lookup;
31 import org.netbeans.spi.looks.Look;
32 import org.netbeans.spi.looks.LookSelector;
33
34 /** Cache that stores and persists mapping between
35  * nodes and their associated look descriptors.
36  *
37  * @author Petr Hrebejk
38  */

39 final class Cache extends Object JavaDoc implements Serializable JavaDoc, Node.Handle {
40     static final long serialVersionUID = 5338245712342L;
41
42     private Node.Handle handle;
43     private Map JavaDoc cache = new HashMap JavaDoc();
44
45     public Cache() {}
46
47     public Cache (Node.Handle del) {
48         this.handle = del instanceof Cache ? ((Cache)del).handle : del;
49     }
50     
51     public Node getNode() throws java.io.IOException JavaDoc {
52         Node n = handle.getNode ();
53         if (n instanceof LookNode) {
54             LookNode ln = (LookNode)n;
55             this.handle = ln.getCache().handle;
56             ln.setCache( this );
57         }
58         return n;
59     }
60
61
62     public void store (LookNode node) {
63
64         org.netbeans.spi.looks.Look ld = node.getLook();
65         Object JavaDoc ro = node.getRepresentedObject();
66         String JavaDoc roID = ld.getName( ro, node.getLookup() );
67
68
69         if ( roID == null ) {
70             return;
71         }
72
73         String JavaDoc nodePath = getLookNodePath( node.getParentNode() );
74
75         List JavaDoc items = (List JavaDoc)cache.get( nodePath );
76         if ( items == null ) {
77             items = new ArrayList JavaDoc();
78             cache.put( nodePath, items );
79         }
80
81         String JavaDoc item[] = { ld.getName(), ro.getClass().getName(), roID};
82         items.add( item );
83
84
85     }
86
87     public Look find (LookNode parentNode, Object JavaDoc representedObject ) {
88
89         String JavaDoc path = getLookNodePath( parentNode );
90
91         List JavaDoc items = (List JavaDoc)cache.get( path );
92
93         if ( items == null ) {
94             return null;
95         }
96
97         // findItem( );
98

99         HashMap JavaDoc ldName2ld = new HashMap JavaDoc();
100
101         for( Iterator JavaDoc it = items.iterator(); it.hasNext(); ) {
102             String JavaDoc[] item = (String JavaDoc[])it.next();
103
104             if (!item[1].equals (representedObject.getClass().getName())) {
105                 continue;
106             }
107
108             Look look = LookSelector_getLook ( parentNode.getLookSelectorForChildren(), item[0], representedObject );
109             if (look == null) {
110                 continue;
111             }
112
113             if (look != null && item[2].equals (look.getName ( representedObject, Lookup.EMPTY ))) { // PENDING
114
return look;
115             }
116         }
117
118         return null;
119     }
120
121
122     // methods to track the state of the tree ----------------------------------
123

124     // PENDING to be removed
125
private static Look LookSelector_getLook ( LookSelector ls, String JavaDoc name, Object JavaDoc representedObject ) {
126
127         Enumeration JavaDoc e = ls.getLooks( representedObject );
128
129         while( e.hasMoreElements() ) {
130             Look l = (Look)e.nextElement();
131             if ( l.getName().equals( name ) ) {
132                 return l;
133             }
134         }
135
136         return null;
137     }
138
139
140     private static String JavaDoc getLookNodePath( Node node ) {
141         StringBuffer JavaDoc buf = new StringBuffer JavaDoc (512);
142
143         for (;;) {
144             if (!(node instanceof LookNode) ) {
145                 break;
146             }
147
148             buf.insert (0, node.getName() );
149             node = node.getParentNode();
150         }
151
152         return buf.toString();
153     }
154
155 }
156
Popular Tags