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