001 /*
002 * Copyright 2008-2017 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 @Override
038 protected void setValueInternal(Object value) {
039 if (null == value) {
040 super.setValueInternal(null);
041 } else if (value instanceof CharSequence) {
042 handleAsString(String.valueOf(value));
043 } else {
044 handleWithImagePropertyEditor(value);
045 }
046 }
047
048 protected void handleAsString(String str) {
049 if (isBlank(str)) {
050 super.setValueInternal(null);
051 } else if (str.contains("|")) {
052 handleAsClassWithArg(str);
053 } else {
054 handleWithImagePropertyEditor(str);
055 }
056 }
057
058 protected void handleWithImagePropertyEditor(Object value) {
059 try {
060 imagePropertyEditor.setValueInternal(value);
061 Image image = (Image) imagePropertyEditor.getValue();
062 if (image != null) {
063 super.setValueInternal(new ImageView(image));
064 } else {
065 super.setValueInternal(null);
066 }
067 } catch (IllegalArgumentException iae) {
068 throw illegalValue(value, Node.class, iae);
069 }
070 }
071
072 protected void handleAsClassWithArg(String str) {
073 String[] args = str.split("\\|");
074 if (args.length == 2) {
075 Class<?> iconClass = null;
076 try {
077 iconClass = GraphicPropertyEditor.class.getClassLoader().loadClass(args[0]);
078 } catch (ClassNotFoundException e) {
079 throw illegalValue(str, Node.class, e);
080 }
081
082 Constructor<?> constructor = null;
083 try {
084 constructor = iconClass.getConstructor(String.class);
085 } catch (NoSuchMethodException e) {
086 throw illegalValue(str, Node.class, e);
087 }
088
089 try {
090 Object o = constructor.newInstance(args[1]);
091 if (o instanceof Node) {
092 super.setValueInternal(o);
093 } else if (o instanceof Image) {
094 super.setValueInternal(new ImageView((Image) o));
095 } else {
096 throw illegalValue(str, Node.class);
097 }
098 } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
099 throw illegalValue(str, Node.class, e);
100 }
101 } else {
102 throw illegalValue(str, Node.class);
103 }
104 }
105 }
|