myfaces-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ole Ersoy <ole_er...@yahoo.com>
Subject Javascript Component
Date Wed, 25 Jan 2006 02:25:36 GMT
Hey Everybody,

I took the Stylesheet component and changed a few
things so that it works the same for Javascript.

Here's the code in case anyone else needs this:

/*
 * Copyright 2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the
"License");
 * you may not use this file except in compliance with
the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in
writing, software
 * distributed under the License is distributed on an
"AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied.
 * See the License for the specific language governing
permissions and
 * limitations under the License.
 */
package org.apache.myfaces.custom.javascript;

import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

/**
 * @author mwessendorf (latest modification by
$Author: matze $) 
 * @version $Revision: 1.3 $ $Date: 2004/10/13
11:50:58 $ 
 * $Log: Stylesheet.java,v $
 * Revision 1.3  2004/10/13 11:50:58  matze
 * renamed packages to org.apache
 *
 * Revision 1.2  2004/08/22 10:36:50  mwessendorf
 * typo
 *
 * Revision 1.1  2004/08/18 15:36:09  mwessendorf
 * added a new Stylesheet-Component
 * 
*/

public class Javascript extends UIOutput {

	public static final String COMPONENT_TYPE =
"org.apache.myfaces.Javascript";
	public static final String COMPONENT_FAMILY =
"javax.faces.Output";
	private static final String DEFAULT_RENDERER_TYPE =
"org.apache.myfaces.Javascript";
	
    private String _path = null;
    //TODO Get from
org.apache.myfaces.renderkit.html.HTML when available
    private static final String LINK_ELEMENT = "link";
    	
	
    //
------------------------------------------------------------
Constructors
    public Javascript() {

        setRendererType(DEFAULT_RENDERER_TYPE);

    }


    public String getFamily() {

        return COMPONENT_FAMILY;

    }


    public String getPath() {

		if (_path != null) return _path;
		ValueBinding vb = getValueBinding("path");
		return vb != null ?
(String)vb.getValue(getFacesContext()) : null;
    }

    public void setPath(String path) {
        this._path = path;
    }

    public void restoreState(FacesContext context,
Object state) {

        Object values[] = (Object[]) state;
        super.restoreState(context, values[0]);
        _path = (String) values[1];

    }

    public Object saveState(FacesContext context) {

        Object values[] = new Object[2];
        values[0] = super.saveState(context);
        values[1] = _path;
        return values;

    }
}


/*
 * Copyright 2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the
"License");
 * you may not use this file except in compliance with
the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in
writing, software
 * distributed under the License is distributed on an
"AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied.
 * See the License for the specific language governing
permissions and
 * limitations under the License.
 */
package org.apache.myfaces.custom.javascript;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.apache.myfaces.renderkit.html.HTML;
import org.apache.myfaces.renderkit.html.HtmlRenderer;

/**
 * @author mwessendorf (latest modification by
$Author: matze $) 
 * @version $Revision: 1.2 $ $Date: 2004/10/13
11:50:58 $ 
 * $Log: StylesheetRenderer.java,v $
 * Revision 1.2  2004/10/13 11:50:58  matze
 * renamed packages to org.apache
 *
 * Revision 1.1  2004/08/18 15:36:09  mwessendorf
 * added a new Stylesheet-Component
 * 
*/

public class JavascriptRenderer extends HtmlRenderer {
    //TODO Get from
org.apache.myfaces.renderkit.html.HTML when available
    private static final String LINK_ELEMENT = "link";
    
	 public void encodeEnd(FacesContext context,
UIComponent component)
        throws IOException {

        if ((context == null) || (component == null))
{
            throw new NullPointerException();
        }
        Javascript javascript = (Javascript)
component;

        ResponseWriter writer =
context.getResponseWriter();
        writer.startElement(LINK_ELEMENT, component);
       
writer.writeAttribute(HTML.SCRIPT_LANGUAGE_ATTR,
HTML.SCRIPT_LANGUAGE_JAVASCRIPT, null);
        writer.writeURIAttribute
            (HTML.SRC_ATTR,
            
context.getExternalContext().getRequestContextPath()+javascript.getPath(),
			 null);
        writer.endElement(LINK_ELEMENT);
    }
}

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE faces-config PUBLIC
 "-//Sun Microsystems, Inc.//DTD JavaServer Faces
Config 1.1//EN"
 "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config
xmlns="http://java.sun.com/JSF/Configuration">
	<component>
	
<component-type>org.apache.myfaces.Javascript</component-type>
	
<component-class>org.apache.myfaces.custom.javascript.Javascript</component-class>
	</component>
    <render-kit>
        <renderer>
        
<component-family>javax.faces.Output</component-family>
           
<renderer-type>org.apache.myfaces.Javascript</renderer-type>
           
<renderer-class>org.apache.myfaces.custom.javascript.JavascriptRenderer</renderer-class>
        </renderer>
    </render-kit>
</faces-config>






__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Mime
View raw message