@@ -0,0 +1,2574 @@
+/*%0A *  Copyright 2012 Eric F. Savage, code@efsavage.com%0A *%0A *   Licensed under the Apache License, Version 2.0 (the %22License%22);%0A *   you may not use this file except in compliance with the License.%0A *   You may obtain a copy of the License at%0A *%0A *       http://www.apache.org/licenses/LICENSE-2.0%0A *%0A *   Unless required by applicable law or agreed to in writing, software%0A *   distributed under the License is distributed on an %22AS IS%22 BASIS,%0A *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A *   See the License for the specific language governing permissions and%0A *   limitations under the License.%0A */%0A%0Apackage com.ajah.spring.mvc.editor;%0A%0Aimport java.beans.PropertyEditorSupport;%0A%0Aimport com.ajah.util.Identifiable;%0A%0A/**%0A * Generic class that will map an enum based on its %7B@link Identifiable#getId()%7D%0A * method.%0A * %0A * @author %3Ca href=%22http://efsavage.com%22%3EEric F. Savage%3C/a%3E, %3Ca%0A *         href=%22mailto:code@efsavage.com%22%3Ecode@efsavage.com%3C/a%3E.%0A * %0A * @param %3CE%3E%0A */%0Apublic class IdentifiableEnumPropertyEditor%3CE extends Identifiable%3C?%3E%3E extends PropertyEditorSupport %7B%0A%0A%09private E%5B%5D values;%0A%0A%09/**%0A%09 * Pass the result of Enum.getValues() because I can't seem to find a way to%0A%09 * derive this from the type parameter.%0A%09 * %0A%09 * @param values%0A%09 *            The values of the enum.%0A%09 */%0A%09public IdentifiableEnumPropertyEditor(E%5B%5D values) %7B%0A%09%09this.values = values;%0A%09%7D%0A%0A%09/**%0A%09 * Sets the property value by parsing a given String. May raise%0A%09 * java.lang.IllegalArgumentException if either the String is badly%0A%09 * formatted or if this kind of property can't be expressed as text.%0A%09 * %0A%09 * @param text%0A%09 *            The string to be parsed.%0A%09 */%0A%09@Override%0A%09public void setAsText(String text) throws java.lang.IllegalArgumentException %7B%0A%09%09for (E candidate : this.values) %7B%0A%09%09%09if (candidate.getId().toString().equals(text)) %7B%0A%09%09%09%09setValue(candidate);%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09throw new java.lang.IllegalArgumentException(text);%0A%09%7D%0A%0A%09/**%0A%09 * Gets the property value as a string suitable for presentation to a human%0A%09 * to edit.%0A%09 * %0A%09 * @return The property value as a string suitable for presentation to a%0A%09 *         human to edit.%0A%09 *         %3Cp%3E%0A%09 *         Returns null if the value can't be expressed as a string.%0A%09 *         %3Cp%3E%0A%09 *         If a non-null value is returned, then the PropertyEditor should%0A%09 *         be prepared to parse that string back in setAsText().%0A%09 */%0A%09@SuppressWarnings(%22unchecked%22)%0A%09@Override%0A%09public String getAsText() %7B%0A%09%09return (this.getValue() != null) ? ((E) this.getValue()).getId().toString() : null;%0A%09%7D%0A%0A%7D%0A

Add property editor for identifiable enums