001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.markup.html.form; 018 019import java.util.List; 020 021import org.apache.wicket.core.util.lang.PropertyResolver; 022import org.apache.wicket.model.IModel; 023import org.apache.wicket.util.io.IClusterable; 024 025/** 026 * Renders one choice. Separates the 'id' values used for internal representation from 'display 027 * values' which are the values shown to the user of components that use this renderer. 028 * 029 * Usage: 030 * <p> 031 * 032 * <pre> 033 * new DropDownChoice<User>("users", new Model<User>(selectedUser), listOfUsers) 034 * </pre> 035 * 036 * creates a DropDownChoice of users and the display value will be toString() and the id the index 037 * of the object in the ListOfUsers. 038 * </p> 039 * <p> 040 * 041 * <pre> 042 * new DropDownChoice<User>("users", new Model<User>(selectedUser), listOfUsers, 043 * new ChoiceRenderer<User>("name")) 044 * </pre> 045 * 046 * creates a DropDownChoice of users and the display value will be looked up by property expression 047 * ("name") and the id the index of the object in the ListOfUsers 048 * </p> 049 * <p> 050 * 051 * <pre> 052 * new DropDownChoice<User>("users", new Model<User>(selectedUser), listOfUsers, 053 * new ChoiceRenderer<User>("name", "id")) 054 * </pre> 055 * 056 * creates a DropDownChoice of users and the display value will be looked up by property expression 057 * ("name") and the id will be looked up by the property expression "id" 058 * </p> 059 * 060 * @author jcompagner 061 * 062 * @param <T> 063 * The model object type 064 */ 065public class ChoiceRenderer<T> implements IChoiceRenderer<T>, IClusterable 066{ 067 private static final long serialVersionUID = 1L; 068 069 /** expression for getting the display value. */ 070 private final String displayExpression; 071 072 /** expression for getting the id. */ 073 private final String idExpression; 074 075 /** 076 * Constructor. 077 * 078 * When you use this constructor, the display value will be determined by calling 079 * toString() on the list object, and the id will be based on the list index. the id value will 080 * be the index 081 */ 082 public ChoiceRenderer() 083 { 084 this(null); 085 } 086 087 /** 088 * Constructor. 089 * 090 * When you use this constructor, the display value will be determined by executing 091 * the given property expression on the list object, and the id will be based on the list index. 092 * The display value will be calculated by the given property expression 093 * 094 * @param displayExpression 095 * A property expression to get the display value 096 */ 097 public ChoiceRenderer(String displayExpression) 098 { 099 this(displayExpression, null); 100 } 101 102 /** 103 * Constructor. 104 * 105 * When you use this constructor, both the id and the display value will be 106 * determined by executing the given property expressions on the list object. 107 * 108 * @param displayExpression 109 * A property expression to get the display value 110 * @param idExpression 111 * A property expression to get the id value 112 */ 113 public ChoiceRenderer(String displayExpression, String idExpression) 114 { 115 this.displayExpression = displayExpression; 116 this.idExpression = idExpression; 117 } 118 119 @Override 120 public Object getDisplayValue(T object) 121 { 122 Object returnValue = object; 123 if ((displayExpression != null) && (object != null)) 124 { 125 returnValue = PropertyResolver.getValue(displayExpression, object); 126 } 127 128 if (returnValue == null) 129 { 130 return ""; 131 } 132 133 return returnValue; 134 } 135 136 @Override 137 public String getIdValue(T object, int index) 138 { 139 if (idExpression == null) 140 { 141 return Integer.toString(index); 142 } 143 144 if (object == null) 145 { 146 return ""; 147 } 148 149 Object returnValue = PropertyResolver.getValue(idExpression, object); 150 if (returnValue == null) 151 { 152 return ""; 153 } 154 155 return returnValue.toString(); 156 } 157}