/* Copyright 2006 - 2010 Under Dusken 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 no.dusken.aranea.admin.editor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.beans.PropertyEditorSupport; /** * @author Jan Ove Øyen <janoveo@underdusken.no> Date: 3/02/2007 Time: 19:13:51 */ public class GenericEnumEditor<T extends Enum> extends PropertyEditorSupport { protected static final Logger log = LoggerFactory.getLogger(GenericEditor.class); private Enum enm = null; public Object getValue() { return enm; } @SuppressWarnings({"unchecked"}) public void setValue(Object val) { enm = (T) val; } public String getAsText() { if (enm != null) { return enm.name(); } else { return ""; } } public void setAsText(String val) { if (enm == null) { return; } try { enm = Enum.valueOf(enm.getClass(), val); setValue(enm); } catch (IllegalArgumentException iae) { log.error("Tried to set illegal {} : ", enm.getClass(), val); } } }