1 17 package org.alfresco.web.ui.repo.component; 18 19 import java.io.IOException ; 20 import java.util.Map ; 21 import java.util.ResourceBundle ; 22 23 import javax.faces.component.NamingContainer; 24 import javax.faces.component.UICommand; 25 import javax.faces.component.UIComponent; 26 import javax.faces.context.FacesContext; 27 import javax.faces.context.ResponseWriter; 28 import javax.faces.event.AbortProcessingException; 29 import javax.faces.event.ActionEvent; 30 import javax.faces.event.FacesEvent; 31 32 import org.alfresco.web.app.Application; 33 import org.alfresco.web.bean.SearchContext; 34 import org.alfresco.web.ui.common.Utils; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 40 public class UISimpleSearch extends UICommand 41 { 42 45 48 public UISimpleSearch() 49 { 50 setRendererType(null); 53 } 54 55 58 public String getFamily() 59 { 60 return "org.alfresco.faces.SimpleSearch"; 61 } 62 63 66 public void restoreState(FacesContext context, Object state) 67 { 68 Object values[] = (Object [])state; 69 super.restoreState(context, values[0]); 71 this.search = (SearchContext)values[1]; 72 } 73 74 77 public Object saveState(FacesContext context) 78 { 79 Object values[] = new Object [2]; 80 values[0] = super.saveState(context); 82 values[1] = this.search; 83 return (values); 84 } 85 86 89 public void decode(FacesContext context) 90 { 91 Map requestMap = context.getExternalContext().getRequestParameterMap(); 92 String fieldId = Utils.getActionHiddenFieldName(context, this); 93 String value = (String )requestMap.get(fieldId); 94 if (value != null) 96 { 97 if (value.equals(this.getClientId(context))) 98 { 99 String searchText = (String )requestMap.get(getClientId(context)); 100 101 if (searchText.length() != 0) 102 { 103 if (logger.isDebugEnabled()) 104 logger.debug("Search text submitted: " + searchText); 105 int option = -1; 106 String optionFieldName = getClientId(context) + NamingContainer.SEPARATOR_CHAR + OPTION_PARAM; 107 String optionStr = (String )requestMap.get(optionFieldName); 108 if (optionStr.length() != 0) 109 { 110 option = Integer.parseInt(optionStr); 111 } 112 if (logger.isDebugEnabled()) 113 logger.debug("Search option submitted: " + option); 114 115 SearchEvent event = new SearchEvent(this, searchText, option); 117 this.queueEvent(event); 118 } 119 } 120 else if (value.equals(ADVSEARCH_PARAM)) 121 { 122 AdvancedSearchEvent event = new AdvancedSearchEvent(this, "advSearch"); 125 this.queueEvent(event); 126 } 127 } 128 } 129 130 133 public void broadcast(FacesEvent event) throws AbortProcessingException 134 { 135 FacesContext fc = getFacesContext(); 136 if (event instanceof SearchEvent) 137 { 138 SearchEvent searchEvent = (SearchEvent)event; 140 141 SearchContext context = new SearchContext(); 143 context.setText(searchEvent.SearchText); 144 context.setMode(searchEvent.SearchMode); 145 context.setForceAndTerms(Application.getClientConfig(fc).getForceAndTerms()); 146 this.search = context; 147 148 super.broadcast(event); 149 } 150 else if (event instanceof AdvancedSearchEvent) 151 { 152 AdvancedSearchEvent searchEvent = (AdvancedSearchEvent)event; 154 fc.getApplication().getNavigationHandler().handleNavigation(fc, null, searchEvent.Outcome); 155 156 } 158 } 159 160 163 public void encodeBegin(FacesContext context) throws IOException 164 { 165 if (isRendered() == false) 166 { 167 return; 168 } 169 170 ResponseWriter out = context.getResponseWriter(); 171 172 ResourceBundle bundle = (ResourceBundle )Application.getBundle(context); 173 174 out.write("<script>"); 176 out.write("function _noenter(event) {" + 177 "if (event && event.keyCode == 13) {" + 178 " _searchSubmit();return false; }" + 179 "else {" + 180 " return true; } }"); 181 out.write("function _searchSubmit() {"); 182 out.write(Utils.generateFormSubmit(context, this, Utils.getActionHiddenFieldName(context, this), getClientId(context))); 183 out.write("}"); 184 out.write("</script>"); 185 186 out.write("<table cellspacing=4 cellpadding=0>"); 188 out.write("<tr><td style='padding-top:2px'>"); 189 190 String searchImage = Utils.buildImageTag(context, "/images/icons/search_icon.gif", 15, 15, 191 bundle.getString(MSG_GO), "_searchSubmit();"); 192 193 out.write(Utils.buildImageTag(context, "/images/icons/search_controls.gif", 27, 13, 194 bundle.getString(MSG_OPTIONS), "javascript:_toggleMenu(event, '_alfsearch');")); 195 196 out.write("<br><div id='_alfsearch' style='position:absolute;display:none'>"); 198 out.write("<table border=0 bgcolor='#eeeeee' style='border-top:thin solid #FFFFFF;border-left:thin solid #FFFFFF;border-right:thin solid #444444;border-bottom:thin solid #444444;' cellspacing=4 cellpadding=0>"); 199 200 String optionFieldName = getClientId(context) + NamingContainer.SEPARATOR_CHAR + OPTION_PARAM; 202 String radioOption = "<tr><td class='userInputForm'><input type='radio' name='" + optionFieldName + "'"; 203 out.write(radioOption); 204 out.write(" VALUE='0'"); 205 int searchMode = getSearchMode(); 206 if (searchMode == 0) out.write(" CHECKED"); 207 out.write("><nobr>" + bundle.getString(MSG_ALL_ITEMS) + "</nobr></td></tr>"); 208 out.write(radioOption); 209 out.write(" VALUE='1'"); 210 if (searchMode == 1) out.write(" CHECKED"); 211 out.write("><nobr>" + bundle.getString(MSG_FILE_NAMES_CONTENTS) + "</nobr></td></tr>"); 212 out.write(radioOption); 213 out.write(" VALUE='2'"); 214 if (searchMode == 2) out.write(" CHECKED"); 215 out.write("><nobr>" + bundle.getString(MSG_FILE_NAMES_ONLY) + "</nobr></td></tr>"); 216 out.write(radioOption); 217 out.write(" VALUE='3'"); 218 if (searchMode == 3) out.write(" CHECKED"); 219 out.write("><nobr>" + bundle.getString(MSG_SPACE_NAMES_ONLY) + "</nobr></td></tr>"); 220 221 out.write("<tr><td><table width=100%><tr><td>"); 223 out.write("<a class='small' HREF='#' onclick=\""); 225 out.write(Utils.generateFormSubmit(context, this, Utils.getActionHiddenFieldName(context, this), ADVSEARCH_PARAM)); 226 out.write("\">"); 227 out.write(bundle.getString(MSG_ADVANCED_SEARCH)); 228 out.write("</a>"); 229 out.write("</td><td align=right>"); 230 out.write(searchImage); 231 out.write("</td></tr></table></td></tr>"); 232 out.write("</table></div>"); 233 234 out.write("</td><td>"); 236 out.write("<input name='"); 237 out.write(getClientId(context)); 238 out.write("' onkeypress=\"return _noenter(event)\""); 240 out.write(" type='text' maxlength='1024' style='width:130px;padding-top:3px;font-size:10px' value=\""); 241 out.write(Utils.replace(getLastSearch(), "\"", """)); 243 out.write("\">"); 244 245 out.write("</td><td>"); 247 out.write(searchImage); 248 249 out.write("</td></tr></table>"); 251 } 252 253 256 public SearchContext getSearchContext() 257 { 258 return this.search; 259 } 260 261 264 public String getLastSearch() 265 { 266 if (search != null) 267 { 268 return this.search.getText(); 269 } 270 else 271 { 272 return ""; 273 } 274 } 275 276 279 public int getSearchMode() 280 { 281 if (search != null) 282 { 283 return this.search.getMode(); 284 } 285 else 286 { 287 return SearchContext.SEARCH_ALL; 288 } 289 } 290 291 292 295 private static Log logger = LogFactory.getLog(UISimpleSearch.class); 296 297 298 private static final String MSG_ADVANCED_SEARCH = "advanced_search"; 299 private static final String MSG_OPTIONS = "options"; 300 private static final String MSG_GO = "go"; 301 private static final String MSG_SPACE_NAMES_ONLY = "space_names"; 302 private static final String MSG_FILE_NAMES_ONLY = "file_names"; 303 private static final String MSG_FILE_NAMES_CONTENTS = "file_names_contents"; 304 private static final String MSG_ALL_ITEMS = "all_items"; 305 306 private static final String OPTION_PARAM = "_option"; 307 private static final String ADVSEARCH_PARAM = "_advsearch"; 308 309 310 private SearchContext search = null; 311 312 313 316 319 public static class SearchEvent extends ActionEvent 320 { 321 private static final long serialVersionUID = 3918135612344774322L; 322 323 public SearchEvent(UIComponent component, String text, int mode) 324 { 325 super(component); 326 SearchText = text; 327 SearchMode = mode; 328 } 329 330 public String SearchText; 331 public int SearchMode; 332 } 333 334 public static class AdvancedSearchEvent extends ActionEvent 335 { 336 public AdvancedSearchEvent(UIComponent component, String outcome) 337 { 338 super(component); 339 Outcome = outcome; 340 } 341 342 public String Outcome; 343 } 344 } 345 | Popular Tags |