001 /*
002 * Copyright 2008-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package griffon.javafx.editors;
017
018 import griffon.core.editors.AbstractPropertyEditor;
019 import griffon.metadata.PropertyEditorFor;
020 import javafx.scene.Node;
021 import javafx.scene.image.Image;
022 import javafx.scene.image.ImageView;
023
024 import java.lang.reflect.Constructor;
025 import java.lang.reflect.InvocationTargetException;
026
027 import static griffon.util.GriffonNameUtils.isBlank;
028
029 /**
030 * @author Andres Almiray
031 * @since 2.3.0
032 */
033 @PropertyEditorFor(Node.class)
034 public class GraphicPropertyEditor extends AbstractPropertyEditor {
035 protected final ImagePropertyEditor imagePropertyEditor = new ImagePropertyEditor();
036
037 protected void setValueInternal(Object value) {
038 if (null == value) {
039 super.setValueInternal(null);
040 } else if (value instanceof CharSequence) {
041 handleAsString(String.valueOf(value));
042 } else {
043 handleWithImagePropertyEditor(value);
044 }
045 }
046
047 protected void handleAsString(String str) {
048 if (isBlank(str)) {
049 super.setValueInternal(null);
050 } else if (str.contains("|")) {
051 handleAsClassWithArg(str);
052 } else {
053 handleWithImagePropertyEditor(str);
054 }
055 }
056
057 protected void handleWithImagePropertyEditor(Object value) {
058 try {
059 imagePropertyEditor.setValueInternal(value);
060 Image image = (Image) imagePropertyEditor.getValue();
061 if (image != null) {
062 super.setValueInternal(new ImageView(image));
063 } else {
064 super.setValueInternal(null);
065 }
066 } catch (IllegalArgumentException iae) {
067 throw illegalValue(value, Node.class, iae);
068 }
069 }
070
071 protected void handleAsClassWithArg(String str) {
072 String[] args = str.split("\\|");
073 if (args.length == 2) {
074 Class<?> iconClass = null;
075 try {
076 iconClass = GraphicPropertyEditor.class.getClassLoader().loadClass(args[0]);
077 } catch (ClassNotFoundException e) {
078 throw illegalValue(str, Node.class, e);
079 }
080
081 Constructor<?> constructor = null;
082 try {
083 constructor = iconClass.getConstructor(String.class);
084 } catch (NoSuchMethodException e) {
085 throw illegalValue(str, Node.class, e);
086 }
087
088 try {
089 Object o = constructor.newInstance(args[1]);
090 if (o instanceof Node) {
091 super.setValueInternal(o);
092 } else if (o instanceof Image) {
093 super.setValueInternal(new ImageView((Image) o));
094 } else {
095 throw illegalValue(str, Node.class);
096 }
097 } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
098 throw illegalValue(str, Node.class, e);
099 }
100 } else {
101 throw illegalValue(str, Node.class);
102 }
103 }
104 }
|