01 /*
02 * Copyright 2015 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package griffon.javafx.editors;
17
18 import griffon.core.editors.AbstractPropertyEditor;
19 import griffon.metadata.PropertyEditorFor;
20 import javafx.scene.Node;
21 import javafx.scene.image.Image;
22 import javafx.scene.image.ImageView;
23
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.InvocationTargetException;
26
27 import static griffon.util.GriffonNameUtils.isBlank;
28
29 /**
30 * @author Andres Almiray
31 */
32 @PropertyEditorFor(Node.class)
33 public class GraphicPropertyEditor extends AbstractPropertyEditor {
34 private final ImagePropertyEditor imagePropertyEditor = new ImagePropertyEditor();
35
36 protected void setValueInternal(Object value) {
37 if (null == value) {
38 super.setValueInternal(null);
39 } else if (value instanceof CharSequence) {
40 handleAsString(String.valueOf(value));
41 } else {
42 try {
43 imagePropertyEditor.setValueInternal(value);
44 Image image = (Image) imagePropertyEditor.getValue();
45 if (image != null) {
46 super.setValueInternal(new ImageView(image));
47 } else {
48 super.setValueInternal(null);
49 }
50 } catch (IllegalArgumentException iae) {
51 throw illegalValue(value, Node.class, iae);
52 }
53 }
54 }
55
56 private void handleAsString(String str) {
57 if (isBlank(str)) {
58 super.setValueInternal(null);
59 } else if (str.contains("|")) {
60 handleAsClassWithArg(str);
61 } else {
62 throw illegalValue(str, Node.class);
63 }
64 }
65
66 private void handleAsClassWithArg(String str) {
67 String[] args = str.split("\\|");
68 if (args.length == 2) {
69 Class<?> iconClass = null;
70 try {
71 iconClass = GraphicPropertyEditor.class.getClassLoader().loadClass(args[0]);
72 } catch (ClassNotFoundException e) {
73 throw illegalValue(str, Node.class, e);
74 }
75
76 Constructor<?> constructor = null;
77 try {
78 constructor = iconClass.getConstructor(String.class);
79 } catch (NoSuchMethodException e) {
80 throw illegalValue(str, Node.class, e);
81 }
82
83 try {
84 Object o = constructor.newInstance(args[1]);
85 if (o instanceof Node) {
86 super.setValueInternal(o);
87 } else if (o instanceof Image) {
88 super.setValueInternal(new ImageView((Image) o));
89 } else {
90 throw illegalValue(str, Node.class);
91 }
92 } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
93 throw illegalValue(str, Node.class, e);
94 }
95 } else {
96 throw illegalValue(str, Node.class);
97 }
98 }
99 }
|