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 Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.xml.tree.lib;

import java.awt.Component;
import java.util.ArrayList;
import java.util.Enumeration;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.beans.*;

import org.openide.nodes.*;

import org.netbeans.modules.xml.tree.lib.GuiUtil;

import org.netbeans.tax.TreeObject;
import org.netbeans.tax.TreeException;
import org.netbeans.tax.TreeAttribute;
import org.netbeans.tax.TreeElement;

/**
 *
 * @author  Libor Kramolis
 * @version 0.1
 */
public final class BeanUtil {

    /** Test if there is a customizer for this object. If true
     * the customizer can be obtained via getCustomizer method.
     *
     * @param object bean to test its customizer
     * @return true if there is a customizer.
     */
    public static boolean hasCustomizer (Object object) {
        // true if we have already computed beanInfo and it has customizer class
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer: object = " + object); // NOI18N

        if (object == null)
            return false;

        try {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer: declaringClass = " + object.getClass().getDeclaringClass()); // NOI18N

	    BeanInfo bi = Introspector.getBeanInfo (object.getClass());
            
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer: beanInfo = " + bi); // NOI18N

	    BeanDescriptor bd = bi.getBeanDescriptor();
            
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer: beanDescriptor = " + bd); // NOI18N

	    Class cc = bd.getCustomizerClass();

            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer: customizerClass = " + cc); // NOI18N

            return ( cc != null);
        } catch (IntrospectionException e) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer: exception = " + e); // NOI18N

            return false;
        }
    }
    

    /** Returns the customizer component for object.
     *
     * @param object bean to get its customizer
     * @return the component or null if there is no customizer
     */
    public static Component getCustomizer (Object object) {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer: object = " + object); // NOI18N

        if (object == null)
            return null;
        
        BeanInfo beanInfo;
        try {
            beanInfo = Introspector.getBeanInfo (object.getClass());
        } catch (IntrospectionException e) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer: exception = " + e); // NOI18N

            return null;
        }
        Class clazz = beanInfo.getBeanDescriptor().getCustomizerClass();
        if (clazz == null) {
            return null;
        }

        Object o;
        try {
            o = clazz.newInstance ();
        } catch (InstantiationException e) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer: exception = " + e); // NOI18N

            return null;
        } catch (IllegalAccessException e) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer: exception = " + e); // NOI18N

            return null;
        }

        if (!!! (o instanceof Customizer) ) {
            // no customizer => no fun
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer: is NOT instanceof Customizer: " + o); // NOI18N

            return null;
        }

        Customizer cust = ((java.beans.Customizer)o);

        // looking for the component
        Component comp = null;
        if (o instanceof Component) {
            comp = (Component)o;
        } else {
            // no component provided
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer: is NOT instanceof Component: " + o); // NOI18N

            return null;
        }

        cust.setObject (object);

        return comp;
    }


    /**
     */
    public static boolean hasCustomizer (Class classClass, String propertyName) {
        BeanInfo beanInfo;
        try {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer2: declaringClass = " + classClass.getDeclaringClass()); // NOI18N

            beanInfo = Introspector.getBeanInfo (classClass);

            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer2: beanInfo = " + beanInfo); // NOI18N
        } catch (IntrospectionException e) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer2: exception = " + e); // NOI18N

            return false;
        }
	PropertyDescriptor[] propDescrs = beanInfo.getPropertyDescriptors();
	PropertyDescriptor propertyDescriptor = null;
	for ( int i = 0; i < propDescrs.length; i++ ) {
	    if ( propertyName.equals (propDescrs[i].getName()) ) {
		propertyDescriptor = propDescrs[i];
		break;
	    }
	}
	if ( propertyDescriptor == null ) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer2: have NOT property: " + propertyName); // NOI18N

	    return false;
	}
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer2: propertyDescriptor: " + propertyDescriptor); // NOI18N

        Class clazz = propertyDescriptor.getPropertyEditorClass();

        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer2: propertyEditorClass: " + clazz); // NOI18N

        if (clazz == null) {
            return false;
        }
        Object peo;
        try {
            peo = clazz.newInstance ();
        } catch (InstantiationException e) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer2: exception = " + e); // NOI18N

            return false;
        } catch (IllegalAccessException e) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer2: exception = " + e); // NOI18N

            return false;
        }
	
        if (!!! (peo instanceof PropertyEditor) ) {
            // no customizer => no fun
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::hasCustomizer2: is NOT instanceof PropertyEditor: " + peo); // NOI18N

            return false;
        }

        PropertyEditor editor = ((PropertyEditor)peo);
	
	return editor.supportsCustomEditor();
    }

    /**
     */
    public static Component getCustomizer (Class classClass, Object property, String propertyName) {
        BeanInfo beanInfo;
        try {
            beanInfo = Introspector.getBeanInfo (classClass);
        } catch (IntrospectionException e) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer2: exception = " + e); // NOI18N

            return null;
        }
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer2: beaninfo = " + beanInfo); // NOI18N

	PropertyDescriptor[] propDescrs = beanInfo.getPropertyDescriptors();
	PropertyDescriptor propertyDescriptor = null;
	for ( int i = 0; i < propDescrs.length; i++ ) {
	    if ( propertyName.equals (propDescrs[i].getName()) ) {
		propertyDescriptor = propDescrs[i];
		break;
	    }
	}
	if ( propertyDescriptor == null ) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer2: have NOT property: " + propertyName); // NOI18N

	    return null;
	}
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer2: propertyDescriptor: " + propertyDescriptor); // NOI18N

        Class clazz = propertyDescriptor.getPropertyEditorClass();

        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer2: propertyEditorClass: " + clazz); // NOI18N

        if (clazz == null) {
            return null;
        }
        Object peo;
        try {
            peo = clazz.newInstance ();
        } catch (InstantiationException e) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer2: exception = " + e); // NOI18N

            return null;
        } catch (IllegalAccessException e) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer2: exception = " + e); // NOI18N

            return null;
        }
	
        if (!!! (peo instanceof PropertyEditor) ) {
            // no customizer => no fun
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer2: is NOT instanceof PropertyEditor: " + peo); // NOI18N

            return null;
        }

        PropertyEditor editor = ((PropertyEditor)peo);
	editor.setValue (property);
	Component comp = editor.getCustomEditor();
	if ( comp == null ) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer2: have NOT customizer: " + editor); // NOI18N

	    return null;
	}
	if (!!! (comp instanceof Customizer) ) {
	    // no customizer => no fun
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::getCustomizer2: is NOT instanceof Customizer: " + comp); // NOI18N

            return null;
        }
        Customizer cust = ((Customizer)comp);

