1 package fr.improve.struts.taglib.layout.field; 2 3 import javax.servlet.jsp.JspException ; 4 5 import org.apache.struts.taglib.html.BaseHandlerTag; 6 import org.apache.struts.taglib.html.TextTag; 7 8 import fr.improve.struts.taglib.layout.event.StaticCodeIncludeLayoutEvent; 9 import fr.improve.struts.taglib.layout.util.LayoutUtils; 10 import fr.improve.struts.taglib.layout.util.TagUtils; 11 12 16 public class SuggestFieldTag extends AbstractFieldTag { 17 18 static class StrutsSuggestFieldTag extends TextTag { 19 20 public void prepareTextEvents(StringBuffer in_buffer) { 21 super.prepareTextEvents(in_buffer); 22 in_buffer.append(" autocomplete=\"off\""); 23 } 24 25 } 26 27 private TextTag textFieldTag = new StrutsSuggestFieldTag(); 28 29 public static final String LOADED = "fr.improve.struts.taglib.layout.field.SuggestFieldTag.LOADED"; 30 31 public static final int DEFAULT_SUGGESTCOUNT = 10; 32 public static final String DEFAULT_SUGGESTENCODING = "ISO-8859-1"; 33 public static final int DEFAULT_MINWORDLENGTH = 1; 34 public static final int DEFAULT_TIMEOUT = 0; 35 protected String suggestAction; 36 protected int suggestCount; 37 protected String suggestEncoding; 38 protected int minWordLength; 39 protected int timeout; 40 protected boolean all = false; 41 42 protected String jspOnkeyup; 43 protected String jspOnkeydown; 44 protected String jspOnkeypress; 45 protected String jspOnFocus; 46 47 48 protected void initDynamicValues() { 49 super.initDynamicValues(); 50 51 jspOnkeyup = onkeyup; 52 jspOnkeydown = onkeydown; 53 jspOnkeypress = onkeypress; 54 jspOnFocus = onfocus; 55 56 String url = null; 57 58 try { 59 url = LayoutUtils.computeURL(pageContext, null, null, null, this.getSuggestAction(), null, null, null, false, "suggest"); 60 } 61 catch (JspException e) { 62 TagUtils.saveException(pageContext, e); 63 throw new RuntimeException (e.getMessage()); 64 } 65 66 if (suggestCount == 0) 67 suggestCount = DEFAULT_SUGGESTCOUNT; 68 69 if (suggestEncoding == null) 70 suggestEncoding = DEFAULT_SUGGESTENCODING; 71 72 if (minWordLength == 0) 73 minWordLength = DEFAULT_MINWORDLENGTH; 74 75 if (timeout == 0) 76 timeout = DEFAULT_TIMEOUT; 77 78 if (all) { 79 minWordLength = 0; 80 onfocus=(onfocus != null ? onfocus : "") + 81 ";return computeFocus('" + this.getStyleId() + 82 "', '" + url + "', " + suggestCount + ", '" + suggestEncoding + 83 "', " + minWordLength + ", " + timeout + ", '" + all + "' )"; 84 86 } 87 88 onkeyup=(onkeyup != null ? onkeyup : "") + 89 ";return computeKeyUp('" + this.getStyleId() + 90 "', getKey(event.keyCode, event.which), '" + 91 url + "', " + suggestCount + ", '" + suggestEncoding + 92 "', " + minWordLength + ", " + timeout + ", '" + all + "' )"; 93 94 onkeydown=(onkeydown != null ? onkeydown : "") + 95 ";return computeKeyDown('" + this.getStyleId() + 96 "', getKey(event.keyCode, event.which) , '" + 97 url + "' )"; 98 99 onkeypress=(onkeypress != null ? onkeypress : "") + 100 ";return computeKeyPress('" + this.getStyleId() + 101 "', getKey(event.keyCode, event.which) )"; 102 } 103 104 protected void copyProperties(BaseHandlerTag in_dest) throws JspException { 105 super.copyProperties(in_dest); 106 textFieldTag.setCols(getCols()); 107 textFieldTag.setMaxlength(getMaxlength()); 108 textFieldTag.setProperty(getProperty()); 109 textFieldTag.setRows(getRows()); 110 textFieldTag.setValue(getValue()); 111 textFieldTag.setAccept(getAccept()); 112 textFieldTag.setName(getName()); 113 } 114 115 protected boolean doBeforeValue() throws javax.servlet.jsp.JspException { 116 fieldTag = textFieldTag; 117 118 loadScript(); 119 120 return true; 121 } 122 123 protected void doAfterValue() throws JspException { 124 125 StringBuffer buffer = new StringBuffer (); 126 127 buffer.append("<div id=\"" + this.getStyleId() + "SuggestionList\" class=\"suggestionList\"></div>"); 129 130 buffer.append("<input type=\"hidden\" id=\"" + this.getStyleId() + "SuggestionList_selectedFieldText" + "\" value=\"0\">"); 132 buffer.append("<input type=\"hidden\" id=\"" + this.getStyleId() + "SuggestionList_selectedSuggestionIndex" + "\" value=\"-1\">"); 133 buffer.append("<input type=\"hidden\" id=\"" + this.getStyleId() + "SuggestionList_typedWord" + "\" value=\"\">"); 134 135 TagUtils.write(pageContext, new StaticCodeIncludeLayoutEvent(this, buffer.toString()).send().toString()); 137 } 138 139 142 protected void loadScript() throws JspException { 143 144 if (pageContext.getRequest().getAttribute(LOADED)==null) { 145 TagUtils.write(pageContext, "<script SRC=\""); 146 TagUtils.write(pageContext, LayoutUtils.getSkin(pageContext.getSession()).getConfigDirectory(pageContext.getRequest())); 147 TagUtils.write(pageContext, "/suggest.js\"></script>"); 148 pageContext.getRequest().setAttribute(LOADED, ""); 149 } 150 151 } 152 153 156 protected void reset() { 157 super.reset(); 158 onkeyup = jspOnkeyup; 159 onkeydown = jspOnkeydown; 160 onkeypress = jspOnkeypress; 161 onfocus = jspOnFocus; 162 } 163 164 public void release(){ 165 super.release(); 166 all = false; 167 } 168 169 172 public String getSuggestAction() { 173 return suggestAction; 174 } 175 176 179 public void setSuggestAction(String suggestAction) { 180 this.suggestAction = suggestAction; 181 } 182 183 186 public int getSuggestCount() { 187 return suggestCount; 188 } 189 192 public void setSuggestCount(int suggestCount) { 193 this.suggestCount = suggestCount; 194 } 195 198 public String getSuggestEncoding() { 199 return suggestEncoding; 200 } 201 204 public void setSuggestEncoding(String suggestEncoding) { 205 this.suggestEncoding = suggestEncoding; 206 } 207 210 public int getMinWordLength() { 211 return minWordLength; 212 } 213 216 public void setMinWordLength(int minWordLength) { 217 this.minWordLength = minWordLength; 218 } 219 222 public int getTimeout() { 223 return timeout; 224 } 225 228 public void setTimeout(int timeout) { 229 this.timeout = timeout; 230 } 231 234 public boolean isAll() { 235 return all; 236 } 237 240 public void setAll(boolean all) { 241 this.all = all; 242 } 243 } 244 | Popular Tags |