/*
 * The contents of this file are subject to the terms 
 * of the Common Development and Distribution License 
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at 
 * https://glassfish.dev.java.net/public/CDDLv1.0.html or
 * glassfish/bootstrap/legal/CDDLv1.0.txt.
 * See the License for the specific language governing 
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL 
 * Header Notice in each file and include the License file 
 * at glassfish/bootstrap/legal/CDDLv1.0.txt.  
 * If applicable, add the following below the CDDL Header, 
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information: 
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 */

package com.sun.enterprise.tools.guiframework.view.descriptors;

import com.iplanet.jato.RequestContext;
import com.iplanet.jato.RequestManager;
import com.iplanet.jato.view.BasicDisplayField;
import com.iplanet.jato.view.ContainerView;
import com.iplanet.jato.view.DisplayField;
import com.iplanet.jato.view.View;

import com.sun.enterprise.tools.guiframework.exception.FrameworkException;

import com.sun.web.ui.model.CCActionTableModelInterface;


/**
 *  This Descriptor is needed so that children of a CCActionTable can be
 *  created from the CCActionTable's model rather than directly.
 */
public class CCActionTableChildDescriptor extends DisplayFieldDescriptor {

    /**
     *	Constructor
     */
    public CCActionTableChildDescriptor(String name) {
	super(name);
    }


    /**
     *	This method returns the model associated with this View (it is actually
     *	obtained from the parent CCActionTableDescriptor).
     *
     *	@return CCActionTableModelInterface
     */
    protected CCActionTableModelInterface getModel() {
	// Find the CCActionTable
	ViewDescriptor viewDesc = getParent();
	while (viewDesc != null) {
	    if (viewDesc instanceof CCActionTableDescriptor) {
		break;
	    }
	    viewDesc = viewDesc.getParent();
	}
	if (viewDesc == null) {
	    throw new FrameworkException(this.getClass().getName()+
		" is not contained within a CCActionTableDescriptor!",
		this, getView(RequestManager.getRequestContext()));
	}
	return ((CCActionTableDescriptor)viewDesc).getModel();
    }


    /**
     *	This is a factory method.
     *
     *	@param	ctx		The RequestContext
     *	@param	container	The container for the newly created 
     *	@param	name		The Name of the View to be created.
     */
    public View getInstance(RequestContext ctx, ContainerView container, String name) {
	CCActionTableModelInterface model = getModel();
        if (model != null && model.isChildSupported(name)) {
	    // Create the View
            View child = model.createChild(container, name);
            
            // possibly add some default values in the model from the view desc.
            if (child instanceof DisplayField) {
                DisplayField field = (DisplayField)child;

		// Set the ModelFieldBinding or BoundName
		setBoundName(field);

		// Set the model action for this field
		String value = (String)getParameter("actionValue");
		if (value != null) {
		    model.setActionValue(name, value);
		}
		
		// Set the model value for this field
		value = (String)getParameter("value");
		if (value != null) {
		    model.setValue(name, value);
		}
            }
            return child;
        }
        return null;
    }
}
