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.TimeDurationUIComponent;
016    
017    public class TimeDurationTag extends UIComponentTag
018    {
019            private static final Logger log = Logger.getLogger(TimeDurationTag.class);
020            
021            public String getComponentType() { return "edu.nrao.sss.TimeDuration"; }
022            public String getRendererType() { return null; }
023            
024            private String value = null, 
025                    valueChangeListener = null,
026                    enabled = null,
027                    showUnits = null,
028                    units = null,
029                    style = null,
030                    styleClass = null;
031            
032            @SuppressWarnings("unchecked")
033            protected void setProperties(UIComponent component)
034            {
035                    super.setProperties(component);
036                    
037                    FacesContext context = FacesContext.getCurrentInstance();
038                    Application app = context.getApplication();
039                    
040                    log.debug("Value is: " + value);
041                    if (value != null) 
042                    { 
043                            if (isValueReference(value))
044                            {
045                                    ValueBinding vb = app.createValueBinding(value);
046    
047                                    component.setValueBinding("value", vb);
048                            }
049                            
050                            else
051                            {
052                                    throw new ReferenceSyntaxException(value + " is not a valid EL expression");
053                            }
054                    }
055                    
056                    if (valueChangeListener != null) 
057                    { 
058                            Class[] params = new Class[1];
059                            params[0] = ValueChangeEvent.class;
060    
061                            MethodBinding mb = app.createMethodBinding(valueChangeListener, params);
062    
063                            ((UIInput)component).setValueChangeListener(mb);
064                    }
065                    
066                    if (enabled != null) 
067                    { 
068                            if (isValueReference(enabled))
069                            {
070                                    ValueBinding vb = app.createValueBinding(enabled);
071    
072                                    component.setValueBinding("enabled", vb);
073                            }
074                            
075                            else
076                            {
077                                    ((TimeDurationUIComponent)component).setEnabled(isTrue(enabled));
078                            }
079                    }
080                    
081                    if (showUnits != null) 
082                    { 
083                            if (isValueReference(showUnits))
084                            {
085                                    ValueBinding vb = app.createValueBinding(showUnits);
086    
087                                    component.setValueBinding("showUnits", vb);
088                            }
089                            
090                            else
091                            {
092                                    ((TimeDurationUIComponent)component).setShowUnits(isTrue(showUnits));
093                            }
094                    }
095                    
096                    if (units != null) 
097                    { 
098                            if (isValueReference(units))
099                            {
100                                    ValueBinding vb = app.createValueBinding(units);
101    
102                                    component.setValueBinding("units", vb);
103                            }
104                            
105                            else
106                            {
107                                    throw new ReferenceSyntaxException(value + " is not a valid EL expression");
108                            }
109                    }
110                    
111                    if (style != null) 
112                    { 
113                            if (isValueReference(style))
114                            {
115                                    ValueBinding vb = app.createValueBinding(style);
116    
117                                    component.setValueBinding("style", vb);
118                            }
119                            
120                            else
121                            {
122                                    component.getAttributes().put("style", style);
123                            }
124                    }
125                    
126                    if (styleClass != null) 
127                    { 
128                            if (isValueReference(styleClass))
129                            {
130                                    ValueBinding vb = app.createValueBinding(styleClass);
131    
132                                    component.setValueBinding("styleClass", vb);
133                            }
134                            
135                            else
136                            {
137                                    component.getAttributes().put("styleClass", styleClass);
138                            }
139                    }
140            }
141            
142            public void release()
143            {
144                    super.release();
145                    value = null;
146                    enabled = null;
147                    showUnits = null;
148                    units = null;
149                    style = null;
150                    styleClass = null;
151            }
152            
153            public void setValue(String v)
154            {
155                    this.value = v;
156            }
157            
158            public void setValueChangeListener(String l)
159            {
160                    this.valueChangeListener = l;
161            }
162            
163            public void setEnabled(String e)
164            {
165                    this.enabled = e;
166            }
167            
168            public void setShowUnits(String su)
169            {
170                    this.showUnits = su;
171            }
172            
173            public void setUnits(String u)
174            {
175                    this.units = u;
176            }
177            
178            public void setStyle(String s)
179            {
180                    this.style = s;
181            }
182            
183            public void setStyleClass(String sc)
184            {
185                    this.styleClass = sc;
186            }
187            
188            public boolean isTrue(String v)
189            {
190                    return ("true".equalsIgnoreCase(v) || "1".equals(v));
191            }
192    }