001    package edu.nrao.sss.webapp.faces.tag;
002    
003    import javax.faces.webapp.UIComponentTag;
004    import javax.faces.component.UIComponent;
005    import javax.faces.component.UIInput;
006    import javax.faces.context.FacesContext;
007    import javax.faces.application.Application;
008    import javax.faces.el.ValueBinding;
009    import javax.faces.el.MethodBinding;
010    import javax.faces.el.ReferenceSyntaxException;
011    import javax.faces.event.ValueChangeEvent;
012    
013    import org.apache.log4j.Logger;
014    
015    import edu.nrao.sss.webapp.faces.component.LatitudeUIComponent;
016    
017    public class LatitudeTag extends UIComponentTag
018    {
019            private static final Logger log = Logger.getLogger(LatitudeTag.class);
020            
021            public String getComponentType() { return "edu.nrao.sss.Latitude"; }
022            public String getRendererType() { return null; }
023            
024            private String value = null, 
025                    valueChangeListener = null,
026                    enabled = null,
027                    style = null,
028                    styleClass = null;
029            
030            @SuppressWarnings("unchecked")
031            protected void setProperties(UIComponent component)
032            {
033                    super.setProperties(component);
034                    
035                    FacesContext context = FacesContext.getCurrentInstance();
036                    Application app = context.getApplication();
037                    
038                    log.debug("Value is: " + value);
039                    if (value != null) 
040                    { 
041                            if (isValueReference(value))
042                            {
043                                    ValueBinding vb = app.createValueBinding(value);
044    
045                                    component.setValueBinding("value", vb);
046                            }
047                            
048                            else
049                            {
050                                    throw new ReferenceSyntaxException(value + " is not a valid EL expression");
051                            }
052                    }
053                    
054                    if (valueChangeListener != null) 
055                    { 
056                            Class[] params = new Class[1];
057                            params[0] = ValueChangeEvent.class;
058    
059                            MethodBinding mb = app.createMethodBinding(valueChangeListener, params);
060    
061                            ((UIInput)component).setValueChangeListener(mb);
062                    }
063                    
064                    if (enabled != null) 
065                    { 
066                            if (isValueReference(enabled))
067                            {
068                                    ValueBinding vb = app.createValueBinding(enabled);
069    
070                                    component.setValueBinding("enabled", vb);
071                            }
072                            
073                            else
074                            {
075                                    ((LatitudeUIComponent)component).setEnabled(isTrue(enabled));
076                            }
077                    }
078                    
079                    if (style != null) 
080                    { 
081                            if (isValueReference(style))
082                            {
083                                    ValueBinding vb = app.createValueBinding(style);
084    
085                                    component.setValueBinding("style", vb);
086                            }
087                            
088                            else
089                            {
090                                    component.getAttributes().put("style", style);
091                            }
092                    }
093                    
094                    if (styleClass != null) 
095                    { 
096                            if (isValueReference(styleClass))
097                            {
098                                    ValueBinding vb = app.createValueBinding(styleClass);
099    
100                                    component.setValueBinding("styleClass", vb);
101                            }
102                            
103                            else
104                            {
105                                    component.getAttributes().put("styleClass", styleClass);
106                            }
107                    }
108            }
109            
110            public void release()
111            {
112                    super.release();
113                    value = null;
114                    enabled = null;
115                    style = null;
116                    styleClass = null;
117            }
118            
119            public void setValue(String v)
120            {
121                    this.value = v;
122            }
123            
124            public void setValueChangeListener(String l)
125            {
126                    this.valueChangeListener = l;
127            }
128            
129            public void setEnabled(String e)
130            {
131                    this.enabled = e;
132            }
133            
134            public void setStyle(String s)
135            {
136                    this.style = s;
137            }
138            
139            public void setStyleClass(String sc)
140            {
141                    this.styleClass = sc;
142            }
143            
144            public boolean isTrue(String v)
145            {
146                    return ("true".equalsIgnoreCase(v) || "1".equals(v));
147            }
148    }