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