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 Rich Unger. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.xml.tree.nodes.propertysheet;

import org.openide.nodes.*;
import org.openide.loaders.DataObject;
import org.netbeans.modules.xml.tree.completion.GrammarManager;
import org.netbeans.modules.xml.tree.completion.TreeHintContext;
import org.netbeans.modules.xml.tree.nodes.ElementNode;
import org.netbeans.modules.xml.tree.nodes.ElementCustomizer;
import org.netbeans.modules.xml.api.model.GrammarQuery;
import org.netbeans.modules.xml.api.model.GrammarResult;
import org.netbeans.modules.xml.api.model.HintContext;
import org.netbeans.modules.xml.core.XMLDataObject;
import org.openide.util.NbBundle;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.beans.PropertyEditor;

import org.netbeans.tax.dom.Wrapper;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

/**
 * Represents a list of xml attributes which can be treated
 * like bean properties in the property sheet.  They
 * have customizers, etc.
 *
 * @author Rich Unger
 */
public class AttributeSet extends Node.PropertySet
{
    public AttributeSet(ElementNode node) {
        Element domElement = Wrapper.wrap(node.getElement());
        List attributes = node.getAllowableAttributes();
        GrammarQuery grammarQuery = GrammarManager.getGrammar(
                (XMLDataObject)node.getCookie(DataObject.class));

        m_properties = new Attribute[attributes.size()];

        Iterator iter = attributes.iterator();
        int i=0;
        while( iter.hasNext() )
        {
            Attr domAttr = (Attr)iter.next();
            m_properties[i] = new Attribute(domAttr, domElement, grammarQuery);
            
            Object obj = node.getCustomizer();
            if( obj instanceof ElementCustomizer )
            {
                m_properties[i].setCustomizer( (ElementCustomizer)obj );
            }

            i++;
        }
    }
    
    public String getName()
    { 
        return NbBundle.getBundle(AttributeSet.class).getString("CTL_Attributes");
    } 

    public Node.Property[] getProperties()
    {
        return m_properties;
    }

    protected static class Attribute extends Node.Property
    {
        public Attribute(Attr domAttr, Element domElement, GrammarQuery grammarQuery)
        {
            super(String.class);
            setName(domAttr.getName());
            m_peer = domAttr;
            m_parent = domElement;
            m_grammarQuery = grammarQuery;
        }

        public void setCustomizer( ElementCustomizer c )
        {
            m_customizer = c;
        }

        public boolean canRead() { return true; }
        public boolean canWrite() { return true; }

        public Object getValue() 
            throws IllegalAccessException, InvocationTargetException
        {
            if( m_customizer != null )
            {
                Object retval = m_customizer.getAttribute( getName() );
                if( retval == null )
                {
                    // for displaying "" in the property sheet instead of "null"
                    return "";
                }
                else
                {
                    return retval;
                }
            }
            else
            {
                return null;
            }
        }

        public void setValue( Object obj ) 
            throws IllegalAccessException, 
                   IllegalArgumentException, 
                   InvocationTargetException
        {
            if( m_customizer != null )
            {
                m_customizer.setAttribute( getName(), (String)obj );
            }
        }

        public boolean supportsDefaultValue()
        {
            HintContext ctx = TreeHintContext.getContext( m_peer, m_parent, "" );
            GrammarResult defaultResult = m_grammarQuery.queryDefault(ctx);

            return defaultResult != null;
        }

        public void restoreDefaultValue() throws IllegalAccessException, 
                                                  InvocationTargetException
        {
            HintContext ctx = TreeHintContext.getContext( m_peer, m_parent, "" );
            GrammarResult defaultResult = m_grammarQuery.queryDefault(ctx);

            if( defaultResult != null )
            { 
                String sDefault = ((Text)defaultResult).getData();
                setValue( sDefault );
            } 
        }

        public PropertyEditor getPropertyEditor()
        {
            HintContext ctx = TreeHintContext.getContext( m_peer, m_parent, "" );
            Enumeration en = m_grammarQuery.queryValues(ctx);

            if( en.hasMoreElements() )
            {
                // formulate the possible values as a String[]
                ArrayList list = new ArrayList();
                while( en.hasMoreElements() )
                { 
                    list.add(en.nextElement());
                } 

                String[] values = new String[list.size()];
                Iterator iter = list.iterator();

                for( int i=0; i < list.size(); i++ )
                { 
                    values[i] = ((Text)iter.next()).getData();
                } 

                // we have fixed enumerated values, so use a combo box
                return new EnumAttrPropEditor(values);
            }
            else
            {
                // use the standard String editor
                return super.getPropertyEditor();
            }
        }

        protected ElementCustomizer m_customizer;
        protected Element m_parent;
        protected Attr m_peer;
        protected GrammarQuery m_grammarQuery;
    }

    protected Attribute[] m_properties;
    
}
... 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.