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-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.xml.tree.nodes;

import java.awt.Component;
import javax.swing.JComponent;
import java.beans.IntrospectionException;

import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;

import org.netbeans.tax.*;

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

/**
 * @author  Libor Kramolis
 * @version 0.1
 */
public final class NodeFactory {
    // toDo:
    // + reset CREATE_ constants to use new set value


    private static String CREATE_ELEMENT_TAG_NAME              = Util.THIS.getString ("TEXT_new_element_name");
    private static String CREATE_ATTRIBUTE_NAME                = Util.THIS.getString ("TEXT_new_attribute_name");
    private static String CREATE_ATTRIBUTE_VALUE               = Util.THIS.getString ("TEXT_new_attribute_value");
    private static String CREATE_TEXT_DATA                     = Util.THIS.getString ("TEXT_new_text_data_value");
    private static String CREATE_CHAR_REF_NAME                 = Util.THIS.getString ("TEXT_new_char_ref_name");
    private static String CREATE_CDATA_SECTION_DATA            = Util.THIS.getString ("TEXT_new_CDATA_value");
    private static String CREATE_PROCESSING_INSTRUCTION_TARGET = Util.THIS.getString ("TEXT_new_PI_target");
    private static String CREATE_PROCESSING_INSTRUCTION_DATA   = Util.THIS.getString ("TEXT_new_PI_data");
    private static String CREATE_COMMENT_DATA                  = Util.THIS.getString ("TEXT_new_comment_text");
    private static String CREATE_DOCUMENT_TYPE_ROOT_ELEMENT    = Util.THIS.getString ("TEXT_new_root");
    private static String CREATE_GENERAL_ENTITY_REFERENCE_NAME = Util.THIS.getString ("TEXT_new_entity");


    //
    // util
    //

    public static AbstractObjectNode createObjectNode (TreeObject treeObject) {
        AbstractObjectNode node = null;

	try {
            if ( treeObject instanceof TreeElement ) {
		node = new ElementNode ((TreeElement)treeObject);

	    } else if ( treeObject instanceof TreeAttribute ) {
		node = new AttributeNode ((TreeAttribute)treeObject);

	    } else if ( treeObject instanceof TreeText ) {
		node = new TextNode ((TreeText)treeObject);

	    } else if ( treeObject instanceof TreeCDATASection ) {
		node = new CDATASectionNode ((TreeCDATASection)treeObject);

	    } else if ( treeObject instanceof TreeComment ) {
		node = new CommentNode ((TreeComment)treeObject);

	    } else if ( treeObject instanceof TreeProcessingInstruction ) {
		node = new ProcessingInstructionNode ((TreeProcessingInstruction)treeObject);

	    } else if ( treeObject instanceof TreeCharacterReference ) {
		node = new CharacterReferenceNode ((TreeCharacterReference)treeObject);

	    } else if ( treeObject instanceof TreeGeneralEntityReference ) {
		node = new GeneralEntityReferenceNode ((TreeGeneralEntityReference)treeObject);

	    } else if ( treeObject instanceof TreeParameterEntityReference ) {
		node = new ParameterEntityReferenceNode ((TreeParameterEntityReference)treeObject);

	    } else if ( treeObject instanceof TreeDocumentType ) {
		node = new DocumentTypeNode ((TreeDocumentType)treeObject);

	    } else if ( treeObject instanceof TreeElementDecl ) {
		node = new ElementDeclNode ((TreeElementDecl)treeObject);

  	    } else if ( treeObject instanceof TreeAttlistDecl ) {
  		node = new AttlistDeclNode ((TreeAttlistDecl)treeObject);

  	    } else if ( treeObject instanceof TreeAttlistDeclAttributeDef ) {
  		node = new AttlistDeclAttributeDefNode ((TreeAttlistDeclAttributeDef)treeObject);

	    } else if ( treeObject instanceof TreeEntityDecl ) {
		node = new EntityDeclNode ((TreeEntityDecl)treeObject);

	    } else if ( treeObject instanceof TreeNotationDecl ) {
		node = new NotationDeclNode ((TreeNotationDecl)treeObject);

	    } else if ( treeObject instanceof TreeConditionalSection ) {
		node = new ConditionalSectionNode ((TreeConditionalSection)treeObject);

	    } else {
		if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\tMISSION IMPOSSIBLE -- createFilterNode : what is it? : " + treeObject, new RuntimeException()); // NOI18N
	    }
	} catch (IntrospectionException ex) {
	    //let it be
	}
        
        return node;        
    }

