基于 Sun JSF 的 PanelHorizontalLayout,tablelayout , rowlayout , cellformat的实现

BaseRender

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

public abstract class BaseRender extends Renderer {
	abstract boolean needSpecialAttribute();
	abstract void writeSpecialAttributes(UIComponent component,
			ResponseWriter writer)throws IOException;
	protected void writeCommonAttributes(UIComponent component,
			ResponseWriter writer, String clientId) throws IOException {
		
		String width = (String) component.getAttributes().get(HtmlElement.WIDTH);
		String height = (String) component.getAttributes().get(HtmlElement.HEIGHT);
		String styleClass = (String) component.getAttributes()
				.get(HtmlElement.STYLECLASS);
		String inlineStyle = (String) component.getAttributes().get(HtmlElement.INLINESTYLE);
		writer.writeAttribute(HtmlElement.ID,clientId, null);
		if (width != null) {
			writer.writeAttribute(HtmlElement.WIDTH, width, null);
		}
		if (height != null) {
			writer.writeAttribute(HtmlElement.HEIGHT, height, null);
		}

		if (inlineStyle != null) {
			writer.writeAttribute(HtmlElement.STYLE, inlineStyle, null);
		}
		if (styleClass != null) {
			writer.writeAttribute(HtmlElement.CLASS, styleClass, null);
		}
        if (needSpecialAttribute()) {
        	writeSpecialAttributes(component,writer);
        }
	}
}





TableLayoutRenderClass
import java.io.IOException;
import java.util.List;

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

