/*
 * $Id: org.eclipse.jdt.ui.prefs 5004 2006-03-17 20:47:08 -0800 (Fri, 17 Mar
 * 2006) eelco12 $ $Revision: 5004 $ $Date: 2006-03-17 20:47:08 -0800 (Fri, 17
 * Mar 2006) $
 * 
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package wicket.extensions.markup.html.form;

import java.util.Arrays;
import java.util.List;

import wicket.MarkupContainer;
import wicket.markup.html.form.DropDownChoice;
import wicket.markup.html.form.IChoiceRenderer;
import wicket.model.IModel;

/**
 * Dropdown choice that makes it trivial to work with enums. Enum's
 * <code>name()</code> is used as the choice id and enum's
 * <code>toString()</code> is used for text.
 * 
 * <pre>
 *     public enum Color {
 *       WHITE(&quot;White&quot;),BLACK(&quot;Black&quot;);
 *       
 *       private String text;
 *       private Color(String text) { this.text=text; }
 *       
 *       public String toString() { return text; }
 *     }
 *     
 *     new EnumDropDownChoice&lt;Color&gt;(this, &quot;color&quot;, new PropertyModel(...), Color.class);
 * </pre>
 * 
 * 
 * 
 * 
 * @author ivaynberg
 * @param <T>
 */
public class EnumDropDownChoice<T extends Enum> extends DropDownChoice<T>
{
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor
	 * 
	 * @param parent
	 * @param id
	 * @param enumType
	 *            class of enum used to provide choices
	 */
	public EnumDropDownChoice(MarkupContainer parent, String id, Class enumType)
	{
		this(parent, id, null, enumType);
	}

	/**
	 * Constructor
	 * 
	 * @param parent
	 * @param id
	 * @param model
	 * @param enumType
	 *            class of enum used to provide choices
	 */
	@SuppressWarnings("unchecked")
	public EnumDropDownChoice(MarkupContainer parent, String id, IModel<T> model, Class enumType)
	{
		super(parent, id);

		if (enumType == null)
		{
			throw new IllegalArgumentException("Argument [[enumType]] cannot be null");
		}
		else if (enumType.isEnum() == false)
		{
			throw new IllegalArgumentException("Argument [[enumType]] must be an enum");
		}


		setModel(model);
		setChoices((List<T>)Arrays.asList(enumType.getEnumConstants()));
		setChoiceRenderer(new EnumRenderer<T>());

	}

	private static class EnumRenderer<T extends Enum> implements IChoiceRenderer<T>
	{

		private static final long serialVersionUID = 1L;

		public Object getDisplayValue(T object)
		{
			return object.toString();
		}

		public String getIdValue(T object, int index)
		{
			return object.name();
		}

	}


}

@@ -904,32 +904,8 @@
 ys;%0D
-%0Aimport java.util.List;%0D
 %0A%0D%0Ai
@@ -2046,24 +2046,27 @@
 ng id, Class
+%3CT%3E
  enumType)%0D%0A
@@ -2391,16 +2391,19 @@
 l, Class
+%3CT%3E
  enumTyp
@@ -2721,17 +2721,8 @@
 ces(
-(List%3CT%3E)
 Arra

fix compile error sun/ improvement