/** * Initializes the <code>items</code> instance variable with an <code>Iterator</code> appropriate * to the UISelectItem(s) value. */ private void initializeItems() { UIComponent kid = (UIComponent) findNextValidChild(); if (kid instanceof UISelectItem) { UISelectItem ui = (UISelectItem) kid; SelectItem item = (SelectItem) ui.getValue(); if (item == null) { item = new SelectItem( ui.getItemValue(), ui.getItemLabel(), ui.getItemDescription(), ui.isItemDisabled(), ui.isItemEscaped(), ui.isNoSelectionOption()); } updateSingeItemIterator(item); items = singleItemIterator; } else if (kid instanceof UISelectItems) { UISelectItems ui = (UISelectItems) kid; Object value = ui.getValue(); if (value instanceof SelectItem) { updateSingeItemIterator((SelectItem) value); items = singleItemIterator; } else if (value.getClass().isArray()) { items = new ArrayIterator(ctx, (UISelectItems) kid, value); } else if (value instanceof Iterable) { items = new IterableItemIterator(ctx, (UISelectItems) kid, (Iterable<?>) value); } else if (value instanceof Map) { items = new MapIterator((Map) value); } else { throw new IllegalArgumentException(); } } }
protected List<SelectItem> getSelectItems(FacesContext context, UIInput component) { List<SelectItem> selectItems = new ArrayList<SelectItem>(); for (UIComponent child : component.getChildren()) { if (child instanceof UISelectItem) { UISelectItem uiSelectItem = (UISelectItem) child; Object selectItemValue = uiSelectItem.getValue(); if (selectItemValue == null) { selectItems.add( new SelectItem( uiSelectItem.getItemValue(), uiSelectItem.getItemLabel(), uiSelectItem.getItemDescription(), uiSelectItem.isItemDisabled(), uiSelectItem.isItemEscaped(), uiSelectItem.isNoSelectionOption())); } else { selectItems.add((SelectItem) selectItemValue); } } else if (child instanceof UISelectItems) { UISelectItems uiSelectItems = ((UISelectItems) child); Object value = uiSelectItems.getValue(); if (value != null) { if (value instanceof SelectItem) { selectItems.add((SelectItem) value); } else if (value.getClass().isArray()) { for (int i = 0; i < Array.getLength(value); i++) { Object item = Array.get(value, i); if (item instanceof SelectItem) selectItems.add((SelectItem) item); else selectItems.add(createSelectItem(context, uiSelectItems, item)); } } else if (value instanceof Map) { Map map = (Map) value; for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { Object key = it.next(); selectItems.add( createSelectItem(context, uiSelectItems, String.valueOf(key), map.get(key))); } } else if (value instanceof Collection) { Collection collection = (Collection) value; for (Iterator it = collection.iterator(); it.hasNext(); ) { Object item = it.next(); if (item instanceof SelectItem) selectItems.add((SelectItem) item); else selectItems.add(createSelectItem(context, uiSelectItems, item)); } } } } } return selectItems; }
/** Move items from components to others. */ public static void moveAllItems( UISelectItems sourceItems, UISelectItems targetItems, UIEditableList hiddenTargetList, boolean setTargetIds) { SelectItem[] all = (SelectItem[]) sourceItems.getValue(); List<SelectItem> toMove = new ArrayList<SelectItem>(); List<SelectItem> toKeep = new ArrayList<SelectItem>(); List<String> hiddenIds = new ArrayList<String>(); if (all != null) { for (SelectItem item : all) { if (!item.isDisabled()) { toMove.add(item); } else { toKeep.add(item); } } } // reset left values sourceItems.setValue(toKeep.toArray(new SelectItem[] {})); // change right values List<SelectItem> newSelectItems = new ArrayList<SelectItem>(); SelectItem[] oldSelectItems = (SelectItem[]) targetItems.getValue(); if (oldSelectItems == null) { newSelectItems.addAll(toMove); } else { newSelectItems.addAll(Arrays.asList(oldSelectItems)); List<String> oldIds = new ArrayList<String>(); for (SelectItem oldItem : oldSelectItems) { String id = oldItem.getValue().toString(); oldIds.add(id); } if (setTargetIds) { hiddenIds.addAll(0, oldIds); } for (SelectItem toMoveItem : toMove) { String id = toMoveItem.getValue().toString(); if (!oldIds.contains(id)) { newSelectItems.add(toMoveItem); if (setTargetIds) { hiddenIds.add(id); } } } } targetItems.setValue(newSelectItems.toArray(new SelectItem[] {})); // update hidden values int numValues = hiddenTargetList.getRowCount(); if (numValues > 0) { for (int i = numValues - 1; i > -1; i--) { hiddenTargetList.removeValue(i); } } for (String newHiddenValue : hiddenIds) { hiddenTargetList.addValue(newHiddenValue); } }
public static void shiftItemsLast( UISelectMany targetSelect, UISelectItems targetItems, UIEditableList hiddenTargetList) { String[] selected = (String[]) targetSelect.getSelectedValues(); SelectItem[] all = (SelectItem[]) targetItems.getValue(); if (selected == null) { // nothing to do return; } all = shiftLast(selected, all); targetItems.setValue(all); clearTargetList(hiddenTargetList); addToTargetList(hiddenTargetList, all); }
public static UISelectItems getPseudoSelectItems(SelectItemsInterface selectItemsInterface) { UISelectItems selectItems = null; if (selectItemsInterface.getVar() != null && selectItemsInterface.getItemValues() != null) { selectItems = new UISelectItems(); selectItems.setValue(selectItemsInterface.getItemValues()); selectItems.getAttributes().put("var", selectItemsInterface.getVar()); if (selectItemsInterface.getItemValue() != null) { selectItems.getAttributes().put("itemValue", selectItemsInterface.getItemValue()); } if (selectItemsInterface.getItemLabel() != null) { selectItems.getAttributes().put("itemLabel", selectItemsInterface.getItemLabel()); } } return selectItems; }
public void restoreState(FacesContext context, Object state) { Object values[] = (Object[]) state; super.restoreState(context, values[0]); itemValue = (String) values[1]; itemLabel = (String) values[2]; nullLabelRequired = ((Boolean) values[3]).booleanValue(); }
// @SuppressWarnings("unchecked") protected void addSelectItems(UISelectItems uiItems, List<SelectItem> items) { boolean isRendered = uiItems.isRendered(); if (!isRendered) { items.add(null); return; } Object value = uiItems.getValue(); if (value instanceof SelectItem) { items.add((SelectItem) value); } else if (value instanceof Object[]) { Object[] array = (Object[]) value; for (Object array1 : array) { // TODO test - this section is untested if (array1 instanceof SelectItemGroup) { resolveAndAddItems((SelectItemGroup) array1, items); } else { items.add((SelectItem) array1); } } } else if (value instanceof Collection) { Iterator<Object> iter = ((Collection<Object>) value).iterator(); Object item; while (iter.hasNext()) { item = iter.next(); if (item instanceof SelectItemGroup) { resolveAndAddItems((SelectItemGroup) item, items); } else { items.add(new SelectItem(item)); } } } else if (value instanceof Map) { for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) value).entrySet()) { Object label = entry.getKey(); SelectItem item = new SelectItem(entry.getValue(), label == null ? (String) null : label.toString()); // TODO test - this section is untested if (item instanceof SelectItemGroup) { resolveAndAddItems((SelectItemGroup) item, items); } else { items.add(item); } } } }
@Override public void restoreState(FacesContext context, Object state) { this.values = (Object[]) state; super.restoreState(context, this.values[0]); this.var = (String) this.values[1]; this.itemLabel = this.values[2]; this.itemValue = this.values[3]; }
protected SelectItem createSelectItem( FacesContext context, UISelectItems uiSelectItems, String itemLabel, Object itemValue) { String var = (String) uiSelectItems.getAttributes().get("var"); if (var != null) { context.getExternalContext().getRequestMap().put(var, itemValue); String description = (String) uiSelectItems.getAttributes().get("itemDescription"); Boolean disabled = Boolean.valueOf(((String) uiSelectItems.getAttributes().get("itemDisabled"))); Boolean escaped = Boolean.valueOf(((String) uiSelectItems.getAttributes().get("itemLabelEscaped"))); Boolean noSelectionOption = Boolean.valueOf(((String) uiSelectItems.getAttributes().get("noSelectionOption"))); return new SelectItem( itemValue, itemLabel, description, disabled, escaped, noSelectionOption); } else { return new SelectItem(itemValue, itemLabel); } }
protected SelectItem createSelectItem( FacesContext context, UISelectItems uiSelectItems, Object object) { String var = (String) uiSelectItems.getAttributes().get("var"); if (var != null) { context.getExternalContext().getRequestMap().put(var, object); Object itemLabelAsObject = uiSelectItems.getAttributes().get("itemLabel"); Object itemValue = uiSelectItems.getAttributes().get("itemValue"); String description = (String) uiSelectItems.getAttributes().get("itemDescription"); Object itemDisabled = uiSelectItems.getAttributes().get("itemDisabled"); Object itemEscaped = uiSelectItems.getAttributes().get("itemLabelEscaped"); Object noSelection = uiSelectItems.getAttributes().get("noSelectionOption"); if (itemValue == null) { itemValue = object; } String itemLabel = itemLabelAsObject == null ? String.valueOf(object) : String.valueOf(itemLabelAsObject); boolean disabled = itemDisabled == null ? false : Boolean.valueOf(itemDisabled.toString()); boolean escaped = itemEscaped == null ? false : Boolean.valueOf(itemEscaped.toString()); boolean noSelectionOption = noSelection == null ? false : Boolean.valueOf(noSelection.toString()); return new SelectItem( itemValue, itemLabel, description, disabled, escaped, noSelectionOption); } else { return new SelectItem(object, String.valueOf(object)); } }
/** * populate the argument component with values, being sensitive to the possible multi-nature of * the values, and to the type of the values. * * @param context the <code>FacesContext</code> for the current request * @param component the <code>UIComponent</code> to populate * @param componentType the component type * @param value the value * @param valueType the value type */ private void populateComponentWithValue( FacesContext context, UIComponent component, String componentType, String value, String valueType) { Application application = context.getApplication(); Converter converter = null; // if we need a converter, and can have a converter if (!"java.lang.String".equals(valueType) && component instanceof ValueHolder) { // if so create it, try { converter = application.createConverter(CarStore.loadClass(valueType, this)); // add it to our component, ((ValueHolder) component).setConverter(converter); } catch (ClassNotFoundException cne) { FacesMessage errMsg = MessageFactory.getMessage(CONVERTER_ERROR_MESSAGE_ID, valueType); throw new IllegalStateException(errMsg.getSummary()); } } // if this component is a SelectOne or SelectMany, take special action if (isMultiValue(componentType)) { // create a UISelectItems instance UISelectItems items = new UISelectItems(); items.setValue(parseStringIntoArrayList(value, converter)); // add it to the component component.getChildren().add(items); } else { // we have a single value if (null != converter) { component.getAttributes().put("value", converter.getAsObject(context, component, value)); } else { component.getAttributes().put("value", value); } } }
private GenericObjectSelectItem(UISelectItems sourceComponent) { var = (String) sourceComponent.getAttributes().get(VAR); itemValue = sourceComponent.getValueExpression(ITEM_VALUE); itemLabel = sourceComponent.getValueExpression(ITEM_LABEL); itemDescription = sourceComponent.getValueExpression(ITEM_DESCRIPTION); itemEscaped = sourceComponent.getValueExpression(ITEM_ESCAPED); itemDisabled = sourceComponent.getValueExpression(ITEM_DISABLED); noSelectionOption = sourceComponent.getValueExpression(NO_SELECTION_OPTION); }
/** * Takes a parent UIComponent and adds all UIComponents which are required for configuring a * metric. i.e. metric name, math expression, value, etc. * * @param metric * @param parent */ private void helperCreateUICompsForMetricConfig(MetricBean metric, UIComponent parent) { // 1)remove all existing child elements that may have been created previously from ALL // pConfigVeryGood, pConfigGood, etc. removeAllInputHelpersForMetricConfig(); // 2) now build the gui elements for building a metric configuration // 2a) Add an output text with the metric's name HtmlOutputText mnameText = new HtmlOutputText(); mnameText.setId("metricName" + metric.getInternalID()); mnameText.setValue(metric.getName()); // add on parent parent.getChildren().add(mnameText); // 2b) Select a math expression HtmlSelectOneMenu select2 = new HtmlSelectOneMenu(); select2.setId("mathSelect" + metric.getInternalID()); UISelectItems items2 = new UISelectItems(); items2.setId("mathvals" + metric.getInternalID()); items2.setValue(metric.getAllAvailableTypes()); Class[] parms2 = new Class[] {ValueChangeEvent.class}; ExpressionFactory ef = FacesContext.getCurrentInstance().getApplication().getExpressionFactory(); MethodExpression mb = ef.createMethodExpression( FacesContext.getCurrentInstance().getELContext(), "#{AutoEvalSerUserConfigBean.processMathExprChange}", null, parms2); MethodExpressionValueChangeListener vcl = new MethodExpressionValueChangeListener(mb); select2.addValueChangeListener(vcl); select2.getChildren().add(items2); select2.setImmediate(true); // place an ajax support on the selectonemenu field HtmlAjaxSupport ajaxSupport2 = new HtmlAjaxSupport(); // Class[] parms = new Class[]{ActionEvent.class}; // ajaxSupport.setAction(FacesContext.getCurrentInstance().getApplication().createMethodBinding("#{AutoEvalSerUserConfigBean.sliderValue}", parms)); ajaxSupport2.setEvent("onchange"); // to add multiple ids for rerendering, separate them with a "," ajaxSupport2.setReRender(select2.getId()); ajaxSupport2.setEventsQueue("foo"); select2.getFacets().put("a4jsupport2", ajaxSupport2); // add to parent parent.getChildren().add(select2); // 2c) For all input types except boolean values: if (metric.isHtmlInputTextUsed()) { // enter a boundary value for a specific added metric HtmlInputText inputText = new HtmlInputText(); inputText.setId("metricBoundary" + metric.getInternalID()); inputText.setValue(metric.getEvalBoundary()); inputText.setSize(10); Class[] parms = new Class[] {ValueChangeEvent.class}; MethodExpression mb2 = ef.createMethodExpression( FacesContext.getCurrentInstance().getELContext(), "#{AutoEvalSerUserConfigBean.processMetricBoundaryValueChange}", null, parms); MethodExpressionValueChangeListener vcl2 = new MethodExpressionValueChangeListener(mb2); inputText.addValueChangeListener(vcl2); inputText.setImmediate(true); // place an ajax support on the InputText field HtmlAjaxSupport ajaxSupport = new HtmlAjaxSupport(); // Class[] parms = new Class[]{ActionEvent.class}; // ajaxSupport.setAction(FacesContext.getCurrentInstance().getApplication().createMethodBinding("#{AutoEvalSerUserConfigBean.sliderValue}", parms)); ajaxSupport.setEvent("onchange"); // to add multiple ids for rerendering, separate them with a "," ajaxSupport.setReRender(inputText.getId()); ajaxSupport.setEventsQueue("foo"); inputText.getFacets().put("a4jsupport", ajaxSupport); // add to parent parent.getChildren().add(inputText); } else { // add a drop-down box for boolean values HtmlSelectOneMenu select = new HtmlSelectOneMenu(); select.setId("booleanSelect" + metric.getInternalID()); UISelectItems items = new UISelectItems(); items.setId("vals" + metric.getInternalID()); List<SelectItem> l = new ArrayList<SelectItem>(); l.add(new SelectItem("true")); l.add(new SelectItem("false")); items.setValue(l); Class[] parms = new Class[] {ValueChangeEvent.class}; MethodExpression mb3 = ef.createMethodExpression( FacesContext.getCurrentInstance().getELContext(), "#{AutoEvalSerUserConfigBean.processMetricBoundaryValueChange}", null, parms); MethodExpressionValueChangeListener vcl3 = new MethodExpressionValueChangeListener(mb3); select.addValueChangeListener(vcl3); select.getChildren().add(items); select.setImmediate(true); // place an ajax support on the selectonemenu field HtmlAjaxSupport ajaxSupport = new HtmlAjaxSupport(); // Class[] parms = new Class[]{ActionEvent.class}; // ajaxSupport.setAction(FacesContext.getCurrentInstance().getApplication().createMethodBinding("#{AutoEvalSerUserConfigBean.sliderValue}", parms)); ajaxSupport.setEvent("onchange"); // to add multiple ids for rerendering, separate them with a "," ajaxSupport.setReRender(select.getId()); ajaxSupport.setEventsQueue("foo"); select.getFacets().put("a4jsupport", ajaxSupport); // add to parent parent.getChildren().add(select); } // 2d) finally the submit button for saving the configuration HtmlCommandButton button_save = new HtmlCommandButton(); button_save.setId("buttonSave" + metric.getInternalID()); button_save.setValue("add config"); Class[] parms3 = new Class[] {ActionEvent.class}; MethodExpression mb4 = ef.createMethodExpression( FacesContext.getCurrentInstance().getELContext(), "#{AutoEvalSerUserConfigBean.command_saveMetricConfiguration}", null, parms3); MethodExpressionActionListener vcl4 = new MethodExpressionActionListener(mb4); button_save.addActionListener(vcl4); UIParameter p = new UIParameter(); p.setId("param_save_button" + metric.getInternalID()); p.setName("pConfigPanel"); p.setValue(parent.getId()); button_save.getChildren().add(p); parent.getChildren().add(button_save); HtmlOutputText message = new HtmlOutputText(); message.setId("message" + metric.getInternalID()); message.setStyle("color:red;"); parent.getChildren().add(message); }