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