| 1 33 34 package com.icesoft.faces.component.selectinputtext; 35 36 import com.icesoft.faces.component.CSS_DEFAULT; 37 import com.icesoft.faces.component.ext.HtmlInputText; 38 import com.icesoft.faces.component.ext.KeyEvent; 39 import com.icesoft.faces.component.ext.renderkit.FormRenderer; 40 import com.icesoft.faces.component.ext.taglib.Util; 41 import com.icesoft.faces.context.effects.JavascriptContext; 42 43 import javax.faces.component.NamingContainer; 44 import javax.faces.component.UIComponent; 45 import javax.faces.context.FacesContext; 46 import javax.faces.el.ValueBinding; 47 import javax.faces.event.AbortProcessingException; 48 import javax.faces.event.ActionEvent; 49 import javax.faces.event.FacesEvent; 50 import javax.faces.event.ValueChangeEvent; 51 import javax.faces.model.SelectItem; 52 import java.io.IOException ; 53 import java.util.*; 54 import java.text.DateFormat ; 55 56 57 67 public class SelectInputText extends HtmlInputText implements NamingContainer { 68 public static final String COMPONENT_TYPE = 69 "com.icesoft.faces.SelectInputText"; 70 71 73 private String styleClass; 75 76 77 81 private String listVar = null; 82 83 86 private Integer rows; 87 private final int DEFAULT_MAX_MATCHS = 10; 88 89 92 private String width; 93 private final String DEFAULT_WIDTH = "150"; 94 95 98 private SelectItem selectedItem = null; 99 100 103 List itemList; 104 105 private String options; 106 107 111 Map itemMap = new HashMap(); 112 113 private int index = -1; 114 115 public SelectInputText() { 116 super(); 117 JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS, 118 FacesContext.getCurrentInstance()); 119 } 120 121 125 public void encodeBegin(FacesContext context) throws IOException { 126 super.encodeBegin(context); 127 } 128 129 133 public void decode(FacesContext facesContext) { 134 super.decode(facesContext); 135 setSelectedItem(facesContext); 136 if (Util.isEventSource(facesContext,this)) { 137 queueEventIfEnterKeyPressed(facesContext); 138 } 140 141 } 142 143 148 private void setSelectedItem(FacesContext facesContext) { 149 Map requestMap = 150 facesContext.getExternalContext().getRequestParameterMap(); 151 String clientId = getClientId(facesContext); 152 String value = (String ) requestMap.get(clientId); 153 setSelectedItem(value); 154 } 155 156 157 163 private boolean hadFocus(FacesContext facesContext) { 164 Object focusId = facesContext.getExternalContext() 165 .getRequestParameterMap().get(FormRenderer.getFocusElementId()); 166 boolean focus = false; 167 if (focusId != null) { 168 if (focusId.toString().equals(getClientId(facesContext))) { 169 focus = true; 170 } 171 } 172 setFocus(focus); 173 return focus; 174 } 175 176 178 181 public Iterator getItemList() { 182 if (itemList == null) { 183 return Collections.EMPTY_LIST.iterator(); 184 } 185 return itemList.iterator(); 186 } 187 188 191 public void setIndex(int index) { 192 this.index = index; 193 194 } 195 196 199 public String getClientId(FacesContext context) { 200 if (context == null) { 201 throw new NullPointerException (); 202 } 203 String baseClientId = super.getClientId(context); 204 if (index >= 0) { 205 return (baseClientId + NamingContainer.SEPARATOR_CHAR + index++); 206 } else { 207 return (baseClientId); 208 } 209 } 210 211 214 void resetId(UIComponent component) { 215 String id = component.getId(); 216 component.setId(id); Iterator kids = component.getChildren().iterator(); 218 while (kids.hasNext()) { 219 UIComponent kid = (UIComponent) kids.next(); 220 resetId(kid); 221 } 222 223 } 224 225 void populateItemList() { 231 if (getSelectFacet() != null) { 232 itemList = getListValue(); 234 } else { 235 itemList = Util.getSelectItems(FacesContext.getCurrentInstance(), 237 this); 238 } 239 try { 240 Iterator items = itemList.iterator(); 241 SelectItem item = null; 242 itemMap.clear(); 243 while (items.hasNext()) { 244 item = (SelectItem) items.next(); 245 itemMap.put(item.getLabel(), item); 246 } 247 } catch(NullPointerException e) { 248 e.printStackTrace(); 249 } 250 } 251 252 255 public void broadcast(FacesEvent event) throws AbortProcessingException { 256 super.broadcast(event); 258 if ((event instanceof ValueChangeEvent)) { 259 populateItemList(); 260 setChangedComponentId( 261 this.getClientId(FacesContext.getCurrentInstance())); 262 } 263 } 264 265 268 public UIComponent getSelectFacet() { 269 return (UIComponent) getFacet("selectInputText"); 270 } 271 272 275 public void setSelectedItem(String key) { 276 277 this.selectedItem = (SelectItem) itemMap.get(key); 278 } 279 280 283 public SelectItem getSelectedItem() { 284 return selectedItem; 285 } 286 287 289 292 public void setRows(int rows) { 293 this.rows = new Integer (rows); 294 } 295 296 299 public int getRows() { 300 if (rows != null) { 301 if (itemMap != null) { 302 return itemMap.size() > 0 && itemMap.size() < rows.intValue() ? 303 itemMap.size() : rows.intValue(); 304 } else { 305 return rows.intValue(); 306 } 307 } 308 309 ValueBinding vb = getValueBinding("rows"); 310 return vb != null ? 311 Integer.parseInt(vb.getValue(getFacesContext()).toString()) : 312 DEFAULT_MAX_MATCHS; 313 } 314 315 318 public void setWidth(String width) { 319 this.width = width; 320 } 321 322 325 public void setListVar(String listVar) { 326 this.listVar = listVar; 327 } 328 329 332 public String getListVar() { 333 if (listVar != null) { 334 return listVar; 335 } 336 ValueBinding vb = getValueBinding("listVar"); 337 return vb != null ? (String ) vb.getValue(getFacesContext()) : null; 338 } 339 340 343 public void setListValue(List listValue) { 344 this.itemList = listValue; 345 } 346 347 350 public List getListValue() { 351 ValueBinding vb = getValueBinding("listValue"); 352 return (List) vb.getValue(FacesContext.getCurrentInstance()); 353 } 354 355 358 public String getWidth() { 359 if (width != null) { 360 return width; 361 } 362 ValueBinding vb = getValueBinding("width"); 363 return vb != null ? vb.getValue(getFacesContext()).toString() : 364 DEFAULT_WIDTH; 365 } 366 367 String getWidthAsStyle() { 368 try { int width = Integer.parseInt(getWidth()); 370 return "width:" + width + "px;"; 371 } catch (NumberFormatException e) { 372 return "width:" + getWidth().trim(); 373 } 374 } 375 376 379 public void setStyleClass(String styleClass) { 380 this.styleClass = styleClass; 381 } 382 383 386 public String getStyleClass() { 387 return Util.getQualifiedStyleClass(this, 388 styleClass, 389 CSS_DEFAULT.DEFAULT_SELECT_INPUT, 390 "styleClass", 391 isDisabled()); 392 } 393 394 397 public String getInputTextClass() { 398 return Util.getQualifiedStyleClass(this, 399 CSS_DEFAULT.DEFAULT_SELECT_INPUT_TEXT_CLASS, isDisabled()); 400 } 401 402 403 406 public String getListClass() { 407 return Util.getQualifiedStyleClass(this, 408 CSS_DEFAULT.DEFAULT_SELECT_INPUT_LIST_CLASS, isDisabled()); 409 } 410 411 412 415 public String getRowClass() { 416 return Util.getQualifiedStyleClass(this, 417 CSS_DEFAULT.DEFAULT_SELECT_INPUT_ROW_CLASS, isDisabled()); 418 } 419 420 421 424 public String getSelectedRowClass() { 425 return Util.getQualifiedStyleClass(this, 426 CSS_DEFAULT.DEFAULT_SELECT_INPUT_SELECTED_ROW_CLASS, isDisabled()); 427 } 428 429 public String getOptions() { 430 return options; 431 } 432 433 public void setOptions(String options) { 434 this.options = options; 435 } 436 437 438 private List changedComponentIds = new ArrayList(); 442 443 446 void setChangedComponentId(Object id) { 447 if (id == null) { 448 changedComponentIds.clear(); 449 } else { 450 changedComponentIds.add(id); 451 } 452 } 453 454 457 boolean hasChanged() { 458 return changedComponentIds 459 .contains(this.getClientId(FacesContext.getCurrentInstance())); 460 } 461 462 467 private void queueEventIfEnterKeyPressed(FacesContext facesContext) { 468 try { 469 Map requestParemeterMap = facesContext.getExternalContext() 470 .getRequestParameterMap(); 471 KeyEvent keyEvent = 472 new KeyEvent(this, requestParemeterMap); 473 474 if (keyEvent.getKeyCode() == KeyEvent.CARRIAGE_RETURN) { 475 queueEvent(new ActionEvent(this)); 476 } 477 if("true".equals(requestParemeterMap.get("ice.event.left"))){ 478 queueEvent(new ActionEvent(this)); 479 } 480 if("onclick".equals(requestParemeterMap.get("ice.event.type"))){ 481 queueEvent(new ActionEvent(this)); 482 } 483 484 485 } catch (Exception e) { 486 e.printStackTrace(); 487 } 488 } 489 490 491 492 496 public Object saveState(FacesContext context) { 497 Object values[] = new Object [8]; 498 values[0] = super.saveState(context); 499 values[1] = styleClass; 500 values[2] = listVar; 501 values[3] = rows; 502 values[4] = width; 503 values[5] = selectedItem; 504 values[6] = itemList; 505 values[7] = options; 506 return ((Object ) (values)); 507 } 508 509 513 public void restoreState(FacesContext context, Object state) { 514 Object values[] = (Object []) state; 515 super.restoreState(context, values[0]); 516 styleClass = (String ) values[1]; 517 listVar = (String ) values[2]; 518 rows = (Integer ) values[3]; 519 width = (String ) values[4]; 520 selectedItem = (SelectItem) values[5]; 521 itemList = (List) values[6]; 522 options = (String )values[7]; 523 } 524 } 525 | Popular Tags |