//          cust.setObject (property); // done by editor.setValue (property);

        return comp;
    }


    /** Computes a descriptor for properties from a bean info.
     *
     * @param bean bean to create properties for
     * @param info about the bean
     * @return three property lists
     */
    public static Node.Property[][] computeProperties (TreeObject bean, BeanInfo info) {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("BeanUtil::computeProperties: bean = " + bean); // NOI18N
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("        ::computeProperties: info = " + info); // NOI18N

        ArrayList property = new ArrayList ();
        ArrayList expert = new ArrayList ();
        ArrayList hidden = new ArrayList ();

        PropertyDescriptor[] propertyDescriptor = info.getPropertyDescriptors ();

        int k = propertyDescriptor.length;
        for (int i = 0; i < k; i ++) {
            Node.Property prop;

            if (propertyDescriptor[i] instanceof IndexedPropertyDescriptor) {
                IndexedPropertyDescriptor p = (IndexedPropertyDescriptor) propertyDescriptor [i];
                IndexedPropertySupport support =  new IndexedPropertySupport
                    (bean,
                     p.getPropertyType (),
                     p.getIndexedPropertyType(),
                     p.getReadMethod (),
                     p.getWriteMethod (),
                     p.getIndexedReadMethod (),
                     p.getIndexedWriteMethod ()
                     );
                support.setName (p.getName ());
                support.setDisplayName (p.getDisplayName ());
                support.setShortDescription (p.getShortDescription ());
                
                for (Enumeration e = p.attributeNames(); e.hasMoreElements();) {
                    String aname = (String)e.nextElement();
                    support.setValue(aname, p.getValue(aname));
                }
                
                prop = support;
            } else {
                PropertyDescriptor p = propertyDescriptor [i];
                TreePropertySupport support = new TreePropertySupport
                    (bean,
                     p.getPropertyType (),
                     p.getReadMethod (),
                     p.getWriteMethod ()
                     );
                support.setName (p.getName ());
                support.setDisplayName (p.getDisplayName ());
                support.setShortDescription (p.getShortDescription ());
                support.setPropertyEditorClass (p.getPropertyEditorClass ());

                for (Enumeration e = p.attributeNames(); e.hasMoreElements();) {
                    String aname = (String)e.nextElement();
                    support.setValue (aname, p.getValue (aname));
                }
                
                prop = support;
            }
            // Propagate helpID's.
            Object help = propertyDescriptor[i].getValue ("helpID"); // NOI18N
            if (help != null && (help instanceof String)) {
                prop.setValue ("helpID", help); // NOI18N
            }
            // Add to right category.
            if (propertyDescriptor[i].isHidden ()) {
                // hidden property
                hidden.add (prop);
            } else {
                if (propertyDescriptor[i].isExpert ()) {
                    expert.add (prop);
                } else {
                    property.add (prop);
                }
            }
        }// for

        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("        ::computeProperties: property = " + property); // NOI18N
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("        ::computeProperties: expert   = " + expert); // NOI18N
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("        ::computeProperties: hidden   = " + hidden); // NOI18N

        Node.Property[][] properties = new Node.Property[3][];

        properties[0] = new Node.Property [property.size()];
        property.toArray (properties[0]);

        properties[1] = new Node.Property [expert.size()];
        expert.toArray (properties[1]);

        properties[2] = new Node.Property [hidden.size()];
        hidden.toArray (properties[2]);

        return properties;
    }

    
    //
    // TreePropertySupport
    //

    /**
     *
     */
    public static class TreePropertySupport extends PropertySupport.Reflection {

        //
        // init
        //

        /** */
        public TreePropertySupport (TreeObject instance, Class valueType, Method getter, Method setter) {
            super (instance, valueType, getter, setter);
        }

        //
        // PropertySupport.Reflection
        //

        /**
         */
        public boolean canWrite () {
            return ( super.canWrite()
                     && ( ((TreeObject)instance).isReadOnly() != true )
                   );
        }


        /**
         */
        public void setValue (Object value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            try {
                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n\nBeanUtil::TreePropertySupport::setValue: value = " + value); // NOI18N

                boolean toSet = true;

                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("        ::                   ::setValue: instance = " + instance); // NOI18N
                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("        ::                   ::setValue: instance.class = " + instance.getClass()); // NOI18N

                if ( instance instanceof TreeAttribute ) {
                    TreeAttribute attr = (TreeAttribute) instance;

                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\nBeanUtil::TreePropertySupport::setValue: value = " + value); // NOI18N
                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("        ::                   ::setValue: getName() = " + getName()); // NOI18N
                    
                    if ( "name".equals (getName()) ) { // NOI18N
                     
                        String name = (String) value;
                        TreeElement element = attr.getOwnerElement();
                        TreeAttribute oldAttribute = (TreeAttribute) element.getAttribute (name);
                        if ( oldAttribute != null ) {
                            toSet = GuiUtil.confirmAction
                                (Util.THIS.getString ("MSG_replace_attribute", name));
                        }                        
                    }
                }

                if ( toSet ) {
                    super.setValue (value);

                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("        ::TreePropertySupport::setValue: OK"); // NOI18N
                }

            } catch (InvocationTargetException exc) {
                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("        ::TreePropertySupport::setValue: InvocationTargetException", new RuntimeException());//, exc); // NOI18N

                Throwable targetExc = exc.getTargetException();
                if ( targetExc instanceof TreeException ) {
                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("        ::TreePropertySupport::setValue: targetException", targetExc); // NOI18N

                    GuiUtil.notifyTreeException ((TreeException)targetExc);
                } else {
                    throw exc;
                }
            }
        }


    } // end: class TreePropertySupport

}
... 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.