KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.List JavaDoc;
29 import org.netbeans.spi.looks.Look;
30 import org.openide.util.Enumerations;
31
32 /** Utility class for converting beween types and names.
33  *
34  * @author Jaroslav Tulach, Petr Hrebejk
35  */

36 public class TypesSearch {
37
38     /** Static utility class
39      */

40     private TypesSearch() {};
41
42     /** Enumeration for a java class.
43      * @param c class
44      * @return enumeration of names of implemented/extended classes in form suitable
45      * for findLook
46      */

47     public static Enumeration JavaDoc namesForClass (Class JavaDoc c) {
48         Enumeration JavaDoc en;
49         if (c != Object JavaDoc.class) {
50             en = Enumerations.queue(Enumerations.singleton(c), new Enumerations.Processor() {
51                 public Object JavaDoc process(Object JavaDoc o, Collection JavaDoc coll) {
52                     Class JavaDoc x = (Class JavaDoc) o;
53                     Class JavaDoc s = x.getSuperclass();
54                     coll.addAll(Arrays.asList(x.getInterfaces()));
55                     if (s != null && s != Object JavaDoc.class) {
56                         // if not object process all interfaces and super classes
57
coll.add(s);
58                     }
59                     return o;
60                 }
61             });
62         } else {
63             en = Enumerations.empty();
64         }
65         
66         Enumeration JavaDoc alter = Enumerations.convert(en, new Enumerations.Processor() {
67             public Object JavaDoc process(Object JavaDoc clazz, Collection JavaDoc ignore) {
68                 Class JavaDoc c = (Class JavaDoc) clazz;
69                 return c.getName().replace('.', '/');
70             }
71         });
72         
73         return Enumerations.concat(alter, Enumerations.singleton("java/lang/Object")); // NOI18N
74
}
75     
76     /** Finds look(s) for a class.
77      * @param prefix prefix string to add to each name or null
78      * @param names list of Strings to check their names (c1/c2/name)
79      * @param looks list where to add all found looks or null if just find one
80      * @return look found or null
81      */

82      static Enumeration JavaDoc findLooks( String JavaDoc prefix, Enumeration JavaDoc names, RegistryBridge rb ) {
83          ArrayList JavaDoc list = new ArrayList JavaDoc ();
84          findLook( prefix, names, list, null, rb );
85          return Collections.enumeration( list );
86      }
87     
88      /** Original version of the method from namespace look */
89      private static Look findLook (String JavaDoc prefix, Enumeration JavaDoc names, List JavaDoc looks, String JavaDoc preferredName, RegistryBridge registryBridge ) {
90         
91         Collection JavaDoc checks = new HashSet JavaDoc();
92         Look preferredLook = null;
93         
94         while (names.hasMoreElements ()) {
95             String JavaDoc check = (String JavaDoc)names.nextElement ();
96             if (!checks.add(check))
97                 continue;
98             if (prefix != null) {
99                 check = prefix + check;
100             }
101
102             Enumeration JavaDoc en = registryBridge.getObjects (check, Look.class);
103             while (en.hasMoreElements ()) {
104                 Object JavaDoc o = en.nextElement ();
105
106                 if (o != null) {
107                     if ( preferredName != null &&
108                          preferredName.equals( ((Look)o).getName() ) ) {
109                         preferredLook = (Look)o;
110                     }
111                     if (looks != null) {
112                         // we are searching for all looks
113
looks.add (o);
114                     } else {
115                         // we need just one
116
return (Look)o;
117                     }
118                 }
119             }
120         }
121         return preferredLook == null ?
122                    ( looks == null || looks.size() == 0 ? null : (Look)looks.get( 0 ) ) :
123                    preferredLook;
124     }
125     
126 }
127
Popular Tags