    // a shortcut to pass additional info to  method
    private static transient boolean newMode = false;
    
    /**
     * Create customizer dialog forsing it to use autoselection modeon
     * text fields if set. It communicates it to actul 
     * method via a static field newMode.
     */
    public static synchronized TreeObject createTreeObjectDialog (Class type, AbstractParentNode parentNode, boolean newMode) {
        try {
            NodeFactory.newMode = newMode;
            return createTreeObjectDialog(type, parentNode);
        } finally {
            NodeFactory.newMode = false;
        }
    }
    

    /**
     */
    public static synchronized TreeObject createTreeObjectDialog (Class type, AbstractParentNode parentNode) {
        TreeObject object = null;

        if ( type == TreeAttribute.class ) {
            object = createAttributeDialog();
        } else if ( type == TreeCDATASection.class ) {
            object = createCDATASectionDialog();
        } else if ( type == TreeCharacterReference.class ) {
            object = createCharacterReferenceDialog();
        } else if ( type == TreeComment.class ) {
            object = createCommentDialog();
        } else if ( type == TreeElement.class ) {
            object = createElementDialog();
        } else if ( type == TreeGeneralEntityReference.class ) {
            object = createGeneralEntityReferenceDialog();
        } else if ( type == TreeProcessingInstruction.class ) {
            object = createProcessingInstructionDialog();
        } else if ( type == TreeText.class ) {
            object = createTextDialog();
        } else if ( type == TreeDocumentType.class ) {
            TreeElement element = ((DocumentNode)parentNode).getDocument().getDocumentElement();
            String elementName = null;
            if ( element != null ) {
                elementName = element.getQName();
            }
            object = createDocumentTypeDialog (elementName);
        }        

        return object;
    }


    /**
     */
    public static TreeElement createElementDialog () {
        try {
            TreeElement elem = new TreeElement (CREATE_ELEMENT_TAG_NAME);
            ElementNode elemNode = new ElementNode (elem);
	    return (TreeElement)customNode (elem, elemNode.getCustomizer(), Util.THIS.getString("TITLE_new_element"));
	} catch (TreeException exc) {
	    GuiUtil.notifyTreeException (exc);
            return null;
        } catch (java.beans.IntrospectionException ex) {
            return null;
        }
    }

    /**
     */
    public static TreeAttribute createAttributeDialog () {
        try {
            TreeAttribute attr = new TreeAttribute (CREATE_ATTRIBUTE_NAME, CREATE_ATTRIBUTE_VALUE);
            AttributeNode attrNode = new AttributeNode (attr);
	    return (TreeAttribute)customNode (attr, attrNode.getCustomizer(), Util.THIS.getString ("TITLE_new_attribute"));
	} catch (TreeException exc) {
	    GuiUtil.notifyTreeException (exc);
            return null;
        } catch (java.beans.IntrospectionException ex) {
            return null;
        }
    }

    /**
     */
    public static TreeText createTextDialog () {
        try {
	    TreeText text = new TreeText (CREATE_TEXT_DATA);
            TextNode textNode = new TextNode (text);
	    return (TreeText)customNode (text, textNode.getCustomizer(), Util.THIS.getString ("TITLE_new_text"));
	} catch (TreeException exc) {
	    GuiUtil.notifyTreeException (exc);
            return null;
        } catch (java.beans.IntrospectionException ex) {
            return null;
        }
    }

    /**
     */
    public static TreeCharacterReference createCharacterReferenceDialog () {
        try {
	    TreeCharacterReference charRef = new TreeCharacterReference (CREATE_CHAR_REF_NAME);
            CharacterReferenceNode charRefNode = new CharacterReferenceNode (charRef);
	    return (TreeCharacterReference)customNode (charRef, charRefNode.getCustomizer(), Util.THIS.getString ("TITLE_new_char_ref"));
	} catch (TreeException exc) {
	    GuiUtil.notifyTreeException (exc);
            return null;
        } catch (java.beans.IntrospectionException ex) {
            return null;
        }
    }

    /**
     */
    public static TreeCDATASection createCDATASectionDialog () {
        try {
            TreeCDATASection cdata = new TreeCDATASection (CREATE_CDATA_SECTION_DATA);
            CDATASectionNode cdataNode = new CDATASectionNode (cdata);
	    return (TreeCDATASection)customNode (cdata, cdataNode.getCustomizer(), Util.THIS.getString ("TITLE_new_CDATA"));
	} catch (TreeException exc) {
	    GuiUtil.notifyTreeException (exc);
            return null;
        } catch (java.beans.IntrospectionException ex) {
            return null;
        }
    }

