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 NetBeans. The Initial Developer of the Original
 * Code is Nokia. Portions Copyright 2003-2004 Nokia.
 * All Rights Reserved.
 */

package org.netbeans.modules.enode;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;

import org.openide.ErrorManager;
import org.openide.util.WeakListener;
import org.openide.util.RequestProcessor;

import org.netbeans.api.enode.ExtensibleNode;
import org.netbeans.api.registry.*;

/**
 * Object that helps the ExtensibleNode to keep track of
 * its icon. 
 * @author David Strupl
 */
public class ExtensibleNodeIcons {
    
    /**
     * Our node.
     */
    private ExtensibleNode enode;
    
    /**
     * We hold a reference to the listener for preventing
     * the garbage collection.
     */
    private Listener listener;
    
    /**
     * Prevent the listeners to be attached more than once.
     */
    private boolean listenersAttached = false;
    
    
    /**
     * Just remember the parameter enode.
     */
    public ExtensibleNodeIcons(ExtensibleNode enode) {
        this.enode = enode;
    }
    
    /** Reads actions from a context. Also adds a listener
     * to the context.
     * @param name of the context.
     * @return array of actions
     */
    public String getIconBase() {
        ArrayList arr = new ArrayList ();
        String paths[] = enode.getPaths();
        for (int i = 0; i < paths.length; i++) {
            String path = ExtensibleNode.E_NODE_ICONS + paths[i];
            try {
                Context con = Context.getDefault().getSubcontext(path);
                if (con == null) {
                    con = Context.getDefault().createSubcontext(path);
                }
                if (!listenersAttached) {
                    ContextListener l1 = getContextListener(con);
                    con.addContextListener(l1);
                }
                List objects = con.getOrderedObjects();
                Iterator it = objects.iterator();
                while (it.hasNext()) {
                    Object obj = it.next();
                    if (obj instanceof String) {
                        arr.add(obj);
                    }
                }
            } catch (ContextException ce) {
                ErrorManager.getDefault().getInstance("org.netbeans.modules.enode").notify(ErrorManager.INFORMATIONAL, ce); // NOI18N
            }
        }
        listenersAttached = true;
        if (arr.isEmpty()) {
            return null;
        }
        return (String)arr.get(0);
    }

    /**
     * Lazy initialization of the listener variable. This method
     * will return a weak listener according to the type argument.
     * In both cases the weak listener references the object hold
     * by the  listener  variable.
     * @param type ObjectChangeListener or NamespaceChangeListener
     */
    private ContextListener getContextListener(Object source) {
        if (listener == null) {
            listener = new Listener();
        }
        return (ContextListener)WeakListener.create(ContextListener.class, listener, source);
    }
    
    /**
     * If something has changed on the system file system, force
     * the icon reload.
     */
    private void changeIcon() {
        RequestProcessor.getDefault().post(new Runnable() {
            public void run() {
                String iBase = getIconBase();
                if (iBase != null) {
                    enode.setIconBase(iBase);
                }
            }
        });
    }
    
    /**
     * Whatever happens in the selected context this listener only calls
     * changeIcon. 
     */
    private class Listener implements ContextListener {
        public void attributeChanged(AttributeEvent evt) {
            changeIcon();
        }
        
        public void bindingChanged(BindingEvent evt) {
            changeIcon();
        }
        
        public void subcontextChanged(SubcontextEvent evt) {
            changeIcon();
        }
    }
}
... 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.