001 package edu.nrao.sss.webapp.faces.component; 002 003 import javax.faces.component.UIInput; 004 import javax.faces.context.FacesContext; 005 import javax.faces.context.ResponseWriter; 006 import javax.faces.el.ValueBinding; 007 import edu.nrao.sss.util.EnumerationUtility; 008 import edu.nrao.sss.measure.ArcUnits; 009 import edu.nrao.sss.measure.Latitude; 010 011 import java.io.IOException; 012 import java.util.Map; 013 import java.util.EnumSet; 014 015 import org.apache.log4j.Logger; 016 017 public class LatitudeUIComponent extends UIInput 018 { 019 private static Logger log = Logger.getLogger(LatitudeUIComponent.class); 020 private Boolean enabled = null; 021 022 private static final EnumSet<ArcUnits> unitSet = EnumerationUtility.getSharedInstance().getSelectableSetFor(ArcUnits.class); 023 024 public void encodeBegin(FacesContext context) 025 throws IOException 026 { 027 Latitude d = (Latitude)getValue(); 028 String styleClass = (String)getAttributes().get("styleClass"); 029 String style = (String)getAttributes().get("style"); 030 031 ResponseWriter writer = context.getResponseWriter(); 032 033 String decId = getClientId(context) + "dec"; 034 String unitId = getClientId(context) + "units"; 035 036 //Label and input field for Latitude value. 037 writer.startElement("div", this); 038 writer.writeAttribute("class", styleClass, null); 039 writer.writeAttribute("style", style, null); 040 041 writer.startElement("input", this); 042 writer.writeAttribute("type", "text", null); 043 writer.writeAttribute("id", decId, null); 044 writer.writeAttribute("name", decId, null); 045 writer.writeAttribute("value", d.getValue(), null); 046 047 if (!isEnabled()) 048 writer.writeAttribute("disabled", "true", null); 049 050 writer.endElement("input"); 051 052 writer.endElement("div"); 053 054 //Label and select menu for arc units. 055 writer.startElement("div", this); 056 writer.writeAttribute("class", styleClass, null); 057 writer.writeAttribute("style", style, null); 058 059 writer.startElement("label", this); 060 writer.writeAttribute("for", unitId, null); 061 writer.write(" in "); 062 writer.endElement("label"); 063 064 writer.startElement("select", this); 065 writer.writeAttribute("name", unitId, null); 066 067 if (!isEnabled()) 068 writer.writeAttribute("disabled", "true", null); 069 070 071 ArcUnits arcUnits = d.getUnits(); 072 073 //Write out the option elements. 074 for (ArcUnits unit : unitSet) 075 { 076 writer.startElement("option", this); 077 if(unit.equals(arcUnits)) 078 { 079 log.debug("Found the selected value! " + arcUnits); 080 writer.writeAttribute("selected", "selected", null); 081 } 082 083 writer.writeAttribute("value", unit.name(), null); 084 085 writer.write(unit.toString()); 086 writer.endElement("option"); 087 } 088 089 writer.endElement("select"); 090 writer.endElement("div"); 091 } 092 093 /** 094 * Overridden to be empty. 095 */ 096 public void encodeEnd(FacesContext context) 097 throws IOException 098 { 099 100 } 101 102 /** 103 * Overridden to be empty. 104 */ 105 public void encodeChildren(FacesContext context) 106 throws IOException 107 { 108 109 } 110 111 public void decode(FacesContext context) 112 { 113 String decId = getClientId(context) + "dec"; 114 String unitId = getClientId(context) + "units"; 115 116 Map parms = context.getExternalContext().getRequestParameterMap(); 117 String dec = (String)parms.get(decId); 118 String unit = (String)parms.get(unitId); 119 120 if (dec == null || unit == null) 121 { 122 setSubmittedValue(null); 123 } 124 125 else 126 //The submitted value will be "$dec $units" 127 setSubmittedValue(dec + ArcUnits.valueOf(unit).getSymbol()); 128 } 129 130 public Boolean isEnabled() 131 { 132 if (this.enabled != null) 133 return this.enabled; 134 135 ValueBinding vb = getValueBinding("enabled"); 136 if (vb != null) 137 { 138 Object val = vb.getValue(getFacesContext()); 139 140 if (val instanceof Boolean && val != null) 141 return (Boolean)val; 142 } 143 144 //else 145 return Boolean.TRUE; 146 } 147 148 public void setEnabled(Boolean e) 149 { 150 this.enabled = e; 151 } 152 153 public Object saveState(FacesContext context) 154 { 155 Object[] state = new Object[2]; 156 state[0] = super.saveState(context); 157 state[1] = this.enabled; 158 159 return state; 160 } 161 162 public void restoreState(FacesContext context, Object state) 163 { 164 Object[] myState = (Object[])state; 165 super.restoreState(context, myState[0]); 166 setEnabled((Boolean)myState[1]); 167 } 168 }