01 /*
02 * SPDX-License-Identifier: Apache-2.0
03 *
04 * Copyright 2008-2017 the original author or authors.
05 *
06 * Licensed under the Apache License, Version 2.0 (the "License");
07 * you may not use this file except in compliance with the License.
08 * You may obtain a copy of the License at
09 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package griffon.core.editors;
19
20 import static griffon.util.GriffonNameUtils.isBlank;
21
22 /**
23 * @author Andres Almiray
24 * @since 2.0.0
25 */
26 @SuppressWarnings("rawtypes")
27 public class EnumPropertyEditor extends AbstractPropertyEditor {
28 private Class<? extends Enum> enumType;
29
30 public Class<? extends Enum> getEnumType() {
31 return enumType;
32 }
33
34 public void setEnumType(Class<? extends Enum> enumType) {
35 this.enumType = enumType;
36 }
37
38 @Override
39 protected void setValueInternal(Object value) {
40 if (null == value) {
41 super.setValueInternal(null);
42 } else if (value instanceof CharSequence) {
43 handleAsString(String.valueOf(value));
44 } else if (value instanceof Enum) {
45 handleAsString(value.toString());
46 } else {
47 throw illegalValue(value, enumType);
48 }
49 }
50
51 @SuppressWarnings("unchecked")
52 protected void handleAsString(String str) {
53 if (isBlank(str)) {
54 super.setValueInternal(null);
55 return;
56 }
57
58 try {
59 super.setValueInternal(Enum.valueOf(enumType, str));
60 } catch (Exception e) {
61 throw illegalValue(str, enumType, e);
62 }
63 }
64 }
|