001    package net.sf.tacos.javascript.jquery.components;
002    
003    import java.util.HashMap;
004    import java.util.List;
005    import java.util.Map;
006    import org.apache.tapestry.IMarkupWriter;
007    import org.apache.tapestry.IRequestCycle;
008    import org.apache.tapestry.PageRenderSupport;
009    import org.apache.tapestry.Tapestry;
010    import org.apache.tapestry.TapestryUtils;
011    import org.apache.tapestry.dojo.form.Autocompleter;
012    import org.apache.tapestry.dojo.form.IAutocompleteModel;
013    import org.apache.tapestry.engine.DirectServiceParameter;
014    import org.apache.tapestry.engine.ILink;
015    import org.apache.tapestry.json.JSONObject;
016    import org.apache.tapestry.valid.ValidatorException;
017    
018    public abstract class JqueryAutocompleter extends Autocompleter {     
019        
020      protected void renderFormWidget(IMarkupWriter writer, IRequestCycle cycle)
021        {
022            IAutocompleteModel model = getModel();
023            if (model == null)
024                throw Tapestry.createRequiredParameterException(this, "model");
025            
026            Object value = getValue();
027            Object key = value != null && !"".equals(value.toString()) ? model.getPrimaryKey(value) : null;
028            
029            renderDelegatePrefix(writer, cycle);
030            
031            writer.beginEmpty("input");
032            writer.attribute("name", getName());
033            writer.attribute("autocomplete", "off"); // turn off native html autocomplete
034            
035            if (isDisabled())
036                writer.attribute("disabled", "disabled");
037            
038            renderIdAttribute(writer, cycle);
039            
040            renderDelegateAttributes(writer, cycle);
041            
042            getValidatableFieldSupport().renderContributions(this, writer, cycle);
043            
044            // Apply informal attributes.
045            renderInformalParameters(writer, cycle);
046            
047            //writer.print(" ");
048            
049            if (isLocal()) 
050            {
051                List list = model.getValues("");
052                for (int i=0; i<list.size(); i++) 
053                {
054                    /*Object optionKey = model.getPrimaryKey(list.get(i));
055    
056                    writer.begin("option");
057                    writer.attribute("value", getDataSqueezer().squeeze(optionKey));
058    
059                    if (optionKey!=null && optionKey.equals(key))
060                        writer.attribute("selected", "selected");
061                    
062                    writer.print(model.getLabelFor(list.get(i)));
063                    writer.end();*/
064                }
065            }
066            
067            //writer.end();
068            renderDelegateSuffix(writer, cycle);
069            
070            Map parms = new HashMap();
071            parms.put("id", getClientId());
072            
073            JSONObject json = new JSONObject();
074            if (!isLocal())
075            {
076                ILink link = getDirectService().getLink(true, new DirectServiceParameter(this));
077                parms.put("url", link.getURL());
078            }
079            
080            json.put("mode", isLocal() ? "local" : "remote");
081            json.put("widgetId", getName());
082            json.put("name", getName());
083            json.put("searchDelay", getSearchDelay());
084            json.put("fadeTime", getFadeTime());
085            json.put("maxListLength", getMaxListLength());
086            json.put("forceValidOption", isForceValidOption());
087            json.put("disabled", isDisabled());
088            json.put("autoComplete", getAutoCompleteField());
089            
090            json.put("value", key != null ? getDataSqueezer().squeeze(key) : "");
091            json.put("label", value != null ? model.getLabelFor(value) : "");
092                    
093            parms.put("props", json.toString());
094            parms.put("form", getForm().getName());
095            parms.put("widget", this);
096            
097            PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cycle, this);
098            getScript().execute(this, cycle, prs, parms);
099        }    
100      
101        protected void rewindFormWidget(IMarkupWriter writer, IRequestCycle cycle)
102        {
103            String value = cycle.getParameter(getName());
104            
105            Object object = null;
106            
107            try
108            {
109                if (value != null && value.length() > 0) {
110                    //object = getModel().getValue(getDataSqueezer().unsqueeze(value));
111                    List list = getModel().getValues(value);
112                    if (list!=null && list.size()>0) {
113                        object = list.get(0);
114                    }
115                }
116                
117                getValidatableFieldSupport().validate(this, writer, cycle, object);
118                
119                setValue(object);
120            }
121            catch (ValidatorException e)
122            {
123                getForm().getDelegate().record(e);
124            }
125        }  
126      
127        public void trigger(IRequestCycle cycle)
128        {
129            setFilter(cycle.getParameter("q"));
130        }  
131        
132        public boolean isJson()
133        {
134            return true;
135        }    
136        
137    }