001    package edu.nrao.sss.webapp.faces.tag;
002    
003    import javax.faces.application.Application;
004    import javax.faces.webapp.UIComponentTag;
005    import javax.faces.component.UIComponent;
006    import javax.faces.component.UISelectItems;
007    import javax.faces.el.ValueBinding;
008    import javax.faces.context.FacesContext;
009    
010    import java.util.Collection;
011    import javax.faces.model.SelectItem;
012    
013    import edu.nrao.sss.util.EnumerationUtility;
014    import edu.nrao.sss.webapp.SelectItemCollectionFactory;
015    
016    import org.apache.log4j.Logger;
017    
018    public class EnumMenuTag extends UIComponentTag 
019    {
020            private static final Logger log = Logger.getLogger(EnumMenuTag.class);
021            private String value = null;
022    
023            // Associate the renderer and component type. Same type as recorded in
024            // faces-config.xml.
025            public String getComponentType() { return "edu.nrao.sss.SelectEnumItems"; }
026            public String getRendererType() { return null; }
027    
028    
029            @SuppressWarnings("unchecked")
030            protected void setProperties(UIComponent component)
031            {
032                    super.setProperties(component);
033    
034                    // set value
035                    if (value != null) 
036                    { 
037                            if (isValueReference(value))
038                            {
039                                    FacesContext context = FacesContext.getCurrentInstance();
040                                    Application app = context.getApplication();
041                                    ValueBinding vb = app.createValueBinding(value);
042    
043                                    //XXX This get value is not be being executed at the correct point
044                                    //in the JSF lifecycle!
045                                    value = (String)vb.getValue(context);
046                            }
047                            
048                            Class enumClass = null;
049                            try
050                            {
051                                    enumClass = Class.forName(value);
052                                    Collection<SelectItem> list = new SelectItemCollectionFactory().createFrom(
053                                            EnumerationUtility.getSharedInstance().getSelectableSetFor(enumClass)
054                                    );
055                                    
056                                    //From this class name we must get a list of select items to set
057                                    //as the value of our component.
058                                    ((UISelectItems)component).setValue(list);
059                            }
060                            catch (Exception e)
061                            {
062                                    log.error("Attempted to create a SelectItem collection from " + value, e);
063                                    ((UISelectItems)component).setValue(null);
064                            }
065                    }                         
066            }
067    
068            /**
069              * clear up any resources we've used.
070              */
071            public void release()
072            {
073                    super.release();
074                    value = null;
075            }
076    
077            /**
078              * Bean method for our value attribute.
079              */
080            public void setValue(String value)
081            {
082                    this.value = value;
083            } 
084    }