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-2003 Sun Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.api.nodes2looks;

import java.util.*;
import java.io.Serializable;

import org.openide.nodes.Node;
import org.openide.util.Lookup;
import org.netbeans.api.nodes2looks.LookNode;
import org.netbeans.spi.looks.Look;
import org.netbeans.spi.looks.LookSelector;

/** Cache that stores and persists mapping between
 * nodes and their associated look descriptors.
 *
 * @author  Petr Hrebejk
 */
final class Cache extends Object implements Serializable, Node.Handle {
    static final long serialVersionUID = 5338245712342L;

    private Node.Handle handle;
    private Map cache = new HashMap();

    public Cache() {}

    public Cache (Node.Handle del) {
        this.handle = del instanceof Cache ? ((Cache)del).handle : del;
    }
    
    public Node getNode() throws java.io.IOException {
        Node n = handle.getNode ();
        if (n instanceof LookNode) {
            LookNode ln = (LookNode)n;
            this.handle = ln.getCache().handle;
            ln.setCache( this );
        }
        return n;
    }


    public void store (LookNode node) {

        org.netbeans.spi.looks.Look ld = node.getLook();
        Object ro = node.getRepresentedObject();
        String roID = ld.getName( ro, node.getLookup() );


        if ( roID == null ) {
            return;
        }

        String nodePath = getLookNodePath( node.getParentNode() );

        List items = (List)cache.get( nodePath );
        if ( items == null ) {
            items = new ArrayList();
            cache.put( nodePath, items );
        }

        String item[] = { ld.getName(), ro.getClass().getName(), roID};
        items.add( item );


    }

    public Look find (LookNode parentNode, Object representedObject ) {

        String path = getLookNodePath( parentNode );

        List items = (List)cache.get( path );

        if ( items == null ) {
            return null;
        }

        // findItem( );

        HashMap ldName2ld = new HashMap();

        for( Iterator it = items.iterator(); it.hasNext(); ) {
            String[] item = (String[])it.next();

            if (!item[1].equals (representedObject.getClass().getName())) {
                continue;
            }

            Look look = LookSelector_getLook ( parentNode.getLookSelectorForChildren(), item[0], representedObject );
            if (look == null) {
                continue;
            }

            if (look != null && item[2].equals (look.getName ( representedObject, Lookup.EMPTY ))) { // PENDING
                return look;
            }
        }

        return null;
    }


    // methods to track the state of the tree ----------------------------------

    // PENDING to be removed
    private static Look LookSelector_getLook ( LookSelector ls, String name, Object representedObject ) {

        Enumeration e = ls.getLooks( representedObject );

        while( e.hasMoreElements() ) {
            Look l = (Look)e.nextElement();
            if ( l.getName().equals( name ) ) {
                return l;
            }
        }

        return null;
    }


    private static String getLookNodePath( Node node ) {
        StringBuffer buf = new StringBuffer (512);

        for (;;) {
            if (!(node instanceof LookNode) ) {
                break;
            }

            buf.insert (0, node.getName() );
            node = node.getParentNode();
        }

        return buf.toString();
    }

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.