    /**
     */
    public static TreeProcessingInstruction createProcessingInstructionDialog () {
        try {
            TreeProcessingInstruction pi = new TreeProcessingInstruction (CREATE_PROCESSING_INSTRUCTION_TARGET, CREATE_PROCESSING_INSTRUCTION_DATA);
            ProcessingInstructionNode piNode = new ProcessingInstructionNode (pi);
	    return (TreeProcessingInstruction)customNode (pi, piNode.getCustomizer(), Util.THIS.getString ("TITLE_new_PI"));
	} catch (TreeException exc) {
	    GuiUtil.notifyTreeException (exc);
            return null;
        } catch (java.beans.IntrospectionException ex) {
            return null;
        }
    }

    /**
     */
    public static TreeComment createCommentDialog () {
        try {
            TreeComment comm = new TreeComment (CREATE_COMMENT_DATA);
            CommentNode commNode = new CommentNode (comm);
	    return (TreeComment)customNode (comm, commNode.getCustomizer(), Util.THIS.getString ("TITLE_new_comment"));
	} catch (TreeException exc) {
	    GuiUtil.notifyTreeException (exc);
            return null;
        } catch (java.beans.IntrospectionException ex) {
            return null;
        }
    }

    /**
     */
    public static TreeDocumentType createDocumentTypeDialog () {
	return createDocumentTypeDialog (null);
    }

    /**
     */
    public static TreeDocumentType createDocumentTypeDialog (String name) {
        try {
            String rootName;
	    if ( name == null ) {
		rootName = CREATE_DOCUMENT_TYPE_ROOT_ELEMENT;
	    } else {
		rootName = name;
	    }
            TreeDocumentType dtd = new TreeDocumentType (rootName);
            DocumentTypeNode dtdNode = new DocumentTypeNode (dtd);
	    return (TreeDocumentType)customNode (dtd, dtdNode.getCustomizer(), Util.THIS.getString ("TITLE_new_document_type"));
	} catch (TreeException exc) {
	    GuiUtil.notifyTreeException (exc);
            return null;
        } catch (java.beans.IntrospectionException ex) {
            return null;
        }

    }

    /**
     */
    public static TreeGeneralEntityReference createGeneralEntityReferenceDialog () {
        try {
            TreeGeneralEntityReference entRef = new TreeGeneralEntityReference (CREATE_GENERAL_ENTITY_REFERENCE_NAME);
            GeneralEntityReferenceNode refNode = new GeneralEntityReferenceNode (entRef);
	    return (TreeGeneralEntityReference)customNode (entRef, refNode.getCustomizer(), Util.THIS.getString ("TITLE_new_entity_ref"));
	} catch (TreeException exc) {
	    GuiUtil.notifyTreeException (exc);
            return null;
        } catch (java.beans.IntrospectionException ex) {
            return null;
        }

    }

    
    //
    // private
    //

    /**
     * Show dialog and let user to customize it.
     * Add hint as "xml-edit-mode" client property to JComponets ("new" or "edit").
     */
    private static TreeNode customNode (TreeNode treeNode, Component panel, String title) {
        TreeDocumentFragment documentFragment = null;
        if ( treeNode instanceof TreeElement ) { // issue #23274
            try {
                documentFragment = new TreeDocumentFragment();
                documentFragment.appendChild ((TreeElement) treeNode);
            } catch (TreeException exc) {
                // ignore it
            }
        }

	DialogDescriptor dd = new DialogDescriptor
	    (panel, title, true, DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION,
	     DialogDescriptor.BOTTOM_ALIGN, null, null);

        //??? Warning same code is also in tax.beans.Lib
        if (panel instanceof JComponent) {
            // set hints to the customizer component
            if (newMode) {
                ((JComponent)panel).putClientProperty("xml-edit-mode", "new");  // NOI18N
            } else {
                ((JComponent)panel).putClientProperty("xml-edit-mode", "edit"); // NOI18N
            }
        }
        
	DialogDisplayer.getDefault ().createDialog (dd).show();

	if (dd.getValue() != DialogDescriptor.OK_OPTION) {
	    return null;
        }

        if ( documentFragment != null ) {
            try {
                documentFragment.removeChild ((TreeChild) treeNode);
            } catch (TreeException exc) {
                // ignore it
            }
        }

	return treeNode;
    }

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