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.image.Image;
021 import javafx.scene.image.ImageView;
022
023 import java.io.File;
024 import java.io.InputStream;
025 import java.lang.reflect.Constructor;
026 import java.lang.reflect.InvocationTargetException;
027 import java.net.MalformedURLException;
028 import java.net.URI;
029 import java.net.URL;
030
031 import static griffon.util.GriffonNameUtils.isBlank;
032
033 /**
034 * @author Andres Almiray
035 */
036 @PropertyEditorFor(Image.class)
037 public class ImagePropertyEditor extends AbstractPropertyEditor {
038 @Override
039 protected void setValueInternal(Object value) {
040 if (null == value) {
041 super.setValueInternal(null);
042 } else if (value instanceof CharSequence) {
043 handleAsString(String.valueOf(value));
044 } else if (value instanceof File) {
045 handleAsFile((File) value);
046 } else if (value instanceof URL) {
047 handleAsURL((URL) value);
048 } else if (value instanceof URI) {
049 handleAsURI((URI) value);
050 } else if (value instanceof InputStream) {
051 handleAsInputStream((InputStream) value);
052 } else if (value instanceof Image) {
053 super.setValueInternal(value);
054 } else {
055 throw illegalValue(value, Image.class);
056 }
057 }
058
059 protected void handleAsString(String str) {
060 if (isBlank(str)) {
061 super.setValueInternal(null);
062 } else if (str.contains("|")) {
063 handleAsClassWithArg(str);
064 } else {
065 handleAsURL(getClass().getClassLoader().getResource(str));
066 }
067 }
068
069 protected void handleAsFile(File file) {
070 try {
071 handleAsURL(file.toURI().toURL());
072 } catch (MalformedURLException e) {
073 throw illegalValue(file, URL.class, e);
074 }
075 }
076
077 protected void handleAsURL(URL url) {
078 try {
079 super.setValueInternal(new Image(url.toString()));
080 } catch (Exception e) {
081 throw illegalValue(url, URL.class, e);
082 }
083 }
084
085 protected void handleAsURI(URI uri) {
086 try {
087 handleAsURL(uri.toURL());
088 } catch (MalformedURLException e) {
089 throw illegalValue(uri, URL.class, e);
090 }
091 }
092
093 protected void handleAsInputStream(InputStream stream) {
094 try {
095 super.setValueInternal(new Image(stream));
096 } catch (Exception e) {
097 throw illegalValue(stream, URL.class, e);
098 }
099 }
100
101 protected void handleAsClassWithArg(String str) {
102 String[] args = str.split("\\|");
103 if (args.length == 2) {
104 Class<?> iconClass = null;
105 try {
106 iconClass = ImagePropertyEditor.class.getClassLoader().loadClass(args[0]);
107 } catch (ClassNotFoundException e) {
108 throw illegalValue(str, Image.class, e);
109 }
110
111 Constructor<?> constructor = null;
112 try {
113 constructor = iconClass.getConstructor(String.class);
114 } catch (NoSuchMethodException e) {
115 throw illegalValue(str, Image.class, e);
116 }
117
118 try {
119 Object o = constructor.newInstance(args[1]);
120 if (o instanceof Image) {
121 super.setValueInternal(o);
122 } else if (o instanceof ImageView) {
123 super.setValueInternal(((ImageView) o).getImage());
124 } else {
125 throw illegalValue(str, Image.class);
126 }
127 } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
128 throw illegalValue(str, Image.class, e);
129 }
130 } else {
131 throw illegalValue(str, Image.class);
132 }
133 }
134 }
|