001 package edu.nrao.sss.webapp.faces.component; 002 003 import javax.faces.component.UIComponentBase; 004 import javax.faces.context.FacesContext; 005 import javax.faces.context.ResponseWriter; 006 import javax.faces.el.ValueBinding; 007 008 import java.io.IOException; 009 010 /** 011 * This component renders an anchor tag with a name attribute, as opposed 012 * to the outputLink component that renders an anchor tag with an href 013 * attribute. 014 */ 015 public class AnchorUIComponent extends UIComponentBase 016 { 017 private String name = null; 018 019 public void encodeBegin(FacesContext context) throws IOException 020 { 021 String name = getName(); 022 023 ResponseWriter writer = context.getResponseWriter(); 024 025 writer.startElement("a", this); 026 writer.writeAttribute("name", name, null); 027 writer.endElement("a"); 028 } 029 030 /** 031 * This method is only important if you're developing your own component 032 * family. If not, you can return whatever. 033 */ 034 public String getFamily() 035 { 036 return "NRAO"; 037 } 038 039 public String getName() 040 { 041 if (this.name != null) 042 return this.name; 043 044 ValueBinding vb = getValueBinding("name"); 045 if (vb != null) 046 { 047 Object val = vb.getValue(getFacesContext()); 048 049 if (val instanceof String) 050 return (String)val; 051 } 052 053 //else 054 return null; 055 } 056 057 public void setName(String n) 058 { 059 this.name = n; 060 } 061 062 public Object saveState(FacesContext context) 063 { 064 Object[] state = new Object[2]; 065 state[0] = super.saveState(context); 066 state[1] = this.name; 067 068 return state; 069 } 070 071 public void restoreState(FacesContext context, Object state) 072 { 073 Object[] mystate = (Object[])state; 074 super.restoreState(context, mystate[0]); 075 076 setName((String)mystate[1]); 077 } 078 }