public class TableLayoutRenderer extends BaseRender {
	public void encodeBegin(FacesContext context, UIComponent component)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		String clientId = component.getClientId(context);
		writer.startElement("table", component);
		writeCommonAttributes(component, writer, clientId);
		//encodeRow(context,component, writer, clientId);
		//encodeCommand(component, writer, clientId);
	}
   public void encodeEnd(FacesContext context, UIComponent component)throws IOException {
	   try {
		context.getResponseWriter().endElement("table");
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   }
    public void encodeChildren(FacesContext context, UIComponent component)
    throws IOException
   {
 	   for(UIComponent child : (List<UIComponent>)component.getChildren()) {

	   try {
			if (child instanceof HtmlRowLayout)   { 
			   //super.encodeChildren(context, child);
				child.encodeBegin(context);
				child.encodeChildren(context);
				child.encodeEnd(context);
			
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   }
   }

	@Override
	boolean needSpecialAttribute() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	void writeSpecialAttributes(UIComponent component, ResponseWriter writer)
			throws IOException {
		// TODO Auto-generated method stub
		
	}

}



HtmlTableLayOut ComponentClass


package com.test.jsftest;

import javax.faces.component.UIPanel;

public class HtmlTableLayout extends UIPanel {
	public HtmlTableLayout() {
		setRendererType("com.test.jsftest.HtmlTableLayout");
		}

}






RowLayout Render Class
import java.io.IOException;
import java.util.List;

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

public class RowLayoutRenderer extends BaseRender {
	public void encodeBegin(FacesContext context, UIComponent component)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();		
		String clientId = component.getClientId(context);
		writer.startElement("tr", component);
		writeCommonAttributes(component, writer, clientId);
		//encodeRow(context,component, writer, clientId);
		
	}
    public void encodeChildren(FacesContext context, UIComponent component)
    throws IOException
   {
 	   try{
 		   for(UIComponent child : (List<UIComponent>)component.getChildren()) {
 			    if (child instanceof HtmlCellFormat)
 			    {
 			    	//super.encodeChildren(context, child);
 					child.encodeBegin(context);
 					child.encodeChildren(context);
 					child.encodeEnd(context);
 			    }
// 			    else
// 			    {
// 			      ResponseWriter rw = context.getResponseWriter();
// 			      rw.startElement("td", null);
// 			      super.encodeChildren(context, child);
// 			      rw.endElement("td");
// 			    }
 			   }

 		   }catch(Exception e) {
 			   
 		   }
   }
	   public void encodeEnd(FacesContext context, UIComponent component) {
		   try {
			context.getResponseWriter().endElement("tr");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   }

	@Override
	boolean needSpecialAttribute() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	void writeSpecialAttributes(UIComponent component, ResponseWriter writer)
			throws IOException {
		// TODO Auto-generated method stub
		
	}
 
	   
	   
	   

  
}






RowLayout  Component Class

package com.test.jsftest;

import javax.faces.component.UIPanel;

public class HtmlRowLayout extends UIPanel {
	public HtmlRowLayout() {
		setRendererType("com.test.jsftest.HtmlRowLayout");
		}
}



CellFormat Render Class




import java.io.IOException;
import java.util.List;

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

public class CellFormatRenderer extends BaseRender {
	public void encodeBegin(FacesContext context, UIComponent component)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		String clientId = component.getClientId(context);
		writer.startElement("td", component);
		writeCommonAttributes(component, writer, clientId);
		//encodeCell(context,component, writer, clientId);
		//encodeCommand(component, writer, clientId);
	}

	   public void encodeEnd(FacesContext context, UIComponent component) {
		   try {
			context.getResponseWriter().endElement("td");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   }
	    public void encodeChildren(FacesContext context, UIComponent component)
	    throws IOException
	   {
	 	   for(UIComponent child : (List<UIComponent>)component.getChildren()) {
			   if (child.isRendered()) {
				   try {
					//super.encodeChildren(context, child);
						child.encodeBegin(context);
						child.encodeChildren(context);
						child.encodeEnd(context);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			   }
		   }
	   }   


	@Override
	boolean needSpecialAttribute() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	void writeSpecialAttributes(UIComponent component, ResponseWriter writer)
			throws IOException {
		// TODO Auto-generated method stub
		
	}

}


CellFormatComponent Class

package com.test.jsftest;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.component.UIPanel;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.FacesEvent;
import javax.faces.event.FacesListener;
import javax.faces.render.Renderer;

public class HtmlCellFormat extends UIPanel {

	public HtmlCellFormat() {
		setRendererType("com.test.jsftest.HtmlCellFormat");
		}

}




PanelHorizontalLayout Class



import java.io.IOException;
import java.util.List;

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

public class PanelHorizontalLayoutRender extends BaseRender {
	
    public boolean getRendersChildren()
    {
        return true;
    }
	public void encodeBegin(FacesContext context, UIComponent component)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		String clientId = component.getClientId(context);
		
		writer.startElement("table", component);
		writeCommonAttributes(component, writer, clientId);
		writer.startElement("tr", component);
		//encodeAttribute(context, component, writer, clientId);
		
		encodeChild(context, writer, component);
		
		//context.getResponseWriter().endElement("tr");
		//context.getResponseWriter().endElement("table");
		// encodeCommand(component, writer, clientId);
	}

	public void encodeEnd(FacesContext context, UIComponent component)
			throws IOException {
		try {
			
			context.getResponseWriter().endElement("tr");
			context.getResponseWriter().endElement("table");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void encodeChildren(FacesContext context, UIComponent component)
			throws IOException {

	}


	
	private void encodeChild(FacesContext context, ResponseWriter writer, UIComponent component) throws IOException {
		for (UIComponent child : (List<UIComponent>) component.getChildren()) {
            
			
			if (child instanceof HtmlCellFormat) {
				child.encodeBegin(context);
				child.encodeChildren(context);
				child.encodeEnd(context);
			}else {
				writer.startElement("td", component);
				
					// super.encodeChildren(context, child);
					child.encodeBegin(context);
					child.encodeChildren(context);
					child.encodeEnd(context);
		        writer.endElement("td");
			}
			
			}
	}
	@Override
	boolean needSpecialAttribute() {
		// TODO Auto-generated method stub
		return true;
	}
	@Override
	void writeSpecialAttributes(UIComponent component, ResponseWriter writer) throws IOException {
		// TODO Auto-generated method stub
		String vAlign = (String) component.getAttributes().get("vAlign");
		String hAlign = (String) component.getAttributes().get("hAlign");
		if (vAlign != null) {
			writer.writeAttribute("vAlign", vAlign, null);
		}
		if (hAlign != null) {
			writer.writeAttribute("align", hAlign, null);
		}
	}
}





PanelGroupLayout
import java.io.IOException;
import java.util.List;

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

public class PanelGroupLayoutRender extends BaseRender {
	private static final String HORIZONTAL_LAY_OUT = "Horizontal";
	private static final String VERTICAL_LAY_OUT = "Vertical";
	
    public boolean getRendersChildren()
    {
        return true;
    }
	public void encodeBegin(FacesContext context, UIComponent component)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		String clientId = component.getClientId(context);
		
		writer.startElement("table", component);
		writeCommonAttributes(component,writer,clientId);
		
		encodeChild(context, writer, component);
		

	}

	public void encodeEnd(FacesContext context, UIComponent component)
			throws IOException {
		try {
			
			
			context.getResponseWriter().endElement("table");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void encodeChildren(FacesContext context, UIComponent component)
			throws IOException {

		
	}


	
	private void encodeChild(FacesContext context, ResponseWriter writer, UIComponent component) throws IOException {
		
		String layoutType = (String) component.getAttributes().get("layout");
		if (layoutType == null || HORIZONTAL_LAY_OUT.equals(layoutType))  {
			writer.startElement("tr", component);
			for (UIComponent child : (List<UIComponent>) component.getChildren()) {
	            
				
				if (child instanceof HtmlCellFormat) {
					child.encodeBegin(context);
					child.encodeChildren(context);
					child.encodeEnd(context);
				}else {
					writer.startElement("td", component);
					
						// super.encodeChildren(context, child);
						child.encodeBegin(context);
						child.encodeChildren(context);
						child.encodeEnd(context);
			        writer.endElement("td");
				}
				
				}
			writer.endElement("tr");
		}else if (VERTICAL_LAY_OUT.equals(layoutType)) {
			
			
			for (UIComponent child : (List<UIComponent>) component.getChildren()) {
				writer.startElement("tr",component);
				
				if (child instanceof HtmlCellFormat) {
					child.encodeBegin(context);
					child.encodeChildren(context);
					child.encodeEnd(context);
				}else {
					
					  writer.startElement("td",component);
						// super.encodeChildren(context, child);
						child.encodeBegin(context);
						child.encodeChildren(context);
						child.encodeEnd(context);
						writer.endElement("td");
			       
				}
				writer.endElement("tr");
				
				}
			
		}
		

	}
	@Override
	boolean needSpecialAttribute() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	void writeSpecialAttributes(UIComponent component, ResponseWriter writer) {
		// TODO Auto-generated method stub
		
	}
}







Faces-config.xml






<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>
	<application>
		<!-- Enables Facelets -->
		<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>  
	</application>
	
   <component>
		<component-type>
		com.test.jsftest.TextWithCmd
		</component-type>
		<component-class>
		com.test.jsftest.UITextWithCmd
		</component-class>
	</component>
	 <component>
		<component-type>
		com.test.jsftest.HtmlCellFormat
		</component-type>
		<component-class>
		com.test.jsftest.HtmlCellFormat
		</component-class>
	</component>
	<component>
		<component-type>
		com.test.jsftest.HtmlRowLayout
		</component-type>
		<component-class>
		com.test.jsftest.HtmlRowLayout
		</component-class>
	</component>

	<component>
		<component-type>
		com.test.jsftest.HtmlTableLayout
		</component-type>
		<component-class>
		com.test.jsftest.HtmlTableLayout
		</component-class>
	</component>
		<render-kit>
		<renderer>
		<component-family>
		javax.faces.Panel
		</component-family>
		<renderer-type>
		com.test.jsftest.HtmlCellFormat
		</renderer-type>
		<renderer-class>
		com.test.jsftest.CellFormatRenderer
		</renderer-class>
		</renderer>
	
		<renderer>
		<component-family>
		javax.faces.Panel
		</component-family>
		<renderer-type>
		com.test.jsftest.HtmlRowLayout
		</renderer-type>
		<renderer-class>
		com.test.jsftest.RowLayoutRenderer
		</renderer-class>
		</renderer>
		
		<renderer>
		<component-family>
		javax.faces.Panel
		</component-family>
		<renderer-type>
		com.test.jsftest.HtmlTableLayout
		</renderer-type>
		<renderer-class>
		com.test.jsftest.TableLayoutRenderer
		</renderer-class>
		</renderer>	
		
		<renderer>
		<component-family>
		javax.faces.Input
		</component-family>
		<renderer-type>
		com.test.jsftest.TextCmd
		</renderer-type>
		<renderer-class>
		com.test.jsftest.TextCmdRenderer
		</renderer-class>
		</renderer>
		</render-kit>
</faces-config>




taglib.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE facelet-taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib xmlns="http://java.sun.com/JSF/Facelet">
  
  <namespace>http://caterpillar.onlyfun.net/custimizedtag</namespace>
	<tag>
		<tag-name>textcmd</tag-name>
		<component>
			<component-type>
				com.test.jsftest.TextWithCmd
			</component-type>
			<renderer-type>
				com.test.jsftest.TextCmd
			</renderer-type>
		</component>
	</tag>
 
	<tag>
		<tag-name>cellFormat</tag-name>
		<component>
			<component-type>
				com.test.jsftest.HtmlCellFormat
			</component-type>
			<renderer-type>
				com.test.jsftest.HtmlCellFormat
			</renderer-type>
		</component>
	</tag>
  
  	<tag>
		<tag-name>RowLayout</tag-name>
		<component>
			<component-type>
				com.test.jsftest.HtmlRowLayout
			</component-type>
			<renderer-type>
				com.test.jsftest.HtmlRowLayout
			</renderer-type>
		</component>
	</tag>
	
	
		<tag>
		<tag-name>TableLayout</tag-name>
		<component>
			<component-type>
				com.test.jsftest.HtmlTableLayout
			</component-type>
			<renderer-type>
				com.test.jsftest.HtmlTableLayout
			</renderer-type>
		</component>
	</tag>
</facelet-taglib>





你可能感兴趣的:(java,xml,JSF,sun)