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.Longitude; 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 LongitudeUIComponent extends UIInput 018 { 019 private static Logger log = Logger.getLogger(LongitudeUIComponent.class); 020 private Boolean enabled = true; 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 Longitude ra = (Longitude)getValue(); 028 String styleClass = (String)getAttributes().get("styleClass"); 029 String style = (String)getAttributes().get("style"); 030 031 ResponseWriter writer = context.getResponseWriter(); 032 033 String raId = getClientId(context) + "ra"; 034 String unitId = getClientId(context) + "units"; 035 036 //Label and input field for ra 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", raId, null); 044 writer.writeAttribute("name", raId, null); 045 writer.writeAttribute("value", ra.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 ArcUnits arcUnits = ra.getUnits(); 071 072 //Write out the option elements. 073 for (ArcUnits unit : unitSet) 074 { 075 writer.startElement("option", this); 076 if(unit.equals(arcUnits)) 077 { 078 log.debug("Found the selected value! " + arcUnits); 079 writer.writeAttribute("selected", "selected", null); 080 } 081 082 writer.writeAttribute("value", unit.name(), null); 083 084 writer.write(unit.toString()); 085 writer.endElement("option"); 086 } 087 088 writer.endElement("select"); 089 writer.endElement("div"); 090 } 091 092 /** 093 * Overridden to be empty. 094 */ 095 public void encodeEnd(FacesContext context) 096 throws IOException 097 { 098 099 } 100 101 /** 102 * Overridden to be empty. 103 */ 104 public void encodeChildren(FacesContext context) 105 throws IOException 106 { 107 108 } 109 110 public void decode(FacesContext context) 111 { 112 String raId = getClientId(context) + "ra"; 113 String unitId = getClientId(context) + "units"; 114 115 Map parms = context.getExternalContext().getRequestParameterMap(); 116 String ra = (String)parms.get(raId); 117 String unit = (String)parms.get(unitId); 118 119 if (ra == null || unit == null) 120 setSubmittedValue(null); 121 122 else 123 //The submitted value will be "$ra $units" 124 setSubmittedValue(ra + ArcUnits.valueOf(unit).getSymbol()); 125 } 126 127 public Boolean isEnabled() 128 { 129 if (this.enabled != null) 130 return this.enabled; 131 132 ValueBinding vb = getValueBinding("enabled"); 133 if (vb != null) 134 { 135 Object val = vb.getValue(getFacesContext()); 136 if (val != null && val instanceof Boolean) 137 return (Boolean)val; 138 } 139 140 //else 141 return Boolean.TRUE; 142 } 143 144 public void setEnabled(Boolean e) 145 { 146 this.enabled = e; 147 } 148 149 public Object saveState(FacesContext context) 150 { 151 Object[] state = new Object[2]; 152 state[0] = super.saveState(context); 153 state[1] = this.enabled; 154 155 return state; 156 } 157 158 public void restoreState(FacesContext context, Object state) 159 { 160 Object[] myState = (Object[])state; 161 super.restoreState(context, myState[0]); 162 setEnabled((Boolean)myState[1]); 163 } 164 }