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 org.apache.wicket.Application; 020import org.apache.wicket.Component; 021import org.apache.wicket.util.lang.Classes; 022 023/** 024 * {@link IChoiceRenderer} implementation that makes it easy to work with java 5 enums. This 025 * renderer will attempt to lookup strings used for the display value using a localizer of a given 026 * component. If the component is not specified, the global instance of localizer will be used for 027 * lookups. 028 * <p> 029 * display value resource key format: {@code <enum.getSimpleClassName()>.<enum.name()>} 030 * </p> 031 * <p> 032 * id value format: {@code <enum.name()>} 033 * </p> 034 * 035 * @author igor.vaynberg 036 * 037 * @param <T> 038 */ 039public class EnumChoiceRenderer<T extends Enum<T>> extends ChoiceRenderer<T> 040{ 041 042 private static final long serialVersionUID = 1L; 043 044 /** 045 * component used to resolve i18n resources for this renderer. 046 */ 047 private final Component resourceSource; 048 049 050 /** 051 * Constructor that creates the choice renderer that will use global instance of localizer to 052 * resolve resource keys. 053 */ 054 public EnumChoiceRenderer() 055 { 056 this(null); 057 } 058 059 /** 060 * Constructor 061 * 062 * @param resourceSource 063 */ 064 public EnumChoiceRenderer(Component resourceSource) 065 { 066 this.resourceSource = resourceSource; 067 } 068 069 /** {@inheritDoc} */ 070 @Override 071 public Object getDisplayValue(T object) 072 { 073 final String value; 074 075 String key = resourceKey(object); 076 077 if (resourceSource != null) 078 { 079 value = resourceSource.getString(key); 080 } 081 else 082 { 083 value = Application.get().getResourceSettings().getLocalizer().getString(key, null); 084 } 085 086 return value; 087 } 088 089 /** 090 * Translates the {@code object} into resource key that will be used to lookup the value shown 091 * to the user 092 * 093 * @param object 094 * @return resource key 095 */ 096 protected String resourceKey(T object) 097 { 098 return Classes.simpleName(object.getDeclaringClass()) + '.' + object.name(); 099 } 100 101 /** {@inheritDoc} */ 102 @Override 103 public String getIdValue(T object, int index) 104 { 105 return object.name(); 106 } 107}