alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is Forte for Java, Community Edition. The Initial
 * Developer of the Original Code is Sun Microsystems, Inc. Portions
 * Copyright 1997-2002 Sun Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.looks;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.netbeans.spi.looks.LookSelector;

import java.util.EventObject;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import org.netbeans.spi.looks.Look;

import org.openide.util.enum.*;

/** Utility class for converting beween types and names.
 *
 * @author Jaroslav Tulach, Petr Hrebejk
 */
public class TypesSearch  {
    
    /** Static utility class
     */
    private TypesSearch() {};

    /** Enumeration for a java class.
     * @param c class 
     * @return enumeration of names of implemented/extended classes in form suitable
     *      for findLook
     */
    public static Enumeration namesForClass (Class c) {
        QueueEnumeration en = new QueueEnumeration () {
            protected void process (Object o) {
                Class x = (Class)o;
                Class s = x.getSuperclass();
                put (x.getInterfaces ());
                if (s != null && s != Object.class) {
                    // if not object process all interfaces and super classes
                    put (s);
                }
            }
        };
        if (c != Object.class) {
            en.put (c);
        }
        
        AlterEnumeration alter = new AlterEnumeration (en) {
            protected Object alter (Object clazz) {
                Class c = (Class)clazz;
                return c.getName ().replace ('.', '/');
            }
        };
        
        return new SequenceEnumeration (alter, new SingletonEnumeration ("java/lang/Object")); // NOI18N
    }
    
    /** Finds look(s) for a class.
     * @param prefix prefix string to add to each name or null
     * @param names list of Strings to check their names (c1/c2/name)
     * @param looks list where to add all found looks or null if just find one
     * @return look found or null 
     */
     static Enumeration findLooks( String prefix, Enumeration names, RegistryBridge rb ) {
         ArrayList list = new ArrayList ();        
         findLook( prefix, names, list, null, rb );
         return Collections.enumeration( list );
     }
    
     /** Original version of the method from namespace look */
     private static Look findLook (String prefix, Enumeration names, List looks, String preferredName, RegistryBridge registryBridge ) {
        
        Collection checks = new HashSet();
        Look preferredLook = null;
        
        while (names.hasMoreElements ()) {
            String check = (String)names.nextElement ();
            if (!checks.add(check))
                continue;
            if (prefix != null) {
                check = prefix + check;
            }

            Enumeration en = registryBridge.getObjects (check, Look.class);
            while (en.hasMoreElements ()) {                    
                Object o = en.nextElement ();

                if (o != null) {
                    if ( preferredName != null && 
                         preferredName.equals( ((Look)o).getName() ) ) {
                        preferredLook = (Look)o;
                    }
                    if (looks != null) {
                        // we are searching for all looks
                        looks.add (o);
                    } else {
                        // we need just one
                        return (Look)o;
                    }
                }
            }            
        }
        return preferredLook == null ? 
                   ( looks == null || looks.size() == 0 ?  null : (Look)looks.get( 0 ) ) :
                   preferredLook;
    }

    
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.