|
What this is
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 Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.xml.tree.lib; import java.lang.ref.WeakReference; import java.beans.beancontext.*; import java.beans.IntrospectionException; import java.util.Map; import java.util.HashMap; import java.util.TreeMap; import java.util.Arrays; import java.util.Comparator; import org.openide.nodes.*; /** Class that represents bean children of a JavaBeans context. * It listens on the bean context changes and creates nodes for * child beans. By default {@link BeanNode}s are created for all * child beans, but this behaviour can be changed by * providing a different factory to the constructor. * * * @author Petr Kuzel * @version based upon BeanChildren */ public class OrderedBeanChildren extends Children.Map { /** default factory for creation of children */ private static final BeanChildren.Factory DEFAULT_FACTORY = new OrderedBeanFactory (); /** bean context to work on */ private BeanContext bean; /** factory for creation of subnodes */ private BeanChildren.Factory factory; /** context listener */ private ContextL contextL; /** Create {@link BeanNode} children based on a Bean context. * @param bean the context */ public OrderedBeanChildren(BeanContext bean) { this (bean, DEFAULT_FACTORY); } /** Create children based on a Bean context. * @param bean the context * @param factory a factory to use for creation of child nodes */ public OrderedBeanChildren (BeanContext bean, BeanChildren.Factory factory) { this.bean = bean; this.factory = factory; } /** Helper method. Converts array of beans to map from * the (beans, Nodes) * @param array array of beans * @return map (Object, Node) */ private java.util.Map createMap (Object[] array) { TreeMap map = new TreeMap (new OrderComparator()); for (int i = 0; i < array.length; i++) { try { if (array[i] instanceof BeanContextSupport) { BeanContextSupport bcs = (BeanContextSupport)array[i]; if (bean.contains (bcs.getBeanContextPeer())) { // sometimes a BeanContextSupport occures in the list of // beans children even there is its peer. we think that // it is desirable to hide the context if the peer is // also present continue; } } map.put (array[i], factory.createNode (array[i])); } catch (IntrospectionException ex) { // ignore the exception } } return map; } /** Comparator able to compare any object. If comparables can not be compared * of they are not comparables return 0 to preserve order. */ private class OrderComparator implements java.util.Comparator { public boolean equals(final java.lang.Object comp) { return comp == null ? false : comp instanceof OrderComparator; } public int compare(final java.lang.Object p1,final java.lang.Object p2) { try { return ((Comparable)p1).compareTo(p2); } catch (ClassCastException ex) { // not comparable or comparable does not support this class return -1; } } } /* Initializes children. * * @return map (Object, Node) */ protected java.util.Map initMap () { // attaches a listener to the bean contextL = new ContextL (this); bean.addBeanContextMembershipListener (contextL); // test if there is a child //if (bean.size () == 0) return null; return createMap (bean.toArray ()); } /** Cease listening to changes in the bean context membership. */ protected void finalize () { if (contextL != null) bean.removeBeanContextMembershipListener (contextL); } /** Default factory. Creates BeanNode for each bean */ private static class OrderedBeanFactory extends Object implements BeanChildren.Factory { /** @return bean node */ public Node createNode (Object bean) throws IntrospectionException { return new OrderedBeanNode (bean); } } /** Context listener. */ private static final class ContextL implements BeanContextMembershipListener { /** weak reference to the BeanChildren object */ private WeakReference ref; /** Constructor */ ContextL (OrderedBeanChildren bc) { ref = new WeakReference (bc); } /** Listener method that is called when a bean is added to * the bean context. * @param bcme event describing the action */ public void childrenAdded (BeanContextMembershipEvent bcme) { OrderedBeanChildren bc = (OrderedBeanChildren)ref.get (); if (bc != null) { //bc.putAll (bc.createMap (bcme.toArray ())); } } /** Listener method that is called when a bean is removed to * the bean context. * @param bcme event describing the action */ public void childrenRemoved (BeanContextMembershipEvent bcme) { OrderedBeanChildren bc = (OrderedBeanChildren)ref.get (); if (bc != null) { //bc.removeAll (Arrays.asList (bcme.toArray ())); } } } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.