📄 carbean.java
字号:
result = converter. getAsString(FacesContext. getCurrentInstance(), component, result); } } } return result; } public int hashCode() { return CarBean.this.components.hashCode(); } public boolean isEmpty() { return CarBean.this.components.isEmpty(); } public java.util.Set keySet() { return CarBean.this.components.keySet(); } public Object put(Object k, Object v) { throw new UnsupportedOperationException(); } public void putAll(Map t) { throw new UnsupportedOperationException(); } public Object remove(Object k) { throw new UnsupportedOperationException(); } public int size() { return CarBean.this.components.size(); } public java.util.Collection values() { ArrayList result = new ArrayList(); Iterator keys = keySet().iterator(); while (keys.hasNext()) { result.add(get(keys.next())); } return result; } }; } /** * <p>For each entry in data, create component and cause it to be * populated with values.</p> */ private void initComponentsFromProperties(FacesContext context, ResourceBundle data) { Application application = context.getApplication(); Enumeration keys = data.getKeys(); String key = null, value = null, componentType = null, valueType = null; UIComponent component = null; // populate the map while (keys.hasMoreElements()) { key = (String) keys.nextElement(); if (key == null) { continue; } // skip secondary keys. if (-1 != key.indexOf("_")) { continue; } value = data.getString(key); componentType = data.getString(key + "_componentType"); valueType = data.getString(key + "_valueType"); if (log.isDebugEnabled()) { log.debug("populating map for " + key + "\n" + "\n\tvalue: " + value + "\n\tcomponentType: " + componentType + "\n\tvalueType: " + valueType); } // create the component for this componentType component = application.createComponent(componentType); populateComponentWithValue(context, component, componentType, value, valueType); components.put(key, component); } } /** * <p>populate the argument component with values, being sensitive * to the possible multi-nature of the values, and to the type of * the values.</p> */ private void populateComponentWithValue(FacesContext context, UIComponent component, String componentType, String value, String valueType) { Application application = context.getApplication(); Converter converter = null; UISelectItems items = null; // if we need a converter, and can have a converter if (!valueType.equals("java.lang.String") && component instanceof ValueHolder) { // if so create it, try { converter = application.createConverter(CarStore.loadClass(valueType, this)); } catch (ClassNotFoundException cne) { FacesMessage errMsg = MessageFactory.getMessage( CONVERTER_ERROR_MESSAGE_ID, (new Object[]{valueType})); throw new IllegalStateException(errMsg.getSummary()); } // add it to our component, ((ValueHolder) component).setConverter(converter); } // if this component is a SelectOne or SelectMany, take special action if (isMultiValue(componentType)) { // create a UISelectItems instance items = new UISelectItems(); items.setValue(parseStringIntoArrayList(context, component, value, valueType, 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); } } } /** * @return true if componentType starts with SelectMany or SelectOne */ private boolean isMultiValue(String componentType) { if (null == componentType) { return false; } return (componentType.startsWith("javax.faces.SelectMany") || componentType.startsWith("javax.faces.SelectOne")); } /** * Tokenizes the passed in String which is a comma separated string of * option values that serve as keys into the main resources file. * For example, optionStr could be "Disc,Drum", which corresponds to * brake options available for the chosen car. This String is tokenized * and used as key into the main resource file to get the localized option * values and stored in the passed in ArrayList. */ public ArrayList parseStringIntoArrayList(FacesContext context, UIComponent component, String value, String valueType, Converter converter) { ArrayList optionsList = null; int i = 0; Object optionValue = null; String optionKey = null, optionLabel = null; Map nonLocalizedOptionValues = null; if (value == null) { return null; } StringTokenizer st = new StringTokenizer(value, ","); optionsList = new ArrayList((st.countTokens()) + 1); while (st.hasMoreTokens()) { optionKey = st.nextToken(); try { optionLabel = resources.getString(optionKey); } catch (MissingResourceException e) { // if we can't find a hit, the key is the label optionLabel = optionKey; } if (null != converter) { // PENDING deal with the converter case } else { optionsList.add(new SelectItem(optionKey, optionLabel)); } } return optionsList; } public String updatePricing() { getCurrentPrice(); return null; } public Integer getCurrentPrice() { // go through our options and try to get the prices int sum = ((Integer) ((ValueHolder) getComponents().get("basePrice")). getValue()).intValue(); Iterator iter = getComponents().keySet().iterator(); String key = null; Object value = null; UIComponent component = null; while (iter.hasNext()) { key = (String) iter.next(); component = (UIComponent) getComponents().get(key); value = component.getAttributes().get("value"); if (null == value || (!(component instanceof UIInput))) { continue; } // if the value is a String, see if we have priceData for it if (value instanceof String) { try { sum += Integer.valueOf(priceData.getString((String) value)) .intValue(); } catch (NumberFormatException e) { } } // if the value is a Boolean, look up the price by name else if (value instanceof Boolean && ((Boolean) value).booleanValue()) { try { sum += Integer.valueOf(priceData.getString(key)).intValue(); } catch (NumberFormatException e) { } } else if (value instanceof Number) { sum += ((Number) value).intValue(); } } Integer result = new Integer(sum); // store the new price into the component for currentPrice ((ValueHolder) getComponents().get("currentPrice")). setValue(result); return result; } public Map getComponents() { return components; } public Map getAttributes() { return attributes; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -